Search in sources :

Example 21 with SpanContext

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

the class ParentContextExtractorTest method shouldUseHttpIfAwsParentNotSampled.

@Test
void shouldUseHttpIfAwsParentNotSampled() {
    // given
    Map<String, String> headers = ImmutableMap.of("X-b3-traceId", "4fd0b6131f19f39af59518d127b0cafe", "x-b3-spanid", "0000000000000123", "X-B3-Sampled", "true");
    environmentVariables.set("_X_AMZN_TRACE_ID", "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=0000000000000456;Sampled=0");
    // when
    Context context = ParentContextExtractor.extract(headers, INSTRUMENTER);
    // then
    Span span = Span.fromContext(context);
    SpanContext spanContext = span.getSpanContext();
    assertThat(spanContext.isValid()).isTrue();
    assertThat(spanContext.isValid()).isTrue();
    assertThat(spanContext.getSpanId()).isEqualTo("0000000000000123");
    assertThat(spanContext.getTraceId()).isEqualTo("4fd0b6131f19f39af59518d127b0cafe");
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContext(io.opentelemetry.api.trace.SpanContext) Span(io.opentelemetry.api.trace.Span) Test(org.junit.jupiter.api.Test)

Example 22 with SpanContext

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

the class ParentContextExtractorTest method shouldPreferAwsParentHeaderIfValidAndSampled.

@Test
void shouldPreferAwsParentHeaderIfValidAndSampled() {
    // given
    Map<String, String> headers = ImmutableMap.of("X-b3-traceId", "4fd0b6131f19f39af59518d127b0cafe", "x-b3-spanid", "0000000000000456", "X-B3-Sampled", "true");
    environmentVariables.set("_X_AMZN_TRACE_ID", "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=0000000000000456;Sampled=1");
    // when
    Context context = ParentContextExtractor.extract(headers, INSTRUMENTER);
    // then
    Span span = Span.fromContext(context);
    SpanContext spanContext = span.getSpanContext();
    assertThat(spanContext.isValid()).isTrue();
    assertThat(spanContext.isValid()).isTrue();
    assertThat(spanContext.getSpanId()).isEqualTo("0000000000000456");
    assertThat(spanContext.getTraceId()).isEqualTo("8a3c60f7d188f8fa79d48a391a778fa6");
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContext(io.opentelemetry.api.trace.SpanContext) Span(io.opentelemetry.api.trace.Span) Test(org.junit.jupiter.api.Test)

Example 23 with SpanContext

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

the class ContextDispatcher method dispatch.

@Override
// Instrumenting deprecated class
@SuppressWarnings("deprecation")
public void dispatch(Remote obj, java.rmi.server.RemoteCall call) throws IOException {
    ObjectInput in = call.getInputStream();
    int operationId = in.readInt();
    // skip 8 bytes
    in.readLong();
    if (PROPAGATOR.isOperationWithPayload(operationId)) {
        ContextPayload payload = ContextPayload.read(in);
        if (payload != null) {
            Context context = payload.extract();
            SpanContext spanContext = Span.fromContext(context).getSpanContext();
            if (spanContext.isValid()) {
                THREAD_LOCAL_CONTEXT.set(context);
            } else {
                THREAD_LOCAL_CONTEXT.set(null);
            }
        }
    }
    // send result stream the client is expecting
    call.getResultStream(true);
    // release held streams to allow next call to continue
    call.releaseInputStream();
    call.releaseOutputStream();
    call.done();
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) ContextPayload(io.opentelemetry.javaagent.instrumentation.rmi.context.ContextPayload) SpanContext(io.opentelemetry.api.trace.SpanContext) ObjectInput(java.io.ObjectInput)

Example 24 with SpanContext

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

the class InstrumenterTest method client.

@Test
void client() {
    Instrumenter<Map<String, String>, Map<String, String>> instrumenter = Instrumenter.<Map<String, String>, Map<String, String>>builder(otelTesting.getOpenTelemetry(), "test", unused -> "span").addAttributesExtractors(new AttributesExtractor1(), new AttributesExtractor2()).addSpanLinksExtractor(new LinksExtractor()).newClientInstrumenter(Map::put);
    Map<String, String> request = new HashMap<>(REQUEST);
    Context context = instrumenter.start(Context.root(), request);
    SpanContext spanContext = Span.fromContext(context).getSpanContext();
    assertThat(spanContext.isValid()).isTrue();
    assertThat(request).containsKey("traceparent");
    instrumenter.end(context, request, RESPONSE, null);
    otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("span").hasKind(SpanKind.CLIENT).hasInstrumentationLibraryInfo(InstrumentationLibraryInfo.create("test", null)).hasTraceId(spanContext.getTraceId()).hasSpanId(spanContext.getSpanId()).hasParentSpanId(SpanId.getInvalid()).hasStatus(StatusData.unset()).hasLinks(expectedSpanLink()).hasAttributesSatisfying(attributes -> assertThat(attributes).containsOnly(attributeEntry("req1", "req1_value"), attributeEntry("req2", "req2_2_value"), attributeEntry("req3", "req3_value"), attributeEntry("resp1", "resp1_value"), attributeEntry("resp2", "resp2_2_value"), attributeEntry("resp3", "resp3_value")))));
}
Also used : NetServerAttributesGetter(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter) TraceFlags(io.opentelemetry.api.trace.TraceFlags) OpenTelemetryExtension(io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension) SpanId(io.opentelemetry.api.trace.SpanId) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) Mock(org.mockito.Mock) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) StatusData(io.opentelemetry.sdk.trace.data.StatusData) Attributes(io.opentelemetry.api.common.Attributes) HashMap(java.util.HashMap) InstrumentationVersion(io.opentelemetry.instrumentation.api.InstrumentationVersion) AtomicReference(java.util.concurrent.atomic.AtomicReference) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) TraceState(io.opentelemetry.api.trace.TraceState) SpanKey(io.opentelemetry.instrumentation.api.internal.SpanKey) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Map(java.util.Map) DbClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.db.DbClientAttributesExtractor) HttpServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor) HttpClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor) Nullable(javax.annotation.Nullable) OpenTelemetryAssertions.attributeEntry(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry) ContextKey(io.opentelemetry.context.ContextKey) Context(io.opentelemetry.context.Context) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) MessageOperation(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessageOperation) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) SpanKind(io.opentelemetry.api.trace.SpanKind) Collectors(java.util.stream.Collectors) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test) NetServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesExtractor) Stream(java.util.stream.Stream) OpenTelemetryAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) TraceId(io.opentelemetry.api.trace.TraceId) MessagingAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessagingAttributesExtractor) LinkData(io.opentelemetry.sdk.trace.data.LinkData) RpcAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcAttributesExtractor) Collections(java.util.Collections) Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContext(io.opentelemetry.api.trace.SpanContext) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 25 with SpanContext

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

the class InstrumenterTest method client_parent.

@Test
void client_parent() {
    Instrumenter<Map<String, String>, Map<String, String>> instrumenter = Instrumenter.<Map<String, String>, Map<String, String>>builder(otelTesting.getOpenTelemetry(), "test", unused -> "span").addAttributesExtractors(new AttributesExtractor1(), new AttributesExtractor2()).newClientInstrumenter(Map::put);
    Context parent = Context.root().with(Span.wrap(SpanContext.create("ff01020304050600ff0a0b0c0d0e0f00", "090a0b0c0d0e0f00", TraceFlags.getSampled(), TraceState.getDefault())));
    Map<String, String> request = new HashMap<>(REQUEST);
    Context context = instrumenter.start(parent, request);
    SpanContext spanContext = Span.fromContext(context).getSpanContext();
    assertThat(spanContext.isValid()).isTrue();
    assertThat(request).containsKey("traceparent");
    instrumenter.end(context, request, RESPONSE, new IllegalStateException("test"));
    otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("span").hasTraceId("ff01020304050600ff0a0b0c0d0e0f00").hasSpanId(spanContext.getSpanId()).hasParentSpanId("090a0b0c0d0e0f00")));
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) NetServerAttributesGetter(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter) TraceFlags(io.opentelemetry.api.trace.TraceFlags) OpenTelemetryExtension(io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension) SpanId(io.opentelemetry.api.trace.SpanId) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) Mock(org.mockito.Mock) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) StatusData(io.opentelemetry.sdk.trace.data.StatusData) Attributes(io.opentelemetry.api.common.Attributes) HashMap(java.util.HashMap) InstrumentationVersion(io.opentelemetry.instrumentation.api.InstrumentationVersion) AtomicReference(java.util.concurrent.atomic.AtomicReference) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) TraceState(io.opentelemetry.api.trace.TraceState) SpanKey(io.opentelemetry.instrumentation.api.internal.SpanKey) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Map(java.util.Map) DbClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.db.DbClientAttributesExtractor) HttpServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor) HttpClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor) Nullable(javax.annotation.Nullable) OpenTelemetryAssertions.attributeEntry(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry) ContextKey(io.opentelemetry.context.ContextKey) Context(io.opentelemetry.context.Context) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) MessageOperation(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessageOperation) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) SpanKind(io.opentelemetry.api.trace.SpanKind) Collectors(java.util.stream.Collectors) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test) NetServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesExtractor) Stream(java.util.stream.Stream) OpenTelemetryAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) TraceId(io.opentelemetry.api.trace.TraceId) MessagingAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessagingAttributesExtractor) LinkData(io.opentelemetry.sdk.trace.data.LinkData) RpcAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcAttributesExtractor) Collections(java.util.Collections) SpanContext(io.opentelemetry.api.trace.SpanContext) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

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