Search in sources :

Example 26 with SpanContext

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

the class InstrumenterTest method server_http.

@Test
void server_http() {
    Instrumenter<Map<String, String>, Map<String, String>> instrumenter = Instrumenter.<Map<String, String>, Map<String, String>>builder(otelTesting.getOpenTelemetry(), "test", unused -> "span").addAttributesExtractors(mockHttpServerAttributes, NetServerAttributesExtractor.create(new ConstantNetPeerIpGetter<>("2.2.2.2")), new AttributesExtractor1(), new AttributesExtractor2()).addSpanLinksExtractor(new LinksExtractor()).newServerInstrumenter(new MapGetter());
    Context context = instrumenter.start(Context.root(), REQUEST);
    SpanContext spanContext = Span.fromContext(context).getSpanContext();
    assertThat(spanContext.isValid()).isTrue();
    assertThat(SpanKey.SERVER.fromContextOrNull(context).getSpanContext()).isEqualTo(spanContext);
    instrumenter.end(context, REQUEST, RESPONSE, null);
    otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("span").hasAttributesSatisfying(attributes -> assertThat(attributes).containsEntry(SemanticAttributes.NET_PEER_IP, "2.2.2.2"))));
}
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) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 27 with SpanContext

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

the class BaseServletHelper method start.

public Context start(Context parentContext, ServletRequestContext<REQUEST> requestContext) {
    Context context = instrumenter.start(parentContext, requestContext);
    REQUEST request = requestContext.request();
    SpanContext spanContext = Span.fromContext(context).getSpanContext();
    // we do this e.g. so that servlet containers can use these values in their access logs
    // TODO: These are only available when using servlet instrumentation or when server
    // instrumentation extends servlet instrumentation e.g. jetty. Either remove or make sure they
    // also work on tomcat and wildfly.
    accessor.setRequestAttribute(request, "trace_id", spanContext.getTraceId());
    accessor.setRequestAttribute(request, "span_id", spanContext.getSpanId());
    context = addServletContextPath(context, request);
    attachServerContext(context, request);
    return context;
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContext(io.opentelemetry.api.trace.SpanContext)

Example 28 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project smallrye-reactive-messaging by smallrye.

the class TracingAmqpToAppWithParentTest method testFromAmqpToAppWithParentSpan.

@Test
public void testFromAmqpToAppWithParentSpan() {
    weld.addBeanClass(MyAppReceivingData.class);
    new MapBasedConfig().put("mp.messaging.incoming.stuff.connector", AmqpConnector.CONNECTOR_NAME).put("mp.messaging.incoming.stuff.host", host).put("mp.messaging.incoming.stuff.port", port).put("amqp-username", username).put("amqp-password", password).write();
    container = weld.initialize();
    MyAppReceivingData bean = container.getBeanManager().createInstance().select(MyAppReceivingData.class).get();
    await().until(() -> isAmqpConnectorReady(container));
    AtomicInteger count = new AtomicInteger();
    List<SpanContext> producedSpanContexts = new CopyOnWriteArrayList<>();
    usage.produce("stuff", 10, () -> AmqpMessage.create().durable(false).ttl(10000).withIntegerAsBody(count.getAndIncrement()).applicationProperties(createTracingSpan(producedSpanContexts, "stuff")).build());
    await().until(() -> bean.list().size() >= 10);
    assertThat(bean.list()).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    List<String> producedTraceIds = producedSpanContexts.stream().map(SpanContext::getTraceId).collect(Collectors.toList());
    assertThat(producedTraceIds).hasSize(10);
    assertThat(bean.tracing()).hasSizeGreaterThanOrEqualTo(10);
    assertThat(bean.tracing()).doesNotContainNull().doesNotHaveDuplicates();
    List<String> receivedTraceIds = bean.tracing().stream().map(tracingMetadata -> Span.fromContext(tracingMetadata.getCurrentContext()).getSpanContext().getTraceId()).collect(Collectors.toList());
    assertThat(receivedTraceIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
    assertThat(receivedTraceIds).containsExactlyInAnyOrderElementsOf(producedTraceIds);
    List<String> spanIds = new ArrayList<>();
    for (TracingMetadata tracing : bean.tracing()) {
        spanIds.add(Span.fromContext(tracing.getCurrentContext()).getSpanContext().getSpanId());
        assertThat(tracing.getPreviousContext()).isNotNull();
        Span previousSpan = Span.fromContextOrNull(tracing.getPreviousContext());
        assertThat(previousSpan).isNotNull();
        assertThat(previousSpan.getSpanContext().getTraceId()).isEqualTo(Span.fromContext(tracing.getCurrentContext()).getSpanContext().getTraceId());
        assertThat(previousSpan.getSpanContext().getSpanId()).isNotEqualTo(Span.fromContext(tracing.getCurrentContext()).getSpanContext().getSpanId());
    }
    assertThat(spanIds).doesNotContainNull().doesNotHaveDuplicates().hasSizeGreaterThanOrEqualTo(10);
    List<String> parentIds = bean.tracing().stream().map(tracingMetadata -> Span.fromContextOrNull(tracingMetadata.getPreviousContext()).getSpanContext().getSpanId()).collect(Collectors.toList());
    assertThat(producedSpanContexts.stream().map(SpanContext::getSpanId)).containsExactlyElementsOf(parentIds);
    for (SpanData data : testExporter.getFinishedSpanItems()) {
        assertThat(data.getSpanId()).isIn(spanIds);
        assertThat(data.getSpanId()).isNotEqualTo(data.getParentSpanId());
        assertThat(data.getKind()).isEqualByComparingTo(SpanKind.CONSUMER);
        assertThat(data.getParentSpanId()).isNotNull();
        assertThat(data.getParentSpanId()).isIn(parentIds);
    }
}
Also used : Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) BeforeEach(org.junit.jupiter.api.BeforeEach) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) SimpleSpanProcessor(io.opentelemetry.sdk.trace.export.SimpleSpanProcessor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContextPropagators(io.opentelemetry.context.propagation.ContextPropagators) SmallRyeConfigProviderResolver(io.smallrye.config.SmallRyeConfigProviderResolver) JsonObject(io.vertx.core.json.JsonObject) Weld(org.jboss.weld.environment.se.Weld) AmqpMessage(io.vertx.mutiny.amqp.AmqpMessage) Context(io.opentelemetry.context.Context) Awaitility.await(org.awaitility.Awaitility.await) Properties(java.util.Properties) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Collectors(java.util.stream.Collectors) SpanKind(io.opentelemetry.api.trace.SpanKind) StandardCharsets(java.nio.charset.StandardCharsets) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) Test(org.junit.jupiter.api.Test) List(java.util.List) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) CompletionStage(java.util.concurrent.CompletionStage) InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) ApplicationScoped(javax.enterprise.context.ApplicationScoped) SpanData(io.opentelemetry.sdk.trace.data.SpanData) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) SpanProcessor(io.opentelemetry.sdk.trace.SpanProcessor) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanData(io.opentelemetry.sdk.trace.data.SpanData) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Span(io.opentelemetry.api.trace.Span) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 29 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project smallrye-reactive-messaging by smallrye.

the class TracingAmqpToAppToAmqpTest method createTracingSpan.

private JsonObject createTracingSpan(List<SpanContext> spanContexts, String topic) {
    Properties properties = new Properties();
    final Span span = AmqpConnector.TRACER.spanBuilder(topic).setSpanKind(SpanKind.PRODUCER).startSpan();
    final Context context = span.storeInContext(Context.current());
    GlobalOpenTelemetry.getPropagators().getTextMapPropagator().inject(context, properties, (headers, key, value) -> {
        if (headers != null) {
            headers.put(key, value.getBytes(StandardCharsets.UTF_8));
        }
    });
    spanContexts.add(span.getSpanContext());
    return JsonObject.mapFrom(properties);
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) Properties(java.util.Properties) Span(io.opentelemetry.api.trace.Span)

Example 30 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project smallrye-reactive-messaging by smallrye.

the class TracingPropagationTest method createTracingSpan.

private Iterable<Header> createTracingSpan(List<SpanContext> spanContexts, String topic) {
    RecordHeaders proposedHeaders = new RecordHeaders();
    final Span span = KafkaConnector.TRACER.spanBuilder(topic).setSpanKind(SpanKind.PRODUCER).startSpan();
    final Context context = span.storeInContext(Context.current());
    GlobalOpenTelemetry.getPropagators().getTextMapPropagator().inject(context, proposedHeaders, (headers, key, value) -> {
        if (headers != null) {
            headers.remove(key).add(key, value.getBytes(StandardCharsets.UTF_8));
        }
    });
    spanContexts.add(span.getSpanContext());
    return proposedHeaders;
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) 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