Skip to content

Commit 60169cd

Browse files
committed
fix(node): fill in span data from http request object
1 parent 618e992 commit 60169cd

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

packages/node/src/integrations/utils/http.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ export function normalizeRequestArgs(
168168
requestOptions = urlToOptions(requestArgs[0]);
169169
} else {
170170
requestOptions = requestArgs[0];
171+
172+
if (!requestOptions.pathname || !requestOptions.search) {
173+
const parsed = new URL(
174+
requestOptions.path || '',
175+
`${requestOptions.protocol || 'http:'}//${requestOptions.hostname}`,
176+
);
177+
requestOptions.pathname = parsed.pathname;
178+
requestOptions.search = parsed.search;
179+
requestOptions.hash = parsed.hash;
180+
}
171181
}
172182

173183
// if the options were given separately from the URL, fold them in

packages/node/test/integrations/http.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,25 @@ describe('tracing', () => {
298298
expect(spans[1].data['http.fragment']).toEqual('learn-more');
299299
});
300300

301+
it('fills in span data from http.RequestOptions object', () => {
302+
nock('http://dogs.are.great').get('/spaniel?tail=wag&cute=true#learn-more').reply(200);
303+
304+
const transaction = createTransactionOnScope();
305+
const spans = (transaction as unknown as Span).spanRecorder?.spans as Span[];
306+
307+
http.request({ method: 'GET', host: 'dogs.are.great', path: '/spaniel?tail=wag&cute=true#learn-more' });
308+
309+
expect(spans.length).toEqual(2);
310+
311+
// our span is at index 1 because the transaction itself is at index 0
312+
expect(spans[1].description).toEqual('GET http://dogs.are.great/spaniel');
313+
expect(spans[1].op).toEqual('http.client');
314+
expect(spans[1].data['http.method']).toEqual('GET');
315+
expect(spans[1].data.url).toEqual('http://dogs.are.great/spaniel');
316+
expect(spans[1].data['http.query']).toEqual('tail=wag&cute=true');
317+
expect(spans[1].data['http.fragment']).toEqual('learn-more');
318+
});
319+
301320
it.each([
302321
['user:pwd', '[Filtered]:[Filtered]@'],
303322
['user:', '[Filtered]:@'],

0 commit comments

Comments
 (0)