Skip to content

Commit dcfc7c7

Browse files
committed
feat(tracing): Deprecate transaction-related options for BrowserTracing
1 parent 2c43038 commit dcfc7c7

File tree

6 files changed

+326
-196
lines changed

6 files changed

+326
-196
lines changed

MIGRATION.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ npx @sentry/migr8@latest
1010
This will let you select which updates to run, and automatically update your code. Make sure to still review all code
1111
changes!
1212

13+
## Deprecate transaction-related options to `BrowserTracing`
14+
15+
When configuring the `BrowserTracing` integration, some options have been renamed:
16+
17+
- `startTransactionOnPageLoad` --> `spanOnPageLoad`
18+
- `startTransactionOnLocationChange` --> `spanOnLocationChange`
19+
- `markBackgroundTransactions` --> `markBackgroundSpan`
20+
21+
Also, `beforeNavigate` is replaced with a `beforeStartSpan` callback, which receives `StartSpanOptions`.
22+
1323
## Deprecate using `getClient()` to check if the SDK was initialized
1424

1525
In v8, `getClient()` will stop returning `undefined` if `Sentry.init()` was not called. For cases where this may be used

packages/core/src/tracing/trace.ts

Lines changed: 2 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
import type {
2-
Instrumenter,
3-
Primitive,
4-
Scope,
5-
Span,
6-
SpanTimeInput,
7-
TransactionContext,
8-
TransactionMetadata,
9-
} from '@sentry/types';
10-
import type { SpanAttributes } from '@sentry/types';
11-
import type { SpanOrigin } from '@sentry/types';
12-
import type { TransactionSource } from '@sentry/types';
1+
import type { Span, SpanTimeInput, StartSpanOptions, TransactionContext } from '@sentry/types';
2+
133
import { dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/utils';
144

155
import { DEBUG_BUILD } from '../debug-build';
@@ -20,105 +10,6 @@ import { handleCallbackErrors } from '../utils/handleCallbackErrors';
2010
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
2111
import { spanTimeInputToSeconds, spanToJSON } from '../utils/spanUtils';
2212

23-
interface StartSpanOptions extends TransactionContext {
24-
/** A manually specified start time for the created `Span` object. */
25-
startTime?: SpanTimeInput;
26-
27-
/** If defined, start this span off this scope instead off the current scope. */
28-
scope?: Scope;
29-
30-
/** The name of the span. */
31-
name: string;
32-
33-
/** An op for the span. This is a categorization for spans. */
34-
op?: string;
35-
36-
/** The origin of the span - if it comes from auto instrumenation or manual instrumentation. */
37-
origin?: SpanOrigin;
38-
39-
/** Attributes for the span. */
40-
attributes?: SpanAttributes;
41-
42-
// All remaining fields are deprecated
43-
44-
/**
45-
* @deprecated Manually set the end timestamp instead.
46-
*/
47-
trimEnd?: boolean;
48-
49-
/**
50-
* @deprecated This cannot be set manually anymore.
51-
*/
52-
parentSampled?: boolean;
53-
54-
/**
55-
* @deprecated Use attributes or set data on scopes instead.
56-
*/
57-
metadata?: Partial<TransactionMetadata>;
58-
59-
/**
60-
* The name thingy.
61-
* @deprecated Use `name` instead.
62-
*/
63-
description?: string;
64-
65-
/**
66-
* @deprecated Use `span.setStatus()` instead.
67-
*/
68-
status?: string;
69-
70-
/**
71-
* @deprecated Use `scope` instead.
72-
*/
73-
parentSpanId?: string;
74-
75-
/**
76-
* @deprecated You cannot manually set the span to sampled anymore.
77-
*/
78-
sampled?: boolean;
79-
80-
/**
81-
* @deprecated You cannot manually set the spanId anymore.
82-
*/
83-
spanId?: string;
84-
85-
/**
86-
* @deprecated You cannot manually set the traceId anymore.
87-
*/
88-
traceId?: string;
89-
90-
/**
91-
* @deprecated Use an attribute instead.
92-
*/
93-
source?: TransactionSource;
94-
95-
/**
96-
* @deprecated Use attributes or set tags on the scope instead.
97-
*/
98-
tags?: { [key: string]: Primitive };
99-
100-
/**
101-
* @deprecated Use attributes instead.
102-
*/
103-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
104-
data?: { [key: string]: any };
105-
106-
/**
107-
* @deprecated Use `startTime` instead.
108-
*/
109-
startTimestamp?: number;
110-
111-
/**
112-
* @deprecated Use `span.end()` instead.
113-
*/
114-
endTimestamp?: number;
115-
116-
/**
117-
* @deprecated You cannot set the instrumenter manually anymore.
118-
*/
119-
instrumenter?: Instrumenter;
120-
}
121-
12213
/**
12314
* Wraps a function with a transaction/span and finishes the span after the function is done.
12415
*

packages/tracing-internal/src/browser/browsertracing.ts

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable max-lines */
1+
/* eslint-disable max-lines, complexity */
22
import type { Hub, IdleTransaction } from '@sentry/core';
33
import {
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';
1320
import { getDomElement, logger, tracingContextFromHeaders } from '@sentry/utils';
1421

1522
import { 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

144180
const 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

Comments
 (0)