Skip to content

Commit 33e965d

Browse files
authored
fix(nextjs): Preserve next.route attribute on root spans (#16297)
1 parent fcdc4d7 commit 33e965d

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/server-components.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ test('Sends a transaction for a request to app router', async ({ page }) => {
2525
'http.status_code': 200,
2626
'http.target': '/server-component/parameter/1337/42',
2727
'otel.kind': 'SERVER',
28+
'next.route': '/server-component/parameter/[...parameters]',
2829
}),
2930
op: 'http.server',
3031
origin: 'auto',

packages/nextjs/src/server/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ export function init(options: NodeOptions): NodeClient | undefined {
176176
const route = spanAttributes['next.route'].replace(/\/route$/, '');
177177
rootSpan.updateName(route);
178178
rootSpan.setAttribute(ATTR_HTTP_ROUTE, route);
179+
// Preserving the original attribute despite internally not depending on it
180+
rootSpan.setAttribute('next.route', route);
179181
}
180182
}
181183

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

327329
if (typeof method === 'string' && typeof route === 'string') {
328-
event.transaction = `${method} ${route.replace(/\/route$/, '')}`;
330+
const cleanRoute = route.replace(/\/route$/, '');
331+
event.transaction = `${method} ${cleanRoute}`;
329332
event.contexts.trace.data[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] = 'route';
333+
// Preserve next.route in case it did not get hoisted
334+
event.contexts.trace.data['next.route'] = cleanRoute;
330335
}
331336

332337
// backfill transaction name for pages that would otherwise contain unparameterized routes

0 commit comments

Comments
 (0)