use of zipkin2.Annotation in project brave by openzipkin.
the class ITHttp method takeSpan.
/**
* Call this to block until a span was reported
*/
protected Span takeSpan() throws InterruptedException {
Span result = spans.take();
assertThat(result.annotations()).extracting(Annotation::value).doesNotContain(CONTEXT_LEAK);
return result;
}
use of zipkin2.Annotation 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();
}
use of zipkin2.Annotation in project brave by openzipkin.
the class MutableSpanMapTest method reportOrphanedSpans_afterGC.
/**
* This is the key feature. Spans orphaned via GC are reported to zipkin on the next action.
*
* <p>This is a customized version of https://github.com/raphw/weak-lock-free/blob/master/src/test/java/com/blogspot/mydailyjava/weaklockfree/WeakConcurrentMapTest.java
*/
@Test
public void reportOrphanedSpans_afterGC() {
TraceContext context1 = context.toBuilder().spanId(1).build();
map.getOrCreate(context1);
TraceContext context2 = context.toBuilder().spanId(2).build();
map.getOrCreate(context2);
TraceContext context3 = context.toBuilder().spanId(3).build();
map.getOrCreate(context3);
TraceContext context4 = context.toBuilder().spanId(4).build();
map.getOrCreate(context4);
// By clearing strong references in this test, we are left with the weak ones in the map
context1 = context2 = null;
blockOnGC();
// After GC, we expect that the weak references of context1 and context2 to be cleared
assertThat(map.delegate.keySet()).extracting(o -> ((Reference) o).get()).containsExactlyInAnyOrder(null, null, context3, context4);
map.reportOrphanedSpans();
// After reporting, we expect no the weak references of null
assertThat(map.delegate.keySet()).extracting(o -> ((Reference) o).get()).containsExactlyInAnyOrder(context3, context4);
// We also expect the spans to have been reported
assertThat(spans).flatExtracting(Span::annotations).extracting(Annotation::value).containsExactly("brave.flush", "brave.flush");
}
use of zipkin2.Annotation in project spring-cloud-gcp by spring-cloud.
the class LabelExtractor method extract.
/**
* Extracts the Stackdriver span labels that are equivalent to the Zipkin Span
* annotations.
*
* @param zipkinSpan The Zipkin Span
* @return A map of the Stackdriver span labels equivalent to the Zipkin annotations.
*/
public Map<String, String> extract(Span zipkinSpan) {
Map<String, String> result = new LinkedHashMap<>();
for (Map.Entry<String, String> tag : zipkinSpan.tags().entrySet()) {
result.put(label(tag.getKey()), tag.getValue());
}
// trace might not show the final destination.
if (zipkinSpan.localEndpoint() != null && zipkinSpan.kind() == Span.Kind.SERVER) {
if (zipkinSpan.localEndpoint().ipv4() != null) {
result.put(label("endpoint.ipv4"), zipkinSpan.localEndpoint().ipv4());
}
if (zipkinSpan.localEndpoint().ipv6() != null) {
result.put(label("endpoint.ipv6"), zipkinSpan.localEndpoint().ipv6());
}
}
for (Annotation annotation : zipkinSpan.annotations()) {
result.put(label(annotation.value()), formatTimestamp(annotation.timestamp()));
}
if (zipkinSpan.localEndpoint() != null && !zipkinSpan.localEndpoint().serviceName().isEmpty()) {
result.put("/component", zipkinSpan.localEndpoint().serviceName());
}
if (zipkinSpan.parentId() == null) {
String agentName = System.getProperty("stackdriver.trace.zipkin.agent", "spring-cloud-gcp-trace");
result.put("/agent", agentName);
}
return result;
}
use of zipkin2.Annotation in project libSBOLj by SynBioDex.
the class AnnotationTest method test_pubConstructors.
@Test
public void test_pubConstructors() throws SBOLValidationException, URISyntaxException {
Annotation CD_annot = gRNA_b_gene.createAnnotation(new QName(prURI, "protein", "pr"), true);
assertTrue(gRNA_b_gene.getAnnotation(CD_annot.getQName()).getBooleanValue());
assertTrue(gRNA_b_gene.getAnnotations().size() == 1);
assertTrue(CD_annot.isBooleanValue());
CD_annot.setBooleanValue(false);
assertFalse(gRNA_b_gene.getAnnotation(CD_annot.getQName()).getBooleanValue());
gRNA_b_gene.removeAnnotation(CD_annot);
assertTrue(gRNA_b_gene.getAnnotations().size() == 0);
// test constructor with double
CD_annot = gRNA_b_gene.createAnnotation(new QName(prURI, "protein", "pr"), 1.0);
assertTrue(gRNA_b_gene.getAnnotation(CD_annot.getQName()).getDoubleValue() == 1.0);
assertTrue(gRNA_b_gene.getAnnotations().size() == 1);
assertTrue(CD_annot.isDoubleValue());
CD_annot.setDoubleValue(5.0);
assertTrue(CD_annot.getDoubleValue() == 5.0);
gRNA_b_gene.removeAnnotation(CD_annot);
assertTrue(gRNA_b_gene.getAnnotations().size() == 0);
// test constructor with int
CD_annot = gRNA_b_gene.createAnnotation(new QName(prURI, "protein", "pr"), 2);
assertTrue(gRNA_b_gene.getAnnotation(CD_annot.getQName()).getIntegerValue() == 2);
assertTrue(gRNA_b_gene.getAnnotations().size() == 1);
assertTrue(CD_annot.isIntegerValue());
CD_annot.setIntegerValue(7);
assertTrue(CD_annot.getIntegerValue() == 7);
gRNA_b_gene.removeAnnotation(CD_annot);
assertTrue(gRNA_b_gene.getAnnotations().size() == 0);
// test constructor with string
CD_annot = gRNA_b_gene.createAnnotation(new QName(prURI, "protein", "pr"), "protein");
assertTrue(gRNA_b_gene.getAnnotation(CD_annot.getQName()).getStringValue().equals("protein"));
assertTrue(gRNA_b_gene.getAnnotations().size() == 1);
assertTrue(CD_annot.isStringValue());
CD_annot.setStringValue("notAProtein");
assertTrue(CD_annot.getStringValue().equals("notAProtein"));
gRNA_b_gene.removeAnnotation(CD_annot);
assertTrue(gRNA_b_gene.getAnnotations().size() == 0);
// test constructor with URI
CD_annot = gRNA_b_gene.createAnnotation(new QName(prURI, "protein", "pr"), new URI("http://www.sbolstandard.org/protein"));
assertTrue(gRNA_b_gene.getAnnotation(CD_annot.getQName()).getURIValue().equals(new URI("http://www.sbolstandard.org/protein")));
assertTrue(gRNA_b_gene.getAnnotations().size() == 1);
assertTrue(CD_annot.isURIValue());
CD_annot.setURIValue(new URI("http://www.sbolstandard.org/gene"));
assertTrue(CD_annot.getURIValue().equals(new URI("http://www.sbolstandard.org/gene")));
gRNA_b_gene.removeAnnotation(CD_annot);
assertTrue(gRNA_b_gene.getAnnotations().size() == 0);
}
Aggregations