Skip to content

Commit

Permalink
fix(tracing-internal): Remove query params from urls with a trailing …
Browse files Browse the repository at this point in the history
…slash (#9328)

fix use case when original req url ends with trailing slash and contains query params.
Example: `api/v1/users/123/posts/?param=1`
  • Loading branch information
LubomirIgonda1 authored Oct 23, 2023
1 parent 9218eaa commit a6c44af
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,55 @@ test('should construct correct url with multiple parameterized routers, when par
});
}
});

test('should construct correct url with multiple parameterized routers, when param is also contain in middle layer route and express used multiple middlewares with route and original url has query params', async () => {
const env = await TestEnv.init(__dirname, `${__dirname}/server.ts`);
const event = await env.getEnvelopeRequest({
url: env.url.replace('test', 'api/api/v1/sub-router/users/123/posts/456?param=1'),
envelopeType: 'transaction',
});
// parse node.js major version
const [major] = process.versions.node.split('.').map(Number);
// Split test result base on major node version because regex d flag is support from node 16+
if (major >= 16) {
assertSentryEvent(event[2] as any, {
transaction: 'GET /api/api/v1/sub-router/users/:userId/posts/:postId',
transaction_info: {
source: 'route',
},
});
} else {
assertSentryEvent(event[2] as any, {
transaction: 'GET /api/api/v1/sub-router/users/123/posts/:postId',
transaction_info: {
source: 'route',
},
});
}
});

test('should construct correct url with multiple parameterized routers, when param is also contain in middle layer route and express used multiple middlewares with route and original url ends with trailing slash and has query params', async () => {
const env = await TestEnv.init(__dirname, `${__dirname}/server.ts`);
const event = await env.getEnvelopeRequest({
url: env.url.replace('test', 'api/api/v1/sub-router/users/123/posts/456/?param=1'),
envelopeType: 'transaction',
});
// parse node.js major version
const [major] = process.versions.node.split('.').map(Number);
// Split test result base on major node version because regex d flag is support from node 16+
if (major >= 16) {
assertSentryEvent(event[2] as any, {
transaction: 'GET /api/api/v1/sub-router/users/:userId/posts/:postId',
transaction_info: {
source: 'route',
},
});
} else {
assertSentryEvent(event[2] as any, {
transaction: 'GET /api/api/v1/sub-router/users/123/posts/:postId',
transaction_info: {
source: 'route',
},
});
}
});
2 changes: 1 addition & 1 deletion packages/tracing-internal/src/node/integrations/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function instrumentRouter(appOrRouter: ExpressRouter): void {
// Now we check if we are in the "last" part of the route. We determine this by comparing the
// number of URL segments from the original URL to that of our reconstructed parameterized URL.
// If we've reached our final destination, we update the transaction name.
const urlLength = getNumberOfUrlSegments(req.originalUrl || '') + numExtraSegments;
const urlLength = getNumberOfUrlSegments(stripUrlQueryAndFragment(req.originalUrl || '')) + numExtraSegments;
const routeLength = getNumberOfUrlSegments(req._reconstructedRoute);

if (urlLength === routeLength) {
Expand Down

0 comments on commit a6c44af

Please sign in to comment.