Skip to content

Commit

Permalink
ref(nextjs): Make Pages Router API Routes instrumentation OTEL ready (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
lforst authored Oct 8, 2024
1 parent 121bcee commit fbb17f9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
captureException,
continueTrace,
getActiveSpan,
getRootSpan,
setHttpStatus,
startSpanManual,
withIsolationScope,
} from '@sentry/core';
import { consoleSandbox, isString, logger, objectify } from '@sentry/utils';

import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../span-attributes-with-logic-attached';
import type { AugmentedNextApiRequest, AugmentedNextApiResponse, NextApiHandler } from '../types';
import { flushSafelyWithTimeout } from '../utils/responseEnd';
import { escapeNextjsTracing } from '../utils/tracingUtils';
Expand All @@ -24,6 +27,15 @@ import { vercelWaitUntil } from '../utils/vercelWaitUntil';
* @returns The wrapped handler
*/
export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameterizedRoute: string): NextApiHandler {
// Since the API route handler spans emitted by Next.js are super buggy with completely wrong timestamps
// (fix pending at the time of writing this: https://github.com/vercel/next.js/pull/70908) we want to intentionally
// drop them. In the future, when Next.js' OTEL instrumentation is in a high-quality place we can potentially think
// about keeping them.
const nextJsOwnedSpan = getActiveSpan();
if (nextJsOwnedSpan) {
getRootSpan(nextJsOwnedSpan)?.setAttribute(TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, true);
}

return new Proxy(apiHandler, {
apply: (
wrappingTarget,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* If this attribute is attached to a transaction, the Next.js SDK will drop that transaction.
*/
export const TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION = 'sentry.drop_transaction';
6 changes: 6 additions & 0 deletions packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { EventProcessor } from '@sentry/types';
import { DEBUG_BUILD } from '../common/debug-build';
import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor';
import { getVercelEnv } from '../common/getVercelEnv';
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../common/span-attributes-with-logic-attached';
import { isBuild } from '../common/utils/isBuild';
import { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration';

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

// Filter transactions that we explicitly want to drop.
if (event.contexts?.trace?.data?.[TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION]) {
return null;
}

return event;
} else {
return event;
Expand Down

0 comments on commit fbb17f9

Please sign in to comment.