Skip to content

Commit

Permalink
fix next continuation
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Dec 20, 2024
1 parent d4d52ca commit 4e98865
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
captureException,
continueTrace,
getActiveSpan,
httpRequestToRequestData,
isString,
logger,
Expand Down Expand Up @@ -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()
? <T>(_opts: unknown, callback: () => T) => callback()
: continueTrace;

return continueTraceOrNot(
{
sentryTrace:
req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined,
Expand Down
8 changes: 6 additions & 2 deletions packages/nextjs/src/common/withServerActionInstrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RequestEventData } from '@sentry/core';
import { RequestEventData, getActiveSpan } from '@sentry/core';
import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
SPAN_STATUS_ERROR,
Expand Down Expand Up @@ -95,7 +95,11 @@ async function withServerActionInstrumentationImplementation<A extends (...args:
} satisfies RequestEventData,
});

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() ? <T>(_opts: unknown, callback: () => T) => callback() : continueTrace;

return continueTraceOrNot(
{
sentryTrace: sentryTraceHeader,
baggage: baggageHeader,
Expand Down
9 changes: 1 addition & 8 deletions packages/opentelemetry/src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<T>(options: Parameters<typeof baseContinueTrace>[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);
}

/**
Expand Down

0 comments on commit 4e98865

Please sign in to comment.