Skip to content

fix(nextjs): Preserve next.route attribute on root spans #16297

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
May 15, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test('Sends a transaction for a request to app router', async ({ page }) => {
'http.status_code': 200,
'http.target': '/server-component/parameter/1337/42',
'otel.kind': 'SERVER',
'next.route': '/server-component/parameter/[...parameters]',
}),
op: 'http.server',
origin: 'auto',
Expand Down
9 changes: 7 additions & 2 deletions packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export function init(options: NodeOptions): NodeClient | undefined {
const route = spanAttributes['next.route'].replace(/\/route$/, '');
rootSpan.updateName(route);
rootSpan.setAttribute(ATTR_HTTP_ROUTE, route);
// Preserving the original attribute despite internally not depending on it
rootSpan.setAttribute('next.route', route);
}
}

Expand Down Expand Up @@ -322,11 +324,14 @@ export function init(options: NodeOptions): NodeClient | undefined {
const method = event.contexts.trace.data[SEMATTRS_HTTP_METHOD];
// eslint-disable-next-line deprecation/deprecation
const target = event.contexts?.trace?.data?.[SEMATTRS_HTTP_TARGET];
const route = event.contexts.trace.data[ATTR_HTTP_ROUTE];
const route = event.contexts.trace.data[ATTR_HTTP_ROUTE] || event.contexts.trace.data['next.route'];

if (typeof method === 'string' && typeof route === 'string') {
event.transaction = `${method} ${route.replace(/\/route$/, '')}`;
const cleanRoute = route.replace(/\/route$/, '');
event.transaction = `${method} ${cleanRoute}`;
event.contexts.trace.data[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] = 'route';
// Preserve next.route in case it did not get hoisted
event.contexts.trace.data['next.route'] = cleanRoute;
}

// backfill transaction name for pages that would otherwise contain unparameterized routes
Expand Down
Loading