Skip to content

Commit

Permalink
fix(instr-undici): wrong user agent reported if no user agent were set (
Browse files Browse the repository at this point in the history
#2282)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
YoannMa and pichlermarc authored Jun 19, 2024
1 parent b08f01f commit 72e3f66
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion plugins/node/instrumentation-undici/src/undici.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ export class UndiciInstrumentation extends InstrumentationBase {
const idx = request.headers.findIndex(
h => h.toLowerCase() === 'user-agent'
);
userAgent = request.headers[idx + 1];
if (idx >= 0) {
userAgent = request.headers[idx + 1];
}
} else if (typeof request.headers === 'string') {
const headers = request.headers.split('\r\n');
const uaHeader = headers.find(h =>
Expand Down
38 changes: 38 additions & 0 deletions plugins/node/instrumentation-undici/test/undici.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,5 +768,43 @@ describe('UndiciInstrumentation `undici` tests', function () {
},
});
});

it('should not report an user-agent if it was not defined', async function () {
let spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 0);

// Do some requests
const headers = {
'foo-client': 'bar',
};

const queryRequestUrl = `${protocol}://${hostname}:${mockServer.port}/?query=test`;
const queryResponse = await undici.request(queryRequestUrl, { headers });
await consumeResponseBody(queryResponse.body);

assert.ok(
queryResponse.headers['propagation-error'] == null,
'propagation is set for instrumented requests'
);

spans = memoryExporter.getFinishedSpans();
const span = spans[0];
assert.ok(span, 'a span is present');
assert.strictEqual(spans.length, 1);
assertSpan(span, {
hostname: 'localhost',
httpStatusCode: queryResponse.statusCode,
httpMethod: 'GET',
path: '/',
query: '?query=test',
reqHeaders: headers,
resHeaders: queryResponse.headers,
});
assert.strictEqual(
span.attributes['user_agent.original'],
undefined,
'user-agent is undefined'
);
});
});
});

0 comments on commit 72e3f66

Please sign in to comment.