|
| 1 | +package sk.solodev.notify.outbox; |
| 2 | + |
| 3 | +import io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext; |
| 4 | +import io.micrometer.tracing.otel.bridge.OtelPropagator; |
| 5 | +import io.micrometer.tracing.otel.bridge.OtelTracer; |
| 6 | +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; |
| 7 | +import io.opentelemetry.context.propagation.ContextPropagators; |
| 8 | +import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; |
| 9 | +import io.opentelemetry.sdk.trace.SdkTracerProvider; |
| 10 | +import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import sk.solodev.notify.NotificationRequest; |
| 14 | +import sk.solodev.notify.test.RecordingNotifier; |
| 15 | +import tools.jackson.databind.json.JsonMapper; |
| 16 | + |
| 17 | +import java.time.Duration; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +/** |
| 22 | + * Proves trace context propagation across the outbox boundary using a real OpenTelemetry SDK: the |
| 23 | + * delivery span created by {@link OutboxRelay} becomes a child of the request span that enqueued, |
| 24 | + * even though they run in different transactions and possibly different threads. |
| 25 | + */ |
| 26 | +class OutboxTracePropagationOtelTest { |
| 27 | + |
| 28 | + record SampleRequest(String to, String body) implements NotificationRequest { } |
| 29 | + |
| 30 | + private static final OutboxProperties PROPERTIES = new OutboxProperties( |
| 31 | + Duration.ofSeconds(5), 100, 7, Duration.ofSeconds(10), Duration.ofMinutes(10), |
| 32 | + "notification_outbox"); |
| 33 | + |
| 34 | + private InMemorySpanExporter spanExporter; |
| 35 | + private OtelTracer tracer; |
| 36 | + private MicrometerOutboxTracePropagator propagator; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + void setUp() { |
| 40 | + spanExporter = InMemorySpanExporter.create(); |
| 41 | + var tracerProvider = SdkTracerProvider.builder() |
| 42 | + .addSpanProcessor(SimpleSpanProcessor.create(spanExporter)) |
| 43 | + .build(); |
| 44 | + var otelTracer = tracerProvider.get("test"); |
| 45 | + var propagators = ContextPropagators.create(W3CTraceContextPropagator.getInstance()); |
| 46 | + |
| 47 | + tracer = new OtelTracer(otelTracer, new OtelCurrentTraceContext(), null); |
| 48 | + propagator = new MicrometerOutboxTracePropagator(tracer, |
| 49 | + new OtelPropagator(propagators, otelTracer)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void theDeliverySpanInheritsTheTraceIdFromTheEnqueuingRequest() { |
| 54 | + // Start a span representing the incoming request |
| 55 | + var requestSpan = tracer.nextSpan().name("incoming-request").start(); |
| 56 | + String originalTraceId = requestSpan.context().traceId(); |
| 57 | + |
| 58 | + // Enqueue within the request span's scope |
| 59 | + OutboxEntry entry; |
| 60 | + var store = new RecordingOutboxStore(); |
| 61 | + var enqueuer = new DefaultOutboxNotifier(store, JsonMapper.builder().build(), |
| 62 | + PROPERTIES, propagator); |
| 63 | + try (var _ = tracer.withSpan(requestSpan)) { |
| 64 | + enqueuer.enqueue(new SampleRequest("+421900123456", "hello")); |
| 65 | + } finally { |
| 66 | + requestSpan.end(); |
| 67 | + } |
| 68 | + |
| 69 | + // The entry should have captured the trace context |
| 70 | + entry = store.inserted.getFirst(); |
| 71 | + assertThat(entry.traceContext()).isNotNull(); |
| 72 | + |
| 73 | + // Now deliver it through the relay |
| 74 | + var notifier = new RecordingNotifier(); |
| 75 | + var relay = new OutboxRelay(notifier, new RecordingOutboxStore(entry), |
| 76 | + JsonMapper.builder().build(), PROPERTIES, propagator); |
| 77 | + |
| 78 | + relay.poll(); |
| 79 | + |
| 80 | + // Export spans and find the delivery span |
| 81 | + var exportedSpans = spanExporter.getFinishedSpanItems(); |
| 82 | + |
| 83 | + // The request span and the delivery span should both be present |
| 84 | + assertThat(exportedSpans).hasSizeGreaterThanOrEqualTo(2); |
| 85 | + |
| 86 | + // Find the delivery span (it's the one created after the request span ended) |
| 87 | + var deliverySpan = exportedSpans.stream() |
| 88 | + .filter(span -> !span.getSpanId().equals(requestSpan.context().spanId())) |
| 89 | + .findFirst() |
| 90 | + .orElseThrow(() -> new AssertionError("Delivery span not found")); |
| 91 | + |
| 92 | + // The critical assertion: the delivery span must have the same trace ID as the request |
| 93 | + assertThat(deliverySpan.getTraceId()) |
| 94 | + .as("Delivery span should inherit the request's trace ID") |
| 95 | + .isEqualTo(originalTraceId); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments