Skip to content

Commit 3cf4bc4

Browse files
committed
ensure we have scopes on root span?!
1 parent 62de860 commit 3cf4bc4

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { spanToJSON } from '@sentry/core';
1+
import { getCurrentScope, spanToJSON } from '@sentry/core';
22
import {
33
SEMANTIC_ATTRIBUTE_SENTRY_OP,
44
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
@@ -13,7 +13,7 @@ import {
1313
startSpan,
1414
withIsolationScope,
1515
} from '@sentry/core';
16-
import { getSpanScopes } from '@sentry/opentelemetry';
16+
import { getSpanScopes, setSpanScopes } from '@sentry/opentelemetry';
1717
import type { Span } from '@sentry/types';
1818
import { winterCGHeadersToDict } from '@sentry/utils';
1919
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
@@ -84,8 +84,15 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
8484
return await startOrUpdateSpan(`${method} ${parameterizedRoute}`, async (rootSpan: Span) => {
8585
// Ensure root span has the correct isolation scope - even if it was triggered by next
8686
const scopes = getSpanScopes(rootSpan);
87+
rootSpan.setAttribute('has_span_scopes', !!scopes);
88+
console.log('getting scopes', scopes);
8789
if (scopes) {
8890
scopes.isolationScope = isolationScope;
91+
} else {
92+
setSpanScopes(rootSpan, {
93+
scope: getCurrentScope(),
94+
isolationScope,
95+
});
8996
}
9097

9198
const response: Response = await handleCallbackErrors(

packages/opentelemetry/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type { OpenTelemetryClient } from './types';
44
export { wrapClientClass } from './custom/client';
55

66
export { getSpanKind } from './utils/getSpanKind';
7-
export { getSpanScopes } from './utils/spanData';
7+
export { getSpanScopes, setSpanScopes } from './utils/spanData';
88

99
export { getScopesFromContext } from './utils/contextData';
1010

0 commit comments

Comments
 (0)