@@ -85,13 +85,6 @@ export interface BrowserTracingOptions {
85
85
// TODO: Should this be an option, or a static class variable and passed
86
86
// in and we use something like `BrowserTracing.addRoutingProcessor()`
87
87
routingInstrumentationProcessors : routingInstrumentationProcessor [ ] ;
88
-
89
- /**
90
- * Flag to enable default routing instrumentation.
91
- *
92
- * Default: true
93
- */
94
- defaultRoutingInstrumentation : boolean ;
95
88
}
96
89
97
90
/**
@@ -108,33 +101,31 @@ export class BrowserTracing implements Integration {
108
101
public static id : string = 'BrowserTracing' ;
109
102
110
103
/** Browser Tracing integration options */
111
- public static options : BrowserTracingOptions ;
104
+ public options : BrowserTracingOptions = {
105
+ beforeNavigate ( location : LocationType ) : string | undefined {
106
+ return location . pathname ;
107
+ } ,
108
+ debug : {
109
+ writeAsBreadcrumbs : false ,
110
+ } ,
111
+ idleTimeout : DEFAULT_IDLE_TIMEOUT ,
112
+ routingInstrumentationProcessors : [ ] ,
113
+ startTransactionOnLocationChange : true ,
114
+ startTransactionOnPageLoad : true ,
115
+ } ;
112
116
113
117
/**
114
118
* @inheritDoc
115
119
*/
116
120
public name : string = BrowserTracing . id ;
117
121
118
- private static _activeTransaction ?: IdleTransaction ;
122
+ private _activeTransaction ?: IdleTransaction ;
119
123
120
- private static _getCurrentHub ?: ( ) => Hub ;
124
+ private _getCurrentHub ?: ( ) => Hub ;
121
125
122
126
public constructor ( _options ?: Partial < BrowserTracingOptions > ) {
123
- const defaults : BrowserTracingOptions = {
124
- beforeNavigate ( location : LocationType ) : string | undefined {
125
- return location . pathname ;
126
- } ,
127
- debug : {
128
- writeAsBreadcrumbs : false ,
129
- } ,
130
- defaultRoutingInstrumentation : true ,
131
- idleTimeout : DEFAULT_IDLE_TIMEOUT ,
132
- routingInstrumentationProcessors : [ ] ,
133
- startTransactionOnLocationChange : true ,
134
- startTransactionOnPageLoad : true ,
135
- } ;
136
- BrowserTracing . options = {
137
- ...defaults ,
127
+ this . options = {
128
+ ...this . options ,
138
129
..._options ,
139
130
} ;
140
131
}
@@ -143,25 +134,24 @@ export class BrowserTracing implements Integration {
143
134
* @inheritDoc
144
135
*/
145
136
public setupOnce ( _ : ( callback : EventProcessor ) => void , getCurrentHub : ( ) => Hub ) : void {
146
- BrowserTracing . _getCurrentHub = getCurrentHub ;
137
+ this . _getCurrentHub = getCurrentHub ;
147
138
148
139
if ( ! global || ! global . location ) {
149
140
return ;
150
141
}
151
142
152
- // TODO: is it fine that this is mutable operation? Could also do = [...routingInstr, setHeaderContext]?
153
- BrowserTracing . options . routingInstrumentationProcessors . push ( setHeaderContext ) ;
154
- if ( BrowserTracing . options . defaultRoutingInstrumentation ) {
155
- BrowserTracing . _initRoutingInstrumentation ( ) ;
156
- }
143
+ this . _initRoutingInstrumentation ( ) ;
157
144
}
158
145
159
146
/** Start routing instrumentation */
160
- private static _initRoutingInstrumentation ( ) : void {
161
- const { startTransactionOnPageLoad, startTransactionOnLocationChange } = BrowserTracing . options ;
147
+ private _initRoutingInstrumentation ( ) : void {
148
+ const { startTransactionOnPageLoad, startTransactionOnLocationChange } = this . options ;
149
+
150
+ // TODO: is it fine that this is mutable operation? Could also do = [...routingInstr, setHeaderContext]?
151
+ this . options . routingInstrumentationProcessors . push ( setHeaderContext ) ;
162
152
163
153
if ( startTransactionOnPageLoad ) {
164
- BrowserTracing . _activeTransaction = BrowserTracing . createRouteTransaction ( 'pageload' ) ;
154
+ this . _activeTransaction = this . _createRouteTransaction ( 'pageload' ) ;
165
155
}
166
156
167
157
let startingUrl : string | undefined = global . location . href ;
@@ -170,8 +160,8 @@ export class BrowserTracing implements Integration {
170
160
callback : ( { to, from } : { to : string ; from ?: string } ) => {
171
161
/**
172
162
* This early return is there to account for some cases where navigation transaction
173
- * starts right after long running pageload. We make sure that if from is undefined
174
- * and that a valid startingURL exists, we don't uncessarily create a navigation transaction.
163
+ * starts right after long running pageload. We make sure that if ` from` is undefined
164
+ * and that a valid ` startingURL` exists, we don't uncessarily create a navigation transaction.
175
165
*
176
166
* This was hard to duplicate, but this behaviour stopped as soon as this fix
177
167
* was applied. This issue might also only be caused in certain development environments
@@ -183,55 +173,51 @@ export class BrowserTracing implements Integration {
183
173
}
184
174
if ( startTransactionOnLocationChange && from !== to ) {
185
175
startingUrl = undefined ;
186
- if ( BrowserTracing . _activeTransaction ) {
176
+ if ( this . _activeTransaction ) {
187
177
// We want to finish all current ongoing idle transactions as we
188
178
// are navigating to a new page.
189
- BrowserTracing . _activeTransaction . finishIdleTransaction ( ) ;
179
+ this . _activeTransaction . finishIdleTransaction ( ) ;
190
180
}
191
- BrowserTracing . _activeTransaction = BrowserTracing . createRouteTransaction ( 'navigation' ) ;
181
+ this . _activeTransaction = this . _createRouteTransaction ( 'navigation' ) ;
192
182
}
193
183
} ,
194
184
type : 'history' ,
195
185
} ) ;
196
186
}
197
187
198
188
/** Create pageload/navigation idle transaction. */
199
- public static createRouteTransaction (
189
+ private _createRouteTransaction (
200
190
op : 'pageload' | 'navigation' ,
201
- ctx ?: TransactionContext ,
191
+ context ?: TransactionContext ,
202
192
) : IdleTransaction | undefined {
203
- if ( ! BrowserTracing . _getCurrentHub ) {
193
+ if ( ! this . _getCurrentHub ) {
204
194
return undefined ;
205
195
}
206
196
207
- const { beforeNavigate, idleTimeout, routingInstrumentationProcessors } = BrowserTracing . options ;
197
+ const { beforeNavigate, idleTimeout, routingInstrumentationProcessors } = this . options ;
208
198
209
199
// if beforeNavigate returns undefined, we should not start a transaction.
210
200
const name = beforeNavigate ( global . location ) ;
211
201
if ( name === undefined ) {
202
+ this . _log ( `[Tracing] Cancelling ${ op } idleTransaction due to beforeNavigate:` ) ;
212
203
return undefined ;
213
204
}
214
205
215
- let context : TransactionContext = { name, op, ...ctx } ;
216
- if ( routingInstrumentationProcessors ) {
217
- for ( const processor of routingInstrumentationProcessors ) {
218
- context = processor ( context ) ;
219
- }
220
- }
206
+ const ctx = createContextFromProcessors ( { name, op, ...context } , routingInstrumentationProcessors ) ;
221
207
222
- const hub = BrowserTracing . _getCurrentHub ( ) ;
223
- BrowserTracing . _log ( `[Tracing] starting ${ op } idleTransaction on scope with context:` , context ) ;
224
- const activeTransaction = startIdleTransaction ( hub , context , idleTimeout , true ) ;
208
+ const hub = this . _getCurrentHub ( ) ;
209
+ this . _log ( `[Tracing] starting ${ op } idleTransaction on scope with context:` , ctx ) ;
210
+ const activeTransaction = startIdleTransaction ( hub , ctx , idleTimeout , true ) ;
225
211
226
212
return activeTransaction ;
227
213
}
228
214
229
215
/**
230
216
* Uses logger.log to log things in the SDK or as breadcrumbs if defined in options
231
217
*/
232
- private static _log ( ...args : any [ ] ) : void {
233
- if ( BrowserTracing . options && BrowserTracing . options . debug && BrowserTracing . options . debug . writeAsBreadcrumbs ) {
234
- const _getCurrentHub = BrowserTracing . _getCurrentHub ;
218
+ private _log ( ...args : any [ ] ) : void {
219
+ if ( this . options && this . options . debug && this . options . debug . writeAsBreadcrumbs ) {
220
+ const _getCurrentHub = this . _getCurrentHub ;
235
221
if ( _getCurrentHub ) {
236
222
_getCurrentHub ( ) . addBreadcrumb ( {
237
223
category : 'tracing' ,
@@ -245,9 +231,23 @@ export class BrowserTracing implements Integration {
245
231
}
246
232
}
247
233
248
- /**
249
- * Returns the value of a meta tag
250
- */
234
+ /** Creates transaction context from a set of processors */
235
+ export function createContextFromProcessors (
236
+ context : TransactionContext ,
237
+ processors : routingInstrumentationProcessor [ ] ,
238
+ ) : TransactionContext {
239
+ let ctx = context ;
240
+ for ( const processor of processors ) {
241
+ const newContext = processor ( context ) ;
242
+ if ( newContext && newContext . name && newContext . op ) {
243
+ ctx = newContext ;
244
+ }
245
+ }
246
+
247
+ return ctx ;
248
+ }
249
+
250
+ /** Returns the value of a meta tag */
251
251
export function getMetaContent ( metaName : string ) : string | null {
252
252
const el = document . querySelector ( `meta[name=${ metaName } ]` ) ;
253
253
return el ? el . getAttribute ( 'content' ) : null ;
0 commit comments