@@ -10,6 +10,7 @@ interface TransactionActivityOptions {
10
10
*/
11
11
onLocationChange ( state : any ) : string ;
12
12
startTransactionOnLocationChange : boolean ;
13
+ tracesSampleRate : number ;
13
14
}
14
15
15
16
/** JSDoc */
@@ -32,6 +33,11 @@ export class TransactionActivity implements Integration {
32
33
*/
33
34
public static id : string = 'TransactionActivity' ;
34
35
36
+ /**
37
+ * Is Tracing enabled, this will be determined once per pageload.
38
+ */
39
+ private static _enabled ?: boolean ;
40
+
35
41
/** JSDoc */
36
42
private static _options : TransactionActivityOptions ;
37
43
@@ -51,55 +57,80 @@ export class TransactionActivity implements Integration {
51
57
/**
52
58
* @inheritDoc
53
59
*/
54
- public constructor (
55
- public readonly _options : TransactionActivityOptions = {
60
+ public constructor ( public readonly _options ?: Partial < TransactionActivityOptions > ) {
61
+ const defaults = {
56
62
idleTimeout : 500 ,
57
63
onLocationChange : ( ) => global . location . href ,
58
64
patchHistory : true ,
59
65
startTransactionOnLocationChange : true ,
60
- } ,
61
- ) {
62
- TransactionActivity . _options = _options ;
66
+ tracesSampleRate : 1 ,
67
+ } ;
68
+ TransactionActivity . _options = {
69
+ ...defaults ,
70
+ ..._options ,
71
+ } ;
63
72
}
64
73
65
74
/**
66
75
* @inheritDoc
67
76
*/
68
77
public setupOnce ( _ : ( callback : EventProcessor ) => void , getCurrentHub : ( ) => Hub ) : void {
69
78
TransactionActivity . _getCurrentHub = getCurrentHub ;
70
- if ( this . _options . patchHistory ) {
71
- // tslint:disable: no-unsafe-any
72
- if ( global . history ) {
79
+ if ( ! TransactionActivity . _isEnabled ( ) ) {
80
+ return ;
81
+ }
82
+ // `${window.location.href}` will be used a temp transaction name
83
+ TransactionActivity . startIdleTransaction ( `${ window . location . href } ` , {
84
+ op : 'pageload' ,
85
+ sampled : true ,
86
+ } ) ;
87
+
88
+ // tslint:disable: no-unsafe-any
89
+ if ( global . history && this . _options && this . _options . patchHistory ) {
90
+ // tslint:disable-next-line: typedef only-arrow-functions
91
+ ( function ( history : any ) {
92
+ const pushState = history . pushState ;
73
93
// tslint:disable-next-line: typedef only-arrow-functions
74
- ( function ( history : any ) {
75
- const pushState = history . pushState ;
76
- // tslint:disable-next-line: typedef only-arrow-functions
77
- history . pushState = function ( state : any ) {
78
- if ( typeof history . onpushstate === 'function' ) {
79
- history . onpushstate ( { state } ) ;
80
- }
81
- // ... whatever else you want to do
82
- // maybe call onhashchange e.handler
83
- return pushState . apply ( history , arguments ) ;
84
- } ;
85
- } ) ( global . history ) ;
86
- global . onpopstate = ( history as any ) . onpushstate = ( _state : any ) => {
87
- if ( this . _options . startTransactionOnLocationChange ) {
88
- TransactionActivity . startIdleTransaction ( `${ global . location . href } ` , {
89
- op : 'navigation' ,
90
- sampled : true ,
91
- } ) ;
94
+ history . pushState = function ( state : any ) {
95
+ if ( typeof history . onpushstate === 'function' ) {
96
+ history . onpushstate ( { state } ) ;
92
97
}
98
+ // ... whatever else you want to do
99
+ // maybe call onhashchange e.handler
100
+ return pushState . apply ( history , arguments ) ;
93
101
} ;
94
- }
95
- // tslint:enable: no-unsafe-any
102
+ } ) ( global . history ) ;
103
+ global . onpopstate = ( history as any ) . onpushstate = ( _state : any ) => {
104
+ if ( this . _options && this . _options . startTransactionOnLocationChange ) {
105
+ TransactionActivity . startIdleTransaction ( `${ global . location . href } ` , {
106
+ op : 'navigation' ,
107
+ sampled : true ,
108
+ } ) ;
109
+ }
110
+ } ;
96
111
}
112
+ // tslint:enable: no-unsafe-any
113
+ }
114
+
115
+ /**
116
+ * Is tracing enabled
117
+ */
118
+ private static _isEnabled ( ) : boolean {
119
+ if ( TransactionActivity . _enabled !== undefined ) {
120
+ return TransactionActivity . _enabled ;
121
+ }
122
+ TransactionActivity . _enabled = Math . random ( ) > TransactionActivity . _options . tracesSampleRate ? false : true ;
123
+ return TransactionActivity . _enabled ;
97
124
}
98
125
99
126
/**
100
127
* Starts a Transaction waiting for activity idle to finish
101
128
*/
102
129
public static startIdleTransaction ( name : string , spanContext ?: SpanContext ) : Span | undefined {
130
+ if ( ! TransactionActivity . _isEnabled ( ) ) {
131
+ // Tracing is not enabled
132
+ return undefined ;
133
+ }
103
134
const activeTransaction = TransactionActivity . _activeTransaction ;
104
135
105
136
if ( activeTransaction ) {
@@ -170,6 +201,10 @@ export class TransactionActivity implements Integration {
170
201
* Starts tracking for a specifc activity
171
202
*/
172
203
public static pushActivity ( name : string , spanContext ?: SpanContext ) : number {
204
+ if ( ! TransactionActivity . _isEnabled ( ) ) {
205
+ // Tracing is not enabled
206
+ return 0 ;
207
+ }
173
208
const _getCurrentHub = TransactionActivity . _getCurrentHub ;
174
209
if ( spanContext && _getCurrentHub ) {
175
210
const hub = _getCurrentHub ( ) ;
@@ -192,6 +227,10 @@ export class TransactionActivity implements Integration {
192
227
* Removes activity and finishes the span in case there is one
193
228
*/
194
229
public static popActivity ( id : number ) : void {
230
+ if ( ! TransactionActivity . _isEnabled ( ) ) {
231
+ // Tracing is not enabled
232
+ return ;
233
+ }
195
234
const activity = TransactionActivity . _activities [ id ] ;
196
235
if ( activity ) {
197
236
if ( activity . span ) {
0 commit comments