|
1 |
| -import { applySdkMetadata, registerSpanErrorInstrumentation } from '@sentry/core'; |
| 1 | +import { context } from '@opentelemetry/api'; |
| 2 | +import { |
| 3 | + applySdkMetadata, |
| 4 | + getCapturedScopesOnSpan, |
| 5 | + getCurrentScope, |
| 6 | + getIsolationScope, |
| 7 | + getRootSpan, |
| 8 | + registerSpanErrorInstrumentation, |
| 9 | + setCapturedScopesOnSpan, |
| 10 | +} from '@sentry/core'; |
| 11 | + |
2 | 12 | import { GLOBAL_OBJ } from '@sentry/utils';
|
3 | 13 | import type { VercelEdgeOptions } from '@sentry/vercel-edge';
|
4 | 14 | import { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge';
|
5 | 15 |
|
| 16 | +import { getScopesFromContext } from '@sentry/opentelemetry'; |
6 | 17 | import { isBuild } from '../common/utils/isBuild';
|
7 | 18 | import { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration';
|
8 | 19 |
|
@@ -39,7 +50,24 @@ export function init(options: VercelEdgeOptions = {}): void {
|
39 | 50 |
|
40 | 51 | applySdkMetadata(opts, 'nextjs');
|
41 | 52 |
|
42 |
| - vercelEdgeInit(opts); |
| 53 | + const client = vercelEdgeInit(opts); |
| 54 | + |
| 55 | + // Create/fork an isolation whenever we create root spans. This is ok because in Next.js we only create root spans on the edge for incoming requests. |
| 56 | + client?.on('spanStart', span => { |
| 57 | + if (span === getRootSpan(span)) { |
| 58 | + const scopes = getCapturedScopesOnSpan(span); |
| 59 | + |
| 60 | + const isolationScope = (scopes.isolationScope || getIsolationScope()).clone(); |
| 61 | + const scope = scopes.scope || getCurrentScope(); |
| 62 | + |
| 63 | + const currentScopesPointer = getScopesFromContext(context.active()); |
| 64 | + if (currentScopesPointer) { |
| 65 | + currentScopesPointer.isolationScope = isolationScope; |
| 66 | + } |
| 67 | + |
| 68 | + setCapturedScopesOnSpan(span, scope, isolationScope); |
| 69 | + } |
| 70 | + }); |
43 | 71 | }
|
44 | 72 |
|
45 | 73 | /**
|
|
0 commit comments