Skip to content

fix(v7/tracing-internal): Fix case when lrp keys offset is 0 #14615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 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,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
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
Loading