From 4e98865a10f6f00c1f14fa0409bf3633747663d2 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 20 Dec 2024 13:13:08 +0100 Subject: [PATCH] fix next continuation --- .../wrapApiHandlerWithSentry.ts | 9 ++++++++- .../nextjs/src/common/withServerActionInstrumentation.ts | 8 ++++++-- packages/opentelemetry/src/trace.ts | 9 +-------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts b/packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts index 3cc5b4d340e5..5cfcf08e2168 100644 --- a/packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts +++ b/packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts @@ -3,6 +3,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, captureException, continueTrace, + getActiveSpan, httpRequestToRequestData, isString, logger, @@ -59,7 +60,13 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz req.__withSentry_applied__ = true; return withIsolationScope(isolationScope => { - return continueTrace( + // 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() + ? (_opts: unknown, callback: () => T) => callback() + : continueTrace; + + return continueTraceOrNot( { 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 8d5ab14c77c3..d5865a827613 100644 --- a/packages/nextjs/src/common/withServerActionInstrumentation.ts +++ b/packages/nextjs/src/common/withServerActionInstrumentation.ts @@ -1,4 +1,4 @@ -import type { RequestEventData } from '@sentry/core'; +import { RequestEventData, getActiveSpan } from '@sentry/core'; import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, SPAN_STATUS_ERROR, @@ -95,7 +95,11 @@ async function withServerActionInstrumentationImplementation(_opts: unknown, callback: () => T) => callback() : continueTrace; + + return continueTraceOrNot( { sentryTrace: sentryTraceHeader, baggage: baggageHeader, diff --git a/packages/opentelemetry/src/trace.ts b/packages/opentelemetry/src/trace.ts index e47445d6c9fa..dc1a2fd09c05 100644 --- a/packages/opentelemetry/src/trace.ts +++ b/packages/opentelemetry/src/trace.ts @@ -19,10 +19,8 @@ import { getRootSpan, getTraceContextFromScope, handleCallbackErrors, - propagationContextFromHeaders, spanToJSON, spanToTraceContext, - withScope, } from '@sentry/core'; import { continueTraceAsRemoteSpan } from './propagator'; import type { OpenTelemetryClient, OpenTelemetrySpanContext } from './types'; @@ -255,12 +253,7 @@ function getContextForScope(scope?: Scope): Context { * It propagates the trace as a remote span, in addition to setting it on the propagation context. */ export function continueTrace(options: Parameters[0], callback: () => T): T { - return withScope(scope => { - const { sentryTrace, baggage } = options; - const propagationContext = propagationContextFromHeaders(sentryTrace, baggage); - scope.setPropagationContext(propagationContext); - return continueTraceAsRemoteSpan(context.active(), options, callback); - }); + return continueTraceAsRemoteSpan(context.active(), options, callback); } /**