Search in sources :

Example 61 with SpanContext

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

the class TracingAmqpToAppToAmqpTest method testFromAmqpToAppToAmqp.

@Test
public void testFromAmqpToAppToAmqp() {
    List<Integer> payloads = new CopyOnWriteArrayList<>();
    List<Context> receivedContexts = new CopyOnWriteArrayList<>();
    usage.consumeIntegersWithTracing("result-topic", payloads::add, receivedContexts::add);
    weld.addBeanClass(MyAppProcessingData.class);
    new MapBasedConfig().put("mp.messaging.outgoing.result-topic.connector", AmqpConnector.CONNECTOR_NAME).put("mp.messaging.outgoing.result-topic.durable", false).put("mp.messaging.outgoing.result-topic.host", host).put("mp.messaging.outgoing.result-topic.port", port).put("mp.messaging.incoming.parent-topic.connector", AmqpConnector.CONNECTOR_NAME).put("mp.messaging.incoming.parent-topic.host", host).put("mp.messaging.incoming.parent-topic.port", port).put("amqp-username", username).put("amqp-password", password).write();
    container = weld.initialize();
    MyAppProcessingData bean = container.getBeanManager().createInstance().select(MyAppProcessingData.class).get();
    await().until(() -> isAmqpConnectorReady(container));
    AtomicInteger count = new AtomicInteger();
    List<SpanContext> producedSpanContexts = new CopyOnWriteArrayList<>();
    usage.produce("parent-topic", 10, () -> AmqpMessage.create().durable(false).ttl(10000).withIntegerAsBody(count.getAndIncrement()).applicationProperties(createTracingSpan(producedSpanContexts, "parent-topic")).build());
    await().atMost(Duration.ofMinutes(5)).until(() -> payloads.size() >= 10);
    assertThat(payloads).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    List<String> producedTraceIds = producedSpanContexts.stream().map(SpanContext::getTraceId).collect(Collectors.toList());
    assertThat(producedTraceIds).hasSize(10);
    assertThat(receivedContexts).hasSize(10);
    assertThat(receivedContexts).doesNotContainNull().doesNotHaveDuplicates();
    List<String> receivedSpanIds = receivedContexts.stream().map(context -> Span.fromContextOrNull(context).getSpanContext().getSpanId()).collect(Collectors.toList());
    assertThat(receivedSpanIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
    List<String> receivedTraceIds = receivedContexts.stream().map(context -> Span.fromContextOrNull(context).getSpanContext().getTraceId()).collect(Collectors.toList());
    assertThat(receivedTraceIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
    assertThat(receivedTraceIds).containsExactlyInAnyOrderElementsOf(producedTraceIds);
    List<String> receivedParentSpanIds = new ArrayList<>();
    assertThat(bean.tracing()).hasSizeGreaterThanOrEqualTo(10);
    assertThat(bean.tracing()).doesNotContainNull().doesNotHaveDuplicates();
    List<String> spanIds = new ArrayList<>();
    for (TracingMetadata tracing : bean.tracing()) {
        Span span = Span.fromContext(tracing.getCurrentContext());
        spanIds.add(span.getSpanContext().getSpanId());
        assertThat(Span.fromContextOrNull(tracing.getPreviousContext())).isNotNull();
    }
    await().atMost(Duration.ofMinutes(2)).until(() -> testExporter.getFinishedSpanItems().size() >= 10);
    List<String> outgoingParentIds = new ArrayList<>();
    List<String> incomingParentIds = new ArrayList<>();
    for (SpanData data : testExporter.getFinishedSpanItems()) {
        if (data.getKind().equals(SpanKind.CONSUMER)) {
            incomingParentIds.add(data.getParentSpanId());
            // Need to skip the spans created during @Incoming processing
            continue;
        }
        assertThat(data.getSpanId()).isIn(receivedSpanIds);
        assertThat(data.getSpanId()).isNotEqualTo(data.getParentSpanId());
        assertThat(data.getTraceId()).isIn(producedTraceIds);
        assertThat(data.getKind()).isEqualByComparingTo(SpanKind.PRODUCER);
        outgoingParentIds.add(data.getParentSpanId());
    }
    // Assert span created on AMQP record is the parent of consumer span we create
    assertThat(producedSpanContexts.stream().map(SpanContext::getSpanId)).containsExactlyElementsOf(incomingParentIds);
    // Assert consumer span is the parent of the producer span we received in AMQP
    assertThat(spanIds.stream()).containsExactlyElementsOf(outgoingParentIds);
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) BeforeEach(org.junit.jupiter.api.BeforeEach) Outgoing(org.eclipse.microprofile.reactive.messaging.Outgoing) 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) Duration(java.time.Duration) 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) 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) 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 62 with SpanContext

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

the class TracingAmqpToAppWithParentTest 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)

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