@@ -13,7 +13,7 @@ import {
13
13
registerRequestInstrumentation ,
14
14
RequestInstrumentationOptions ,
15
15
} from './request' ;
16
- import { defaultBeforeNavigate , defaultRoutingInstrumentation } from './router' ;
16
+ import { defaultRoutingInstrumentation } from './router' ;
17
17
18
18
export const DEFAULT_MAX_TRANSACTION_DURATION_SECONDS = 600 ;
19
19
@@ -61,12 +61,17 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
61
61
markBackgroundTransactions : boolean ;
62
62
63
63
/**
64
- * beforeNavigate is called before a pageload/navigation transaction is created and allows for users
65
- * to set custom transaction context. Default behavior is to return `window.location.pathname` .
64
+ * beforeNavigate is called before a pageload/navigation transaction is created and allows users to modify transaction
65
+ * context data, or drop the transaction entirely (by setting `sampled = false` in the context) .
66
66
*
67
- * If undefined is returned, a pageload/navigation transaction will not be created.
67
+ * Note: For legacy reasons, transactions can also be dropped by returning `undefined`, though that option may
68
+ * disappear in the future.
69
+ *
70
+ * @param context: The context data which will be passed to `startTransaction` by default
71
+ *
72
+ * @returns A (potentially) modified context object, with `sampled = false` if the transaction should be dropped.
68
73
*/
69
- beforeNavigate ( context : TransactionContext ) : TransactionContext | undefined ;
74
+ beforeNavigate ? ( context : TransactionContext ) : TransactionContext | undefined ;
70
75
71
76
/**
72
77
* Instrumentation that creates routing change transactions. By default creates
@@ -80,7 +85,6 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
80
85
}
81
86
82
87
const DEFAULT_BROWSER_TRACING_OPTIONS = {
83
- beforeNavigate : defaultBeforeNavigate ,
84
88
idleTimeout : DEFAULT_IDLE_TIMEOUT ,
85
89
markBackgroundTransactions : true ,
86
90
maxTransactionDuration : DEFAULT_MAX_TRANSACTION_DURATION_SECONDS ,
@@ -188,21 +192,25 @@ export class BrowserTracing implements Integration {
188
192
// eslint-disable-next-line @typescript-eslint/unbound-method
189
193
const { beforeNavigate, idleTimeout, maxTransactionDuration } = this . options ;
190
194
191
- // if beforeNavigate returns undefined, we should not start a transaction.
192
- const ctx = beforeNavigate ( {
195
+ const expandedContext = {
193
196
...context ,
194
197
...getHeaderContext ( ) ,
195
198
trimEnd : true ,
196
- } ) ;
199
+ } ;
200
+ const modifiedContext = typeof beforeNavigate === 'function' ? beforeNavigate ( expandedContext ) : expandedContext ;
197
201
198
- if ( ctx === undefined ) {
199
- logger . log ( `[Tracing] Did not create ${ context . op } idleTransaction due to beforeNavigate` ) ;
200
- return undefined ;
202
+ // TODO (kmclb): for backwards compatibility reasons, beforeNavigate can return undefined to "drop" the transaction
203
+ // (prevent it from being sent to Sentry). This can be removed in V6, after which time modifiedContext and
204
+ // finalContext will be one and the same.
205
+ const finalContext = modifiedContext === undefined ? { ...expandedContext , sampled : false } : modifiedContext ;
206
+
207
+ if ( finalContext . sampled === false ) {
208
+ logger . log ( `[Tracing] Will not send ${ finalContext . op } transaction because of beforeNavigate.` ) ;
201
209
}
202
210
203
211
const hub = this . _getCurrentHub ( ) ;
204
- logger . log ( `[Tracing] starting ${ ctx . op } idleTransaction on scope` ) ;
205
- const idleTransaction = startIdleTransaction ( hub , ctx , idleTimeout , true ) ;
212
+ logger . log ( `[Tracing] Starting ${ finalContext . op } transaction on scope` ) ;
213
+ const idleTransaction = startIdleTransaction ( hub , finalContext , idleTimeout , true ) ;
206
214
idleTransaction . registerBeforeFinishCallback ( ( transaction , endTimestamp ) => {
207
215
this . _metrics . addPerformanceEntries ( transaction ) ;
208
216
adjustTransactionDuration ( secToMs ( maxTransactionDuration ) , transaction , endTimestamp ) ;
0 commit comments