Skip to content

Commit

Permalink
fix(instrumentation-undici): fix a possible crash if the request path…
Browse files Browse the repository at this point in the history
… is a full URL (#2518)

Closes: #2471
  • Loading branch information
trentm authored Nov 7, 2024
1 parent 0309cae commit 28e209a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
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);
});
});
});

0 comments on commit 28e209a

Please sign in to comment.