- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.7k
          feat(sveltekit): Add custom browserTracingIntegration()
          #10450
        
          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
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    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
        
          
          
            150 changes: 149 additions & 1 deletion
          
          150 
        
  packages/sveltekit/src/client/browserTracingIntegration.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 | 
|---|---|---|
| @@ -1,14 +1,162 @@ | ||
| import { BrowserTracing as OriginalBrowserTracing } from '@sentry/svelte'; | ||
| import { navigating, page } from '$app/stores'; | ||
| import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core'; | ||
| import { | ||
| BrowserTracing as OriginalBrowserTracing, | ||
| WINDOW, | ||
| browserTracingIntegration as originalBrowserTracingIntegration, | ||
| getActiveSpan, | ||
| startBrowserTracingNavigationSpan, | ||
| startBrowserTracingPageLoadSpan, | ||
| startInactiveSpan, | ||
| } from '@sentry/svelte'; | ||
| import type { Client, Integration, Span } from '@sentry/types'; | ||
| import { svelteKitRoutingInstrumentation } from './router'; | ||
|  | ||
| /** | ||
| * A custom BrowserTracing integration for Sveltekit. | ||
| * | ||
| * @deprecated use `browserTracingIntegration()` instead. The new `browserTracingIntegration()` | ||
| * includes SvelteKit-specific routing instrumentation out of the box. Therefore there's no need | ||
| * to pass in `svelteKitRoutingInstrumentation` anymore. | ||
| */ | ||
| export class BrowserTracing extends OriginalBrowserTracing { | ||
| public constructor(options?: ConstructorParameters<typeof OriginalBrowserTracing>[0]) { | ||
| super({ | ||
| // eslint-disable-next-line deprecation/deprecation | ||
| routingInstrumentation: svelteKitRoutingInstrumentation, | ||
| ...options, | ||
| }); | ||
| } | ||
| } | ||
|  | ||
| /** | ||
| * A custom `BrowserTracing` integration for SvelteKit. | ||
| */ | ||
| export function browserTracingIntegration( | ||
| options: Parameters<typeof originalBrowserTracingIntegration>[0] = {}, | ||
| ): Integration { | ||
| const integration = { | ||
| ...originalBrowserTracingIntegration({ | ||
| ...options, | ||
| instrumentNavigation: false, | ||
| instrumentPageLoad: false, | ||
| }), | ||
| }; | ||
|  | ||
| return { | ||
| ...integration, | ||
| afterAllSetup: client => { | ||
| integration.afterAllSetup(client); | ||
|  | ||
| if (options.instrumentPageLoad !== false) { | ||
| _instrumentPageload(client); | ||
| } | ||
|  | ||
| if (options.instrumentNavigation !== false) { | ||
| _instrumentNavigations(client); | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
|  | ||
| function _instrumentPageload(client: Client): void { | ||
| const initialPath = WINDOW && WINDOW.location && WINDOW.location.pathname; | ||
|  | ||
| startBrowserTracingPageLoadSpan(client, { | ||
| name: initialPath, | ||
| op: 'pageload', | ||
| description: initialPath, | ||
| tags: { | ||
| 'routing.instrumentation': '@sentry/sveltekit', | ||
| }, | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.sveltekit', | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url', | ||
| }, | ||
| }); | ||
|  | ||
| const pageloadSpan = getActiveSpan(); | ||
|  | ||
| page.subscribe(page => { | ||
| if (!page) { | ||
| return; | ||
| } | ||
|  | ||
| const routeId = page.route && page.route.id; | ||
|  | ||
| if (pageloadSpan && routeId) { | ||
| pageloadSpan.updateName(routeId); | ||
| pageloadSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route'); | ||
| } | ||
| }); | ||
| } | ||
|  | ||
| /** | ||
| * Use the `navigating` store to start a transaction on navigations. | ||
| */ | ||
| function _instrumentNavigations(client: Client): void { | ||
| let routingSpan: Span | undefined; | ||
| let activeSpan: Span | undefined; | ||
|  | ||
| navigating.subscribe(navigation => { | ||
| if (!navigation) { | ||
| // `navigating` emits a 'null' value when the navigation is completed. | ||
| // So in this case, we can finish the routing span. If the transaction was an IdleTransaction, | ||
| // it will finish automatically and if it was user-created users also need to finish it. | ||
| if (routingSpan) { | ||
| routingSpan.end(); | ||
| routingSpan = undefined; | ||
| } | ||
| return; | ||
| } | ||
|  | ||
| const from = navigation.from; | ||
| const to = navigation.to; | ||
|  | ||
| // for the origin we can fall back to window.location.pathname because in this emission, it still is set to the origin path | ||
| const rawRouteOrigin = (from && from.url.pathname) || (WINDOW && WINDOW.location && WINDOW.location.pathname); | ||
|  | ||
| const rawRouteDestination = to && to.url.pathname; | ||
|  | ||
| // We don't want to create transactions for navigations of same origin and destination. | ||
| // We need to look at the raw URL here because parameterized routes can still differ in their raw parameters. | ||
| if (rawRouteOrigin === rawRouteDestination) { | ||
| return; | ||
| } | ||
|  | ||
| const parameterizedRouteOrigin = from && from.route.id; | ||
| const parameterizedRouteDestination = to && to.route.id; | ||
|  | ||
| activeSpan = getActiveSpan(); | ||
|  | ||
| if (!activeSpan) { | ||
| startBrowserTracingNavigationSpan(client, { | ||
| name: parameterizedRouteDestination || rawRouteDestination || 'unknown', | ||
| op: 'navigation', | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.sveltekit', | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: parameterizedRouteDestination ? 'route' : 'url', | ||
| }, | ||
| tags: { | ||
| 'routing.instrumentation': '@sentry/sveltekit', | ||
| }, | ||
| }); | ||
| activeSpan = getActiveSpan(); | ||
| } | ||
|  | ||
| if (activeSpan) { | ||
| if (routingSpan) { | ||
| // If a routing span is still open from a previous navigation, we finish it. | ||
| routingSpan.end(); | ||
| } | ||
| routingSpan = startInactiveSpan({ | ||
| op: 'ui.sveltekit.routing', | ||
| name: 'SvelteKit Route Change', | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.sveltekit', | ||
| }, | ||
| }); | ||
| activeSpan.setAttribute('sentry.sveltekit.navigation.from', parameterizedRouteOrigin || undefined); | ||
| } | ||
| }); | ||
| } | ||
  
    
      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
    
  
  
    
              
      
      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.
nice ❤️ pretty clean!