Search in sources :

Example 1 with ContextKey

use of io.opentelemetry.context.ContextKey in project opentelemetry-java-instrumentation by open-telemetry.

the class InstrumenterTest method shouldUseContextCustomizer.

@Test
void shouldUseContextCustomizer() {
    // given
    ContextKey<String> testKey = ContextKey.named("test");
    Instrumenter<String, String> instrumenter = Instrumenter.<String, String>builder(otelTesting.getOpenTelemetry(), "test", request -> "test span").addContextCustomizer((context, request, attributes) -> context.with(testKey, "testVal")).newInstrumenter();
    // when
    Context context = instrumenter.start(Context.root(), "request");
    // then
    assertThat(context.get(testKey)).isEqualTo("testVal");
}
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) Test(org.junit.jupiter.api.Test)

Aggregations

Attributes (io.opentelemetry.api.common.Attributes)1 AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)1 Span (io.opentelemetry.api.trace.Span)1 SpanContext (io.opentelemetry.api.trace.SpanContext)1 SpanId (io.opentelemetry.api.trace.SpanId)1 SpanKind (io.opentelemetry.api.trace.SpanKind)1 TraceFlags (io.opentelemetry.api.trace.TraceFlags)1 TraceId (io.opentelemetry.api.trace.TraceId)1 TraceState (io.opentelemetry.api.trace.TraceState)1 W3CTraceContextPropagator (io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator)1 Context (io.opentelemetry.context.Context)1 ContextKey (io.opentelemetry.context.ContextKey)1 TextMapGetter (io.opentelemetry.context.propagation.TextMapGetter)1 InstrumentationVersion (io.opentelemetry.instrumentation.api.InstrumentationVersion)1 DbClientAttributesExtractor (io.opentelemetry.instrumentation.api.instrumenter.db.DbClientAttributesExtractor)1 HttpClientAttributesExtractor (io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor)1 HttpServerAttributesExtractor (io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor)1 MessageOperation (io.opentelemetry.instrumentation.api.instrumenter.messaging.MessageOperation)1 MessagingAttributesExtractor (io.opentelemetry.instrumentation.api.instrumenter.messaging.MessagingAttributesExtractor)1 NetServerAttributesExtractor (io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesExtractor)1