use of zipkin2.proto3.Span in project brave by openzipkin.
the class ITTracingFeature_Container method tagsResource.
@Test
public void tagsResource() throws Exception {
get("/foo");
Span span = takeSpan();
assertThat(span.tags()).containsEntry("jaxrs.resource.class", "TestResource").containsEntry("jaxrs.resource.method", "foo");
}
use of zipkin2.proto3.Span in project brave by openzipkin.
the class TracingStatementInterceptor method parseServerAddress.
/**
* MySQL exposes the host connecting to, but not the port. This attempts to get the port from the
* JDBC URL. Ex. 5555 from {@code jdbc:mysql://localhost:5555/database}, or 3306 if absent.
*/
static void parseServerAddress(Connection connection, Span span) {
try {
// strip "jdbc:"
URI url = URI.create(connection.getMetaData().getURL().substring(5));
int port = url.getPort() == -1 ? 3306 : url.getPort();
String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName");
if (remoteServiceName == null || "".equals(remoteServiceName)) {
String databaseName = connection.getCatalog();
if (databaseName != null && !databaseName.isEmpty()) {
remoteServiceName = "mysql-" + databaseName;
} else {
remoteServiceName = "mysql";
}
}
Endpoint.Builder builder = Endpoint.newBuilder().serviceName(remoteServiceName).port(port);
if (!builder.parseIp(connection.getHost()))
return;
span.remoteEndpoint(builder.build());
} catch (Exception e) {
// remote address is optional
}
}
use of zipkin2.proto3.Span in project brave by openzipkin.
the class TracingHttpServerHandler method maybeParseClientAddress.
/**
* Like {@link HttpServerHandler}, but accepts a channel
*/
void maybeParseClientAddress(Channel channel, HttpRequest request, Span span) {
Endpoint.Builder remoteEndpoint = Endpoint.newBuilder();
if (adapter.parseClientAddress(request, remoteEndpoint)) {
span.remoteEndpoint(remoteEndpoint.build());
} else {
InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
span.remoteEndpoint(Endpoint.newBuilder().ip(remoteAddress.getAddress()).port(remoteAddress.getPort()).build());
}
}
use of zipkin2.proto3.Span in project brave by openzipkin.
the class TracingJdbcEventListener method parseServerAddress.
/**
* This attempts to get the ip and port from the JDBC URL. Ex. localhost and 5555 from {@code
* jdbc:mysql://localhost:5555/mydatabase}.
*/
void parseServerAddress(Connection connection, Span span) {
try {
// strip "jdbc:"
final String urlAsString = connection.getMetaData().getURL().substring(5);
// Remove all white space according to RFC 2396
URI url = URI.create(urlAsString.replace(" ", ""));
String defaultRemoteServiceName = remoteServiceName;
Matcher matcher = URL_SERVICE_NAME_FINDER.matcher(url.toString());
if (matcher.find() && matcher.groupCount() == 1) {
String parsedServiceName = matcher.group(1);
if (parsedServiceName != null && !parsedServiceName.isEmpty()) {
// Do not override global service name if parsed service name is invalid
defaultRemoteServiceName = parsedServiceName;
}
}
Endpoint.Builder builder = Endpoint.newBuilder();
int port = url.getPort();
if (port > 0)
builder.port(port);
boolean parsed = builder.parseIp(url.getHost());
if (defaultRemoteServiceName == null || "".equals(defaultRemoteServiceName)) {
String databaseName = connection.getCatalog();
if (databaseName != null && !databaseName.isEmpty()) {
builder.serviceName(databaseName);
} else {
if (!parsed)
return;
}
} else {
builder.serviceName(defaultRemoteServiceName);
}
span.remoteEndpoint(builder.build());
} catch (Exception e) {
// remote address is optional
}
}
use of zipkin2.proto3.Span in project brave by openzipkin.
the class V2SpanConverter method toSpan.
/**
* Converts the input, parsing {@link Span#kind()} into RPC annotations.
*/
public static zipkin.Span toSpan(Span in) {
String traceId = in.traceId();
zipkin.Span.Builder result = zipkin.Span.builder().traceId(HexCodec.lowerHexToUnsignedLong(traceId)).parentId(in.parentId() != null ? HexCodec.lowerHexToUnsignedLong(in.parentId()) : null).id(HexCodec.lowerHexToUnsignedLong(in.id())).debug(in.debug()).name(// avoid a NPE
in.name() != null ? in.name() : "");
if (traceId.length() == 32) {
result.traceIdHigh(HexCodec.lowerHexToUnsignedLong(traceId, 0));
}
long startTs = in.timestamp() == null ? 0L : in.timestamp();
Long endTs = in.duration() == null ? 0L : in.timestamp() + in.duration();
if (startTs != 0L) {
result.timestamp(startTs);
result.duration(in.duration());
}
zipkin.Endpoint local = in.localEndpoint() != null ? toEndpoint(in.localEndpoint()) : null;
zipkin.Endpoint remote = in.remoteEndpoint() != null ? toEndpoint(in.remoteEndpoint()) : null;
Kind kind = in.kind();
Annotation cs = null, sr = null, ss = null, cr = null, ms = null, mr = null, ws = null, wr = null;
String remoteEndpointType = null;
boolean wroteEndpoint = false;
for (int i = 0, length = in.annotations().size(); i < length; i++) {
zipkin2.Annotation input = in.annotations().get(i);
Annotation a = Annotation.create(input.timestamp(), input.value(), local);
if (a.value.length() == 2) {
if (a.value.equals(Constants.CLIENT_SEND)) {
kind = Kind.CLIENT;
cs = a;
remoteEndpointType = SERVER_ADDR;
} else if (a.value.equals(Constants.SERVER_RECV)) {
kind = Kind.SERVER;
sr = a;
remoteEndpointType = CLIENT_ADDR;
} else if (a.value.equals(Constants.SERVER_SEND)) {
kind = Kind.SERVER;
ss = a;
} else if (a.value.equals(Constants.CLIENT_RECV)) {
kind = Kind.CLIENT;
cr = a;
} else if (a.value.equals(Constants.MESSAGE_SEND)) {
kind = Kind.PRODUCER;
ms = a;
} else if (a.value.equals(Constants.MESSAGE_RECV)) {
kind = Kind.CONSUMER;
mr = a;
} else if (a.value.equals(Constants.WIRE_SEND)) {
ws = a;
} else if (a.value.equals(Constants.WIRE_RECV)) {
wr = a;
} else {
wroteEndpoint = true;
result.addAnnotation(a);
}
} else {
wroteEndpoint = true;
result.addAnnotation(a);
}
}
if (kind != null) {
switch(kind) {
case CLIENT:
remoteEndpointType = Constants.SERVER_ADDR;
if (startTs != 0L)
cs = Annotation.create(startTs, Constants.CLIENT_SEND, local);
if (endTs != 0L)
cr = Annotation.create(endTs, Constants.CLIENT_RECV, local);
break;
case SERVER:
remoteEndpointType = Constants.CLIENT_ADDR;
if (startTs != 0L)
sr = Annotation.create(startTs, Constants.SERVER_RECV, local);
if (endTs != 0L)
ss = Annotation.create(endTs, Constants.SERVER_SEND, local);
break;
case PRODUCER:
remoteEndpointType = Constants.MESSAGE_ADDR;
if (startTs != 0L)
ms = Annotation.create(startTs, Constants.MESSAGE_SEND, local);
if (endTs != 0L)
ws = Annotation.create(endTs, Constants.WIRE_SEND, local);
break;
case CONSUMER:
remoteEndpointType = Constants.MESSAGE_ADDR;
if (startTs != 0L && endTs != 0L) {
wr = Annotation.create(startTs, Constants.WIRE_RECV, local);
mr = Annotation.create(endTs, Constants.MESSAGE_RECV, local);
} else if (startTs != 0L) {
mr = Annotation.create(startTs, Constants.MESSAGE_RECV, local);
}
break;
default:
throw new AssertionError("update kind mapping");
}
}
for (Map.Entry<String, String> tag : in.tags().entrySet()) {
wroteEndpoint = true;
result.addBinaryAnnotation(BinaryAnnotation.create(tag.getKey(), tag.getValue(), local));
}
if (cs != null || sr != null || ss != null || cr != null || ws != null || wr != null || ms != null || mr != null) {
if (cs != null)
result.addAnnotation(cs);
if (sr != null)
result.addAnnotation(sr);
if (ss != null)
result.addAnnotation(ss);
if (cr != null)
result.addAnnotation(cr);
if (ws != null)
result.addAnnotation(ws);
if (wr != null)
result.addAnnotation(wr);
if (ms != null)
result.addAnnotation(ms);
if (mr != null)
result.addAnnotation(mr);
wroteEndpoint = true;
} else if (local != null && remote != null) {
// special-case when we are missing core annotations, but we have both address annotations
result.addBinaryAnnotation(BinaryAnnotation.address(CLIENT_ADDR, local));
wroteEndpoint = true;
remoteEndpointType = SERVER_ADDR;
}
if (remoteEndpointType != null && remote != null) {
result.addBinaryAnnotation(BinaryAnnotation.address(remoteEndpointType, remote));
}
// don't report server-side timestamp on shared or incomplete spans
if (Boolean.TRUE.equals(in.shared()) && sr != null) {
result.timestamp(null).duration(null);
}
if (local != null && !wroteEndpoint) {
// create a dummy annotation
result.addBinaryAnnotation(BinaryAnnotation.create(LOCAL_COMPONENT, "", local));
}
return result.build();
}
Aggregations