- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.7k
 
feat(next): Handle existing root spans for isolation scope #11479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            39 changes: 39 additions & 0 deletions
          
          39 
        
  packages/nextjs/src/common/utils/withIsolationScopeOrReuseFromRootSpan.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this feels like a reasonable starting point here I guess? We should sync this with the (not yet existing) isolation scope integration!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also use
next.span_namehere as this attribute is added to (I think) almost all spans. Look here