Search in sources :

Example 1 with BinaryAnnotation

use of com.twitter.zipkin.thriftjava.BinaryAnnotation in project jaeger-client-java by jaegertracing.

the class ThriftSpanConverter method buildBinaryAnnotations.

private static List<BinaryAnnotation> buildBinaryAnnotations(JaegerSpan jaegerSpan, Endpoint host) {
    List<BinaryAnnotation> binaryAnnotations = new ArrayList<BinaryAnnotation>();
    Map<String, Object> tags = jaegerSpan.getTags();
    boolean isRpc = ConverterUtil.isRpc(jaegerSpan);
    boolean isClient = ConverterUtil.isRpcClient(jaegerSpan);
    boolean firstSpanInProcess = jaegerSpan.getReferences().isEmpty() || ConverterUtil.isRpcServer(jaegerSpan);
    if (firstSpanInProcess) {
        Map<String, ?> processTags = jaegerSpan.getTracer().tags();
        // taken care of separately.
        for (Map.Entry<String, ?> entry : processTags.entrySet()) {
            String tagKey = entry.getKey();
            if (!Constants.TRACER_IP_TAG_KEY.equals(tagKey)) {
                Object tagValue = entry.getValue();
                // add a tracer. prefix to process tags for zipkin
                binaryAnnotations.add(buildBinaryAnnotation("tracer." + tagKey, tagValue));
            }
        }
    }
    Endpoint peerEndpoint = extractPeerEndpoint(tags);
    if (peerEndpoint != null && isClient) {
        String key = isClient ? zipkincoreConstants.SERVER_ADDR : zipkincoreConstants.CLIENT_ADDR;
        binaryAnnotations.add(new BinaryAnnotation().setKey(key).setValue(new byte[] { 1 }).setAnnotation_type(AnnotationType.BOOL).setHost(peerEndpoint));
    }
    if (!isRpc) {
        byte[] componentName;
        Object componentTag = tags.get(Tags.COMPONENT.getKey());
        if (componentTag instanceof String) {
            componentName = componentTag.toString().getBytes(UTF_8);
        } else {
            // spans always have associated tracers, and service names
            componentName = jaegerSpan.getTracer().getServiceName().getBytes(UTF_8);
        }
        binaryAnnotations.add(new BinaryAnnotation().setKey(zipkincoreConstants.LOCAL_COMPONENT).setValue(componentName).setAnnotation_type(AnnotationType.STRING).setHost(host));
    }
    if (tags != null) {
        for (Map.Entry<String, Object> entry : tags.entrySet()) {
            String tagKey = entry.getKey();
            // Every value is converted to string because zipkin search doesn't
            // work well with ints, and bytes.
            Object tagValue = entry.getValue();
            binaryAnnotations.add(buildBinaryAnnotation(tagKey, tagValue));
        }
    }
    return binaryAnnotations;
}
Also used : ArrayList(java.util.ArrayList) BinaryAnnotation(com.twitter.zipkin.thriftjava.BinaryAnnotation) Endpoint(com.twitter.zipkin.thriftjava.Endpoint) Map(java.util.Map)

Example 2 with BinaryAnnotation

use of com.twitter.zipkin.thriftjava.BinaryAnnotation in project jaeger-client-java by jaegertracing.

the class ThriftSpanConverterTest method testTracerTags.

@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);
        }
    }
}
Also used : InMemoryReporter(io.jaegertracing.internal.reporters.InMemoryReporter) BinaryAnnotation(com.twitter.zipkin.thriftjava.BinaryAnnotation) JaegerSpan(io.jaegertracing.internal.JaegerSpan) ConstSampler(io.jaegertracing.internal.samplers.ConstSampler) HashMap(java.util.HashMap) Map(java.util.Map) TextMap(io.opentracing.propagation.TextMap) TreeMap(java.util.TreeMap) JaegerTracer(io.jaegertracing.internal.JaegerTracer) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 3 with BinaryAnnotation

use of com.twitter.zipkin.thriftjava.BinaryAnnotation in project jaeger-client-java by jaegertracing.

the class ThriftSpanConverter method buildBinaryAnnotations.

private static List<BinaryAnnotation> buildBinaryAnnotations(Span span, Endpoint host) {
    List<BinaryAnnotation> binaryAnnotations = new ArrayList<BinaryAnnotation>();
    Map<String, Object> tags = span.getTags();
    boolean isRpc = isRpc(span);
    boolean isClient = isRpcClient(span);
    boolean firstSpanInProcess = span.getReferences().isEmpty() || isRpcServer(span);
    if (firstSpanInProcess) {
        Map<String, ?> processTags = span.getTracer().tags();
        // taken care of separately.
        for (Map.Entry<String, ?> entry : processTags.entrySet()) {
            String tagKey = entry.getKey();
            if (!Constants.TRACER_IP_TAG_KEY.equals(tagKey)) {
                Object tagValue = entry.getValue();
                // add a tracer. prefix to process tags for zipkin
                binaryAnnotations.add(buildBinaryAnnotation("tracer." + tagKey, tagValue));
            }
        }
    }
    Endpoint peerEndpoint = extractPeerEndpoint(tags);
    if (peerEndpoint != null && isClient) {
        String key = isClient ? zipkincoreConstants.SERVER_ADDR : zipkincoreConstants.CLIENT_ADDR;
        binaryAnnotations.add(new BinaryAnnotation().setKey(key).setValue(new byte[] { 1 }).setAnnotation_type(AnnotationType.BOOL).setHost(peerEndpoint));
    }
    if (!isRpc) {
        byte[] componentName;
        Object componentTag = tags.get(Tags.COMPONENT.getKey());
        if (componentTag instanceof String) {
            componentName = componentTag.toString().getBytes(UTF_8);
        } else {
            // spans always have associated tracers, and service names
            componentName = span.getTracer().getServiceName().getBytes(UTF_8);
        }
        binaryAnnotations.add(new BinaryAnnotation().setKey(zipkincoreConstants.LOCAL_COMPONENT).setValue(componentName).setAnnotation_type(AnnotationType.STRING).setHost(host));
    }
    if (tags != null) {
        for (Map.Entry<String, Object> entry : tags.entrySet()) {
            String tagKey = entry.getKey();
            // Every value is converted to string because zipkin search doesn't
            // work well with ints, and bytes.
            Object tagValue = entry.getValue();
            binaryAnnotations.add(buildBinaryAnnotation(tagKey, tagValue));
        }
    }
    return binaryAnnotations;
}
Also used : ArrayList(java.util.ArrayList) BinaryAnnotation(com.twitter.zipkin.thriftjava.BinaryAnnotation) Endpoint(com.twitter.zipkin.thriftjava.Endpoint) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with BinaryAnnotation

use of com.twitter.zipkin.thriftjava.BinaryAnnotation in project jaeger-client-java by jaegertracing.

the class ThriftSpanConverter method buildBinaryAnnotation.

private static BinaryAnnotation buildBinaryAnnotation(String tagKey, Object tagValue) {
    BinaryAnnotation banno = new BinaryAnnotation().setKey(tagKey);
    banno.setValue(String.valueOf(tagValue).getBytes(UTF_8)).setAnnotation_type(AnnotationType.STRING);
    return banno;
}
Also used : BinaryAnnotation(com.twitter.zipkin.thriftjava.BinaryAnnotation)

Example 5 with BinaryAnnotation

use of com.twitter.zipkin.thriftjava.BinaryAnnotation in project jaeger-client-java by jaegertracing.

the class ThriftSpanConverterTest method testTracerTags.

@Test
@UseDataProvider("dataProviderTracerTags")
public void testTracerTags(SpanType spanType, Map<String, String> expectedTags) throws Exception {
    InMemoryReporter spanReporter = new InMemoryReporter();
    Tracer tracer = new Tracer.Builder("x", spanReporter, new ConstSampler(true)).withZipkinSharedRpcSpan().withTag("tag.str", "y").withTag("tag.bool", true).withTag("tag.num", 1).build();
    Span span = (Span) tracer.buildSpan("root").startManual();
    if (spanType == SpanType.CHILD) {
        span = (Span) tracer.buildSpan("child").asChildOf(span).startManual();
    } else if (spanType == SpanType.RPC_SERVER) {
        span = (Span) tracer.buildSpan("rpc-server").asChildOf(span).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER).startManual();
    }
    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);
        }
    }
}
Also used : InMemoryReporter(com.uber.jaeger.reporters.InMemoryReporter) Tracer(com.uber.jaeger.Tracer) Span(com.uber.jaeger.Span) BinaryAnnotation(com.twitter.zipkin.thriftjava.BinaryAnnotation) ConstSampler(com.uber.jaeger.samplers.ConstSampler) HashMap(java.util.HashMap) Map(java.util.Map) TextMap(io.opentracing.propagation.TextMap) TreeMap(java.util.TreeMap) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

BinaryAnnotation (com.twitter.zipkin.thriftjava.BinaryAnnotation)6 Map (java.util.Map)4 HashMap (java.util.HashMap)3 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2 Endpoint (com.twitter.zipkin.thriftjava.Endpoint)2 TextMap (io.opentracing.propagation.TextMap)2 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 Test (org.junit.Test)2 Span (com.uber.jaeger.Span)1 Tracer (com.uber.jaeger.Tracer)1 InMemoryReporter (com.uber.jaeger.reporters.InMemoryReporter)1 ConstSampler (com.uber.jaeger.samplers.ConstSampler)1 JaegerSpan (io.jaegertracing.internal.JaegerSpan)1 JaegerTracer (io.jaegertracing.internal.JaegerTracer)1 InMemoryReporter (io.jaegertracing.internal.reporters.InMemoryReporter)1 ConstSampler (io.jaegertracing.internal.samplers.ConstSampler)1