Skip to content
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

fix(angular): Run tracing calls outside of Angular #11748

Merged
merged 1 commit into from
May 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 52 additions & 42 deletions packages/angular/src/tracing.ts
Expand Up @@ -89,12 +89,14 @@ export class TraceService implements OnDestroy {
if (client) {
// see comment in `_isPageloadOngoing` for rationale
if (!this._isPageloadOngoing()) {
startBrowserTracingNavigationSpan(client, {
name: strippedUrl,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.angular',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
},
runOutsideAngular(() => {
startBrowserTracingNavigationSpan(client, {
name: strippedUrl,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.angular',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
},
});
});
} else {
// The first time we end up here, we set the pageload flag to false
Expand All @@ -104,18 +106,20 @@ export class TraceService implements OnDestroy {
}

this._routingSpan =
startInactiveSpan({
name: `${navigationEvent.url}`,
op: ANGULAR_ROUTING_OP,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
url: strippedUrl,
...(navigationEvent.navigationTrigger && {
navigationTrigger: navigationEvent.navigationTrigger,
}),
},
}) || null;
runOutsideAngular(() =>
startInactiveSpan({
name: `${navigationEvent.url}`,
op: ANGULAR_ROUTING_OP,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
url: strippedUrl,
...(navigationEvent.navigationTrigger && {
navigationTrigger: navigationEvent.navigationTrigger,
}),
},
}),
) || null;

return;
}
Expand Down Expand Up @@ -252,11 +256,13 @@ export class TraceDirective implements OnInit, AfterViewInit {
}

if (getActiveSpan()) {
this._tracingSpan = startInactiveSpan({
name: `<${this.componentName}>`,
op: ANGULAR_INIT_OP,
attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_directive' },
});
this._tracingSpan = runOutsideAngular(() =>
startInactiveSpan({
name: `<${this.componentName}>`,
op: ANGULAR_INIT_OP,
attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_directive' },
}),
);
}
}

Expand All @@ -266,7 +272,7 @@ export class TraceDirective implements OnInit, AfterViewInit {
*/
public ngAfterViewInit(): void {
if (this._tracingSpan) {
this._tracingSpan.end();
runOutsideAngular(() => this._tracingSpan!.end());
}
}
}
Expand Down Expand Up @@ -298,14 +304,16 @@ export function TraceClass(options?: TraceClassOptions): ClassDecorator {
const originalOnInit = target.prototype.ngOnInit;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
target.prototype.ngOnInit = function (...args: any[]): ReturnType<typeof originalOnInit> {
tracingSpan = startInactiveSpan({
onlyIfParent: true,
name: `<${options && options.name ? options.name : 'unnamed'}>`,
op: ANGULAR_INIT_OP,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_class_decorator',
},
});
tracingSpan = runOutsideAngular(() =>
startInactiveSpan({
onlyIfParent: true,
name: `<${options && options.name ? options.name : 'unnamed'}>`,
op: ANGULAR_INIT_OP,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_class_decorator',
},
}),
);

if (originalOnInit) {
return originalOnInit.apply(this, args);
Expand All @@ -316,7 +324,7 @@ export function TraceClass(options?: TraceClassOptions): ClassDecorator {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
target.prototype.ngAfterViewInit = function (...args: any[]): ReturnType<typeof originalAfterViewInit> {
if (tracingSpan) {
tracingSpan.end();
runOutsideAngular(() => tracingSpan.end());
}
if (originalAfterViewInit) {
return originalAfterViewInit.apply(this, args);
Expand Down Expand Up @@ -344,15 +352,17 @@ export function TraceMethod(options?: TraceMethodOptions): MethodDecorator {
descriptor.value = function (...args: any[]): ReturnType<typeof originalMethod> {
const now = timestampInSeconds();

startInactiveSpan({
onlyIfParent: true,
name: `<${options && options.name ? options.name : 'unnamed'}>`,
op: `${ANGULAR_OP}.${String(propertyKey)}`,
startTime: now,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_method_decorator',
},
}).end(now);
runOutsideAngular(() => {
startInactiveSpan({
onlyIfParent: true,
name: `<${options && options.name ? options.name : 'unnamed'}>`,
op: `${ANGULAR_OP}.${String(propertyKey)}`,
startTime: now,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_method_decorator',
},
}).end(now);
});

if (originalMethod) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
Expand Down