-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(tracing): Implement new browserTracingIntegration()
#10327
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,14 @@ import type { ActivatedRouteSnapshot, Event, RouterState } from '@angular/router | |
// eslint-disable-next-line @typescript-eslint/consistent-type-imports | ||
import { NavigationCancel, NavigationError, Router } from '@angular/router'; | ||
import { NavigationEnd, NavigationStart, ResolveEnd } from '@angular/router'; | ||
import { WINDOW, getCurrentScope } from '@sentry/browser'; | ||
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, spanToJSON } from '@sentry/core'; | ||
import type { Span, Transaction, TransactionContext } from '@sentry/types'; | ||
import { | ||
WINDOW, | ||
browserTracingIntegration as originalBrowserTracingIntegration, | ||
browserTracingStartNavigationSpan, | ||
getCurrentScope, | ||
} from '@sentry/browser'; | ||
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, getActiveSpan, spanToJSON, startInactiveSpan } from '@sentry/core'; | ||
import type { Integration, Span, Transaction, TransactionContext } from '@sentry/types'; | ||
import { logger, stripUrlQueryAndFragment, timestampInSeconds } from '@sentry/utils'; | ||
import type { Observable } from 'rxjs'; | ||
import { Subscription } from 'rxjs'; | ||
|
@@ -23,6 +28,8 @@ let instrumentationInitialized: boolean; | |
let stashedStartTransaction: (context: TransactionContext) => Transaction | undefined; | ||
let stashedStartTransactionOnLocationChange: boolean; | ||
|
||
let hooksBasedInstrumentation = false; | ||
|
||
/** | ||
* Creates routing instrumentation for Angular Router. | ||
*/ | ||
|
@@ -49,6 +56,23 @@ export function routingInstrumentation( | |
|
||
export const instrumentAngularRouting = routingInstrumentation; | ||
|
||
/** | ||
* A custom BrowserTracing integration for Angular. | ||
*/ | ||
export function browserTracingIntegration( | ||
options?: Parameters<typeof originalBrowserTracingIntegration>[0], | ||
): Integration { | ||
instrumentationInitialized = true; | ||
hooksBasedInstrumentation = true; | ||
|
||
return originalBrowserTracingIntegration({ | ||
...options, | ||
instrumentPageLoad: true, | ||
// We handle this manually | ||
instrumentNavigation: false, | ||
}); | ||
} | ||
Comment on lines
+70
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess one downside of this approach I just thought of is that there will be no navigation span/txn at all, if users don't register the I think this is okay though - "incorrect" or incomplete SDK setup shouldn't be something we care too much about IMO. (Also, this is documented pretty clearly in docs) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I think I will refactor this based on what we talked about that we actually never overwrite the defaults like this but only do it via checking if something else registered a hook instead! |
||
|
||
/** | ||
* Grabs active transaction off scope. | ||
* | ||
|
@@ -74,7 +98,43 @@ export class TraceService implements OnDestroy { | |
return; | ||
} | ||
|
||
if (this._routingSpan) { | ||
this._routingSpan.end(); | ||
this._routingSpan = null; | ||
} | ||
|
||
const strippedUrl = stripUrlQueryAndFragment(navigationEvent.url); | ||
|
||
if (hooksBasedInstrumentation) { | ||
if (!getActiveSpan()) { | ||
browserTracingStartNavigationSpan({ | ||
name: strippedUrl, | ||
op: 'navigation', | ||
origin: 'auto.navigation.angular', | ||
attributes: { | ||
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url', | ||
}, | ||
}); | ||
} | ||
|
||
// eslint-disable-next-line deprecation/deprecation | ||
this._routingSpan = | ||
startInactiveSpan({ | ||
name: `${navigationEvent.url}`, | ||
op: ANGULAR_ROUTING_OP, | ||
origin: 'auto.ui.angular', | ||
tags: { | ||
'routing.instrumentation': '@sentry/angular', | ||
url: strippedUrl, | ||
...(navigationEvent.navigationTrigger && { | ||
navigationTrigger: navigationEvent.navigationTrigger, | ||
}), | ||
}, | ||
}) || null; | ||
|
||
return; | ||
} | ||
|
||
// eslint-disable-next-line deprecation/deprecation | ||
let activeTransaction = getActiveTransaction(); | ||
|
||
|
@@ -90,9 +150,6 @@ export class TraceService implements OnDestroy { | |
} | ||
|
||
if (activeTransaction) { | ||
if (this._routingSpan) { | ||
this._routingSpan.end(); | ||
} | ||
// eslint-disable-next-line deprecation/deprecation | ||
this._routingSpan = activeTransaction.startChild({ | ||
description: `${navigationEvent.url}`, | ||
|
Uh oh!
There was an error while loading. Please reload this page.