Skip to content

Commit 72e3f66

Browse files
YoannMapichlermarc
andauthored
fix(instr-undici): wrong user agent reported if no user agent were set (#2282)
Co-authored-by: Marc Pichler <[email protected]>
1 parent b08f01f commit 72e3f66

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

plugins/node/instrumentation-undici/src/undici.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ export class UndiciInstrumentation extends InstrumentationBase {
205205
const idx = request.headers.findIndex(
206206
h => h.toLowerCase() === 'user-agent'
207207
);
208-
userAgent = request.headers[idx + 1];
208+
if (idx >= 0) {
209+
userAgent = request.headers[idx + 1];
210+
}
209211
} else if (typeof request.headers === 'string') {
210212
const headers = request.headers.split('\r\n');
211213
const uaHeader = headers.find(h =>

plugins/node/instrumentation-undici/test/undici.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -768,5 +768,43 @@ describe('UndiciInstrumentation `undici` tests', function () {
768768
},
769769
});
770770
});
771+
772+
it('should not report an user-agent if it was not defined', async function () {
773+
let spans = memoryExporter.getFinishedSpans();
774+
assert.strictEqual(spans.length, 0);
775+
776+
// Do some requests
777+
const headers = {
778+
'foo-client': 'bar',
779+
};
780+
781+
const queryRequestUrl = `${protocol}://${hostname}:${mockServer.port}/?query=test`;
782+
const queryResponse = await undici.request(queryRequestUrl, { headers });
783+
await consumeResponseBody(queryResponse.body);
784+
785+
assert.ok(
786+
queryResponse.headers['propagation-error'] == null,
787+
'propagation is set for instrumented requests'
788+
);
789+
790+
spans = memoryExporter.getFinishedSpans();
791+
const span = spans[0];
792+
assert.ok(span, 'a span is present');
793+
assert.strictEqual(spans.length, 1);
794+
assertSpan(span, {
795+
hostname: 'localhost',
796+
httpStatusCode: queryResponse.statusCode,
797+
httpMethod: 'GET',
798+
path: '/',
799+
query: '?query=test',
800+
reqHeaders: headers,
801+
resHeaders: queryResponse.headers,
802+
});
803+
assert.strictEqual(
804+
span.attributes['user_agent.original'],
805+
undefined,
806+
'user-agent is undefined'
807+
);
808+
});
771809
});
772810
});

0 commit comments

Comments
 (0)