forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracing.ts
More file actions
235 lines (208 loc) · 7.57 KB
/
tracing.ts
File metadata and controls
235 lines (208 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { AfterViewInit, Directive, Injectable, Input, NgModule, OnDestroy, OnInit } from '@angular/core';
import { Event, NavigationEnd, NavigationStart, Router } from '@angular/router';
import { getCurrentHub } from '@sentry/browser';
import { Span, Transaction, TransactionContext } from '@sentry/types';
import { getGlobalObject, logger, stripUrlQueryAndFragment, timestampWithMs } from '@sentry/utils';
import { Observable, Subscription } from 'rxjs';
import { filter, tap } from 'rxjs/operators';
import { runOutsideAngular } from './zone';
let instrumentationInitialized: boolean;
let stashedStartTransaction: (context: TransactionContext) => Transaction | undefined;
let stashedStartTransactionOnLocationChange: boolean;
const global = getGlobalObject<Window>();
/**
* Creates routing instrumentation for Angular Router.
*/
export function routingInstrumentation(
customStartTransaction: (context: TransactionContext) => Transaction | undefined,
startTransactionOnPageLoad: boolean = true,
startTransactionOnLocationChange: boolean = true,
): void {
instrumentationInitialized = true;
stashedStartTransaction = customStartTransaction;
stashedStartTransactionOnLocationChange = startTransactionOnLocationChange;
if (startTransactionOnPageLoad) {
customStartTransaction({
name: global.location.pathname,
op: 'pageload',
});
}
}
export const instrumentAngularRouting = routingInstrumentation;
/**
* Grabs active transaction off scope
*/
export function getActiveTransaction(): Transaction | undefined {
const currentHub = getCurrentHub();
if (currentHub) {
const scope = currentHub.getScope();
if (scope) {
return scope.getTransaction();
}
}
return undefined;
}
/**
* Angular's Service responsible for hooking into Angular Router and tracking current navigation process.
* Creates a new transaction for every route change and measures a duration of routing process.
*/
@Injectable({ providedIn: 'root' })
export class TraceService implements OnDestroy {
public navStart$: Observable<Event> = this._router.events.pipe(
filter(event => event instanceof NavigationStart),
tap(event => {
if (!instrumentationInitialized) {
logger.error('Angular integration has tracing enabled, but Tracing integration is not configured');
return;
}
const navigationEvent = event as NavigationStart;
const strippedUrl = stripUrlQueryAndFragment(navigationEvent.url);
let activeTransaction = getActiveTransaction();
if (!activeTransaction && stashedStartTransactionOnLocationChange) {
activeTransaction = stashedStartTransaction({
name: strippedUrl,
op: 'navigation',
});
}
if (activeTransaction) {
this._routingSpan = activeTransaction.startChild({
description: `${navigationEvent.url}`,
op: `angular.routing`,
tags: {
'routing.instrumentation': '@sentry/angular',
url: strippedUrl,
...(navigationEvent.navigationTrigger && {
navigationTrigger: navigationEvent.navigationTrigger,
}),
},
});
}
}),
);
public navEnd$: Observable<Event> = this._router.events.pipe(
filter(event => event instanceof NavigationEnd),
tap(() => {
if (this._routingSpan) {
runOutsideAngular(() => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this._routingSpan!.finish();
});
this._routingSpan = null;
}
}),
);
private _routingSpan: Span | null = null;
private _subscription: Subscription = new Subscription();
public constructor(private readonly _router: Router) {
this._subscription.add(this.navStart$.subscribe());
this._subscription.add(this.navEnd$.subscribe());
}
/**
* This is used to prevent memory leaks when the root view is created and destroyed multiple times,
* since `subscribe` callbacks capture `this` and prevent many resources from being GC'd.
*/
public ngOnDestroy(): void {
this._subscription.unsubscribe();
}
}
const UNKNOWN_COMPONENT = 'unknown';
/**
* A directive that can be used to capture initialization lifecycle of the whole component.
*/
@Directive({ selector: '[trace]' })
export class TraceDirective implements OnInit, AfterViewInit {
@Input('trace') public componentName: string = UNKNOWN_COMPONENT;
private _tracingSpan?: Span;
/**
* Implementation of OnInit lifecycle method
* @inheritdoc
*/
public ngOnInit(): void {
const activeTransaction = getActiveTransaction();
if (activeTransaction) {
this._tracingSpan = activeTransaction.startChild({
description: `<${this.componentName}>`,
op: `angular.initialize`,
});
}
}
/**
* Implementation of AfterViewInit lifecycle method
* @inheritdoc
*/
public ngAfterViewInit(): void {
if (this._tracingSpan) {
this._tracingSpan.finish();
}
}
}
/**
* A module serves as a single compilation unit for the `TraceDirective` and can be re-used by any other module.
*/
@NgModule({
declarations: [TraceDirective],
exports: [TraceDirective],
})
export class TraceModule {}
/**
* Decorator function that can be used to capture initialization lifecycle of the whole component.
*/
export function TraceClassDecorator(): ClassDecorator {
let tracingSpan: Span;
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
return target => {
const originalOnInit = target.prototype.ngOnInit;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
target.prototype.ngOnInit = function(...args: any[]): ReturnType<typeof originalOnInit> {
const activeTransaction = getActiveTransaction();
if (activeTransaction) {
tracingSpan = activeTransaction.startChild({
description: `<${target.name}>`,
op: `angular.initialize`,
});
}
if (originalOnInit) {
return originalOnInit.apply(this, args);
}
};
const originalAfterViewInit = target.prototype.ngAfterViewInit;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
target.prototype.ngAfterViewInit = function(...args: any[]): ReturnType<typeof originalAfterViewInit> {
if (tracingSpan) {
tracingSpan.finish();
}
if (originalAfterViewInit) {
return originalAfterViewInit.apply(this, args);
}
};
};
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
}
/**
* Decorator function that can be used to capture a single lifecycle methods of the component.
*/
export function TraceMethodDecorator(): MethodDecorator {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/ban-types
return (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
const originalMethod = descriptor.value;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
descriptor.value = function(...args: any[]): ReturnType<typeof originalMethod> {
const now = timestampWithMs();
const activeTransaction = getActiveTransaction();
if (activeTransaction) {
activeTransaction.startChild({
description: `<${target.constructor.name}>`,
endTimestamp: now,
op: `angular.${String(propertyKey)}`,
startTimestamp: now,
});
}
if (originalMethod) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return originalMethod.apply(this, args);
}
};
return descriptor;
};
}