use of zipkin2.v1.V1Span in project zipkin by openzipkin.
the class ThriftCodec method read.
public static boolean read(ReadBuffer buffer, Collection<Span> out) {
if (buffer.available() == 0)
return false;
try {
V1Span v1Span = new V1ThriftSpanReader().read(buffer);
V1SpanConverter.create().convert(v1Span, out);
return true;
} catch (Exception e) {
throw exceptionReading("Span", e);
}
}
use of zipkin2.v1.V1Span in project zipkin by openzipkin.
the class V1JsonSpanWriter method write.
@Override
public void write(Span value, WriteBuffer b) {
V1Span v1Span = converter.convert(value);
v1SpanWriter.write(v1Span, b);
}
use of zipkin2.v1.V1Span in project zipkin by openzipkin.
the class V1SpanConverter method processBinaryAnnotations.
void processBinaryAnnotations(V1Span source) {
zipkin2.Endpoint ca = null, sa = null, ma = null;
for (int i = 0, length = source.binaryAnnotations.size(); i < length; i++) {
V1BinaryAnnotation b = source.binaryAnnotations.get(i);
// endpoint. Hence, we leniently parse.
if ("ca".equals(b.key)) {
ca = b.endpoint;
continue;
} else if ("sa".equals(b.key)) {
sa = b.endpoint;
continue;
} else if ("ma".equals(b.key)) {
ma = b.endpoint;
continue;
}
Span.Builder currentSpan = forEndpoint(source, b.endpoint);
// don't add marker "lc" tags
if ("lc".equals(b.key) && b.stringValue.isEmpty())
continue;
currentSpan.putTag(b.key, b.stringValue);
}
boolean noCoreAnnotations = cs == null && cr == null && ss == null && sr == null;
// special-case when we are missing core annotations, but we have both address annotations
if (noCoreAnnotations && (ca != null || sa != null)) {
if (ca != null && sa != null) {
forEndpoint(source, ca).remoteEndpoint(sa);
} else if (sa != null) {
// "sa" is a default for a remote address, don't make it a client span
forEndpoint(source, null).remoteEndpoint(sa);
} else {
// ca != null: treat it like a server
forEndpoint(source, null).kind(Kind.SERVER).remoteEndpoint(ca);
}
return;
}
V1Annotation server = sr != null ? sr : ss;
if (ca != null && server != null && !ca.equals(server.endpoint)) {
// the same service name as "sa". Removing the service name prevents creating loopback links.
if (hasSameServiceName(ca, server.endpoint)) {
ca = ca.toBuilder().serviceName(null).build();
}
forEndpoint(source, server.endpoint).remoteEndpoint(ca);
}
if (sa != null) {
// client span
if (cs != null) {
forEndpoint(source, cs.endpoint).remoteEndpoint(sa);
} else if (cr != null) {
forEndpoint(source, cr.endpoint).remoteEndpoint(sa);
}
}
if (ma != null) {
// a messaging span. This will ensure both sides have the address of the broker.
if (ms != null)
forEndpoint(source, ms.endpoint).remoteEndpoint(ma);
if (mr != null)
forEndpoint(source, mr.endpoint).remoteEndpoint(ma);
}
}
use of zipkin2.v1.V1Span in project zipkin by openzipkin.
the class V1SpanConverter method processAnnotations.
void processAnnotations(V1Span source) {
for (int i = 0, length = source.annotations.size(); i < length; i++) {
V1Annotation a = source.annotations.get(i);
Span.Builder currentSpan = forEndpoint(source, a.endpoint);
// core annotations require an endpoint. Don't give special treatment when that's missing
if (a.value.length() == 2 && a.endpoint != null) {
if (a.value.equals("cs")) {
currentSpan.kind(Kind.CLIENT);
cs = a;
} else if (a.value.equals("sr")) {
currentSpan.kind(Kind.SERVER);
sr = a;
} else if (a.value.equals("ss")) {
currentSpan.kind(Kind.SERVER);
ss = a;
} else if (a.value.equals("cr")) {
currentSpan.kind(Kind.CLIENT);
cr = a;
} else if (a.value.equals("ms")) {
currentSpan.kind(Kind.PRODUCER);
ms = a;
} else if (a.value.equals("mr")) {
currentSpan.kind(Kind.CONSUMER);
mr = a;
} else if (a.value.equals("ws")) {
ws = a;
} else if (a.value.equals("wr")) {
wr = a;
} else {
currentSpan.addAnnotation(a.timestamp, a.value);
}
} else {
currentSpan.addAnnotation(a.timestamp, a.value);
}
}
// When bridging between event and span model, you can end up missing a start annotation
if (cs == null && endTimestampReflectsSpanDuration(cr, source)) {
cs = V1Annotation.create(source.timestamp, "cs", cr.endpoint);
}
if (sr == null && endTimestampReflectsSpanDuration(ss, source)) {
sr = V1Annotation.create(source.timestamp, "sr", ss.endpoint);
}
if (cs != null && sr != null) {
// in a shared span, the client side owns span duration by annotations or explicit timestamp
maybeTimestampDuration(source, cs, cr);
// special-case loopback: We need to make sure on loopback there are two span2s
Span.Builder client = forEndpoint(source, cs.endpoint);
Span.Builder server;
if (hasSameServiceName(cs.endpoint, sr.endpoint)) {
client.kind(Kind.CLIENT);
// fork a new span for the server side
server = newSpanBuilder(source, sr.endpoint).kind(Kind.SERVER);
} else {
server = forEndpoint(source, sr.endpoint);
}
// the server side is smaller than that, we have to read annotations to find out
server.shared(true).timestamp(sr.timestamp);
if (ss != null)
server.duration(ss.timestamp - sr.timestamp);
// one-way has no duration
if (cr == null && source.duration == 0)
client.duration(null);
} else if (cs != null && cr != null) {
maybeTimestampDuration(source, cs, cr);
} else if (sr != null && ss != null) {
maybeTimestampDuration(source, sr, ss);
} else {
// otherwise, the span is incomplete. revert special-casing
handleIncompleteRpc(source);
}
// implied shared. When we only see the server-side, carry this signal over.
if (cs == null && sr != null && // case could be due to the client-side of that RPC.
(source.timestamp == 0 || (ss != null && source.duration == 0))) {
forEndpoint(source, sr.endpoint).shared(true);
}
// ms and mr are not supposed to be in the same span, but in case they are..
if (ms != null && mr != null) {
// special-case loopback: We need to make sure on loopback there are two span2s
Span.Builder producer = forEndpoint(source, ms.endpoint);
Span.Builder consumer;
if (hasSameServiceName(ms.endpoint, mr.endpoint)) {
producer.kind(Kind.PRODUCER);
// fork a new span for the consumer side
consumer = newSpanBuilder(source, mr.endpoint).kind(Kind.CONSUMER);
} else {
consumer = forEndpoint(source, mr.endpoint);
}
consumer.shared(true);
if (wr != null) {
consumer.timestamp(wr.timestamp).duration(mr.timestamp - wr.timestamp);
} else {
consumer.timestamp(mr.timestamp);
}
producer.timestamp(ms.timestamp).duration(ws != null ? ws.timestamp - ms.timestamp : null);
} else if (ms != null) {
maybeTimestampDuration(source, ms, ws);
} else if (mr != null) {
if (wr != null) {
maybeTimestampDuration(source, wr, mr);
} else {
maybeTimestampDuration(source, mr, null);
}
} else {
if (ws != null)
forEndpoint(source, ws.endpoint).addAnnotation(ws.timestamp, ws.value);
if (wr != null)
forEndpoint(source, wr.endpoint).addAnnotation(wr.timestamp, wr.value);
}
}
use of zipkin2.v1.V1Span in project zipkin by openzipkin.
the class V2SpanConverter method convert.
public V1Span convert(Span value) {
md.parse(value);
result.clear().traceId(value.traceId()).parentId(value.parentId()).id(value.id()).name(value.name()).debug(value.debug());
// Don't report timestamp and duration on shared spans (should be server, but not necessarily)
if (!Boolean.TRUE.equals(value.shared())) {
result.timestamp(value.timestampAsLong());
result.duration(value.durationAsLong());
}
boolean beginAnnotation = md.startTs != 0L && md.begin != null;
boolean endAnnotation = md.endTs != 0L && md.end != null;
Endpoint ep = value.localEndpoint();
int annotationCount = value.annotations().size();
if (beginAnnotation) {
annotationCount++;
result.addAnnotation(md.startTs, md.begin, ep);
}
for (int i = 0, length = value.annotations().size(); i < length; i++) {
Annotation a = value.annotations().get(i);
if (beginAnnotation && a.value().equals(md.begin))
continue;
if (endAnnotation && a.value().equals(md.end))
continue;
result.addAnnotation(a.timestamp(), a.value(), ep);
}
if (endAnnotation) {
annotationCount++;
result.addAnnotation(md.endTs, md.end, ep);
}
for (Map.Entry<String, String> b : value.tags().entrySet()) {
result.addBinaryAnnotation(b.getKey(), b.getValue(), ep);
}
boolean writeLocalComponent = annotationCount == 0 && ep != null && value.tags().isEmpty();
boolean hasRemoteEndpoint = md.addr != null && value.remoteEndpoint() != null;
// write an empty "lc" annotation to avoid missing the localEndpoint in an in-process span
if (writeLocalComponent)
result.addBinaryAnnotation("lc", "", ep);
if (hasRemoteEndpoint)
result.addBinaryAnnotation(md.addr, value.remoteEndpoint());
return result.build();
}
Aggregations