Skip to content

Commit 332f8e7

Browse files
fix(tracing-internal): Fix case when originalURL contain query params (#9531)
This PR fix use case when original req url contain query params. For example: `api/v1/users/posts?param=1` ### Old behavior sentry build req._reconstructedRoute as: api/v1/users ### New behavior sentry build req._reconstructedRoute as: api/v1/users/posts
1 parent aed4bf0 commit 332f8e7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/tracing-internal/src/node/integrations/express.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,9 @@ export function preventDuplicateSegments(
545545
reconstructedRoute?: string,
546546
layerPath?: string,
547547
): string | undefined {
548-
const originalUrlSplit = originalUrl?.split('/').filter(v => !!v);
548+
// filter query params
549+
const normalizeURL = stripUrlQueryAndFragment(originalUrl || '');
550+
const originalUrlSplit = normalizeURL?.split('/').filter(v => !!v);
549551
let tempCounter = 0;
550552
const currentOffset = reconstructedRoute?.split('/').filter(v => !!v).length || 0;
551553
const result = layerPath

packages/tracing-internal/test/node/express.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ describe('unit Test for preventDuplicateSegments', () => {
3535
const result1 = preventDuplicateSegments(originalUrl, reconstructedRoute, layerPath);
3636
expect(result1).toBe('1234');
3737
});
38+
39+
it('should prevent duplicate segment v1 originalUrl with query param without trailing slash', () => {
40+
const originalUrl = '/api/v1/1234?queryParam=123';
41+
const reconstructedRoute = '/api/v1';
42+
const layerPath = '/v1/1234';
43+
const result1 = preventDuplicateSegments(originalUrl, reconstructedRoute, layerPath);
44+
expect(result1).toBe('1234');
45+
});
46+
47+
it('should prevent duplicate segment v1 originalUrl with query param with trailing slash', () => {
48+
const originalUrl = '/api/v1/1234/?queryParam=123';
49+
const reconstructedRoute = '/api/v1';
50+
const layerPath = '/v1/1234';
51+
const result1 = preventDuplicateSegments(originalUrl, reconstructedRoute, layerPath);
52+
expect(result1).toBe('1234');
53+
});
3854
});
3955
describe('preventDuplicateSegments should handle empty input gracefully', () => {
4056
it('Empty input values', () => {

0 commit comments

Comments
 (0)