Search in sources :

Example 1 with TracingMetadata

use of io.smallrye.reactive.messaging.TracingMetadata in project smallrye-reactive-messaging by smallrye.

the class AmqpConnector method incomingTrace.

private void incomingTrace(AmqpMessage<?> message) {
    TracingMetadata tracingMetadata = TracingMetadata.fromMessage(message).orElse(TracingMetadata.empty());
    final SpanBuilder spanBuilder = TRACER.spanBuilder(message.getAddress() + " receive").setSpanKind(SpanKind.CONSUMER);
    // Handle possible parent span
    final Context parentSpanContext = tracingMetadata.getPreviousContext();
    if (parentSpanContext != null) {
        spanBuilder.setParent(parentSpanContext);
    } else {
        spanBuilder.setNoParent();
    }
    final Span span = spanBuilder.startSpan();
    // Set Span attributes
    span.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "AMQP 1.0");
    span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, message.getAddress());
    span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "queue");
    // Make available as parent for subsequent spans inside message processing
    span.makeCurrent();
    message.injectTracingMetadata(tracingMetadata.withSpan(span));
    span.end();
}
Also used : Context(io.opentelemetry.context.Context) SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) Span(io.opentelemetry.api.trace.Span)

Example 2 with TracingMetadata

use of io.smallrye.reactive.messaging.TracingMetadata 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 3 with TracingMetadata

use of io.smallrye.reactive.messaging.TracingMetadata in project smallrye-reactive-messaging by smallrye.

the class TracingAmqpToAppNoParentTest method testFromAmqpToAppWithNoParent.

@Test
public void testFromAmqpToAppWithNoParent() {
    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("mp.messaging.incoming.stuff.address", "no-parent-stuff").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();
    usage.produce("no-parent-stuff", 10, count::getAndIncrement);
    await().until(() -> bean.list().size() >= 10);
    assertThat(bean.list()).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    assertThat(bean.tracing()).hasSizeGreaterThanOrEqualTo(10);
    assertThat(bean.tracing()).doesNotContainNull().doesNotHaveDuplicates();
    List<String> spanIds = new ArrayList<>();
    for (TracingMetadata tracing : bean.tracing()) {
        spanIds.add(Span.fromContext(tracing.getCurrentContext()).getSpanContext().getSpanId());
        assertThat(Span.fromContextOrNull(tracing.getPreviousContext())).isNull();
    }
    assertThat(spanIds).doesNotContainNull().doesNotHaveDuplicates().hasSizeGreaterThanOrEqualTo(10);
    for (SpanData data : testExporter.getFinishedSpanItems()) {
        assertThat(data.getSpanId()).isIn(spanIds);
        assertThat(data.getSpanId()).isNotEqualTo(data.getParentSpanId());
        assertThat(data.getKind()).isEqualByComparingTo(SpanKind.CONSUMER);
    }
}
Also used : SpanData(io.opentelemetry.sdk.trace.data.SpanData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)

Example 4 with TracingMetadata

use of io.smallrye.reactive.messaging.TracingMetadata in project smallrye-reactive-messaging by smallrye.

the class KafkaSink method createOutgoingTrace.

private void createOutgoingTrace(Message<?> message, String topic, int partition, Headers headers) {
    if (isTracingEnabled) {
        Optional<TracingMetadata> tracingMetadata = TracingMetadata.fromMessage(message);
        final SpanBuilder spanBuilder = TRACER.spanBuilder(topic + " send").setSpanKind(SpanKind.PRODUCER);
        if (tracingMetadata.isPresent()) {
            // Handle possible parent span
            final Context parentSpanContext = tracingMetadata.get().getCurrentContext();
            if (parentSpanContext != null) {
                spanBuilder.setParent(parentSpanContext);
            } else {
                spanBuilder.setNoParent();
            }
        } else {
            spanBuilder.setNoParent();
        }
        final Span span = spanBuilder.startSpan();
        Scope scope = span.makeCurrent();
        // Set Span attributes
        if (partition != -1) {
            span.setAttribute(SemanticAttributes.MESSAGING_KAFKA_PARTITION, partition);
        }
        span.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "kafka");
        span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, topic);
        span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic");
        // Set span onto headers
        GlobalOpenTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), headers, HeaderInjectAdapter.SETTER);
        span.end();
        scope.close();
    }
}
Also used : Context(io.opentelemetry.context.Context) SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) Scope(io.opentelemetry.context.Scope) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) Span(io.opentelemetry.api.trace.Span)

Example 5 with TracingMetadata

use of io.smallrye.reactive.messaging.TracingMetadata in project smallrye-reactive-messaging by smallrye.

the class KafkaSource method incomingTrace.

public void incomingTrace(IncomingKafkaRecord<K, V> kafkaRecord, boolean insideBatch) {
    if (isTracingEnabled && TRACER != null) {
        TracingMetadata tracingMetadata = TracingMetadata.fromMessage(kafkaRecord).orElse(TracingMetadata.empty());
        final SpanBuilder spanBuilder = TRACER.spanBuilder(kafkaRecord.getTopic() + " receive").setSpanKind(SpanKind.CONSUMER);
        // Handle possible parent span
        final Context parentSpanContext = tracingMetadata.getPreviousContext();
        if (parentSpanContext != null) {
            spanBuilder.setParent(parentSpanContext);
        } else {
            spanBuilder.setNoParent();
        }
        final Span span = spanBuilder.startSpan();
        // Set Span attributes
        span.setAttribute(SemanticAttributes.MESSAGING_KAFKA_PARTITION, kafkaRecord.getPartition());
        span.setAttribute("offset", kafkaRecord.getOffset());
        span.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "kafka");
        span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, kafkaRecord.getTopic());
        span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic");
        final String groupId = client.get(ConsumerConfig.GROUP_ID_CONFIG);
        final String clientId = client.get(ConsumerConfig.CLIENT_ID_CONFIG);
        span.setAttribute("messaging.consumer_id", constructConsumerId(groupId, clientId));
        span.setAttribute(SemanticAttributes.MESSAGING_KAFKA_CONSUMER_GROUP, groupId);
        if (!clientId.isEmpty()) {
            span.setAttribute(SemanticAttributes.MESSAGING_KAFKA_CLIENT_ID, clientId);
        }
        if (!insideBatch) {
            // Make available as parent for subsequent spans inside message processing
            span.makeCurrent();
        }
        kafkaRecord.injectTracingMetadata(tracingMetadata.withSpan(span));
        span.end();
    }
}
Also used : Context(io.opentelemetry.context.Context) EventLoopContext(io.vertx.core.impl.EventLoopContext) SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) Span(io.opentelemetry.api.trace.Span)

Aggregations

TracingMetadata (io.smallrye.reactive.messaging.TracingMetadata)15 Context (io.opentelemetry.context.Context)12 Span (io.opentelemetry.api.trace.Span)11 SpanData (io.opentelemetry.sdk.trace.data.SpanData)8 ArrayList (java.util.ArrayList)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 GlobalOpenTelemetry (io.opentelemetry.api.GlobalOpenTelemetry)7 SpanKind (io.opentelemetry.api.trace.SpanKind)7 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 Test (org.junit.jupiter.api.Test)7 SpanBuilder (io.opentelemetry.api.trace.SpanBuilder)6 Message (org.eclipse.microprofile.reactive.messaging.Message)6 SpanContext (io.opentelemetry.api.trace.SpanContext)5 W3CTraceContextPropagator (io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator)5 ContextPropagators (io.opentelemetry.context.propagation.ContextPropagators)5 OpenTelemetrySdk (io.opentelemetry.sdk.OpenTelemetrySdk)5 InMemorySpanExporter (io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter)5 SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)5 SpanProcessor (io.opentelemetry.sdk.trace.SpanProcessor)5