From d593264cda62425d8444d4903e8a31e5f6e193a1 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Tue, 27 Oct 2026 16:35:47 -0400 Subject: [PATCH] test: assert fetch span parentage in the exported payload The fetch propagation tests asserted the outgoing headers, which imply the parentage, but nothing asserted the exported spans themselves: that a fetch made while handling a traced request is a direct child of the server span, in the same trace, and parents the downstream server span in turn. Signed-off-by: Bryan English --- test/test-undici.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/test-undici.js b/test/test-undici.js index 2655464..77871cb 100644 --- a/test/test-undici.js +++ b/test/test-undici.js @@ -7,6 +7,7 @@ const { startHTTPServer, flushAndCollect, getClientSpan, + getServerSpan, rawRequest, spanAttrs, closeAll, @@ -157,6 +158,44 @@ describe('otel undici spans', () => { ); }); + it('parents fetch spans to the server span in the same trace', async () => { + const incomingTraceId = 'a'.repeat(32); + + const fetchTarget = await startHTTPServer(); + // A fetch made while handling a traced request. + const frontend = await startHTTPServer((req, res) => { + fetch(`http://127.0.0.1:${fetchTarget.port}/down`).then((fr) => { + fr.text().then(() => { + res.writeHead(200); + res.end('ok'); + }); + }); + }); + + await rawRequest(frontend.port, '/up', { + traceparent: `00-${incomingTraceId}-0123456789abcdef-01`, + }); + + const spans = await flushAndCollect(collector); + await closeAll(fetchTarget, frontend); + + // The full chain, asserted on the exported spans: the server span + // parents the fetch client span, and the fetch span parents the + // downstream server span, all in the incoming trace. + const serverSpan = getServerSpan(spans, '/up'); + const fetchSpan = getClientSpan(spans, '/down'); + const downstreamSpan = getServerSpan(spans, '/down'); + assert.ok(serverSpan, 'Expected a server span for the inbound request'); + assert.ok(fetchSpan, 'Expected a client span for the fetch'); + assert.ok(downstreamSpan, 'Expected a server span downstream of the fetch'); + + assert.strictEqual(serverSpan.traceId, incomingTraceId); + assert.strictEqual(fetchSpan.traceId, incomingTraceId); + assert.strictEqual(fetchSpan.parentSpanId, serverSpan.spanId); + assert.strictEqual(downstreamSpan.traceId, incomingTraceId); + assert.strictEqual(downstreamSpan.parentSpanId, fetchSpan.spanId); + }); + it('removes a stale tracestate when the span has none', async () => { let receivedTracestate = 'sentinel'; const target = await startHTTPServer((req, res) => {