-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next): Handle existing root spans for isolation scope (#11479)
This updates handling of next.js instrumentation to re-use an isolation scope from a root span. This should ensure we have consistent isolation scopes, no matter if next.js auto creates spans or not.
- Loading branch information
Showing
11 changed files
with
250 additions
and
17 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
packages/nextjs/src/common/utils/withIsolationScopeOrReuseFromRootSpan.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
getActiveSpan, | ||
getCapturedScopesOnSpan, | ||
getDefaultIsolationScope, | ||
getRootSpan, | ||
spanToJSON, | ||
withIsolationScope, | ||
} from '@sentry/core'; | ||
import type { Scope } from '@sentry/types'; | ||
|
||
/** | ||
* Wrap a callback with a new isolation scope. | ||
* However, if we have an active root span that was generated by next, we want to reuse the isolation scope from that span. | ||
*/ | ||
export function withIsolationScopeOrReuseFromRootSpan<T>(cb: (isolationScope: Scope) => T): T { | ||
const activeSpan = getActiveSpan(); | ||
|
||
if (!activeSpan) { | ||
return withIsolationScope(cb); | ||
} | ||
|
||
const rootSpan = getRootSpan(activeSpan); | ||
|
||
// Verify this is a next span | ||
if (!spanToJSON(rootSpan).data?.['next.route']) { | ||
return withIsolationScope(cb); | ||
} | ||
|
||
const scopes = getCapturedScopesOnSpan(rootSpan); | ||
|
||
const isolationScope = scopes.isolationScope; | ||
|
||
// If this is the default isolation scope, we still want to fork one | ||
if (isolationScope === getDefaultIsolationScope()) { | ||
return withIsolationScope(cb); | ||
} | ||
|
||
return withIsolationScope(isolationScope, cb); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/nextjs/test/edge/withIsolationScopeOrReuseFromRootSpan.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
Scope, | ||
getCurrentScope, | ||
getGlobalScope, | ||
getIsolationScope, | ||
setCapturedScopesOnSpan, | ||
startSpan, | ||
} from '@sentry/core'; | ||
import { GLOBAL_OBJ } from '@sentry/utils'; | ||
import { init } from '@sentry/vercel-edge'; | ||
import { AsyncLocalStorage } from 'async_hooks'; | ||
|
||
import { withIsolationScopeOrReuseFromRootSpan } from '../../src/common/utils/withIsolationScopeOrReuseFromRootSpan'; | ||
|
||
describe('withIsolationScopeOrReuseFromRootSpan', () => { | ||
beforeEach(() => { | ||
getIsolationScope().clear(); | ||
getCurrentScope().clear(); | ||
getGlobalScope().clear(); | ||
(GLOBAL_OBJ as any).AsyncLocalStorage = AsyncLocalStorage; | ||
|
||
init({ | ||
enableTracing: true, | ||
}); | ||
}); | ||
|
||
it('works without any span', () => { | ||
const initialIsolationScope = getIsolationScope(); | ||
initialIsolationScope.setTag('aa', 'aa'); | ||
|
||
withIsolationScopeOrReuseFromRootSpan(isolationScope => { | ||
isolationScope.setTag('bb', 'bb'); | ||
expect(isolationScope).not.toBe(initialIsolationScope); | ||
expect(isolationScope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' }); | ||
}); | ||
}); | ||
|
||
it('works with a non-next.js span', () => { | ||
const initialIsolationScope = getIsolationScope(); | ||
initialIsolationScope.setTag('aa', 'aa'); | ||
|
||
const customScope = new Scope(); | ||
|
||
startSpan({ name: 'other' }, span => { | ||
setCapturedScopesOnSpan(span, getCurrentScope(), customScope); | ||
|
||
withIsolationScopeOrReuseFromRootSpan(isolationScope => { | ||
isolationScope.setTag('bb', 'bb'); | ||
expect(isolationScope).not.toBe(initialIsolationScope); | ||
expect(isolationScope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' }); | ||
}); | ||
}); | ||
}); | ||
|
||
it('works with a next.js span', () => { | ||
const initialIsolationScope = getIsolationScope(); | ||
initialIsolationScope.setTag('aa', 'aa'); | ||
|
||
const customScope = new Scope(); | ||
|
||
startSpan( | ||
{ | ||
name: 'other', | ||
attributes: { 'next.route': 'aha' }, | ||
}, | ||
span => { | ||
setCapturedScopesOnSpan(span, getCurrentScope(), customScope); | ||
|
||
withIsolationScopeOrReuseFromRootSpan(isolationScope => { | ||
isolationScope.setTag('bb', 'bb'); | ||
expect(isolationScope).toBe(customScope); | ||
expect(isolationScope.getScopeData().tags).toEqual({ bb: 'bb' }); | ||
}); | ||
}, | ||
); | ||
}); | ||
|
||
it('works with a next.js span that has default isolation scope', () => { | ||
const initialIsolationScope = getIsolationScope(); | ||
initialIsolationScope.setTag('aa', 'aa'); | ||
|
||
startSpan( | ||
{ | ||
name: 'other', | ||
attributes: { 'next.route': 'aha' }, | ||
}, | ||
() => { | ||
withIsolationScopeOrReuseFromRootSpan(isolationScope => { | ||
isolationScope.setTag('bb', 'bb'); | ||
expect(isolationScope).not.toBe(initialIsolationScope); | ||
expect(isolationScope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' }); | ||
}); | ||
}, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.