Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instrumentation-undici): fix a possible crash if the request path is a full URL #2518

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion plugins/node/instrumentation-undici/src/undici.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,14 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
}

const startTime = hrTime();
const requestUrl = new URL(request.origin + request.path);
let requestUrl;
try {
requestUrl = new URL(request.path, request.origin);
} catch (err) {
this._diag.warn('could not determine url.full:', err);
// Skip instrumenting this request.
return;
}
const urlScheme = requestUrl.protocol.replace(':', '');
const requestMethod = this.getRequestMethod(request.method);
const attributes: Attributes = {
Expand Down
28 changes: 28 additions & 0 deletions plugins/node/instrumentation-undici/test/undici.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,5 +831,33 @@ describe('UndiciInstrumentation `undici` tests', function () {
'user-agent is undefined'
);
});

it('should create valid span if request.path is a full URL', async function () {
let spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 0);

const origin = `${protocol}://${hostname}:${mockServer.port}`;
const fullUrl = `${origin}/?query=test`;
const client = new undici.Client(origin);
const res = await client.request({
path: fullUrl,
method: 'GET',
});
await consumeResponseBody(res.body);

spans = memoryExporter.getFinishedSpans();
const span = spans[0];
assert.ok(span, 'a span is present');
assert.strictEqual(spans.length, 1);
assertSpan(span, {
hostname: 'localhost',
httpStatusCode: res.statusCode,
httpMethod: 'GET',
path: '/',
query: '?query=test',
resHeaders: res.headers,
});
assert.strictEqual(span.attributes['url.full'], fullUrl);
});
});
});