Skip to content

Commit fbb17f9

Browse files
author
Luca Forstner
authored
ref(nextjs): Make Pages Router API Routes instrumentation OTEL ready (#13904)
Drop all spans/transactions emitted by Next.js for pages router API routes because they are currently super buggy with wrong timestamps. Fix pending: vercel/next.js#70908 This is just a bit of prep-work so we can at some point disable the logic where we turn on all Next.js spans and still have high quality data.
1 parent 121bcee commit fbb17f9

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import {
22
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
33
captureException,
44
continueTrace,
5+
getActiveSpan,
6+
getRootSpan,
57
setHttpStatus,
68
startSpanManual,
79
withIsolationScope,
810
} from '@sentry/core';
911
import { consoleSandbox, isString, logger, objectify } from '@sentry/utils';
1012

1113
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
14+
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../span-attributes-with-logic-attached';
1215
import type { AugmentedNextApiRequest, AugmentedNextApiResponse, NextApiHandler } from '../types';
1316
import { flushSafelyWithTimeout } from '../utils/responseEnd';
1417
import { escapeNextjsTracing } from '../utils/tracingUtils';
@@ -24,6 +27,15 @@ import { vercelWaitUntil } from '../utils/vercelWaitUntil';
2427
* @returns The wrapped handler
2528
*/
2629
export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameterizedRoute: string): NextApiHandler {
30+
// Since the API route handler spans emitted by Next.js are super buggy with completely wrong timestamps
31+
// (fix pending at the time of writing this: https://github.com/vercel/next.js/pull/70908) we want to intentionally
32+
// drop them. In the future, when Next.js' OTEL instrumentation is in a high-quality place we can potentially think
33+
// about keeping them.
34+
const nextJsOwnedSpan = getActiveSpan();
35+
if (nextJsOwnedSpan) {
36+
getRootSpan(nextJsOwnedSpan)?.setAttribute(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true);
37+
}
38+
2739
return new Proxy(apiHandler, {
2840
apply: (
2941
wrappingTarget,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* If this attribute is attached to a transaction, the Next.js SDK will drop that transaction.
3+
*/
4+
export const TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION = 'sentry.drop_transaction';

packages/nextjs/src/server/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type { EventProcessor } from '@sentry/types';
2121
import { DEBUG_BUILD } from '../common/debug-build';
2222
import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor';
2323
import { getVercelEnv } from '../common/getVercelEnv';
24+
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../common/span-attributes-with-logic-attached';
2425
import { isBuild } from '../common/utils/isBuild';
2526
import { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration';
2627

@@ -249,6 +250,11 @@ export function init(options: NodeOptions): NodeClient | undefined {
249250
return null;
250251
}
251252

253+
// Filter transactions that we explicitly want to drop.
254+
if (event.contexts?.trace?.data?.[TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION]) {
255+
return null;
256+
}
257+
252258
return event;
253259
} else {
254260
return event;

0 commit comments

Comments
 (0)