V2SpanConverter { public static zipkin2.Span convertSpan(JaegerSpan span) { JaegerTracer tracer = span.getTracer(); zipkin2.Endpoint.Builder host = zipkin2.Endpoint.newBuilder() .serviceName(tracer.getServiceName()); if (tracer.getIpv4() != 0) { host.parseIp(convertIp(tracer.getIpv4())); } zipkin2.Endpoint peerEndpoint = extractPeerEndpoint(span.getTags()); JaegerSpanContext context = span.context(); zipkin2.Span.Builder builder = zipkin2.Span.newBuilder() .id(Long.toHexString(context.getSpanId())) .traceId(context.getTraceIdHigh(), context.getTraceIdLow()) .name(span.getOperationName()) .parentId(Long.toHexString(context.getParentId())) .debug(context.isDebug()) .localEndpoint(host.build()) .remoteEndpoint(peerEndpoint) .kind(convertKind(span.getTags().get(Tags.SPAN_KIND.getKey()))) .timestamp(span.getStart()) .duration(span.getDuration()); buildAnnotations(span, builder); buildTags(span, builder); return builder.build(); }  static zipkin2.Span convertSpan(JaegerSpan span); static zipkin2.Endpoint extractPeerEndpoint(Map<String, Object> tags);  }
@Test public void testAddsTracerIpAsLocalIpV4() { tracer = new JaegerTracer.Builder("test-service-name") .withReporter(new InMemoryReporter()) .withSampler(new ConstSampler(true)) .withZipkinSharedRpcSpan() .withTag(Constants.TRACER_IP_TAG_KEY, "1.2.3.4") .build(); JaegerSpan span = tracer.buildSpan("operation-name").start(); zipkin2.Span zipkinSpan = V2SpanConverter.convertSpan(span); assertEquals(zipkinSpan.localEndpoint().ipv4(), "1.2.3.4"); }
@Test public void testDoesntAddUnknownTracerIpAsLocalIpV4() { tracer = new JaegerTracer.Builder("test-service-name") .withReporter(new InMemoryReporter()) .withSampler(new ConstSampler(true)) .withZipkinSharedRpcSpan() .withTag(Constants.TRACER_IP_TAG_KEY, "") .build(); JaegerSpan span = tracer.buildSpan("operation-name").start(); zipkin2.Span zipkinSpan = V2SpanConverter.convertSpan(span); assertNull(zipkinSpan.localEndpoint().ipv4()); }
@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(); } zipkin2.Span zipkinSpan = V2SpanConverter.convertSpan(span); Map<String, String> zipkinTags = zipkinSpan.tags(); for (Map.Entry<String, String> entry : expectedTags.entrySet()) { String key = entry.getKey(); Object expectedValue = entry.getValue(); String tagValue = zipkinTags.get(key); if (expectedValue.equals(UNDEF)) { assertNull("Not expecting " + key + " for " + spanType, tagValue); } else if (expectedValue.equals(ANY)) { assertNotNull(key, tagValue); } else { assertEquals("Expecting " + key + " for " + spanType, expectedValue, tagValue); } } }
@Test public void testSpanKindConsumerHasCorrectKind() { JaegerSpan span = tracer.buildSpan("operation-name").start(); Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_CONSUMER); zipkin2.Span zipkinSpan = V2SpanConverter.convertSpan(span); assertEquals(zipkinSpan.kind(), zipkin2.Span.Kind.CONSUMER); }
@Test public void testSpanKindProducerHasCorrectKind() { JaegerSpan span = tracer.buildSpan("operation-name").start(); Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_PRODUCER); zipkin2.Span zipkinSpan = V2SpanConverter.convertSpan(span); assertEquals(zipkinSpan.kind(), zipkin2.Span.Kind.PRODUCER); }
@Test public void testExpectedLocalComponentNameUsed() { String expectedComponentName = "local-name"; JaegerSpan span = tracer.buildSpan("operation-name").start(); Tags.COMPONENT.set(span, expectedComponentName); zipkin2.Span zipkinSpan = V2SpanConverter.convertSpan(span); String actualComponent = zipkinSpan.tags().get(Tags.COMPONENT.getKey()); 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); zipkin2.Span zipkinSpan = V2SpanConverter.convertSpan(span); List<String> annotationValues = new ArrayList<String>(); for (Annotation annotation : zipkinSpan.annotations()) { annotationValues.add(annotation.value()); } 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() { JaegerSpanContext c = new JaegerSpanContext(1L, 1L, 1L, 0L, (byte)0x01); JaegerSpan span = tracer128.buildSpan("operation-name").asChildOf(c).start(); zipkin2.Span zipkinSpan = V2SpanConverter.convertSpan(span); assertNotEquals(0, span.context().getTraceIdHigh()); assertEquals(span.context().getTraceId(), zipkinSpan.traceId()); }