Search in sources :

Example 6 with TextMapExtractAdapter

use of io.opentracing.propagation.TextMapExtractAdapter 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)

Example 7 with TextMapExtractAdapter

use of io.opentracing.propagation.TextMapExtractAdapter in project motan by weibocom.

the class OpenTracingFilter method extractTraceInfo.

protected Span extractTraceInfo(Request request, Tracer tracer) {
    String operationName = buildOperationName(request);
    SpanBuilder span = tracer.buildSpan(operationName);
    try {
        SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(request.getAttachments()));
        if (spanContext != null) {
            span.asChildOf(spanContext);
        }
    } catch (Exception e) {
        span.withTag("Error", "extract from request fail, error msg:" + e.getMessage());
    }
    return span.start();
}
Also used : SpanBuilder(io.opentracing.Tracer.SpanBuilder) TextMapExtractAdapter(io.opentracing.propagation.TextMapExtractAdapter) SpanContext(io.opentracing.SpanContext)

Example 8 with TextMapExtractAdapter

use of io.opentracing.propagation.TextMapExtractAdapter in project wso2-synapse by wso2.

the class JaegerSpanHandler method startSpan.

/**
 * Starts a span, and stores necessary information in the span store to retrieve them back when needed.
 * @param statisticDataUnit Statistic data unit object, which was collected during a statistic event.
 * @param synCtx            Message context.
 * @param spanStore         Span store object.
 */
private void startSpan(StatisticDataUnit statisticDataUnit, MessageContext synCtx, SpanStore spanStore) {
    SpanWrapper parentSpanWrapper = ParentResolver.resolveParent(statisticDataUnit, spanStore, synCtx);
    Span parentSpan = null;
    if (parentSpanWrapper != null) {
        parentSpan = parentSpanWrapper.getSpan();
    }
    Span span;
    SpanContext spanContext;
    Map<String, String> tracerSpecificCarrier = new HashMap<>();
    Map headersMap = (Map) ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    Object statusCode = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty("HTTP_SC");
    Object statusDescription = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty("HTTP_DESC");
    // We only need to extract span context from headers when there are trp headers available
    if (isOuterLevelSpan(statisticDataUnit, spanStore) && headersMap != null) {
        // Extract span context from headers
        spanContext = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapExtractAdapter(headersMap));
        span = tracer.buildSpan(statisticDataUnit.getComponentName()).asChildOf(spanContext).start();
    } else {
        span = tracer.buildSpan(statisticDataUnit.getComponentName()).asChildOf(parentSpan).start();
        spanContext = span.context();
    }
    // Fix null pointer issue occurs when spanContext become null
    if (spanContext != null) {
        // Set tracing headers
        tracer.inject(spanContext, Format.Builtin.HTTP_HEADERS, new TextMapInjectAdapter(tracerSpecificCarrier));
    }
    // <property name="TRANSPORT_HEADERS" action="remove" scope="axis2"/>
    if (headersMap != null) {
        headersMap.putAll(tracerSpecificCarrier);
        statisticDataUnit.setTransportHeaderMap(headersMap);
    }
    if (statusCode != null) {
        statisticDataUnit.setStatusCode(statusCode.toString());
    }
    if (statusDescription != null) {
        statisticDataUnit.setStatusDescription(statusDescription.toString());
    }
    if (statisticDataUnit.getComponentType() != null & statisticDataUnit.getComponentType() == ComponentType.ENDPOINT) {
        statisticDataUnit.setEndpoint(synCtx.getEndpoint(statisticDataUnit.getComponentName()));
    }
    String spanId = TracingUtils.extractId(statisticDataUnit);
    SpanWrapper spanWrapper = spanStore.addSpanWrapper(spanId, span, statisticDataUnit, parentSpanWrapper, synCtx);
    if (isOuterLevelSpan(statisticDataUnit, spanStore)) {
        spanStore.assignOuterLevelSpan(spanWrapper);
    }
}
Also used : SpanWrapper(org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper) TextMapExtractAdapter(io.opentracing.propagation.TextMapExtractAdapter) SpanContext(io.opentracing.SpanContext) HashMap(java.util.HashMap) TextMapInjectAdapter(io.opentracing.propagation.TextMapInjectAdapter) Span(io.opentracing.Span) HashMap(java.util.HashMap) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

TextMapExtractAdapter (io.opentracing.propagation.TextMapExtractAdapter)8 Test (org.junit.Test)6 BraveSpanContext (brave.opentracing.BraveSpanContext)4 LinkedHashMap (java.util.LinkedHashMap)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 HashMap (java.util.HashMap)3 SpanContext (io.opentracing.SpanContext)2 TextMap (io.opentracing.propagation.TextMap)2 TextMapInjectAdapter (io.opentracing.propagation.TextMapInjectAdapter)2 Span (com.uber.jaeger.Span)1 SpanContext (com.uber.jaeger.SpanContext)1 InMemoryReporter (com.uber.jaeger.reporters.InMemoryReporter)1 ConstSampler (com.uber.jaeger.samplers.ConstSampler)1 Span (io.opentracing.Span)1 SpanBuilder (io.opentracing.Tracer.SpanBuilder)1 Map (java.util.Map)1 SpanWrapper (org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1