Search in sources :

Example 16 with TextMap

use of io.opentracing.propagation.TextMap in project jaeger-client-java by jaegertracing.

the class B3TextMapCodec method extract.

@Override
public JaegerSpanContext extract(TextMap carrier) {
    Long traceIdLow = null;
    // It's enough to check for a null low trace id
    Long traceIdHigh = 0L;
    Long spanId = null;
    // Conventionally, parent id == 0 means the root span
    Long parentId = 0L;
    byte flags = 0;
    Map<String, String> baggage = null;
    for (Map.Entry<String, String> entry : carrier) {
        if (entry.getKey().equalsIgnoreCase(SAMPLED_NAME)) {
            String value = entry.getValue();
            if ("1".equals(value) || "true".equalsIgnoreCase(value)) {
                flags |= SAMPLED_FLAG;
            }
        } else if (entry.getKey().equalsIgnoreCase(TRACE_ID_NAME)) {
            traceIdLow = HexCodec.lowerHexToUnsignedLong(entry.getValue());
            traceIdHigh = HexCodec.higherHexToUnsignedLong(entry.getValue());
        } else if (entry.getKey().equalsIgnoreCase(PARENT_SPAN_ID_NAME)) {
            parentId = HexCodec.lowerHexToUnsignedLong(entry.getValue());
        } else if (entry.getKey().equalsIgnoreCase(SPAN_ID_NAME)) {
            spanId = HexCodec.lowerHexToUnsignedLong(entry.getValue());
        } else if (entry.getKey().equalsIgnoreCase(FLAGS_NAME)) {
            if (entry.getValue().equals("1")) {
                flags |= DEBUG_FLAG;
            }
        } else if (entry.getKey().startsWith(baggagePrefix)) {
            if (baggage == null) {
                baggage = new HashMap<String, String>();
            }
            baggage.put(keys.unprefixedKey(entry.getKey(), baggagePrefix), entry.getValue());
        }
    }
    if (null != traceIdLow && null != parentId && null != spanId) {
        JaegerSpanContext spanContext = objectFactory.createSpanContext(traceIdHigh, traceIdLow, spanId, parentId, flags, Collections.<String, String>emptyMap(), // debugId
        null);
        if (baggage != null) {
            spanContext = spanContext.withBaggage(baggage);
        }
        return spanContext;
    }
    return null;
}
Also used : HashMap(java.util.HashMap) TextMap(io.opentracing.propagation.TextMap) Map(java.util.Map) HashMap(java.util.HashMap) JaegerSpanContext(io.jaegertracing.internal.JaegerSpanContext)

Example 17 with TextMap

use of io.opentracing.propagation.TextMap in project brave by openzipkin.

the class BraveTracer method extract.

@Override
public <C> BraveSpanContext extract(Format<C> format, C carrier) {
    if (format != Format.Builtin.HTTP_HEADERS) {
        throw new UnsupportedOperationException(format.toString());
    }
    TraceContextOrSamplingFlags extracted = extractor.extract(new TextMapView(propagationKeys, (TextMap) carrier));
    TraceContext context = extracted.context() != null ? tracer.joinSpan(extracted.context()).context() : tracer.nextSpan(extracted).context();
    return new BraveSpanContext(context);
}
Also used : TraceContext(brave.propagation.TraceContext) TextMap(io.opentracing.propagation.TextMap) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags)

Example 18 with TextMap

use of io.opentracing.propagation.TextMap in project incubator-skywalking by apache.

the class SkywalkingTracerInjectInterceptor method afterMethod.

@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
    Format format = (Format) allArguments[1];
    if (Format.Builtin.TEXT_MAP.equals(format) || Format.Builtin.HTTP_HEADERS.equals(format)) {
        TextMap carrier = (TextMap) allArguments[2];
        ContextCarrier contextCarrier = new ContextCarrier();
        ContextManager.inject(contextCarrier);
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            carrier.put(next.getHeadKey(), next.getHeadValue());
        }
    } else {
    // Don't support other format yet.
    }
    return null;
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) Format(io.opentracing.propagation.Format) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) TextMap(io.opentracing.propagation.TextMap)

Example 19 with TextMap

use of io.opentracing.propagation.TextMap in project jaeger-client-java by jaegertracing.

the class ThriftSpanConverterTest method testRpcChildSpanHasTheSameId.

@Test
public void testRpcChildSpanHasTheSameId() {
    String expectedOperation = "parent";
    Span client = (Span) tracer.buildSpan(expectedOperation).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).startManual();
    Map<String, String> map = new HashMap<>();
    TextMap carrier = new TextMapInjectAdapter(map);
    tracer.inject(client.context(), Format.Builtin.TEXT_MAP, carrier);
    carrier = new TextMapExtractAdapter(map);
    SpanContext ctx = (SpanContext) tracer.extract(Format.Builtin.TEXT_MAP, carrier);
    assertEquals(client.context().getSpanId(), ctx.getSpanId());
    Span server = (Span) tracer.buildSpan("child").withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER).asChildOf(ctx).startManual();
    assertEquals("client and server must have the same span ID", client.context().getSpanId(), server.context().getSpanId());
}
Also used : TextMapExtractAdapter(io.opentracing.propagation.TextMapExtractAdapter) SpanContext(com.uber.jaeger.SpanContext) HashMap(java.util.HashMap) TextMapInjectAdapter(io.opentracing.propagation.TextMapInjectAdapter) TextMap(io.opentracing.propagation.TextMap) Span(com.uber.jaeger.Span) Test(org.junit.Test)

Example 20 with TextMap

use of io.opentracing.propagation.TextMap in project jaeger-client-java by jaegertracing.

the class PropagationTest method testDebugCorrelationId.

@Test
public void testDebugCorrelationId() {
    Tracer tracer = new Tracer.Builder("test", new InMemoryReporter(), new ConstSampler(true)).build();
    Map<String, String> headers = new HashMap<>();
    headers.put(Constants.DEBUG_ID_HEADER_KEY, "Coraline");
    TextMap carrier = new TextMapExtractAdapter(headers);
    SpanContext spanContext = (SpanContext) tracer.extract(Format.Builtin.TEXT_MAP, carrier);
    assertTrue(spanContext.isDebugIdContainerOnly());
    assertEquals("Coraline", spanContext.getDebugId());
    Span span = (Span) tracer.buildSpan("span").asChildOf(spanContext).start();
    spanContext = (SpanContext) span.context();
    assertTrue(spanContext.isSampled());
    assertTrue(spanContext.isDebug());
    assertEquals("Coraline", span.getTags().get(Constants.DEBUG_ID_HEADER_KEY));
}
Also used : InMemoryReporter(com.uber.jaeger.reporters.InMemoryReporter) TextMapExtractAdapter(io.opentracing.propagation.TextMapExtractAdapter) HashMap(java.util.HashMap) ConstSampler(com.uber.jaeger.samplers.ConstSampler) TextMap(io.opentracing.propagation.TextMap) Test(org.junit.Test)

Aggregations

TextMap (io.opentracing.propagation.TextMap)29 Test (org.junit.Test)16 HashMap (java.util.HashMap)14 Map (java.util.Map)7 SpanContext (com.uber.jaeger.SpanContext)6 JaegerSpanContext (io.jaegertracing.internal.JaegerSpanContext)6 ConstSampler (com.uber.jaeger.samplers.ConstSampler)3 Format (io.opentracing.propagation.Format)3 TextMapAdapter (io.opentracing.propagation.TextMapAdapter)3 SpanId (com.github.kristofa.brave.SpanId)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2 Metrics (com.uber.jaeger.metrics.Metrics)2 InMemoryReporter (com.uber.jaeger.reporters.InMemoryReporter)2 RequestTraceSpanContext (fish.payara.notification.requesttracing.RequestTraceSpanContext)2 JaegerSpan (io.jaegertracing.internal.JaegerSpan)2 TextMapExtractAdapter (io.opentracing.propagation.TextMapExtractAdapter)2 MultiMap (io.vertx.core.MultiMap)2 ByteBuffer (java.nio.ByteBuffer)2 Iterator (java.util.Iterator)2 Entry (java.util.Map.Entry)2