Search in sources :

Example 6 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.

the class ImmutableSpanContextTest method testWithIdValidationAndValidIds.

@Test
public void testWithIdValidationAndValidIds() {
    SpanContext spanContext = ImmutableSpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TraceState.getDefault(), false, false);
    assertThat(spanContext.isValid()).isTrue();
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) Test(org.junit.jupiter.api.Test)

Example 7 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.

the class ImmutableSpanContextTest method testSkipIdValidationAndInvalidSpanId.

@Test
public void testSkipIdValidationAndInvalidSpanId() {
    SpanContext spanContext = ImmutableSpanContext.create(TRACE_ID, SpanId.getInvalid(), TraceFlags.getDefault(), TraceState.getDefault(), false, true);
    assertThat(spanContext.isValid()).isTrue();
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) Test(org.junit.jupiter.api.Test)

Example 8 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.

the class W3CTraceContextPropagator method extractImpl.

private static <C> SpanContext extractImpl(@Nullable C carrier, TextMapGetter<C> getter) {
    String traceParent = getter.get(carrier, TRACE_PARENT);
    if (traceParent == null) {
        return SpanContext.getInvalid();
    }
    SpanContext contextFromParentHeader = extractContextFromTraceParent(traceParent);
    if (!contextFromParentHeader.isValid()) {
        return contextFromParentHeader;
    }
    String traceStateHeader = getter.get(carrier, TRACE_STATE);
    if (traceStateHeader == null || traceStateHeader.isEmpty()) {
        return contextFromParentHeader;
    }
    try {
        TraceState traceState = decodeTraceState(traceStateHeader);
        return SpanContext.createFromRemoteParent(contextFromParentHeader.getTraceId(), contextFromParentHeader.getSpanId(), contextFromParentHeader.getTraceFlags(), traceState);
    } catch (IllegalArgumentException e) {
        logger.fine("Unparseable tracestate header. Returning span context without state.");
        return contextFromParentHeader;
    }
}
Also used : W3CTraceContextEncoding.decodeTraceState(io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.decodeTraceState) W3CTraceContextEncoding.encodeTraceState(io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.encodeTraceState) TraceState(io.opentelemetry.api.trace.TraceState) SpanContext(io.opentelemetry.api.trace.SpanContext)

Example 9 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.

the class OtTracePropagator method extract.

@Override
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
    if (context == null) {
        return Context.root();
    }
    if (getter == null) {
        return context;
    }
    String incomingTraceId = getter.get(carrier, TRACE_ID_HEADER);
    String traceId = incomingTraceId == null ? TraceId.getInvalid() : StringUtils.padLeft(incomingTraceId, MAX_TRACE_ID_LENGTH);
    String spanId = getter.get(carrier, SPAN_ID_HEADER);
    String sampled = getter.get(carrier, SAMPLED_HEADER);
    SpanContext spanContext = buildSpanContext(traceId, spanId, sampled);
    if (!spanContext.isValid()) {
        return context;
    }
    Context extractedContext = context.with(Span.wrap(spanContext));
    // Baggage is only extracted if there is a valid SpanContext
    if (carrier != null) {
        BaggageBuilder baggageBuilder = Baggage.builder();
        for (String key : getter.keys(carrier)) {
            if (!key.startsWith(PREFIX_BAGGAGE_HEADER)) {
                continue;
            }
            String value = getter.get(carrier, key);
            if (value == null) {
                continue;
            }
            baggageBuilder.put(key.replace(OtTracePropagator.PREFIX_BAGGAGE_HEADER, ""), value);
        }
        Baggage baggage = baggageBuilder.build();
        if (!baggage.isEmpty()) {
            extractedContext = extractedContext.with(baggage);
        }
    }
    return extractedContext;
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContext(io.opentelemetry.api.trace.SpanContext) BaggageBuilder(io.opentelemetry.api.baggage.BaggageBuilder) Baggage(io.opentelemetry.api.baggage.Baggage)

Example 10 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.

the class JaegerPropagator method inject.

@Override
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
    if (context == null) {
        return;
    }
    if (setter == null) {
        return;
    }
    SpanContext spanContext = Span.fromContext(context).getSpanContext();
    if (spanContext.isValid()) {
        injectSpan(spanContext, carrier, setter);
    }
    injectBaggage(Baggage.fromContext(context), carrier, setter);
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext)

Aggregations

SpanContext (io.opentelemetry.api.trace.SpanContext)62 Test (org.junit.jupiter.api.Test)33 Span (io.opentelemetry.api.trace.Span)31 Context (io.opentelemetry.context.Context)31 SpanKind (io.opentelemetry.api.trace.SpanKind)13 TraceState (io.opentelemetry.api.trace.TraceState)12 W3CTraceContextPropagator (io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator)12 Collectors (java.util.stream.Collectors)12 Attributes (io.opentelemetry.api.common.Attributes)11 TraceFlags (io.opentelemetry.api.trace.TraceFlags)10 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)10 Instant (java.time.Instant)10 SpanId (io.opentelemetry.api.trace.SpanId)9 TraceId (io.opentelemetry.api.trace.TraceId)9 LinkData (io.opentelemetry.sdk.trace.data.LinkData)9 Mockito.when (org.mockito.Mockito.when)9 AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)8 ContextKey (io.opentelemetry.context.ContextKey)7 TextMapGetter (io.opentelemetry.context.propagation.TextMapGetter)7 InstrumentationVersion (io.opentelemetry.instrumentation.api.InstrumentationVersion)7