Skip to content

Commit

Permalink
fix(node): fill in span data from http request options object
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-zhukov committed Sep 26, 2023
1 parent a57b66d commit 4de669b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/node/src/integrations/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ export function normalizeRequestArgs(
requestOptions = urlToOptions(requestArgs[0]);
} else {
requestOptions = requestArgs[0];

try {
const parsed = new URL(
requestOptions.path || '',
`${requestOptions.protocol || 'http:'}//${requestOptions.hostname}`,
);
requestOptions = {
pathname: parsed.pathname,
search: parsed.search,
hash: parsed.hash,
...requestOptions,
};
} catch (e) {
// ignore
}
}

// if the options were given separately from the URL, fold them in
Expand Down
19 changes: 19 additions & 0 deletions packages/node/test/integrations/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,25 @@ describe('tracing', () => {
expect(spans[1].data['http.fragment']).toEqual('learn-more');
});

it('fills in span data from http.RequestOptions object', () => {
nock('http://dogs.are.great').get('/spaniel?tail=wag&cute=true#learn-more').reply(200);

const transaction = createTransactionOnScope();
const spans = (transaction as unknown as Span).spanRecorder?.spans as Span[];

http.request({ method: 'GET', host: 'dogs.are.great', path: '/spaniel?tail=wag&cute=true#learn-more' });

expect(spans.length).toEqual(2);

// our span is at index 1 because the transaction itself is at index 0
expect(spans[1].description).toEqual('GET http://dogs.are.great/spaniel');
expect(spans[1].op).toEqual('http.client');
expect(spans[1].data['http.method']).toEqual('GET');
expect(spans[1].data.url).toEqual('http://dogs.are.great/spaniel');
expect(spans[1].data['http.query']).toEqual('tail=wag&cute=true');
expect(spans[1].data['http.fragment']).toEqual('learn-more');
});

it.each([
['user:pwd', '[Filtered]:[Filtered]@'],
['user:', '[Filtered]:@'],
Expand Down

0 comments on commit 4de669b

Please sign in to comment.