Skip to content
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

ref(nextjs): Make Pages Router API Routes instrumentation OTEL ready #13904

Merged
merged 1 commit into from
Oct 8, 2024
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 @@ -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 @@ -244,6 +245,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
Loading