Skip to content

Commit d9fe8c6

Browse files
committed
make beforeNavigate always return transaction
1 parent 8168f83 commit d9fe8c6

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

packages/tracing/src/browser/browsertracing.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,15 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
6161
markBackgroundTransactions: boolean;
6262

6363
/**
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).
6666
*
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.
6873
*/
6974
beforeNavigate?(context: TransactionContext): TransactionContext | undefined;
7075

@@ -187,22 +192,25 @@ export class BrowserTracing implements Integration {
187192
// eslint-disable-next-line @typescript-eslint/unbound-method
188193
const { beforeNavigate, idleTimeout, maxTransactionDuration } = this.options;
189194

190-
// if beforeNavigate returns undefined, we should not start a transaction.
191195
const expandedContext = {
192196
...context,
193197
...getHeaderContext(),
194198
trimEnd: true,
195199
};
196200
const modifiedContext = typeof beforeNavigate === 'function' ? beforeNavigate(expandedContext) : expandedContext;
197201

198-
if (modifiedContext === 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.`);
201209
}
202210

203211
const hub = this._getCurrentHub();
204-
const idleTransaction = startIdleTransaction(hub, modifiedContext, idleTimeout, true);
205-
logger.log(`[Tracing] Starting ${modifiedContext.op} transaction on scope`);
212+
const idleTransaction = startIdleTransaction(hub, finalContext, idleTimeout, true);
213+
logger.log(`[Tracing] Starting ${finalContext.op} transaction on scope`);
206214
idleTransaction.registerBeforeFinishCallback((transaction, endTimestamp) => {
207215
this._metrics.addPerformanceEntries(transaction);
208216
adjustTransactionDuration(secToMs(maxTransactionDuration), transaction, endTimestamp);

packages/tracing/test/browser/browsertracing.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,14 @@ describe('BrowserTracing', () => {
177177
expect(mockBeforeNavigation).toHaveBeenCalledTimes(1);
178178
});
179179

180-
it('does not create a transaction if it returns undefined', () => {
180+
it('creates a transaction with sampled = false if it returns undefined', () => {
181181
const mockBeforeNavigation = jest.fn().mockReturnValue(undefined);
182182
createBrowserTracing(true, {
183183
beforeNavigate: mockBeforeNavigation,
184184
routingInstrumentation: customRoutingInstrumentation,
185185
});
186186
const transaction = getActiveTransaction(hub) as IdleTransaction;
187-
expect(transaction).not.toBeDefined();
187+
expect(transaction.sampled).toBe(false);
188188

189189
expect(mockBeforeNavigation).toHaveBeenCalledTimes(1);
190190
});

0 commit comments

Comments
 (0)