Skip to content

fix(nextjs): Add tracing extensions in all serverside wrappers #9537

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

Merged
merged 1 commit into from
Nov 13, 2023
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
4 changes: 3 additions & 1 deletion packages/nextjs/src/common/utils/edgeWrapperUtils.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -12,6 +12,8 @@ export function withEdgeWrapping<H extends EdgeRouteHandler>(
options: { spanDescription: string; spanOp: string; mechanismFunctionName: string },
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
return async function (this: unknown, ...args) {
addTracingExtensions();

const req = args[0];
const currentScope = getCurrentHub().getScope();
const prevSpan = currentScope.getSpan();
Expand Down
10 changes: 9 additions & 1 deletion packages/nextjs/src/common/wrapApiHandlerWithSentry.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,6 +24,7 @@ export function wrapApiHandlerWithSentryVercelCrons<F extends (...args: any[]) =
return originalFunction.apply(thisArg, args);
}

addTracingExtensions();
const [req] = args as [NextApiRequest | EdgeRequest];

let maybePromiseResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub } from '@sentry/core';
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
import type App from 'next/app';

Expand Down Expand Up @@ -26,6 +26,8 @@ export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetI
return wrappingTarget.apply(thisArg, args);
}

addTracingExtensions();

const [context] = args;
const { req, res } = context.ctx;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub } from '@sentry/core';
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
import type Document from 'next/document';

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

addTracingExtensions();

const [context] = args;
const { req, res } = context;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub } from '@sentry/core';
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
import type { NextPageContext } from 'next';
import type { ErrorProps } from 'next/error';
Expand Down Expand Up @@ -29,6 +29,8 @@ export function wrapErrorGetInitialPropsWithSentry(
return wrappingTarget.apply(thisArg, args);
}

addTracingExtensions();

const [context] = args;
const { req, res } = context;

Expand Down
4 changes: 3 additions & 1 deletion packages/nextjs/src/common/wrapGetInitialPropsWithSentry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub } from '@sentry/core';
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
import type { NextPage } from 'next';

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

addTracingExtensions();

const [context] = args;
const { req, res } = context;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub } from '@sentry/core';
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
import type { GetServerSideProps } from 'next';

Expand Down Expand Up @@ -26,6 +26,8 @@ export function wrapGetServerSidePropsWithSentry(
return wrappingTarget.apply(thisArg, args);
}

addTracingExtensions();

const [context] = args;
const { req, res } = context;

Expand Down
4 changes: 3 additions & 1 deletion packages/nextjs/src/common/wrapGetStaticPropsWithSentry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub } from '@sentry/core';
import { addTracingExtensions, getCurrentHub } from '@sentry/core';
import type { GetStaticProps } from 'next';

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

addTracingExtensions();

const errorWrappedGetStaticProps = withErrorInstrumentation(wrappingTarget);
const options = getCurrentHub().getClient()?.getOptions();

Expand Down
3 changes: 2 additions & 1 deletion packages/nextjs/src/common/wrapPageComponentWithSentry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { captureException, configureScope, runWithAsyncContext } from '@sentry/core';
import { addTracingExtensions, captureException, configureScope, runWithAsyncContext } from '@sentry/core';
import { addExceptionMechanism, extractTraceparentData } from '@sentry/utils';

interface FunctionComponent {
Expand All @@ -21,6 +21,7 @@ function isReactClassComponent(target: unknown): target is ClassComponent {
* Wraps a page component with Sentry error instrumentation.
*/
export function wrapPageComponentWithSentry(pageComponent: FunctionComponent | ClassComponent): unknown {
addTracingExtensions();
if (isReactClassComponent(pageComponent)) {
return class SentryWrappedPageComponent extends pageComponent {
public render(...args: unknown[]): unknown {
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { captureException, flush, getCurrentHub, runWithAsyncContext, trace } from '@sentry/core';
import { addTracingExtensions, captureException, flush, getCurrentHub, runWithAsyncContext, trace } from '@sentry/core';
import { addExceptionMechanism, tracingContextFromHeaders } from '@sentry/utils';

import { isRedirectNavigationError } from './nextNavigationErrorUtils';
Expand All @@ -13,8 +13,8 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
routeHandler: F,
context: RouteHandlerContext,
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>> {
addTracingExtensions();
const { method, parameterizedRoute, baggageHeader, sentryTraceHeader } = context;

return new Proxy(routeHandler, {
apply: (originalFunction, thisArg, args) => {
return runWithAsyncContext(async () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/nextjs/src/common/wrapServerComponentWithSentry.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,6 +19,7 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => 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
Expand Down