Skip to content

Commit 805e1af

Browse files
committed
for not only middleware on edge
1 parent 40f32cc commit 805e1af

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

packages/nextjs/src/edge/index.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { getScopesFromContext } from '@sentry/opentelemetry';
2323
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
2424
import { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge';
25+
import { ATTR_NEXT_SPAN_TYPE } from '../common/nextSpanAttributes';
2526
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../common/span-attributes-with-logic-attached';
2627
import { addHeadersAsAttributes } from '../common/utils/addHeadersAsAttributes';
2728
import { dropMiddlewareTunnelRequests } from '../common/utils/dropMiddlewareTunnelRequests';
@@ -90,19 +91,21 @@ export function init(options: VercelEdgeOptions = {}): void {
9091
if (spanAttributes?.['next.span_type'] === 'Middleware.execute') {
9192
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'http.server.middleware');
9293
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'url');
94+
}
9395

94-
if (isRootSpan) {
95-
// Fork isolation scope for middleware requests
96-
const scopes = getCapturedScopesOnSpan(span);
97-
const isolationScope = (scopes.isolationScope || getIsolationScope()).clone();
98-
const scope = scopes.scope || getCurrentScope();
99-
const currentScopesPointer = getScopesFromContext(context.active());
100-
if (currentScopesPointer) {
101-
currentScopesPointer.isolationScope = isolationScope;
102-
}
96+
// We want to fork the isolation scope for incoming requests
97+
if (spanAttributes?.[ATTR_NEXT_SPAN_TYPE] === 'BaseServer.handleRequest' && isRootSpan) {
98+
const scopes = getCapturedScopesOnSpan(span);
10399

104-
setCapturedScopesOnSpan(span, scope, isolationScope);
100+
const isolationScope = (scopes.isolationScope || getIsolationScope()).clone();
101+
const scope = scopes.scope || getCurrentScope();
102+
103+
const currentScopesPointer = getScopesFromContext(context.active());
104+
if (currentScopesPointer) {
105+
currentScopesPointer.isolationScope = isolationScope;
105106
}
107+
108+
setCapturedScopesOnSpan(span, scope, isolationScope);
106109
}
107110

108111
if (isRootSpan) {

0 commit comments

Comments
 (0)