1
- /* eslint-disable max-lines */
1
+ /* eslint-disable max-lines, complexity */
2
2
import type { Hub , IdleTransaction } from '@sentry/core' ;
3
3
import {
4
4
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
@@ -9,7 +9,14 @@ import {
9
9
spanToJSON ,
10
10
startIdleTransaction ,
11
11
} from '@sentry/core' ;
12
- import type { EventProcessor , Integration , Transaction , TransactionContext , TransactionSource } from '@sentry/types' ;
12
+ import type {
13
+ EventProcessor ,
14
+ Integration ,
15
+ StartSpanOptions ,
16
+ Transaction ,
17
+ TransactionContext ,
18
+ TransactionSource ,
19
+ } from '@sentry/types' ;
13
20
import { getDomElement , logger , tracingContextFromHeaders } from '@sentry/utils' ;
14
21
15
22
import { DEBUG_BUILD } from '../common/debug-build' ;
@@ -60,27 +67,48 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
60
67
heartbeatInterval : number ;
61
68
62
69
/**
63
- * Flag to enable/disable creation of `navigation` transaction on history changes.
70
+ * If a span should be created on location (history) changes.
71
+ * Default: true
72
+ */
73
+ spanOnLocationChange : boolean ;
74
+
75
+ /**
76
+ * If a span should be created on pageload.
77
+ * Default: true
78
+ */
79
+ spanOnPageLoad : boolean ;
80
+
81
+ /**
82
+ * Flag spans where tabs moved to background with "cancelled". Browser background tab timing is
83
+ * not suited towards doing precise measurements of operations. By default, we recommend that this option
84
+ * be enabled as background transactions can mess up your statistics in nondeterministic ways.
64
85
*
65
86
* Default: true
66
87
*/
67
- startTransactionOnLocationChange : boolean ;
88
+ markBackgroundSpan : boolean ;
89
+
90
+ /**
91
+ * Flag to enable/disable creation of `navigation` transaction on history changes.
92
+ * Default: true
93
+ * @deprecated Configure `spanOnLocationChange` instead.
94
+ */
95
+ startTransactionOnLocationChange ?: boolean ;
68
96
69
97
/**
70
98
* Flag to enable/disable creation of `pageload` transaction on first pageload.
71
- *
72
99
* Default: true
100
+ * @deprecated Configure `spanOnPageLoad` instead.
73
101
*/
74
- startTransactionOnPageLoad : boolean ;
102
+ startTransactionOnPageLoad ? : boolean ;
75
103
76
104
/**
77
105
* Flag Transactions where tabs moved to background with "cancelled". Browser background tab timing is
78
106
* not suited towards doing precise measurements of operations. By default, we recommend that this option
79
107
* be enabled as background transactions can mess up your statistics in nondeterministic ways.
80
- *
81
108
* Default: true
109
+ * @deprecated Configure `markBackgroundSpan` instead.
82
110
*/
83
- markBackgroundTransactions : boolean ;
111
+ markBackgroundTransactions ? : boolean ;
84
112
85
113
/**
86
114
* If true, Sentry will capture long tasks and add them to the corresponding transaction.
@@ -117,6 +145,12 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
117
145
onStartRouteTransaction : ( t : Transaction | undefined , ctx : TransactionContext , getCurrentHub : ( ) => Hub ) => void ;
118
146
} > ;
119
147
148
+ /**
149
+ * A callback which is called before a span for a pageload or navigation is started.
150
+ * It receives the options passed to `startSpan`, and expects to return an updated options object.
151
+ */
152
+ beforeStartSpan ?: ( options : StartSpanOptions ) => StartSpanOptions ;
153
+
120
154
/**
121
155
* beforeNavigate is called before a pageload/navigation transaction is created and allows users to modify transaction
122
156
* context data, or drop the transaction entirely (by setting `sampled = false` in the context).
@@ -126,6 +160,8 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
126
160
* @param context: The context data which will be passed to `startTransaction` by default
127
161
*
128
162
* @returns A (potentially) modified context object, with `sampled = false` if the transaction should be dropped.
163
+ *
164
+ * @deprecated Use `beforeStartSpan` instead.
129
165
*/
130
166
beforeNavigate ?( this : void , context : TransactionContext ) : TransactionContext | undefined ;
131
167
@@ -143,10 +179,10 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
143
179
144
180
const DEFAULT_BROWSER_TRACING_OPTIONS : BrowserTracingOptions = {
145
181
...TRACING_DEFAULTS ,
146
- markBackgroundTransactions : true ,
147
182
routingInstrumentation : instrumentRoutingWithDefaults ,
148
- startTransactionOnLocationChange : true ,
149
- startTransactionOnPageLoad : true ,
183
+ spanOnLocationChange : true ,
184
+ spanOnPageLoad : true ,
185
+ markBackgroundSpan : true ,
150
186
enableLongTask : true ,
151
187
_experiments : { } ,
152
188
...defaultRequestInstrumentationOptions ,
@@ -182,20 +218,42 @@ export class BrowserTracing implements Integration {
182
218
183
219
private _hasSetTracePropagationTargets : boolean ;
184
220
185
- public constructor ( _options ? : Partial < BrowserTracingOptions > ) {
221
+ public constructor ( _options : Partial < BrowserTracingOptions > = { } ) {
186
222
this . name = BROWSER_TRACING_INTEGRATION_ID ;
187
223
this . _hasSetTracePropagationTargets = false ;
188
224
189
225
addTracingExtensions ( ) ;
190
226
191
227
if ( DEBUG_BUILD ) {
192
228
this . _hasSetTracePropagationTargets = ! ! (
193
- _options &&
194
229
// eslint-disable-next-line deprecation/deprecation
195
230
( _options . tracePropagationTargets || _options . tracingOrigins )
196
231
) ;
197
232
}
198
233
234
+ // Migrate legacy options
235
+ // TODO v8: Remove this
236
+ /* eslint-disable deprecation/deprecation */
237
+ if ( typeof _options . startTransactionOnPageLoad === 'boolean' ) {
238
+ _options . spanOnPageLoad = _options . startTransactionOnPageLoad ;
239
+ }
240
+ if ( typeof _options . startTransactionOnLocationChange === 'boolean' ) {
241
+ _options . spanOnLocationChange = _options . startTransactionOnLocationChange ;
242
+ }
243
+ if ( typeof _options . markBackgroundTransactions === 'boolean' ) {
244
+ _options . markBackgroundSpan = _options . markBackgroundTransactions ;
245
+ }
246
+ /* eslint-enable deprecation/deprecation */
247
+
248
+ // TODO (v8): remove this block after tracingOrigins is removed
249
+ // Set tracePropagationTargets to tracingOrigins if specified by the user
250
+ // In case both are specified, tracePropagationTargets takes precedence
251
+ // eslint-disable-next-line deprecation/deprecation
252
+ if ( ! _options . tracePropagationTargets && _options . tracingOrigins ) {
253
+ // eslint-disable-next-line deprecation/deprecation
254
+ _options . tracePropagationTargets = _options . tracingOrigins ;
255
+ }
256
+
199
257
this . options = {
200
258
...DEFAULT_BROWSER_TRACING_OPTIONS ,
201
259
..._options ,
@@ -207,15 +265,6 @@ export class BrowserTracing implements Integration {
207
265
this . options . enableLongTask = this . options . _experiments . enableLongTask ;
208
266
}
209
267
210
- // TODO (v8): remove this block after tracingOrigins is removed
211
- // Set tracePropagationTargets to tracingOrigins if specified by the user
212
- // In case both are specified, tracePropagationTargets takes precedence
213
- // eslint-disable-next-line deprecation/deprecation
214
- if ( _options && ! _options . tracePropagationTargets && _options . tracingOrigins ) {
215
- // eslint-disable-next-line deprecation/deprecation
216
- this . options . tracePropagationTargets = _options . tracingOrigins ;
217
- }
218
-
219
268
this . _collectWebVitals = startTrackingWebVitals ( ) ;
220
269
if ( this . options . enableLongTask ) {
221
270
startTrackingLongTasks ( ) ;
@@ -237,9 +286,9 @@ export class BrowserTracing implements Integration {
237
286
238
287
const {
239
288
routingInstrumentation : instrumentRouting ,
240
- startTransactionOnLocationChange ,
241
- startTransactionOnPageLoad ,
242
- markBackgroundTransactions ,
289
+ spanOnPageLoad ,
290
+ spanOnLocationChange ,
291
+ markBackgroundSpan ,
243
292
traceFetch,
244
293
traceXHR,
245
294
shouldCreateSpanForRequest,
@@ -275,11 +324,11 @@ export class BrowserTracing implements Integration {
275
324
276
325
return transaction ;
277
326
} ,
278
- startTransactionOnPageLoad ,
279
- startTransactionOnLocationChange ,
327
+ spanOnPageLoad ,
328
+ spanOnLocationChange ,
280
329
) ;
281
330
282
- if ( markBackgroundTransactions ) {
331
+ if ( markBackgroundSpan ) {
283
332
registerBackgroundTabDetection ( ) ;
284
333
}
285
334
@@ -306,7 +355,14 @@ export class BrowserTracing implements Integration {
306
355
307
356
const hub = this . _getCurrentHub ( ) ;
308
357
309
- const { beforeNavigate, idleTimeout, finalTimeout, heartbeatInterval } = this . options ;
358
+ const {
359
+ // eslint-disable-next-line deprecation/deprecation
360
+ beforeNavigate,
361
+ beforeStartSpan,
362
+ idleTimeout,
363
+ finalTimeout,
364
+ heartbeatInterval,
365
+ } = this . options ;
310
366
311
367
const isPageloadTransaction = context . op === 'pageload' ;
312
368
@@ -328,7 +384,11 @@ export class BrowserTracing implements Integration {
328
384
trimEnd : true ,
329
385
} ;
330
386
331
- const modifiedContext = typeof beforeNavigate === 'function' ? beforeNavigate ( expandedContext ) : expandedContext ;
387
+ const contextAfterProcessing = beforeStartSpan ? beforeStartSpan ( expandedContext ) : expandedContext ;
388
+
389
+ const modifiedContext =
390
+ // eslint-disable-next-line deprecation/deprecation
391
+ typeof beforeNavigate === 'function' ? beforeNavigate ( contextAfterProcessing ) : contextAfterProcessing ;
332
392
333
393
// For backwards compatibility reasons, beforeNavigate can return undefined to "drop" the transaction (prevent it
334
394
// from being sent to Sentry).
0 commit comments