Skip to content

Commit 78892ad

Browse files
fix(tracing-internal): Fix case when lrp keys offset is 0
1 parent fde81a6 commit 78892ad

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,9 @@ export const extractOriginalRoute = (
413413
regexp?: Layer['regexp'],
414414
keys?: Layer['keys'],
415415
): string | undefined => {
416-
if (!path || !regexp || !keys || Object.keys(keys).length === 0 || !keys[0]?.offset) {
416+
if (!path || !regexp || !keys || Object.keys(keys).length === 0 || keys[0]?.offset === undefined || keys[0]?.offset === null) {
417417
return undefined;
418418
}
419-
420419
const orderedKeys = keys.sort((a, b) => a.offset - b.offset);
421420

422421
// add d flag for getting indices from regexp result

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,20 @@ if (major >= 16) {
8787
expect(extractOriginalRoute(path, regex, [key])).toBeUndefined();
8888
});
8989

90-
it('should return the original route path when valid inputs are provided', () => {
90+
it('should return the original route path when valid inputs are provided first static value then dynamic', () => {
9191
const path = '/router/123';
9292
const regex = /^\/router\/(\d+)$/;
9393
const keys = [{ name: 'pathParam', offset: 8, optional: false }];
9494
expect(extractOriginalRoute(path, regex, keys)).toBe('/router/:pathParam');
9595
});
9696

97+
it('should return the original route path when valid inputs are provided first dynamic value then static', () => {
98+
const path = '/123/router';
99+
const regex = /^(?:\/([^/]+?))\/router\/?(?=\/|$)/i;
100+
const keys = [{ name: 'pathParam', offset: 0, optional: false }];
101+
expect(extractOriginalRoute(path, regex, keys)).toBe('/:pathParam/router');
102+
});
103+
97104
it('should handle multiple parameters in the route', () => {
98105
const path = '/user/42/profile/username';
99106
const regex = /^\/user\/(\d+)\/profile\/(\w+)$/;

0 commit comments

Comments
 (0)