diff --git a/packages/tracing-internal/src/node/integrations/express.ts b/packages/tracing-internal/src/node/integrations/express.ts index ae9a9f7aeb98..245f2d231327 100644 --- a/packages/tracing-internal/src/node/integrations/express.ts +++ b/packages/tracing-internal/src/node/integrations/express.ts @@ -413,10 +413,16 @@ export const extractOriginalRoute = ( regexp?: Layer['regexp'], keys?: Layer['keys'], ): string | undefined => { - if (!path || !regexp || !keys || Object.keys(keys).length === 0 || !keys[0]?.offset) { + if ( + !path || + !regexp || + !keys || + Object.keys(keys).length === 0 || + keys[0]?.offset === undefined || + keys[0]?.offset === null + ) { return undefined; } - const orderedKeys = keys.sort((a, b) => a.offset - b.offset); // add d flag for getting indices from regexp result diff --git a/packages/tracing-internal/test/node/express.test.ts b/packages/tracing-internal/test/node/express.test.ts index 1631971d9863..3bb7c0892428 100644 --- a/packages/tracing-internal/test/node/express.test.ts +++ b/packages/tracing-internal/test/node/express.test.ts @@ -87,13 +87,20 @@ if (major >= 16) { expect(extractOriginalRoute(path, regex, [key])).toBeUndefined(); }); - it('should return the original route path when valid inputs are provided', () => { + it('should return the original route path when valid inputs are provided first static value then dynamic', () => { const path = '/router/123'; const regex = /^\/router\/(\d+)$/; const keys = [{ name: 'pathParam', offset: 8, optional: false }]; expect(extractOriginalRoute(path, regex, keys)).toBe('/router/:pathParam'); }); + it('should return the original route path when valid inputs are provided first dynamic value then static', () => { + const path = '/123/router'; + const regex = /^(?:\/([^/]+?))\/router\/?(?=\/|$)/i; + const keys = [{ name: 'pathParam', offset: 0, optional: false }]; + expect(extractOriginalRoute(path, regex, keys)).toBe('/:pathParam/router'); + }); + it('should handle multiple parameters in the route', () => { const path = '/user/42/profile/username'; const regex = /^\/user\/(\d+)\/profile\/(\w+)$/;