Skip to content

Commit d47eb1a

Browse files
authored
fix(node): fill in span data from http request options object (#9112)
Currently, span data is missing for Node.js requests created with options object as the only argument, such as `http.request(options)`.
1 parent 647c7b9 commit d47eb1a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,21 @@ export function normalizeRequestArgs(
168168
requestOptions = urlToOptions(requestArgs[0]);
169169
} else {
170170
requestOptions = requestArgs[0];
171+
172+
try {
173+
const parsed = new URL(
174+
requestOptions.path || '',
175+
`${requestOptions.protocol || 'http:'}//${requestOptions.hostname}`,
176+
);
177+
requestOptions = {
178+
pathname: parsed.pathname,
179+
search: parsed.search,
180+
hash: parsed.hash,
181+
...requestOptions,
182+
};
183+
} catch (e) {
184+
// ignore
185+
}
171186
}
172187

173188
// 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)