Skip to content

Commit 28e209a

Browse files
authored
fix(instrumentation-undici): fix a possible crash if the request path is a full URL (#2518)
Closes: #2471
1 parent 0309cae commit 28e209a

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,14 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
188188
}
189189

190190
const startTime = hrTime();
191-
const requestUrl = new URL(request.origin + request.path);
191+
let requestUrl;
192+
try {
193+
requestUrl = new URL(request.path, request.origin);
194+
} catch (err) {
195+
this._diag.warn('could not determine url.full:', err);
196+
// Skip instrumenting this request.
197+
return;
198+
}
192199
const urlScheme = requestUrl.protocol.replace(':', '');
193200
const requestMethod = this.getRequestMethod(request.method);
194201
const attributes: Attributes = {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,5 +831,33 @@ describe('UndiciInstrumentation `undici` tests', function () {
831831
'user-agent is undefined'
832832
);
833833
});
834+
835+
it('should create valid span if request.path is a full URL', async function () {
836+
let spans = memoryExporter.getFinishedSpans();
837+
assert.strictEqual(spans.length, 0);
838+
839+
const origin = `${protocol}://${hostname}:${mockServer.port}`;
840+
const fullUrl = `${origin}/?query=test`;
841+
const client = new undici.Client(origin);
842+
const res = await client.request({
843+
path: fullUrl,
844+
method: 'GET',
845+
});
846+
await consumeResponseBody(res.body);
847+
848+
spans = memoryExporter.getFinishedSpans();
849+
const span = spans[0];
850+
assert.ok(span, 'a span is present');
851+
assert.strictEqual(spans.length, 1);
852+
assertSpan(span, {
853+
hostname: 'localhost',
854+
httpStatusCode: res.statusCode,
855+
httpMethod: 'GET',
856+
path: '/',
857+
query: '?query=test',
858+
resHeaders: res.headers,
859+
});
860+
assert.strictEqual(span.attributes['url.full'], fullUrl);
861+
});
834862
});
835863
});

0 commit comments

Comments
 (0)