Skip to content

Commit

Permalink
fix(tracing-internal): Fix case when lrp keys offset is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
LubomirIgonda1 committed Dec 6, 2024
1 parent fde81a6 commit 78892ad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 1 addition & 2 deletions packages/tracing-internal/src/node/integrations/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,9 @@ 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
Expand Down
9 changes: 8 additions & 1 deletion packages/tracing-internal/test/node/express.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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+)$/;
Expand Down

0 comments on commit 78892ad

Please sign in to comment.