Skip to content

Commit e6a572d

Browse files
committed
make stripping of span urls optional
1 parent d315f0f commit e6a572d

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

packages/node/src/integrations/http.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,21 @@ function addRequestBreadcrumb(event: string, url: string, req: http.IncomingMess
149149
/**
150150
* Assemble a URL to be used for breadcrumbs and spans.
151151
*
152-
* @param requestArgs URL string or object containing the component parts
152+
* @param url URL string or object containing the component parts
153+
* @param strip Whether or not to strip querystring and fragment. Defaults to false.
154+
*
153155
* @returns Fully-formed URL
154156
*/
155-
export function extractUrl(requestArgs: string | http.ClientRequestArgs): string {
156-
if (typeof requestArgs === 'string') {
157-
return stripUrlQueryAndFragment(requestArgs);
157+
export function extractUrl(url: string | http.ClientRequestArgs, strip: boolean = false): string {
158+
if (typeof url === 'string') {
159+
return strip ? stripUrlQueryAndFragment(url) : url;
158160
}
159-
const protocol = requestArgs.protocol || '';
160-
const hostname = requestArgs.hostname || requestArgs.host || '';
161+
162+
const protocol = url.protocol || '';
163+
const hostname = url.hostname || url.host || '';
161164
// Don't log standard :80 (http) and :443 (https) ports to reduce the noise
162-
const port = !requestArgs.port || requestArgs.port === 80 || requestArgs.port === 443 ? '' : `:${requestArgs.port}`;
163-
const path = requestArgs.path ? stripUrlQueryAndFragment(requestArgs.path) : '/';
165+
const port = !url.port || url.port === 80 || url.port === 443 ? '' : `:${url.port}`;
166+
const path = url.path ? (strip ? stripUrlQueryAndFragment(url.path) : url.path) : '/';
164167

165168
// internal routes end up with too many slashes
166169
return `${protocol}//${hostname}${port}${path}`.replace('///', '/');

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

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,69 @@ describe('extractUrl()', () => {
2222
expect(extractUrl(urlParts)).toBe('http://dogs.are.great:1231/yay/');
2323
});
2424

25-
it('strips query string from url string', () => {
25+
it("by default doesn't strip query string from url string", () => {
2626
const urlWithQueryString = `${urlString}${queryString}`;
27-
expect(extractUrl(urlWithQueryString)).toBe(urlString);
27+
expect(extractUrl(urlWithQueryString)).toBe(urlWithQueryString);
2828
});
2929

30-
it('strips query string from path in http.RequestOptions object', () => {
30+
it("by default doesn't strip query string from path in http.RequestOptions object", () => {
3131
const urlPartsWithQueryString = { ...urlParts, path: `${urlParts.path}${queryString}` };
32-
expect(extractUrl(urlPartsWithQueryString)).toBe(urlString);
32+
const urlWithQueryString = `${urlString}${queryString}`;
33+
34+
expect(extractUrl(urlPartsWithQueryString)).toBe(urlWithQueryString);
35+
});
36+
37+
it("by default doesn't strip fragment from url string", () => {
38+
const urlWithFragment = `${urlString}${fragment}`;
39+
expect(extractUrl(urlWithFragment)).toBe(urlWithFragment);
40+
});
41+
42+
it("by default doesn't strip fragment from path in http.RequestOptions object", () => {
43+
const urlPartsWithFragment = { ...urlParts, path: `${urlParts.path}${fragment}` };
44+
const urlWithFragment = `${urlString}${fragment}`;
45+
46+
expect(extractUrl(urlPartsWithFragment)).toBe(urlWithFragment);
47+
});
48+
49+
it("by default doesn't strip query string and fragment from url string", () => {
50+
const urlWithQueryStringAndFragment = `${urlString}${queryString}${fragment}`;
51+
expect(extractUrl(urlWithQueryStringAndFragment)).toBe(urlWithQueryStringAndFragment);
52+
});
53+
54+
it("by default doesn't strip query string and fragment from path in http.RequestOptions object", () => {
55+
const urlPartsWithQueryStringAndFragment = { ...urlParts, path: `${urlParts.path}${queryString}${fragment}` };
56+
const urlWithQueryStringAndFragment = `${urlString}${queryString}${fragment}`;
57+
58+
expect(extractUrl(urlPartsWithQueryStringAndFragment)).toBe(urlWithQueryStringAndFragment);
59+
});
60+
61+
it('strips query string from url string when asked', () => {
62+
const urlWithQueryString = `${urlString}${queryString}`;
63+
expect(extractUrl(urlWithQueryString, true)).toBe(urlString);
64+
});
65+
66+
it('strips query string from path in http.RequestOptions object when asked', () => {
67+
const urlPartsWithQueryString = { ...urlParts, path: `${urlParts.path}${queryString}` };
68+
expect(extractUrl(urlPartsWithQueryString, true)).toBe(urlString);
3369
});
3470

35-
it('strips fragment from url string', () => {
71+
it('strips fragment from url string when asked', () => {
3672
const urlWithFragment = `${urlString}${fragment}`;
37-
expect(extractUrl(urlWithFragment)).toBe(urlString);
73+
expect(extractUrl(urlWithFragment, true)).toBe(urlString);
3874
});
3975

40-
it('strips fragment from path in http.RequestOptions object', () => {
76+
it('strips fragment from path in http.RequestOptions object when asked', () => {
4177
const urlPartsWithFragment = { ...urlParts, path: `${urlParts.path}${fragment}` };
42-
expect(extractUrl(urlPartsWithFragment)).toBe(urlString);
78+
expect(extractUrl(urlPartsWithFragment, true)).toBe(urlString);
4379
});
4480

45-
it('strips query string and fragment from url string', () => {
81+
it('strips query string and fragment from url string when asked', () => {
4682
const urlWithQueryStringAndFragment = `${urlString}${queryString}${fragment}`;
47-
expect(extractUrl(urlWithQueryStringAndFragment)).toBe(urlString);
83+
expect(extractUrl(urlWithQueryStringAndFragment, true)).toBe(urlString);
4884
});
4985

50-
it('strips query string and fragment from path in http.RequestOptions object', () => {
86+
it('strips query string and fragment from path in http.RequestOptions object when asked', () => {
5187
const urlPartsWithQueryStringAndFragment = { ...urlParts, path: `${urlParts.path}${queryString}${fragment}` };
52-
expect(extractUrl(urlPartsWithQueryStringAndFragment)).toBe(urlString);
88+
expect(extractUrl(urlPartsWithQueryStringAndFragment, true)).toBe(urlString);
5389
});
5490
}); // end describe('extractUrl()')

0 commit comments

Comments
 (0)