Skip to content

Commit 8dd5213

Browse files
committed
fix next continuation
1 parent 8add20a commit 8dd5213

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
44
captureException,
55
continueTrace,
6+
getActiveSpan,
67
httpRequestToRequestData,
78
isString,
89
logger,
@@ -59,7 +60,13 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
5960
req.__withSentry_applied__ = true;
6061

6162
return withIsolationScope(isolationScope => {
62-
return continueTrace(
63+
// Normally, there is an active span here (from Next.js OTEL) and we just use that as parent
64+
// Else, we manually continueTrace from the incoming headers
65+
const continueTraceOrNot = getActiveSpan()
66+
? <T>(_opts: unknown, callback: () => T) => callback()
67+
: continueTrace;
68+
69+
return continueTraceOrNot(
6370
{
6471
sentryTrace:
6572
req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined,

packages/nextjs/src/common/withServerActionInstrumentation.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { RequestEventData } from '@sentry/core';
1+
import { RequestEventData, getActiveSpan } from '@sentry/core';
22
import {
33
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
44
SPAN_STATUS_ERROR,
@@ -95,7 +95,11 @@ async function withServerActionInstrumentationImplementation<A extends (...args:
9595
} satisfies RequestEventData,
9696
});
9797

98-
return continueTrace(
98+
// Normally, there is an active span here (from Next.js OTEL) and we just use that as parent
99+
// Else, we manually continueTrace from the incoming headers
100+
const continueTraceOrNot = getActiveSpan() ? <T>(_opts: unknown, callback: () => T) => callback() : continueTrace;
101+
102+
return continueTraceOrNot(
99103
{
100104
sentryTrace: sentryTraceHeader,
101105
baggage: baggageHeader,

packages/opentelemetry/src/trace.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ import {
1919
getRootSpan,
2020
getTraceContextFromScope,
2121
handleCallbackErrors,
22-
propagationContextFromHeaders,
2322
spanToJSON,
2423
spanToTraceContext,
25-
withScope,
2624
} from '@sentry/core';
2725
import { continueTraceAsRemoteSpan } from './propagator';
2826
import type { OpenTelemetryClient, OpenTelemetrySpanContext } from './types';
@@ -255,12 +253,7 @@ function getContextForScope(scope?: Scope): Context {
255253
* It propagates the trace as a remote span, in addition to setting it on the propagation context.
256254
*/
257255
export function continueTrace<T>(options: Parameters<typeof baseContinueTrace>[0], callback: () => T): T {
258-
return withScope(scope => {
259-
const { sentryTrace, baggage } = options;
260-
const propagationContext = propagationContextFromHeaders(sentryTrace, baggage);
261-
scope.setPropagationContext(propagationContext);
262-
return continueTraceAsRemoteSpan(context.active(), options, callback);
263-
});
256+
return continueTraceAsRemoteSpan(context.active(), options, callback);
264257
}
265258

266259
/**

0 commit comments

Comments
 (0)