Skip to content

Commit ad782b3

Browse files
authored
fix(js): Fix hierarchy and examples for sample rate configuration (#13153)
1 parent 7bfd991 commit ad782b3

File tree

1 file changed

+24
-22
lines changed
  • docs/platforms/javascript/common/tracing/configure-sampling

1 file changed

+24
-22
lines changed

docs/platforms/javascript/common/tracing/configure-sampling/index.mdx

+24-22
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ Sentry's tracing functionality helps you monitor application performance by capt
1010

1111
The JavaScript SDK provides two main options for controlling the sampling rate:
1212

13-
1. Uniform Sample Rate (`tracesSampleRate`)
13+
### Uniform Sample Rate (`tracesSampleRate`)
1414
This option sets a fixed percentage of transactions to be captured:
1515

1616
<PlatformContent includePath="/tracing/sample-rate" />
1717

1818
With `tracesSampleRate` set to `0.25`, approximately 25% of transactions will be recorded and sent to Sentry. This provides an even cross-section of transactions regardless of where in your app they occur.
1919

20-
2. Sampling Function (`tracesSampler`)
20+
### Sampling Function (`tracesSampler`)
2121

2222
For more granular control, you can use the `tracesSampler` function. This approach allows you to:
2323

@@ -28,45 +28,47 @@ For more granular control, you can use the `tracesSampler` function. This approa
2828

2929
<PlatformContent includePath="/tracing/trace-sampler" />
3030

31-
### Trace Sampler Examples
31+
#### Trace Sampler Examples
3232

3333
1. Prioritizing Critical User Flows
3434

3535
```javascript
3636
tracesSampler: (samplingContext) => {
37-
const { name, attributes } = samplingContext;
38-
37+
const { name, attributes, inheritOrSampleWith } = samplingContext;
38+
3939
// Sample all checkout transactions
4040
if (name.includes('/checkout') || attributes?.flow === 'checkout') {
4141
return 1.0;
4242
}
43-
43+
4444
// Sample 50% of login transactions
4545
if (name.includes('/login') || attributes?.flow === 'login') {
4646
return 0.5;
4747
}
48-
48+
4949
// Sample 10% of everything else
50-
return 0.1;
50+
return inheritOrSampleWith(0.1);
5151
}
5252
```
5353
5454
2. Handling Different Environments
5555
5656
```javascript
5757
tracesSampler: (samplingContext) => {
58+
const { inheritOrSampleWith } = samplingContext;
59+
5860
// Sample all transactions in development
5961
if (process.env.NODE_ENV === 'development') {
6062
return 1.0;
6163
}
62-
64+
6365
// Sample 5% in production
6466
if (process.env.NODE_ENV === 'production') {
6567
return 0.05;
6668
}
67-
69+
6870
// Sample 20% in staging
69-
return 0.2;
71+
return inheritOrSampleWith(0.2);
7072
}
7173
```
7274
@@ -75,22 +77,22 @@ tracesSampler: (samplingContext) => {
7577
```javascript
7678
tracesSampler: (samplingContext) => {
7779
const { attributes, inheritOrSampleWith } = samplingContext;
78-
80+
7981
// Always sample for premium users
8082
if (attributes?.userTier === 'premium') {
8183
return 1.0;
8284
}
83-
85+
8486
// Sample more transactions for users experiencing errors
8587
if (attributes?.hasRecentErrors === true) {
8688
return 0.8;
8789
}
88-
90+
8991
// Sample less for high-volume, low-value paths
9092
if (attributes?.path?.includes('/api/metrics')) {
9193
return 0.01;
9294
}
93-
95+
9496
// Default sampling rate
9597
return inheritOrSampleWith(0.2);
9698
}
@@ -104,16 +106,16 @@ When the `tracesSampler` function is called, it receives a `samplingContext` obj
104106
typescriptCopyinterface SamplingContext {
105107
// Name of the span/transaction
106108
name: string;
107-
109+
108110
// Initial attributes of the span/transaction
109111
attributes: SpanAttributes | undefined;
110-
112+
111113
// Whether the parent span was sampled (undefined if no incoming trace)
112114
parentSampled: boolean | undefined;
113-
115+
114116
// Sample rate from incoming trace (undefined if no incoming trace)
115117
parentSampleRate: number | undefined;
116-
118+
117119
// Utility function to inherit parent decision or fallback
118120
inheritOrSampleWith: (fallbackRate: number) => number;
119121
}
@@ -134,12 +136,12 @@ In distributed systems, trace information is propagated between services. The in
134136
```javascript
135137
tracesSampler: (samplingContext) => {
136138
const { name, inheritOrSampleWith } = samplingContext;
137-
139+
138140
// Apply specific rules first
139141
if (name.includes('critical-path')) {
140142
return 1.0; // Always sample
141143
}
142-
144+
143145
// Otherwise inherit parent sampling decision or fall back to 0.1
144146
return inheritOrSampleWith(0.1);
145147
}
@@ -190,4 +192,4 @@ webSocket.send(JSON.stringify({
190192
191193
Effective sampling is key to getting the most value from Sentry's performance monitoring while minimizing overhead. The `tracesSampler` function gives you precise control over which transactions to record, allowing you to focus on the most important parts of your application.
192194
193-
By implementing a thoughtful sampling strategy, you'll get the performance insights you need without overwhelming your systems or your Sentry quota.
195+
By implementing a thoughtful sampling strategy, you'll get the performance insights you need without overwhelming your systems or your Sentry quota.

0 commit comments

Comments
 (0)