1- /* eslint-disable max-lines */
1+ /* eslint-disable max-lines, complexity */
22import type { Hub , IdleTransaction } from '@sentry/core' ;
33import {
44 SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
@@ -9,7 +9,14 @@ import {
99 spanToJSON ,
1010 startIdleTransaction ,
1111} 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' ;
1320import { getDomElement , logger , tracingContextFromHeaders } from '@sentry/utils' ;
1421
1522import { DEBUG_BUILD } from '../common/debug-build' ;
@@ -60,27 +67,48 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
6067 heartbeatInterval : number ;
6168
6269 /**
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.
6485 *
6586 * Default: true
6687 */
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 ;
6896
6997 /**
7098 * Flag to enable/disable creation of `pageload` transaction on first pageload.
71- *
7299 * Default: true
100+ * @deprecated Configure `spanOnPageLoad` instead.
73101 */
74- startTransactionOnPageLoad : boolean ;
102+ startTransactionOnPageLoad ? : boolean ;
75103
76104 /**
77105 * Flag Transactions where tabs moved to background with "cancelled". Browser background tab timing is
78106 * not suited towards doing precise measurements of operations. By default, we recommend that this option
79107 * be enabled as background transactions can mess up your statistics in nondeterministic ways.
80- *
81108 * Default: true
109+ * @deprecated Configure `markBackgroundSpan` instead.
82110 */
83- markBackgroundTransactions : boolean ;
111+ markBackgroundTransactions ? : boolean ;
84112
85113 /**
86114 * If true, Sentry will capture long tasks and add them to the corresponding transaction.
@@ -117,6 +145,12 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
117145 onStartRouteTransaction : ( t : Transaction | undefined , ctx : TransactionContext , getCurrentHub : ( ) => Hub ) => void ;
118146 } > ;
119147
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+
120154 /**
121155 * beforeNavigate is called before a pageload/navigation transaction is created and allows users to modify transaction
122156 * context data, or drop the transaction entirely (by setting `sampled = false` in the context).
@@ -126,6 +160,8 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
126160 * @param context: The context data which will be passed to `startTransaction` by default
127161 *
128162 * @returns A (potentially) modified context object, with `sampled = false` if the transaction should be dropped.
163+ *
164+ * @deprecated Use `beforeStartSpan` instead.
129165 */
130166 beforeNavigate ?( this : void , context : TransactionContext ) : TransactionContext | undefined ;
131167
@@ -143,10 +179,10 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
143179
144180const DEFAULT_BROWSER_TRACING_OPTIONS : BrowserTracingOptions = {
145181 ...TRACING_DEFAULTS ,
146- markBackgroundTransactions : true ,
147182 routingInstrumentation : instrumentRoutingWithDefaults ,
148- startTransactionOnLocationChange : true ,
149- startTransactionOnPageLoad : true ,
183+ spanOnLocationChange : true ,
184+ spanOnPageLoad : true ,
185+ markBackgroundSpan : true ,
150186 enableLongTask : true ,
151187 _experiments : { } ,
152188 ...defaultRequestInstrumentationOptions ,
@@ -182,20 +218,42 @@ export class BrowserTracing implements Integration {
182218
183219 private _hasSetTracePropagationTargets : boolean ;
184220
185- public constructor ( _options ? : Partial < BrowserTracingOptions > ) {
221+ public constructor ( _options : Partial < BrowserTracingOptions > = { } ) {
186222 this . name = BROWSER_TRACING_INTEGRATION_ID ;
187223 this . _hasSetTracePropagationTargets = false ;
188224
189225 addTracingExtensions ( ) ;
190226
191227 if ( DEBUG_BUILD ) {
192228 this . _hasSetTracePropagationTargets = ! ! (
193- _options &&
194229 // eslint-disable-next-line deprecation/deprecation
195230 ( _options . tracePropagationTargets || _options . tracingOrigins )
196231 ) ;
197232 }
198233
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+
199257 this . options = {
200258 ...DEFAULT_BROWSER_TRACING_OPTIONS ,
201259 ..._options ,
@@ -207,15 +265,6 @@ export class BrowserTracing implements Integration {
207265 this . options . enableLongTask = this . options . _experiments . enableLongTask ;
208266 }
209267
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-
219268 this . _collectWebVitals = startTrackingWebVitals ( ) ;
220269 if ( this . options . enableLongTask ) {
221270 startTrackingLongTasks ( ) ;
@@ -237,9 +286,9 @@ export class BrowserTracing implements Integration {
237286
238287 const {
239288 routingInstrumentation : instrumentRouting ,
240- startTransactionOnLocationChange ,
241- startTransactionOnPageLoad ,
242- markBackgroundTransactions ,
289+ spanOnPageLoad ,
290+ spanOnLocationChange ,
291+ markBackgroundSpan ,
243292 traceFetch,
244293 traceXHR,
245294 shouldCreateSpanForRequest,
@@ -275,11 +324,11 @@ export class BrowserTracing implements Integration {
275324
276325 return transaction ;
277326 } ,
278- startTransactionOnPageLoad ,
279- startTransactionOnLocationChange ,
327+ spanOnPageLoad ,
328+ spanOnLocationChange ,
280329 ) ;
281330
282- if ( markBackgroundTransactions ) {
331+ if ( markBackgroundSpan ) {
283332 registerBackgroundTabDetection ( ) ;
284333 }
285334
@@ -306,7 +355,14 @@ export class BrowserTracing implements Integration {
306355
307356 const hub = this . _getCurrentHub ( ) ;
308357
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 ;
310366
311367 const isPageloadTransaction = context . op === 'pageload' ;
312368
@@ -328,7 +384,11 @@ export class BrowserTracing implements Integration {
328384 trimEnd : true ,
329385 } ;
330386
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 ;
332392
333393 // For backwards compatibility reasons, beforeNavigate can return undefined to "drop" the transaction (prevent it
334394 // from being sent to Sentry).
0 commit comments