|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('node:assert'); |
| 4 | +const { after, before, describe, it } = require('node:test'); |
| 5 | +const { |
| 6 | + startTracing, |
| 7 | + startHTTPServer, |
| 8 | + flushAndCollect, |
| 9 | + getClientSpan, |
| 10 | + rawRequest, |
| 11 | + spanAttrs, |
| 12 | + closeAll, |
| 13 | +} = require('./common/otel'); |
| 14 | + |
| 15 | +describe('otel undici spans', () => { |
| 16 | + let collector; |
| 17 | + |
| 18 | + before(async () => { |
| 19 | + collector = await startTracing({ |
| 20 | + filter: 'node:http,node:undici', |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + after(() => collector.close()); |
| 25 | + |
| 26 | + it( |
| 27 | + 'creates client spans for fetch requests and overrides existing ' + |
| 28 | + 'trace context headers', |
| 29 | + async () => { |
| 30 | + const receivedTraceparent = {}; |
| 31 | + const target = await startHTTPServer((req, res) => { |
| 32 | + receivedTraceparent[req.url] = req.headers.traceparent; |
| 33 | + res.writeHead(200); |
| 34 | + res.end('ok'); |
| 35 | + }); |
| 36 | + |
| 37 | + try { |
| 38 | + const res = await fetch(`http://127.0.0.1:${target.port}/fetch-test`); |
| 39 | + await res.text(); |
| 40 | + } catch {} |
| 41 | + |
| 42 | + // A caller-supplied traceparent must be overridden, not duplicated: |
| 43 | + // undici's addHeader appends, so a duplicate would arrive as a single |
| 44 | + // comma-separated value. |
| 45 | + try { |
| 46 | + const res = await fetch( |
| 47 | + `http://127.0.0.1:${target.port}/override-test`, |
| 48 | + { |
| 49 | + headers: { |
| 50 | + traceparent: |
| 51 | + '00-00000000000000000000000000000000-' + '0000000000000000-00', |
| 52 | + }, |
| 53 | + }, |
| 54 | + ); |
| 55 | + await res.text(); |
| 56 | + } catch {} |
| 57 | + |
| 58 | + const spans = await flushAndCollect(collector); |
| 59 | + await target.close(); |
| 60 | + |
| 61 | + assert.ok(spans.length > 0, 'Expected at least one span from fetch'); |
| 62 | + const clientSpan = getClientSpan(spans, '/fetch-test'); |
| 63 | + assert.ok(clientSpan, 'Expected a CLIENT span from fetch'); |
| 64 | + |
| 65 | + assert.partialDeepStrictEqual(spanAttrs(clientSpan), { |
| 66 | + 'http.request.method': 'GET', |
| 67 | + 'server.address': `http://127.0.0.1:${target.port}`, |
| 68 | + 'server.port': String(target.port), |
| 69 | + 'url.full': `http://127.0.0.1:${target.port}/fetch-test`, |
| 70 | + 'http.response.status_code': '200', |
| 71 | + }); |
| 72 | + |
| 73 | + const overridden = receivedTraceparent['/override-test']; |
| 74 | + assert.ok(overridden, 'Expected a traceparent on the override request'); |
| 75 | + assert.match(overridden, /^00-[0-9a-f]{32}-[0-9a-f]{16}-0[01]$/); |
| 76 | + assert.ok( |
| 77 | + !overridden.includes(','), |
| 78 | + 'traceparent must be overridden, not duplicated', |
| 79 | + ); |
| 80 | + }, |
| 81 | + ); |
| 82 | + |
| 83 | + it('creates an error span for a failed fetch', async () => { |
| 84 | + // A response that dies mid-transfer must end the span with the error |
| 85 | + // status and an exception event, through the undici:request:error |
| 86 | + // channel. A fetch that fails to connect never dispatches, so it |
| 87 | + // produces no span at all. |
| 88 | + const target = await startHTTPServer((req, res) => { |
| 89 | + res.writeHead(200); |
| 90 | + res.write('partial'); |
| 91 | + setTimeout(() => res.destroy(), 50); |
| 92 | + }); |
| 93 | + |
| 94 | + await fetch(`http://127.0.0.1:${target.port}/destroy`) |
| 95 | + .then((res) => res.text()) |
| 96 | + .catch(() => {}); |
| 97 | + |
| 98 | + const spans = await flushAndCollect(collector); |
| 99 | + await target.close(); |
| 100 | + |
| 101 | + const errorSpan = getClientSpan(spans, '/destroy'); |
| 102 | + assert.ok(errorSpan, 'Expected an error client span for the failed fetch'); |
| 103 | + |
| 104 | + assert.ok(errorSpan.status, 'Error span should have status'); |
| 105 | + assert.strictEqual(errorSpan.status.code, 2); // STATUS_ERROR |
| 106 | + assert.ok( |
| 107 | + errorSpan.events?.some((e) => e.name === 'exception'), |
| 108 | + 'Should have an exception event', |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + it('overrides a stale tracestate with the inherited one', async () => { |
| 113 | + const incomingTraceId = 'a'.repeat(32); |
| 114 | + const incomingTracestate = 'vendor=inherited'; |
| 115 | + |
| 116 | + let seen; |
| 117 | + // The fetch target records the trace context headers it receives. |
| 118 | + const fetchTarget = await startHTTPServer((req, res) => { |
| 119 | + seen = { |
| 120 | + traceparent: req.headers.traceparent, |
| 121 | + tracestate: req.headers.tracestate, |
| 122 | + }; |
| 123 | + res.writeHead(200); |
| 124 | + res.end('ok'); |
| 125 | + }); |
| 126 | + |
| 127 | + // A fetch made while handling a traced request inherits the incoming |
| 128 | + // tracestate and must override the caller-supplied stale one. |
| 129 | + const frontend = await startHTTPServer((req, res) => { |
| 130 | + fetch(`http://127.0.0.1:${fetchTarget.port}/down`, { |
| 131 | + headers: { tracestate: 'stale=caller-value' }, |
| 132 | + }).then((fr) => { |
| 133 | + fr.text().then(() => { |
| 134 | + res.writeHead(200); |
| 135 | + res.end('ok'); |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + await rawRequest(frontend.port, '/up', { |
| 141 | + traceparent: `00-${incomingTraceId}-0123456789abcdef-01`, |
| 142 | + tracestate: incomingTracestate, |
| 143 | + }); |
| 144 | + |
| 145 | + await flushAndCollect(collector); |
| 146 | + await closeAll(fetchTarget, frontend); |
| 147 | + |
| 148 | + assert.ok(seen?.traceparent, 'Expected a traceparent on the fetch'); |
| 149 | + assert.ok( |
| 150 | + seen.traceparent.includes(incomingTraceId), |
| 151 | + 'The fetch must carry the incoming trace ID', |
| 152 | + ); |
| 153 | + assert.strictEqual( |
| 154 | + seen.tracestate, |
| 155 | + incomingTracestate, |
| 156 | + 'The inherited tracestate must win over the stale one', |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + it('removes a stale tracestate when the span has none', async () => { |
| 161 | + let receivedTracestate = 'sentinel'; |
| 162 | + const target = await startHTTPServer((req, res) => { |
| 163 | + receivedTracestate = req.headers.tracestate; |
| 164 | + res.writeHead(200); |
| 165 | + res.end('ok'); |
| 166 | + }); |
| 167 | + |
| 168 | + try { |
| 169 | + const res = await fetch(`http://127.0.0.1:${target.port}/stale`, { |
| 170 | + headers: { tracestate: 'vendor=state-for-an-old-trace' }, |
| 171 | + }); |
| 172 | + await res.text(); |
| 173 | + } catch {} |
| 174 | + |
| 175 | + const spans = await flushAndCollect(collector); |
| 176 | + await target.close(); |
| 177 | + |
| 178 | + assert.ok(getClientSpan(spans, '/stale'), 'Expected a client span'); |
| 179 | + |
| 180 | + // A stale tracestate from an unrelated trace must not travel with |
| 181 | + // the fresh trace context, even though the span has none to inject. |
| 182 | + assert.strictEqual( |
| 183 | + receivedTracestate, |
| 184 | + undefined, |
| 185 | + 'stale tracestate must not be forwarded', |
| 186 | + ); |
| 187 | + }); |
| 188 | +}); |
0 commit comments