Skip to content

Commit 55abfa8

Browse files
committed
fix(nextjs): Backfill route on pages-router API transaction
The pages-router API wrapper no longer creates its own transaction and instead binds onto Next.js's BaseServer.handleRequest root span. For an API route that span carries no http.route/next.route, so enhanceHandleRequestRootSpan never sets the route source and the transaction keeps a "custom" source. Set the route backfill attribute the same way the data-fetcher wrapper does so the transaction gets a "route" source and a parameterized http.route.
1 parent 3109b7e commit 55abfa8

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
withIsolationScope,
1111
} from '@sentry/core';
1212
import type { NextApiRequest } from 'next';
13+
import { TRANSACTION_ATTR_SENTRY_ROUTE_BACKFILL } from '../span-attributes-with-logic-attached';
1314
import type { AugmentedNextApiResponse, NextApiHandler } from '../types';
1415
import { flushSafelyWithTimeout, waitUntil } from '../utils/responseEnd';
1516

@@ -65,6 +66,11 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
6566
const rootSpan = activeSpan ? getRootSpan(activeSpan) : undefined;
6667
if (rootSpan) {
6768
setCapturedScopesOnSpan(rootSpan, getCurrentScope(), isolationScope);
69+
70+
// The `BaseServer.handleRequest` root span for a pages-router API route carries no `http.route`, so it would
71+
// otherwise be named from the raw URL with a `url` source. Backfill the parameterized route so the transaction
72+
// gets a `route` source and a parameterized `http.route`.
73+
rootSpan.setAttribute(TRANSACTION_ATTR_SENTRY_ROUTE_BACKFILL, parameterizedRoute);
6874
}
6975

7076
try {

0 commit comments

Comments
 (0)