diff --git a/packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts b/packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts index 5cfcf08e2168..856e80ee72f8 100644 --- a/packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts +++ b/packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts @@ -62,11 +62,11 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz return withIsolationScope(isolationScope => { // Normally, there is an active span here (from Next.js OTEL) and we just use that as parent // Else, we manually continueTrace from the incoming headers - const continueTraceOrNot = getActiveSpan() + const continueTraceIfNoActiveSpan = getActiveSpan() ? (_opts: unknown, callback: () => T) => callback() : continueTrace; - return continueTraceOrNot( + return continueTraceIfNoActiveSpan( { sentryTrace: req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined, diff --git a/packages/nextjs/src/common/withServerActionInstrumentation.ts b/packages/nextjs/src/common/withServerActionInstrumentation.ts index d5865a827613..8b7cc32144f3 100644 --- a/packages/nextjs/src/common/withServerActionInstrumentation.ts +++ b/packages/nextjs/src/common/withServerActionInstrumentation.ts @@ -97,9 +97,11 @@ async function withServerActionInstrumentationImplementation(_opts: unknown, callback: () => T) => callback() : continueTrace; + const continueTraceIfNoActiveSpan = getActiveSpan() + ? (_opts: unknown, callback: () => T) => callback() + : continueTrace; - return continueTraceOrNot( + return continueTraceIfNoActiveSpan( { sentryTrace: sentryTraceHeader, baggage: baggageHeader,