Skip to content

Commit d9a67b4

Browse files
committed
fix(angular): run tracing stuff outside Angular
This commit updates all tracing functionality to run outside the Angular zone. Before this change, it hindered server-side rendering and hydration, causing instability in the app. The app achieves stability when there are no micro/macro tasks running. As a result, the HTML can now be serialized and sent to the client.
1 parent 7eb000c commit d9a67b4

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

packages/angular/src/tracing.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ export class TraceService implements OnDestroy {
8989
if (client) {
9090
// see comment in `_isPageloadOngoing` for rationale
9191
if (!this._isPageloadOngoing()) {
92-
startBrowserTracingNavigationSpan(client, {
93-
name: strippedUrl,
94-
attributes: {
95-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.angular',
96-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
97-
},
92+
runOutsideAngular(() => {
93+
startBrowserTracingNavigationSpan(client, {
94+
name: strippedUrl,
95+
attributes: {
96+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.angular',
97+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
98+
},
99+
});
98100
});
99101
} else {
100102
// The first time we end up here, we set the pageload flag to false
@@ -104,7 +106,7 @@ export class TraceService implements OnDestroy {
104106
}
105107

106108
this._routingSpan =
107-
startInactiveSpan({
109+
runOutsideAngular(() => startInactiveSpan({
108110
name: `${navigationEvent.url}`,
109111
op: ANGULAR_ROUTING_OP,
110112
attributes: {
@@ -115,7 +117,7 @@ export class TraceService implements OnDestroy {
115117
navigationTrigger: navigationEvent.navigationTrigger,
116118
}),
117119
},
118-
}) || null;
120+
})) || null;
119121

120122
return;
121123
}
@@ -252,11 +254,11 @@ export class TraceDirective implements OnInit, AfterViewInit {
252254
}
253255

254256
if (getActiveSpan()) {
255-
this._tracingSpan = startInactiveSpan({
257+
this._tracingSpan = runOutsideAngular(() => startInactiveSpan({
256258
name: `<${this.componentName}>`,
257259
op: ANGULAR_INIT_OP,
258260
attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_directive' },
259-
});
261+
}));
260262
}
261263
}
262264

@@ -266,7 +268,7 @@ export class TraceDirective implements OnInit, AfterViewInit {
266268
*/
267269
public ngAfterViewInit(): void {
268270
if (this._tracingSpan) {
269-
this._tracingSpan.end();
271+
runOutsideAngular(() => this._tracingSpan!.end());
270272
}
271273
}
272274
}
@@ -298,14 +300,14 @@ export function TraceClass(options?: TraceClassOptions): ClassDecorator {
298300
const originalOnInit = target.prototype.ngOnInit;
299301
// eslint-disable-next-line @typescript-eslint/no-explicit-any
300302
target.prototype.ngOnInit = function (...args: any[]): ReturnType<typeof originalOnInit> {
301-
tracingSpan = startInactiveSpan({
303+
tracingSpan = runOutsideAngular(() => startInactiveSpan({
302304
onlyIfParent: true,
303305
name: `<${options && options.name ? options.name : 'unnamed'}>`,
304306
op: ANGULAR_INIT_OP,
305307
attributes: {
306308
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_class_decorator',
307309
},
308-
});
310+
}));
309311

310312
if (originalOnInit) {
311313
return originalOnInit.apply(this, args);
@@ -316,7 +318,7 @@ export function TraceClass(options?: TraceClassOptions): ClassDecorator {
316318
// eslint-disable-next-line @typescript-eslint/no-explicit-any
317319
target.prototype.ngAfterViewInit = function (...args: any[]): ReturnType<typeof originalAfterViewInit> {
318320
if (tracingSpan) {
319-
tracingSpan.end();
321+
runOutsideAngular(() => tracingSpan.end());
320322
}
321323
if (originalAfterViewInit) {
322324
return originalAfterViewInit.apply(this, args);
@@ -344,15 +346,17 @@ export function TraceMethod(options?: TraceMethodOptions): MethodDecorator {
344346
descriptor.value = function (...args: any[]): ReturnType<typeof originalMethod> {
345347
const now = timestampInSeconds();
346348

347-
startInactiveSpan({
348-
onlyIfParent: true,
349-
name: `<${options && options.name ? options.name : 'unnamed'}>`,
350-
op: `${ANGULAR_OP}.${String(propertyKey)}`,
351-
startTime: now,
352-
attributes: {
353-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_method_decorator',
354-
},
355-
}).end(now);
349+
runOutsideAngular(() => {
350+
startInactiveSpan({
351+
onlyIfParent: true,
352+
name: `<${options && options.name ? options.name : 'unnamed'}>`,
353+
op: `${ANGULAR_OP}.${String(propertyKey)}`,
354+
startTime: now,
355+
attributes: {
356+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.angular.trace_method_decorator',
357+
},
358+
}).end(now);
359+
});
356360

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

0 commit comments

Comments
 (0)