1
- import { EventProcessor , Hub , Integration , IntegrationClass , Span } from '@sentry/types' ;
1
+ import { EventProcessor , Hub , Integration , Scope , Span , Transaction } from '@sentry/types' ;
2
2
import { basename , getGlobalObject , logger , timestampWithMs } from '@sentry/utils' ;
3
3
4
- /**
5
- * Used to extract Tracing integration from the current client,
6
- * without the need to import `Tracing` itself from the @sentry/apm package.
7
- */
8
- const TRACING_GETTER = ( {
9
- id : 'Tracing' ,
10
- } as any ) as IntegrationClass < Integration > ;
11
-
12
4
/** Global Vue object limited to the methods/attributes we require */
13
5
interface VueInstance {
14
6
config : {
@@ -71,7 +63,7 @@ interface TracingOptions {
71
63
* Or to an array of specific component names (case-sensitive).
72
64
*/
73
65
trackComponents : boolean | string [ ] ;
74
- /** How long to wait until the tracked root activity is marked as finished and sent of to Sentry */
66
+ /** How long to wait until the tracked root span is marked as finished and sent of to Sentry */
75
67
timeout : number ;
76
68
/**
77
69
* List of hooks to keep track of during component lifecycle.
@@ -137,7 +129,6 @@ export class Vue implements Integration {
137
129
private readonly _componentsCache : { [ key : string ] : string } = { } ;
138
130
private _rootSpan ?: Span ;
139
131
private _rootSpanTimer ?: ReturnType < typeof setTimeout > ;
140
- private _tracingActivity ?: number ;
141
132
142
133
/**
143
134
* @inheritDoc
@@ -221,27 +212,18 @@ export class Vue implements Integration {
221
212
// On the first handler call (before), it'll be undefined, as `$once` will add it in the future.
222
213
// However, on the second call (after), it'll be already in place.
223
214
if ( this . _rootSpan ) {
224
- this . _finishRootSpan ( now , getCurrentHub ) ;
215
+ this . _finishRootSpan ( now ) ;
225
216
} else {
226
217
vm . $once ( `hook:${ hook } ` , ( ) => {
227
- // Create an activity on the first event call. There'll be no second call, as rootSpan will be in place,
218
+ // Create an span on the first event call. There'll be no second call, as rootSpan will be in place,
228
219
// thus new event handler won't be attached.
229
220
230
- // We do this whole dance with `TRACING_GETTER` to prevent `@sentry/apm` from becoming a peerDependency.
231
- // We also need to ask for the `.constructor`, as `pushActivity` and `popActivity` are static, not instance methods.
232
- const tracingIntegration = getCurrentHub ( ) . getIntegration ( TRACING_GETTER ) ;
233
- if ( tracingIntegration ) {
234
- // tslint:disable-next-line:no-unsafe-any
235
- this . _tracingActivity = ( tracingIntegration as any ) . constructor . pushActivity ( 'Vue Application Render' ) ;
236
- // tslint:disable-next-line:no-unsafe-any
237
- const transaction = ( tracingIntegration as any ) . constructor . getTransaction ( ) ;
238
- if ( transaction ) {
239
- // tslint:disable-next-line:no-unsafe-any
240
- this . _rootSpan = transaction . startChild ( {
241
- description : 'Application Render' ,
242
- op : 'Vue' ,
243
- } ) ;
244
- }
221
+ const activeTransaction = getActiveTransaction ( getCurrentHub ( ) ) ;
222
+ if ( activeTransaction ) {
223
+ this . _rootSpan = activeTransaction . startChild ( {
224
+ description : 'Application Render' ,
225
+ op : 'Vue' ,
226
+ } ) ;
245
227
}
246
228
} ) ;
247
229
}
@@ -264,7 +246,7 @@ export class Vue implements Integration {
264
246
// However, on the second call (after), it'll be already in place.
265
247
if ( span ) {
266
248
span . finish ( ) ;
267
- this . _finishRootSpan ( now , getCurrentHub ) ;
249
+ this . _finishRootSpan ( now ) ;
268
250
} else {
269
251
vm . $once ( `hook:${ hook } ` , ( ) => {
270
252
if ( this . _rootSpan ) {
@@ -305,24 +287,15 @@ export class Vue implements Integration {
305
287
} ) ;
306
288
} ;
307
289
308
- /** Finish top-level span and activity with a debounce configured using `timeout` option */
309
- private _finishRootSpan ( timestamp : number , getCurrentHub : ( ) => Hub ) : void {
290
+ /** Finish top-level span with a debounce configured using `timeout` option */
291
+ private _finishRootSpan ( timestamp : number ) : void {
310
292
if ( this . _rootSpanTimer ) {
311
293
clearTimeout ( this . _rootSpanTimer ) ;
312
294
}
313
295
314
296
this . _rootSpanTimer = setTimeout ( ( ) => {
315
- if ( this . _tracingActivity ) {
316
- // We do this whole dance with `TRACING_GETTER` to prevent `@sentry/apm` from becoming a peerDependency.
317
- // We also need to ask for the `.constructor`, as `pushActivity` and `popActivity` are static, not instance methods.
318
- const tracingIntegration = getCurrentHub ( ) . getIntegration ( TRACING_GETTER ) ;
319
- if ( tracingIntegration ) {
320
- // tslint:disable-next-line:no-unsafe-any
321
- ( tracingIntegration as any ) . constructor . popActivity ( this . _tracingActivity ) ;
322
- if ( this . _rootSpan ) {
323
- this . _rootSpan . finish ( timestamp ) ;
324
- }
325
- }
297
+ if ( this . _rootSpan ) {
298
+ this . _rootSpan . finish ( timestamp ) ;
326
299
}
327
300
} , this . _options . tracingOptions . timeout ) ;
328
301
}
@@ -333,7 +306,7 @@ export class Vue implements Integration {
333
306
334
307
this . _options . Vue . mixin ( {
335
308
beforeCreate ( this : ViewModel ) : void {
336
- if ( getCurrentHub ( ) . getIntegration ( TRACING_GETTER ) ) {
309
+ if ( getActiveTransaction ( getCurrentHub ( ) ) ) {
337
310
// `this` points to currently rendered component
338
311
applyTracingHooks ( this , getCurrentHub ) ;
339
312
} else {
@@ -405,3 +378,21 @@ export class Vue implements Integration {
405
378
}
406
379
}
407
380
}
381
+
382
+ // tslint:disable-next-line: completed-docs
383
+ interface HubType extends Hub {
384
+ // tslint:disable-next-line: completed-docs
385
+ getScope ?( ) : Scope | undefined ;
386
+ }
387
+
388
+ /** Grabs active transaction off scope */
389
+ export function getActiveTransaction < T extends Transaction > ( hub : HubType ) : T | undefined {
390
+ if ( hub && hub . getScope ) {
391
+ const scope = hub . getScope ( ) as Scope ;
392
+ if ( scope ) {
393
+ return scope . getTransaction ( ) as T | undefined ;
394
+ }
395
+ }
396
+
397
+ return undefined ;
398
+ }
0 commit comments