From a6c44afaf3d398c2dea58887e9e756e56e5f05cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=BDubom=C3=ADrIgonda?= <9303146+LubomirIgonda1@users.noreply.github.com> Date: Mon, 23 Oct 2023 12:33:15 +0200 Subject: [PATCH] fix(tracing-internal): Remove query params from urls with a trailing 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` --- .../multiple-routers/complex-router/test.ts | 52 +++++++++++++++++++ .../src/node/integrations/express.ts | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/packages/node-integration-tests/suites/express/multiple-routers/complex-router/test.ts b/packages/node-integration-tests/suites/express/multiple-routers/complex-router/test.ts index b8079bfdc0ac..05de34cc8159 100644 --- a/packages/node-integration-tests/suites/express/multiple-routers/complex-router/test.ts +++ b/packages/node-integration-tests/suites/express/multiple-routers/complex-router/test.ts @@ -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', + }, + }); + } +}); diff --git a/packages/tracing-internal/src/node/integrations/express.ts b/packages/tracing-internal/src/node/integrations/express.ts index 4327f11c5ea7..e46096d9ed84 100644 --- a/packages/tracing-internal/src/node/integrations/express.ts +++ b/packages/tracing-internal/src/node/integrations/express.ts @@ -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) {