Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): fill in span data from http request options object #9112

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things:

  1. Let's try-catch this to ensure this does not fail if we cannot generate an URL there, just to be on the safe side
  2. Let's instead set the parsed values only if they aren't set.

So something like this:

try {
  const parsed = new URL(...);
  requestOptions = { 
    pathname: parsed.pathname, 
    search: parsed.search, 
    hash: parsed.hash,
    ...requestOptions
  };
} catch (e) {
  // ignore errors here
}

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