Skip to content

Commit 27686ba

Browse files
author
Luca Forstner
authored
fix(nextjs): Add tracing extensions in all serverside wrappers (#9537)
1 parent d80e27b commit 27686ba

12 files changed

+45
-13
lines changed

packages/nextjs/src/common/utils/edgeWrapperUtils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { captureException, flush, getCurrentHub, startTransaction } from '@sentry/core';
1+
import { addTracingExtensions, captureException, flush, getCurrentHub, startTransaction } from '@sentry/core';
22
import type { Span } from '@sentry/types';
33
import { addExceptionMechanism, logger, objectify, tracingContextFromHeaders } from '@sentry/utils';
44

@@ -12,6 +12,8 @@ export function withEdgeWrapping<H extends EdgeRouteHandler>(
1212
options: { spanDescription: string; spanOp: string; mechanismFunctionName: string },
1313
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
1414
return async function (this: unknown, ...args) {
15+
addTracingExtensions();
16+
1517
const req = args[0];
1618
const currentScope = getCurrentHub().getScope();
1719
const prevSpan = currentScope.getSpan();

packages/nextjs/src/common/wrapApiHandlerWithSentry.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { captureException, getCurrentHub, runWithAsyncContext, startTransaction } from '@sentry/core';
1+
import {
2+
addTracingExtensions,
3+
captureException,
4+
getCurrentHub,
5+
runWithAsyncContext,
6+
startTransaction,
7+
} from '@sentry/core';
28
import type { Transaction } from '@sentry/types';
39
import {
410
addExceptionMechanism,
@@ -74,6 +80,8 @@ export function withSentry(apiHandler: NextApiHandler, parameterizedRoute?: stri
7480
}
7581
req.__withSentry_applied__ = true;
7682

83+
addTracingExtensions();
84+
7785
// eslint-disable-next-line complexity, @typescript-eslint/no-explicit-any
7886
const boundHandler = runWithAsyncContext(
7987
// eslint-disable-next-line complexity

packages/nextjs/src/common/wrapApiHandlerWithSentryVercelCrons.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { captureCheckIn, runWithAsyncContext } from '@sentry/core';
1+
import { addTracingExtensions, captureCheckIn, runWithAsyncContext } from '@sentry/core';
22
import type { NextApiRequest } from 'next';
33

44
import type { VercelCronsConfig } from './types';
@@ -24,6 +24,7 @@ export function wrapApiHandlerWithSentryVercelCrons<F extends (...args: any[]) =
2424
return originalFunction.apply(thisArg, args);
2525
}
2626

27+
addTracingExtensions();
2728
const [req] = args as [NextApiRequest | EdgeRequest];
2829

2930
let maybePromiseResult;

packages/nextjs/src/common/wrapAppGetInitialPropsWithSentry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/core';
1+
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
22
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import type App from 'next/app';
44

@@ -26,6 +26,8 @@ export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetI
2626
return wrappingTarget.apply(thisArg, args);
2727
}
2828

29+
addTracingExtensions();
30+
2931
const [context] = args;
3032
const { req, res } = context.ctx;
3133

packages/nextjs/src/common/wrapDocumentGetInitialPropsWithSentry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/core';
1+
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
22
import type Document from 'next/document';
33

44
import { isBuild } from './utils/isBuild';
@@ -23,6 +23,8 @@ export function wrapDocumentGetInitialPropsWithSentry(
2323
return wrappingTarget.apply(thisArg, args);
2424
}
2525

26+
addTracingExtensions();
27+
2628
const [context] = args;
2729
const { req, res } = context;
2830

packages/nextjs/src/common/wrapErrorGetInitialPropsWithSentry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/core';
1+
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
22
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import type { NextPageContext } from 'next';
44
import type { ErrorProps } from 'next/error';
@@ -29,6 +29,8 @@ export function wrapErrorGetInitialPropsWithSentry(
2929
return wrappingTarget.apply(thisArg, args);
3030
}
3131

32+
addTracingExtensions();
33+
3234
const [context] = args;
3335
const { req, res } = context;
3436

packages/nextjs/src/common/wrapGetInitialPropsWithSentry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/core';
1+
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
22
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import type { NextPage } from 'next';
44

@@ -25,6 +25,8 @@ export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialPro
2525
return wrappingTarget.apply(thisArg, args);
2626
}
2727

28+
addTracingExtensions();
29+
2830
const [context] = args;
2931
const { req, res } = context;
3032

packages/nextjs/src/common/wrapGetServerSidePropsWithSentry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/core';
1+
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
22
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
33
import type { GetServerSideProps } from 'next';
44

@@ -26,6 +26,8 @@ export function wrapGetServerSidePropsWithSentry(
2626
return wrappingTarget.apply(thisArg, args);
2727
}
2828

29+
addTracingExtensions();
30+
2931
const [context] = args;
3032
const { req, res } = context;
3133

packages/nextjs/src/common/wrapGetStaticPropsWithSentry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/core';
1+
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
22
import type { GetStaticProps } from 'next';
33

44
import { isBuild } from './utils/isBuild';
@@ -23,6 +23,8 @@ export function wrapGetStaticPropsWithSentry(
2323
return wrappingTarget.apply(thisArg, args);
2424
}
2525

26+
addTracingExtensions();
27+
2628
const errorWrappedGetStaticProps = withErrorInstrumentation(wrappingTarget);
2729
const options = getCurrentHub().getClient()?.getOptions();
2830

packages/nextjs/src/common/wrapPageComponentWithSentry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { captureException, configureScope, runWithAsyncContext } from '@sentry/core';
1+
import { addTracingExtensions, captureException, configureScope, runWithAsyncContext } from '@sentry/core';
22
import { addExceptionMechanism, extractTraceparentData } from '@sentry/utils';
33

44
interface FunctionComponent {
@@ -21,6 +21,7 @@ function isReactClassComponent(target: unknown): target is ClassComponent {
2121
* Wraps a page component with Sentry error instrumentation.
2222
*/
2323
export function wrapPageComponentWithSentry(pageComponent: FunctionComponent | ClassComponent): unknown {
24+
addTracingExtensions();
2425
if (isReactClassComponent(pageComponent)) {
2526
return class SentryWrappedPageComponent extends pageComponent {
2627
public render(...args: unknown[]): unknown {

packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { captureException, flush, getCurrentHub, runWithAsyncContext, trace } from '@sentry/core';
1+
import { addTracingExtensions, captureException, flush, getCurrentHub, runWithAsyncContext, trace } from '@sentry/core';
22
import { addExceptionMechanism, tracingContextFromHeaders } from '@sentry/utils';
33

44
import { isRedirectNavigationError } from './nextNavigationErrorUtils';
@@ -13,8 +13,8 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
1313
routeHandler: F,
1414
context: RouteHandlerContext,
1515
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>> {
16+
addTracingExtensions();
1617
const { method, parameterizedRoute, baggageHeader, sentryTraceHeader } = context;
17-
1818
return new Proxy(routeHandler, {
1919
apply: (originalFunction, thisArg, args) => {
2020
return runWithAsyncContext(async () => {

packages/nextjs/src/common/wrapServerComponentWithSentry.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { captureException, flush, getCurrentHub, runWithAsyncContext, startTransaction } from '@sentry/core';
1+
import {
2+
addTracingExtensions,
3+
captureException,
4+
flush,
5+
getCurrentHub,
6+
runWithAsyncContext,
7+
startTransaction,
8+
} from '@sentry/core';
29
import { addExceptionMechanism, tracingContextFromHeaders } from '@sentry/utils';
310

411
import { isNotFoundNavigationError, isRedirectNavigationError } from '../common/nextNavigationErrorUtils';
@@ -12,6 +19,7 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
1219
appDirComponent: F,
1320
context: ServerComponentContext,
1421
): F {
22+
addTracingExtensions();
1523
const { componentRoute, componentType } = context;
1624

1725
// Even though users may define server components as async functions, for the client bundles

0 commit comments

Comments
 (0)