From 27686ba79e6c586cebc99dfdcf5f559a46d22d50 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 13 Nov 2023 17:06:28 +0100 Subject: [PATCH] fix(nextjs): Add tracing extensions in all serverside wrappers (#9537) --- packages/nextjs/src/common/utils/edgeWrapperUtils.ts | 4 +++- packages/nextjs/src/common/wrapApiHandlerWithSentry.ts | 10 +++++++++- .../src/common/wrapApiHandlerWithSentryVercelCrons.ts | 3 ++- .../src/common/wrapAppGetInitialPropsWithSentry.ts | 4 +++- .../common/wrapDocumentGetInitialPropsWithSentry.ts | 4 +++- .../src/common/wrapErrorGetInitialPropsWithSentry.ts | 4 +++- .../nextjs/src/common/wrapGetInitialPropsWithSentry.ts | 4 +++- .../src/common/wrapGetServerSidePropsWithSentry.ts | 4 +++- .../nextjs/src/common/wrapGetStaticPropsWithSentry.ts | 4 +++- .../nextjs/src/common/wrapPageComponentWithSentry.ts | 3 ++- .../nextjs/src/common/wrapRouteHandlerWithSentry.ts | 4 ++-- .../nextjs/src/common/wrapServerComponentWithSentry.ts | 10 +++++++++- 12 files changed, 45 insertions(+), 13 deletions(-) diff --git a/packages/nextjs/src/common/utils/edgeWrapperUtils.ts b/packages/nextjs/src/common/utils/edgeWrapperUtils.ts index 26239f3db2ee..df169a6f7e2d 100644 --- a/packages/nextjs/src/common/utils/edgeWrapperUtils.ts +++ b/packages/nextjs/src/common/utils/edgeWrapperUtils.ts @@ -1,4 +1,4 @@ -import { captureException, flush, getCurrentHub, startTransaction } from '@sentry/core'; +import { addTracingExtensions, captureException, flush, getCurrentHub, startTransaction } from '@sentry/core'; import type { Span } from '@sentry/types'; import { addExceptionMechanism, logger, objectify, tracingContextFromHeaders } from '@sentry/utils'; @@ -12,6 +12,8 @@ export function withEdgeWrapping( options: { spanDescription: string; spanOp: string; mechanismFunctionName: string }, ): (...params: Parameters) => Promise> { return async function (this: unknown, ...args) { + addTracingExtensions(); + const req = args[0]; const currentScope = getCurrentHub().getScope(); const prevSpan = currentScope.getSpan(); diff --git a/packages/nextjs/src/common/wrapApiHandlerWithSentry.ts b/packages/nextjs/src/common/wrapApiHandlerWithSentry.ts index fdc26537607d..5a93a257a209 100644 --- a/packages/nextjs/src/common/wrapApiHandlerWithSentry.ts +++ b/packages/nextjs/src/common/wrapApiHandlerWithSentry.ts @@ -1,4 +1,10 @@ -import { captureException, getCurrentHub, runWithAsyncContext, startTransaction } from '@sentry/core'; +import { + addTracingExtensions, + captureException, + getCurrentHub, + runWithAsyncContext, + startTransaction, +} from '@sentry/core'; import type { Transaction } from '@sentry/types'; import { addExceptionMechanism, @@ -74,6 +80,8 @@ export function withSentry(apiHandler: NextApiHandler, parameterizedRoute?: stri } req.__withSentry_applied__ = true; + addTracingExtensions(); + // eslint-disable-next-line complexity, @typescript-eslint/no-explicit-any const boundHandler = runWithAsyncContext( // eslint-disable-next-line complexity diff --git a/packages/nextjs/src/common/wrapApiHandlerWithSentryVercelCrons.ts b/packages/nextjs/src/common/wrapApiHandlerWithSentryVercelCrons.ts index 43672a1acf28..ae7bb19be43e 100644 --- a/packages/nextjs/src/common/wrapApiHandlerWithSentryVercelCrons.ts +++ b/packages/nextjs/src/common/wrapApiHandlerWithSentryVercelCrons.ts @@ -1,4 +1,4 @@ -import { captureCheckIn, runWithAsyncContext } from '@sentry/core'; +import { addTracingExtensions, captureCheckIn, runWithAsyncContext } from '@sentry/core'; import type { NextApiRequest } from 'next'; import type { VercelCronsConfig } from './types'; @@ -24,6 +24,7 @@ export function wrapApiHandlerWithSentryVercelCrons any>( routeHandler: F, context: RouteHandlerContext, ): (...args: Parameters) => ReturnType extends Promise ? ReturnType : Promise> { + addTracingExtensions(); const { method, parameterizedRoute, baggageHeader, sentryTraceHeader } = context; - return new Proxy(routeHandler, { apply: (originalFunction, thisArg, args) => { return runWithAsyncContext(async () => { diff --git a/packages/nextjs/src/common/wrapServerComponentWithSentry.ts b/packages/nextjs/src/common/wrapServerComponentWithSentry.ts index 6efa50d2b804..e909bd114c7c 100644 --- a/packages/nextjs/src/common/wrapServerComponentWithSentry.ts +++ b/packages/nextjs/src/common/wrapServerComponentWithSentry.ts @@ -1,4 +1,11 @@ -import { captureException, flush, getCurrentHub, runWithAsyncContext, startTransaction } from '@sentry/core'; +import { + addTracingExtensions, + captureException, + flush, + getCurrentHub, + runWithAsyncContext, + startTransaction, +} from '@sentry/core'; import { addExceptionMechanism, tracingContextFromHeaders } from '@sentry/utils'; import { isNotFoundNavigationError, isRedirectNavigationError } from '../common/nextNavigationErrorUtils'; @@ -12,6 +19,7 @@ export function wrapServerComponentWithSentry any> appDirComponent: F, context: ServerComponentContext, ): F { + addTracingExtensions(); const { componentRoute, componentType } = context; // Even though users may define server components as async functions, for the client bundles