Search in sources :

Example 16 with SpanContext

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

the class CamelPropagationUtilTest method shouldExtractAwsParentForSqsEndpoint.

@Test
public void shouldExtractAwsParentForSqsEndpoint() {
    // given
    Endpoint endpoint = new SqsEndpoint("", new SqsComponent(), new SqsConfiguration());
    Map<String, Object> exchangeHeaders = Collections.singletonMap("AWSTraceHeader", "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\n");
    // when
    Context parent = CamelPropagationUtil.extractParent(exchangeHeaders, endpoint);
    // then
    Span parentSpan = Span.fromContext(parent);
    SpanContext parentSpanContext = parentSpan.getSpanContext();
    assertThat(parentSpanContext.getTraceId()).isEqualTo("5759e988bd862e3fe1be46a994272793");
    assertThat(parentSpanContext.getSpanId()).isEqualTo("53995c3f42cd8ad8");
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SqsComponent(org.apache.camel.component.aws.sqs.SqsComponent) SpanContext(io.opentelemetry.api.trace.SpanContext) HttpEndpoint(org.apache.camel.component.http.HttpEndpoint) SqsEndpoint(org.apache.camel.component.aws.sqs.SqsEndpoint) Endpoint(org.apache.camel.Endpoint) SqsConfiguration(org.apache.camel.component.aws.sqs.SqsConfiguration) SqsEndpoint(org.apache.camel.component.aws.sqs.SqsEndpoint) Span(io.opentelemetry.api.trace.Span) Test(org.junit.jupiter.api.Test)

Example 17 with SpanContext

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

the class OpenTelemetryContextDataProvider method supplyContextData.

/**
 * Returns context from the current span when available.
 *
 * @return A map containing string versions of the traceId, spanId, and traceFlags, which can then
 *     be accessed from layout components
 */
@Override
public Map<String, String> supplyContextData() {
    Span currentSpan = Span.current();
    if (!currentSpan.getSpanContext().isValid()) {
        return Collections.emptyMap();
    }
    Map<String, String> contextData = new HashMap<>();
    SpanContext spanContext = currentSpan.getSpanContext();
    contextData.put(TRACE_ID, spanContext.getTraceId());
    contextData.put(SPAN_ID, spanContext.getSpanId());
    contextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());
    return contextData;
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) HashMap(java.util.HashMap) Span(io.opentelemetry.api.trace.Span)

Example 18 with SpanContext

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

the class OpenTelemetryAppender method wrapEvent.

public static ILoggingEvent wrapEvent(ILoggingEvent event) {
    Span currentSpan = Span.current();
    if (!currentSpan.getSpanContext().isValid()) {
        return event;
    }
    Map<String, String> eventContext = event.getMDCPropertyMap();
    if (eventContext != null && eventContext.containsKey(TRACE_ID)) {
        // Assume already instrumented event if traceId is present.
        return event;
    }
    Map<String, String> contextData = new HashMap<>();
    SpanContext spanContext = currentSpan.getSpanContext();
    contextData.put(TRACE_ID, spanContext.getTraceId());
    contextData.put(SPAN_ID, spanContext.getSpanId());
    contextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());
    if (eventContext == null) {
        eventContext = contextData;
    } else {
        eventContext = new UnionMap<>(eventContext, contextData);
    }
    return new LoggingEventWrapper(event, eventContext);
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) HashMap(java.util.HashMap) Span(io.opentelemetry.api.trace.Span)

Example 19 with SpanContext

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

the class SpanDecoratingContextDataInjector method injectContextData.

@Override
public StringMap injectContextData(List<Property> list, StringMap stringMap) {
    StringMap contextData = delegate.injectContextData(list, stringMap);
    if (contextData.containsKey(TRACE_ID)) {
        // Assume already instrumented event if traceId is present.
        return contextData;
    }
    SpanContext currentContext = Java8BytecodeBridge.currentSpan().getSpanContext();
    if (!currentContext.isValid()) {
        return contextData;
    }
    StringMap newContextData = new SortedArrayStringMap(contextData);
    newContextData.putValue(TRACE_ID, currentContext.getTraceId());
    newContextData.putValue(SPAN_ID, currentContext.getSpanId());
    newContextData.putValue(TRACE_FLAGS, currentContext.getTraceFlags().asHex());
    return newContextData;
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) SpanContext(io.opentelemetry.api.trace.SpanContext) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap)

Example 20 with SpanContext

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

the class ParentContextExtractor method isValidAndSampled.

private static boolean isValidAndSampled(Context context) {
    if (context == null) {
        return false;
    }
    Span parentSpan = Span.fromContext(context);
    SpanContext parentSpanContext = parentSpan.getSpanContext();
    return (parentSpanContext.isValid() && parentSpanContext.isSampled());
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) Span(io.opentelemetry.api.trace.Span)

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