1
1
import { Hub , Scope } from '@sentry/hub' ;
2
- import { Event , EventProcessor , Integration , Span , SpanContext } from '@sentry/types' ;
2
+ import { Event , EventProcessor , Integration , Severity , Span , SpanContext } from '@sentry/types' ;
3
3
import {
4
4
addInstrumentationHandler ,
5
5
getGlobalObject ,
6
6
isMatchingPattern ,
7
7
logger ,
8
+ safeJoin ,
8
9
supportsNativeFetch ,
9
10
} from '@sentry/utils' ;
10
11
@@ -77,6 +78,27 @@ interface TracingOptions {
77
78
* Default: true
78
79
*/
79
80
discardBackgroundSpans : boolean ;
81
+
82
+ /**
83
+ * This is only if you want to debug in prod.
84
+ * writeAsBreadcrumbs: Instead of having console.log statements we log messages to breadcrumbs
85
+ * so you can investigate whats happening in production with your users to figure why things might not appear the
86
+ * way you expect them to.
87
+ *
88
+ * spanDebugTimingInfo: Add timing info to spans at the point where we create them to figure out browser timing
89
+ * issues.
90
+ *
91
+ * You shouldn't care about this.
92
+ *
93
+ * Default: {
94
+ * writeAsBreadcrumbs: false;
95
+ * spanDebugTimingInfo: false;
96
+ * }
97
+ */
98
+ debug : {
99
+ writeAsBreadcrumbs : boolean ;
100
+ spanDebugTimingInfo : boolean ;
101
+ } ;
80
102
}
81
103
82
104
/** JSDoc */
@@ -138,6 +160,10 @@ export class Tracing implements Integration {
138
160
global . performance . mark ( 'sentry-tracing-init' ) ;
139
161
}
140
162
const defaults = {
163
+ debug : {
164
+ spanDebugTimingInfo : false ,
165
+ writeAsBreadcrumbs : false ,
166
+ } ,
141
167
discardBackgroundSpans : true ,
142
168
idleTimeout : 500 ,
143
169
maxTransactionDuration : 600 ,
@@ -210,8 +236,17 @@ export class Tracing implements Integration {
210
236
event . timestamp - event . start_timestamp < 0 ) ;
211
237
212
238
if ( Tracing . options . maxTransactionDuration !== 0 && event . type === 'transaction' && isOutdatedTransaction ) {
213
- logger . log ( '[Tracing] Discarded transaction since it maxed out maxTransactionDuration' ) ;
214
- return null ;
239
+ Tracing . _log ( '[Tracing] Discarded transaction since it maxed out maxTransactionDuration' ) ;
240
+ if ( event . contexts && event . contexts . trace ) {
241
+ event . contexts . trace = {
242
+ ...event . contexts . trace ,
243
+ status : SpanStatus . DeadlineExceeded ,
244
+ } ;
245
+ event . tags = {
246
+ ...event . tags ,
247
+ maxTransactionDurationExceeded : 'true' ,
248
+ } ;
249
+ }
215
250
}
216
251
217
252
return event ;
@@ -243,7 +278,7 @@ export class Tracing implements Integration {
243
278
}
244
279
if ( Tracing . _heartbeatCounter >= 3 ) {
245
280
if ( Tracing . _activeTransaction ) {
246
- logger . log (
281
+ Tracing . _log (
247
282
"[Tracing] Heartbeat safeguard kicked in, finishing transaction since activities content hasn't changed for 3 beats" ,
248
283
) ;
249
284
Tracing . _activeTransaction . setStatus ( SpanStatus . DeadlineExceeded ) ;
@@ -263,8 +298,10 @@ export class Tracing implements Integration {
263
298
if ( Tracing . options . discardBackgroundSpans && global . document ) {
264
299
document . addEventListener ( 'visibilitychange' , ( ) => {
265
300
if ( document . hidden && Tracing . _activeTransaction ) {
266
- logger . log ( '[Tracing] Discarded active transaction incl. activities since tab moved to the background' ) ;
267
- Tracing . _resetActiveTransaction ( ) ;
301
+ Tracing . _log ( '[Tracing] Discarded active transaction incl. activities since tab moved to the background' ) ;
302
+ Tracing . _activeTransaction . setStatus ( SpanStatus . Cancelled ) ;
303
+ Tracing . _activeTransaction . setTag ( 'visibilitychange' , 'document.hidden' ) ;
304
+ Tracing . finishIdleTransaction ( ) ;
268
305
}
269
306
} ) ;
270
307
}
@@ -337,7 +374,7 @@ export class Tracing implements Integration {
337
374
/**
338
375
* If an error or unhandled promise occurs, we mark the active transaction as failed
339
376
*/
340
- logger . log ( `[Tracing] Global error occured, setting status in transaction: ${ SpanStatus . InternalError } ` ) ;
377
+ Tracing . _log ( `[Tracing] Global error occured, setting status in transaction: ${ SpanStatus . InternalError } ` ) ;
341
378
Tracing . _activeTransaction . setStatus ( SpanStatus . InternalError ) ;
342
379
}
343
380
}
@@ -351,6 +388,25 @@ export class Tracing implements Integration {
351
388
} ) ;
352
389
}
353
390
391
+ /**
392
+ * Uses logger.log to log things in the SDK or as breadcrumbs if defined in options
393
+ */
394
+ private static _log ( ...args : any [ ] ) : void {
395
+ if ( Tracing . options . debug && Tracing . options . debug . writeAsBreadcrumbs ) {
396
+ const _getCurrentHub = Tracing . _getCurrentHub ;
397
+ if ( _getCurrentHub ) {
398
+ _getCurrentHub ( ) . addBreadcrumb ( {
399
+ category : 'tracing' ,
400
+ level : Severity . Debug ,
401
+ message : safeJoin ( args , ' ' ) ,
402
+ type : 'debug' ,
403
+ } ) ;
404
+ return ;
405
+ }
406
+ }
407
+ logger . log ( args ) ;
408
+ }
409
+
354
410
/**
355
411
* Starts a Transaction waiting for activity idle to finish
356
412
*/
@@ -360,7 +416,7 @@ export class Tracing implements Integration {
360
416
// b) A activity wasn't popped correctly and therefore the transaction is stalling
361
417
Tracing . finishIdleTransaction ( ) ;
362
418
363
- logger . log ( '[Tracing] startIdleTransaction, name:' , name ) ;
419
+ Tracing . _log ( '[Tracing] startIdleTransaction, name:' , name ) ;
364
420
365
421
const _getCurrentHub = Tracing . _getCurrentHub ;
366
422
if ( ! _getCurrentHub ) {
@@ -401,7 +457,7 @@ export class Tracing implements Integration {
401
457
const active = Tracing . _activeTransaction as SpanClass ;
402
458
if ( active ) {
403
459
Tracing . _addPerformanceEntries ( active ) ;
404
- logger . log ( '[Tracing] finishIdleTransaction' , active . transaction ) ;
460
+ Tracing . _log ( '[Tracing] finishIdleTransaction' , active . transaction ) ;
405
461
active . finish ( /*trimEnd*/ true ) ;
406
462
Tracing . _resetActiveTransaction ( ) ;
407
463
}
@@ -420,7 +476,7 @@ export class Tracing implements Integration {
420
476
return ;
421
477
}
422
478
423
- logger . log ( '[Tracing] Adding & adjusting spans using Performance API' ) ;
479
+ Tracing . _log ( '[Tracing] Adding & adjusting spans using Performance API' ) ;
424
480
425
481
const timeOrigin = Tracing . _msToSec ( performance . timeOrigin ) ;
426
482
@@ -553,7 +609,7 @@ export class Tracing implements Integration {
553
609
public static setTransactionStatus ( status : SpanStatus ) : void {
554
610
const active = Tracing . _activeTransaction ;
555
611
if ( active ) {
556
- logger . log ( '[Tracing] setTransactionStatus' , status ) ;
612
+ Tracing . _log ( '[Tracing] setTransactionStatus' , status ) ;
557
613
active . setStatus ( status ) ;
558
614
}
559
615
}
@@ -566,6 +622,29 @@ export class Tracing implements Integration {
566
622
return time / 1000 ;
567
623
}
568
624
625
+ /**
626
+ * Adds debug data to the span
627
+ */
628
+ private static _addSpanDebugInfo ( span : Span ) : void {
629
+ // tslint:disable: no-unsafe-any
630
+ const debugData : any = { } ;
631
+ if ( global . performance ) {
632
+ debugData . performance = true ;
633
+ debugData [ 'performance.timeOrigin' ] = global . performance . timeOrigin ;
634
+ debugData [ 'performance.now' ] = global . performance . now ( ) ;
635
+ // tslint:disable-next-line: deprecation
636
+ if ( global . performance . timing ) {
637
+ // tslint:disable-next-line: deprecation
638
+ debugData [ 'performance.timing.navigationStart' ] = performance . timing . navigationStart ;
639
+ }
640
+ } else {
641
+ debugData . performance = false ;
642
+ }
643
+ debugData [ 'Date.now()' ] = Date . now ( ) ;
644
+ span . setData ( 'sentry_debug' , debugData ) ;
645
+ // tslint:enable: no-unsafe-any
646
+ }
647
+
569
648
/**
570
649
* Starts tracking for a specifc activity
571
650
*
@@ -583,7 +662,7 @@ export class Tracing implements Integration {
583
662
const activeTransaction = Tracing . _activeTransaction ;
584
663
585
664
if ( ! activeTransaction ) {
586
- logger . log ( `[Tracing] Not pushing activity ${ name } since there is no active transaction` ) ;
665
+ Tracing . _log ( `[Tracing] Not pushing activity ${ name } since there is no active transaction` ) ;
587
666
return 0 ;
588
667
}
589
668
@@ -606,10 +685,10 @@ export class Tracing implements Integration {
606
685
} ;
607
686
}
608
687
609
- logger . log ( `[Tracing] pushActivity: ${ name } #${ Tracing . _currentIndex } ` ) ;
610
- logger . log ( '[Tracing] activies count' , Object . keys ( Tracing . _activities ) . length ) ;
688
+ Tracing . _log ( `[Tracing] pushActivity: ${ name } #${ Tracing . _currentIndex } ` ) ;
689
+ Tracing . _log ( '[Tracing] activies count' , Object . keys ( Tracing . _activities ) . length ) ;
611
690
if ( options && typeof options . autoPopAfter === 'number' ) {
612
- logger . log ( `[Tracing] auto pop of: ${ name } #${ Tracing . _currentIndex } in ${ options . autoPopAfter } ms` ) ;
691
+ Tracing . _log ( `[Tracing] auto pop of: ${ name } #${ Tracing . _currentIndex } in ${ options . autoPopAfter } ms` ) ;
613
692
const index = Tracing . _currentIndex ;
614
693
setTimeout ( ( ) => {
615
694
Tracing . popActivity ( index , {
@@ -634,7 +713,7 @@ export class Tracing implements Integration {
634
713
const activity = Tracing . _activities [ id ] ;
635
714
636
715
if ( activity ) {
637
- logger . log ( `[Tracing] popActivity ${ activity . name } #${ id } ` ) ;
716
+ Tracing . _log ( `[Tracing] popActivity ${ activity . name } #${ id } ` ) ;
638
717
const span = activity . span ;
639
718
if ( span ) {
640
719
if ( spanData ) {
@@ -648,6 +727,9 @@ export class Tracing implements Integration {
648
727
}
649
728
} ) ;
650
729
}
730
+ if ( Tracing . options . debug && Tracing . options . debug . spanDebugTimingInfo ) {
731
+ Tracing . _addSpanDebugInfo ( span ) ;
732
+ }
651
733
span . finish ( ) ;
652
734
}
653
735
// tslint:disable-next-line: no-dynamic-delete
@@ -657,11 +739,11 @@ export class Tracing implements Integration {
657
739
const count = Object . keys ( Tracing . _activities ) . length ;
658
740
clearTimeout ( Tracing . _debounce ) ;
659
741
660
- logger . log ( '[Tracing] activies count' , count ) ;
742
+ Tracing . _log ( '[Tracing] activies count' , count ) ;
661
743
662
744
if ( count === 0 && Tracing . _activeTransaction ) {
663
745
const timeout = Tracing . options && Tracing . options . idleTimeout ;
664
- logger . log ( `[Tracing] Flushing Transaction in ${ timeout } ms` ) ;
746
+ Tracing . _log ( `[Tracing] Flushing Transaction in ${ timeout } ms` ) ;
665
747
Tracing . _debounce = ( setTimeout ( ( ) => {
666
748
Tracing . finishIdleTransaction ( ) ;
667
749
} , timeout ) as any ) as number ;
0 commit comments