Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions test/test-undici.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
startHTTPServer,
flushAndCollect,
getClientSpan,
getServerSpan,
rawRequest,
spanAttrs,
closeAll,
Expand Down Expand Up @@ -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) => {
Expand Down
Loading