ThriftSpanConverter { public static com.twitter.zipkin.thriftjava.Span convertSpan(JaegerSpan jaegerSpan) { JaegerTracer tracer = jaegerSpan.getTracer(); Endpoint host = new Endpoint(tracer.getIpv4(), (short) 0, tracer.getServiceName()); JaegerSpanContext context = jaegerSpan.context(); return new com.twitter.zipkin.thriftjava.Span( context.getTraceIdLow(), jaegerSpan.getOperationName(), context.getSpanId(), buildAnnotations(jaegerSpan, host), buildBinaryAnnotations(jaegerSpan, host)) .setParent_id(context.getParentId()) .setTrace_id_high(context.getTraceIdHigh()) .setDebug(context.isDebug()) .setTimestamp(jaegerSpan.getStart()) .setDuration(jaegerSpan.getDuration()); }  static com.twitter.zipkin.thriftjava.Span convertSpan(JaegerSpan jaegerSpan); static Endpoint extractPeerEndpoint(Map<String, Object> tags);  }
@Test @UseDataProvider("dataProviderTracerTags") public void testTracerTags(SpanType spanType, Map<String, String> expectedTags) { InMemoryReporter spanReporter = new InMemoryReporter(); JaegerTracer tracer = new JaegerTracer.Builder("x") .withReporter(spanReporter) .withSampler(new ConstSampler(true)) .withZipkinSharedRpcSpan() .withTag("tag.str", "y") .withTag("tag.bool", true) .withTag("tag.num", 1) .build(); JaegerSpan span = tracer.buildSpan("root").start(); if (spanType == SpanType.CHILD) { span = tracer.buildSpan("child").asChildOf(span).start(); } else if (spanType == SpanType.RPC_SERVER) { span = tracer.buildSpan("rpc-server") .asChildOf(span) .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER) .start(); } com.twitter.zipkin.thriftjava.Span zipkinSpan = ThriftSpanConverter.convertSpan(span); List<BinaryAnnotation> annotations = zipkinSpan.getBinary_annotations(); for (Map.Entry<String, String> entry : expectedTags.entrySet()) { String key = entry.getKey(); Object expectedValue = entry.getValue(); BinaryAnnotation anno = findBinaryAnnotation(annotations, key); if (expectedValue.equals(UNDEF)) { assertNull("Not expecting " + key + " for " + spanType, anno); } else if (expectedValue.equals(ANY)) { assertEquals(key, anno.getKey()); } else { String actualValue = new String(anno.getValue(), StandardCharsets.UTF_8); assertEquals("Expecting " + key + " for " + spanType, expectedValue, actualValue); } } }
@Test public void testSpanKindServerCreatesAnnotations() { JaegerSpan span = tracer.buildSpan("operation-name").start(); Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_SERVER); com.twitter.zipkin.thriftjava.Span zipkinSpan = ThriftSpanConverter.convertSpan(span); List<Annotation> annotations = zipkinSpan.getAnnotations(); boolean serverReceiveFound = false; boolean serverSendFound = false; for (Annotation anno : annotations) { if (anno.getValue().equals(zipkincoreConstants.SERVER_RECV)) { serverReceiveFound = true; } if (anno.getValue().equals(zipkincoreConstants.SERVER_SEND)) { serverSendFound = true; } } assertTrue(serverReceiveFound); assertTrue(serverSendFound); }
@Test public void testSpanKindClientCreatesAnnotations() { JaegerSpan span = tracer.buildSpan("operation-name").start(); Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_CLIENT); com.twitter.zipkin.thriftjava.Span zipkinSpan = ThriftSpanConverter.convertSpan(span); List<Annotation> annotations = zipkinSpan.getAnnotations(); boolean clientReceiveFound = false; boolean clientSendFound = false; for (Annotation anno : annotations) { if (anno.getValue().equals(zipkincoreConstants.CLIENT_RECV)) { clientReceiveFound = true; } if (anno.getValue().equals(zipkincoreConstants.CLIENT_SEND)) { clientSendFound = true; } } assertTrue(clientReceiveFound); assertTrue(clientSendFound); }
@Test public void testExpectedLocalComponentNameUsed() { String expectedComponentName = "local-name"; JaegerSpan span = tracer.buildSpan("operation-name").start(); Tags.COMPONENT.set(span, expectedComponentName); com.twitter.zipkin.thriftjava.Span zipkinSpan = ThriftSpanConverter.convertSpan(span); String actualComponent = new String(zipkinSpan.getBinary_annotations().get(3).getValue(), StandardCharsets.UTF_8); assertEquals(expectedComponentName, actualComponent); }
@Test public void testSpanLogsCreateAnnotations() { JaegerSpan span = tracer.buildSpan("span-with-logs").start(); span.log("event"); Map<String, Object> fields = new TreeMap<String, Object>(); fields.put("event", "structured data"); fields.put("string", "something"); fields.put("number", 42); fields.put("boolean", true); span.log(fields); com.twitter.zipkin.thriftjava.Span zipkinSpan = ThriftSpanConverter.convertSpan(span); List<String> annotationValues = new ArrayList<String>(); for (Annotation annotation : zipkinSpan.getAnnotations()) { annotationValues.add(annotation.getValue()); } List<String> expectedValues = new ArrayList<String>(); expectedValues.add("event"); expectedValues.add("{\"boolean\":true,\"event\":\"structured data\",\"number\":42,\"string\":\"something\"}"); assertEquals("zipkin span should contain matching annotations for span logs", expectedValues, annotationValues); }
@Test public void testConvertSpanWith128BitTraceId() { JaegerSpan span = tracer128.buildSpan("operation-name").start(); com.twitter.zipkin.thriftjava.Span zipkinSpan = ThriftSpanConverter.convertSpan(span); assertNotEquals(0, span.context().getTraceIdHigh()); assertEquals(span.context().getTraceIdLow(), zipkinSpan.getTrace_id()); assertEquals(span.context().getTraceIdHigh(), zipkinSpan.getTrace_id_high()); }