Skip to content

Commit 7dae9f6

Browse files
feat(interactions): Add interactions sample rate to browser tracing integrations (#11382)
Adds a `interactionsSampleRate` option to browser tracing integrations to allow users to apply an additional sample rate filter on interaction spans. `interactionsSampleRate` is applied on top of the global `tracesSampleRate`. Therefore if `interactionsSampleRate` is `0.5` and `tracesSampleRate` is `0.1`, then the actual sample rate for interactions is `0.05`
1 parent 264d9b1 commit 7dae9f6

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
114114
*/
115115
enableInp: boolean;
116116

117+
/**
118+
* Sample rate to determine interaction span sampling.
119+
* interactionsSampleRate is applied on top of the global tracesSampleRate.
120+
* ie a tracesSampleRate of 0.1 and interactionsSampleRate of 0.5 will result in a 0.05 sample rate for interactions.
121+
*
122+
* Default: 1
123+
*/
124+
interactionsSampleRate: number;
125+
117126
/**
118127
* _metricOptions allows the user to send options to change how metrics are collected.
119128
*
@@ -154,6 +163,7 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
154163
markBackgroundSpan: true,
155164
enableLongTask: true,
156165
enableInp: false,
166+
interactionsSampleRate: 1,
157167
_experiments: {},
158168
...defaultRequestInstrumentationOptions,
159169
};
@@ -196,7 +206,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
196206
/** Stores a mapping of interactionIds from PerformanceEventTimings to the origin interaction path */
197207
const interactionIdToRouteNameMapping: InteractionRouteNameMapping = {};
198208
if (options.enableInp) {
199-
startTrackingINP(interactionIdToRouteNameMapping);
209+
startTrackingINP(interactionIdToRouteNameMapping, options.interactionsSampleRate);
200210
}
201211

202212
if (options.enableLongTask) {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
9898
*/
9999
enableInp: boolean;
100100

101+
/**
102+
* Sample rate to determine interaction span sampling.
103+
* interactionsSampleRate is applied on top of the global tracesSampleRate.
104+
* ie a tracesSampleRate of 0.1 and interactionsSampleRate of 0.5 will result in a 0.05 sample rate for interactions.
105+
*
106+
* Default: 1
107+
*/
108+
interactionsSampleRate: number;
109+
101110
/**
102111
* _metricOptions allows the user to send options to change how metrics are collected.
103112
*
@@ -158,6 +167,7 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
158167
startTransactionOnPageLoad: true,
159168
enableLongTask: true,
160169
enableInp: false,
170+
interactionsSampleRate: 1,
161171
_experiments: {},
162172
...defaultRequestInstrumentationOptions,
163173
};
@@ -238,7 +248,7 @@ export class BrowserTracing implements Integration {
238248
this._interactionIdToRouteNameMapping = {};
239249

240250
if (this.options.enableInp) {
241-
startTrackingINP(this._interactionIdToRouteNameMapping);
251+
startTrackingINP(this._interactionIdToRouteNameMapping, this.options.interactionsSampleRate);
242252
}
243253
if (this.options.enableLongTask) {
244254
startTrackingLongTasks();

packages/tracing-internal/src/browser/metrics/index.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,13 @@ export function startTrackingInteractions(): void {
148148
/**
149149
* Start tracking INP webvital events.
150150
*/
151-
export function startTrackingINP(interactionIdtoRouteNameMapping: InteractionRouteNameMapping): () => void {
151+
export function startTrackingINP(
152+
interactionIdtoRouteNameMapping: InteractionRouteNameMapping,
153+
interactionsSampleRate: number,
154+
): () => void {
152155
const performance = getBrowserPerformanceAPI();
153156
if (performance && browserPerformanceTimeOrigin) {
154-
const inpCallback = _trackINP(interactionIdtoRouteNameMapping);
157+
const inpCallback = _trackINP(interactionIdtoRouteNameMapping, interactionsSampleRate);
155158

156159
return (): void => {
157160
inpCallback();
@@ -247,7 +250,10 @@ const INP_ENTRY_MAP: Record<string, 'click' | 'hover' | 'drag' | 'press'> = {
247250
};
248251

249252
/** Starts tracking the Interaction to Next Paint on the current page. */
250-
function _trackINP(interactionIdToRouteNameMapping: InteractionRouteNameMapping): () => void {
253+
function _trackINP(
254+
interactionIdToRouteNameMapping: InteractionRouteNameMapping,
255+
interactionsSampleRate: number,
256+
): () => void {
251257
return addInpInstrumentationHandler(({ metric }) => {
252258
if (metric.value === undefined) {
253259
return;
@@ -293,7 +299,8 @@ function _trackINP(interactionIdToRouteNameMapping: InteractionRouteNameMapping)
293299
});
294300

295301
/** Check to see if the span should be sampled */
296-
const sampleRate = getSampleRate(parentContext, options);
302+
const sampleRate = getSampleRate(parentContext, options, interactionsSampleRate);
303+
297304
if (!sampleRate) {
298305
return;
299306
}
@@ -690,7 +697,11 @@ function _addTtfbRequestTimeToMeasurements(_measurements: Measurements): void {
690697
}
691698

692699
/** Taken from @sentry/core sampling.ts */
693-
function getSampleRate(transactionContext: TransactionContext | undefined, options: ClientOptions): number | boolean {
700+
function getSampleRate(
701+
transactionContext: TransactionContext | undefined,
702+
options: ClientOptions,
703+
interactionsSampleRate: number,
704+
): number | boolean {
694705
if (!hasTracingEnabled(options)) {
695706
return false;
696707
}
@@ -715,8 +726,13 @@ function getSampleRate(transactionContext: TransactionContext | undefined, optio
715726
sampleRate = 1;
716727
}
717728
if (!isValidSampleRate(sampleRate)) {
718-
DEBUG_BUILD && logger.warn('[Tracing] Discarding transaction because of invalid sample rate.');
729+
DEBUG_BUILD && logger.warn('[Tracing] Discarding interaction span because of invalid sample rate.');
719730
return false;
720731
}
721-
return sampleRate;
732+
if (sampleRate === true) {
733+
return interactionsSampleRate;
734+
} else if (sampleRate === false) {
735+
return 0;
736+
}
737+
return sampleRate * interactionsSampleRate;
722738
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ conditionalTest({ min: 10 })('BrowserTracing', () => {
9292

9393
expect(browserTracing.options).toEqual({
9494
enableInp: false,
95+
interactionsSampleRate: 1,
9596
enableLongTask: true,
9697
_experiments: {},
9798
...TRACING_DEFAULTS,
@@ -112,6 +113,7 @@ conditionalTest({ min: 10 })('BrowserTracing', () => {
112113

113114
expect(browserTracing.options).toEqual({
114115
enableInp: false,
116+
interactionsSampleRate: 1,
115117
enableLongTask: false,
116118
...TRACING_DEFAULTS,
117119
markBackgroundTransactions: true,
@@ -132,6 +134,7 @@ conditionalTest({ min: 10 })('BrowserTracing', () => {
132134

133135
expect(browserTracing.options).toEqual({
134136
enableInp: false,
137+
interactionsSampleRate: 1,
135138
enableLongTask: false,
136139
_experiments: {},
137140
...TRACING_DEFAULTS,

0 commit comments

Comments
 (0)