|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { trace, context as otelContext } from '@opentelemetry/api'; |
| 4 | +import { |
| 5 | + BasicTracerProvider, |
| 6 | + InMemorySpanExporter, |
| 7 | + SimpleSpanProcessor, |
| 8 | +} from '@opentelemetry/sdk-trace-base'; |
| 9 | +import { captureTraceparent, linkedSpan } from './tracing.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Trace assertion test for issue #778: confirms `captureTraceparent()` + |
| 13 | + * `linkedSpan()` correctly re-link a span across an async boundary — the |
| 14 | + * exact shape of the outbox relay's write-now/deliver-later gap, where |
| 15 | + * OTel's automatic context propagation can't reach on its own since the |
| 16 | + * delivery happens on a completely separate event-loop tick with no |
| 17 | + * ambient span. |
| 18 | + */ |
| 19 | + |
| 20 | +function setupInMemoryTracing() { |
| 21 | + const exporter = new InMemorySpanExporter(); |
| 22 | + const provider = new BasicTracerProvider({ |
| 23 | + spanProcessors: [new SimpleSpanProcessor(exporter)], |
| 24 | + }); |
| 25 | + trace.setGlobalTracerProvider(provider); |
| 26 | + return { provider, exporter }; |
| 27 | +} |
| 28 | + |
| 29 | +test('linkedSpan re-establishes parent/child across an async boundary', async () => { |
| 30 | + const { provider, exporter } = setupInMemoryTracing(); |
| 31 | + try { |
| 32 | + const tracer = trace.getTracer('test'); |
| 33 | + |
| 34 | + // Simulate the HTTP request that enqueues the outbox row: start a span, |
| 35 | + // capture its traceparent (what writeOutbox stores alongside the row), |
| 36 | + // then end the span — mirroring the request finishing well before the |
| 37 | + // relay ever picks the row up. |
| 38 | + let traceparent; |
| 39 | + let parentSpanId; |
| 40 | + let parentTraceId; |
| 41 | + await tracer.startActiveSpan('http.request', async (parentSpan) => { |
| 42 | + const ctx = parentSpan.spanContext(); |
| 43 | + parentSpanId = ctx.spanId; |
| 44 | + parentTraceId = ctx.traceId; |
| 45 | + traceparent = captureTraceparent(); |
| 46 | + parentSpan.end(); |
| 47 | + }); |
| 48 | + |
| 49 | + assert.ok(traceparent, 'expected a traceparent to be captured from the active span'); |
| 50 | + |
| 51 | + // Simulate the relay's later, unrelated poll tick: no ambient span here |
| 52 | + // at all — linkedSpan must reconstruct the parent purely from the |
| 53 | + // stored traceparent string. |
| 54 | + await otelContext.with(otelContext.active(), async () => { |
| 55 | + await linkedSpan(traceparent, 'outbox.deliver', { 'outbox.event_type': 'test.event' }, async () => { |
| 56 | + // handler body — nothing to do for this assertion. |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + await provider.forceFlush(); |
| 61 | + const spans = exporter.getFinishedSpans(); |
| 62 | + const deliverSpan = spans.find((s) => s.name === 'outbox.deliver'); |
| 63 | + |
| 64 | + assert.ok(deliverSpan, 'expected an outbox.deliver span to have been recorded'); |
| 65 | + assert.equal( |
| 66 | + deliverSpan.parentSpanId, |
| 67 | + parentSpanId, |
| 68 | + 'outbox.deliver span must be a child of the request span that enqueued it', |
| 69 | + ); |
| 70 | + assert.equal( |
| 71 | + deliverSpan.spanContext().traceId, |
| 72 | + parentTraceId, |
| 73 | + 'outbox.deliver span must share the same trace id as the request that enqueued it', |
| 74 | + ); |
| 75 | + } finally { |
| 76 | + await provider.shutdown(); |
| 77 | + } |
| 78 | +}); |
| 79 | + |
| 80 | +test('linkedSpan still traces (unlinked) when no traceparent is available', async () => { |
| 81 | + const { provider, exporter } = setupInMemoryTracing(); |
| 82 | + try { |
| 83 | + await linkedSpan(null, 'outbox.deliver', {}, async () => {}); |
| 84 | + await provider.forceFlush(); |
| 85 | + const spans = exporter.getFinishedSpans(); |
| 86 | + const deliverSpan = spans.find((s) => s.name === 'outbox.deliver'); |
| 87 | + assert.ok(deliverSpan, 'expected a span even without a traceparent to link to'); |
| 88 | + assert.equal(deliverSpan.parentSpanId, undefined, 'span should have no parent when unlinked'); |
| 89 | + } finally { |
| 90 | + await provider.shutdown(); |
| 91 | + } |
1 | 92 | /** |
2 | 93 | * Tests for distributed tracing across async boundaries |
3 | 94 | * |
|
0 commit comments