Skip to content

Commit d78510f

Browse files
committed
Sampling clarity updates
1 parent e0d2463 commit d78510f

File tree

1 file changed

+31
-90
lines changed
  • docs/platforms/javascript/common/tracing/configure-sampling

1 file changed

+31
-90
lines changed

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

Lines changed: 31 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ Sentry's tracing functionality helps you monitor application performance by capt
1111
The JavaScript SDK provides two main options for controlling the sampling rate:
1212

1313
1. Uniform Sample Rate (`tracesSampleRate`)
14-
This option sets a fixed percentage of transactions to be captured:
14+
`tracesSampleRate` is floating point value 0.0 and 1.0, which controls the probability that a transaction will be sampled.
1515

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

18-
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.
18+
With `tracesSampleRate` set to `0.25`, each transaction in your application is randomly sampled with a probability of `0.25`, so you can expect that one in every four transactions will be sent to Sentry.
1919

2020
2. Sampling Function (`tracesSampler`)
2121

22-
For more granular control, you can use the `tracesSampler` function. This approach allows you to:
22+
For more granular control, you provide a `tracesSampler` function. This approach allows you to:
2323

2424
- Apply different sampling rates to different types of transactions
2525
- Filter out specific transactions entirely
@@ -28,6 +28,29 @@ For more granular control, you can use the `tracesSampler` function. This approa
2828

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

31+
### The Sampling Context Object
32+
33+
When the `tracesSampler` function is called, it receives a `samplingContext` object with valuable information to help make sampling decisions:
34+
35+
```typescript
36+
typescriptCopyinterface SamplingContext {
37+
// Name of the span/transaction
38+
name: string;
39+
40+
// Initial attributes of the span/transaction
41+
attributes: SpanAttributes | undefined;
42+
43+
// Whether the parent span was sampled (undefined if no incoming trace)
44+
parentSampled: boolean | undefined;
45+
46+
// Sample rate from incoming trace (undefined if no incoming trace)
47+
parentSampleRate: number | undefined;
48+
49+
// Utility function to inherit parent decision or fallback
50+
inheritOrSampleWith: (fallbackRate: number) => number;
51+
}
52+
```
53+
3154
### Trace Sampler Examples
3255

3356
1. Prioritizing Critical User Flows
@@ -96,98 +119,16 @@ tracesSampler: (samplingContext) => {
96119
}
97120
```
98121
99-
## The Sampling Context Object
100-
101-
When the `tracesSampler` function is called, it receives a `samplingContext` object with valuable information to help make sampling decisions:
102-
103-
```typescript
104-
typescriptCopyinterface SamplingContext {
105-
// Name of the span/transaction
106-
name: string;
107-
108-
// Initial attributes of the span/transaction
109-
attributes: SpanAttributes | undefined;
110-
111-
// Whether the parent span was sampled (undefined if no incoming trace)
112-
parentSampled: boolean | undefined;
113-
114-
// Sample rate from incoming trace (undefined if no incoming trace)
115-
parentSampleRate: number | undefined;
116-
117-
// Utility function to inherit parent decision or fallback
118-
inheritOrSampleWith: (fallbackRate: number) => number;
119-
}
120-
```
121-
122-
The sampling context contains:
123-
124-
- `name`: The name of the transaction/span
125-
- `attributes`: Initial tags and attributes set on the transaction
126-
- `parentSampled`: Whether the parent transaction was sampled (for distributed tracing)
127-
- `parentSampleRate`: The sample rate used in the parent transaction
128-
- `inheritOrSampleWith`: A utility function to handle inheritance logic (recommended)
129-
130-
## Inheritance in Distributed Tracing
131-
132-
In distributed systems, trace information is propagated between services. The inheritOrSampleWith function simplifies handling parent sampling decisions:
133-
134-
```javascript
135-
tracesSampler: (samplingContext) => {
136-
const { name, inheritOrSampleWith } = samplingContext;
137-
138-
// Apply specific rules first
139-
if (name.includes('critical-path')) {
140-
return 1.0; // Always sample
141-
}
142-
143-
// Otherwise inherit parent sampling decision or fall back to 0.1
144-
return inheritOrSampleWith(0.1);
145-
}
146-
```
147-
This approach ensures consistent sampling decisions across your entire distributed trace. All transactions in a given trace will share the same sampling decision, preventing broken or incomplete traces.
148-
149-
**Note:** The `inheritOrSampleWith` helper was introduced in version 9 of the SDK. For earlier versions, you can implement similar logic manually using the `parentSampled` property.
150-
151122
## Sampling Decision Precedence
152123
153124
When multiple sampling mechanisms could apply, Sentry follows this order of precedence:
154125
155-
- If `tracesSampler` is defined, its decision is used (can consider parent sampling)
156-
- If no `tracesSampler` but parent sampling is available, parent decision is used
126+
- If a sampling decision is passed to `startSpan`, that decision is used
127+
- If `tracesSampler` is defined, its decision is used. Although the `tracesSampler` can override the parent sampling decision, most users will want to ensure their `tracesSampler` respects the parent sampling decision.
128+
- If no `tracesSampler` is defined, but there is a parent sampling decision from an incoming distributed trace, we use the parent sampling decision
157129
- If neither of the above, `tracesSampleRate` is used
158-
- If none of the above are set, no transactions are sampled (0%)
159-
160-
## How Sampling Propagates in Distributed Traces
161-
162-
Sentry uses a "head-based" sampling approach:
163-
164-
- A sampling decision is made in the originating service (the "head")
165-
- This decision is propagated to all downstream services via HTTP headers
166-
167-
The two key headers are:
168-
- `sentry-trace`: Contains trace ID, span ID, and sampling decision
169-
- `baggage`: Contains additional trace metadata including sample rate
170-
171-
Sentry automatically attaches these headers to outgoing HTTP requests when using the `browserTracingIntegration`. For other communication channels like WebSockets, you can manually propagate trace information:
172-
173-
```javascript
174-
// Extract trace data from the current scope
175-
const traceData = Sentry.getTraceData();
176-
const sentryTraceHeader = traceData["sentry-trace"];
177-
const sentryBaggageHeader = traceData["baggage"];
178-
179-
// Add to your custom request (example using WebSocket)
180-
webSocket.send(JSON.stringify({
181-
message: "Your data here",
182-
metadata: {
183-
sentryTrace: sentryTraceHeader,
184-
baggage: sentryBaggageHeader
185-
}
186-
}));
187-
```
130+
- If none of the above are set, no transactions are sampled. This is equivalent to setting `tracesSampleRate` to `0.0`.
188131
189132
## Conclusion
190133
191-
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.
192-
193-
By implementing a thoughtful sampling strategy, you'll get the performance insights you need without overwhelming your systems or your Sentry quota.
134+
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.

0 commit comments

Comments
 (0)