You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/platforms/python/tracing/configure-sampling/index.mdx
+46-47Lines changed: 46 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,37 +1,53 @@
1
1
---
2
2
title: Configure Sampling
3
3
description: "Learn how to configure sampling in your app."
4
-
sidebar_order: 30
4
+
sidebar_order: 40
5
5
---
6
6
7
-
Sentry's tracing functionality helps you monitor application performance by capturing distributed traces, attaching attributes, and adding span performance metrics across your application. However, capturing traces for every transaction can generate significant volumes of data. Sampling allows you to control the amount of spans that are sent to Sentry from your application.
7
+
If you find that Sentry's tracing functionality is generating too much data, for example, if you notice your spans quota is quickly being exhausted, you can choose to sample your traces.
8
8
9
-
Effective sampling is key to getting the most value from Sentry's performance monitoring while minimizing overhead. The `traces_sampler` function gives you precise control over which transactions to record, allowing you to focus on the most important parts of your application.
9
+
Effective sampling is key to getting the most value from Sentry's performance monitoring while minimizing overhead. The Python SDK provides two ways to control the sampling rate. You can review the options and [examples](#trace-sampler-examples) below.
10
10
11
11
## Sampling Configuration Options
12
12
13
-
The Python SDK provides two main options for controlling the sampling rate:
13
+
### 1. Uniform Sample Rate (`traces_sample_rate`)
14
14
15
-
1. Uniform Sample Rate (`traces_sample_rate`)
16
-
17
-
This option sets a fixed percentage of transactions to be captured:
15
+
`traces_sample_rate` is a floating-point value between `0.0` and `1.0`, inclusive, which controls the probability with which each transaction will be sampled:
With `traces_sample_rate` 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.
19
+
With `traces_sample_rate` 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.
22
20
23
-
2. Sampling Function (`traces_sampler`)
21
+
### 2. Sampling Function (`traces_sampler`)
24
22
25
-
For more granular control, you can use the`traces_sampler` function. This approach allows you to:
23
+
For more granular control, you can provide a`traces_sampler` function. This approach allows you to:
26
24
27
25
- Apply different sampling rates to different types of transactions
28
26
- Filter out specific transactions entirely
29
27
- Make sampling decisions based on transaction data
30
28
- Control the inheritance of sampling decisions in distributed traces
31
29
30
+
<Alert>
31
+
32
+
It is strongly recommended when using a custom `traces_sampler` that you respect the parent sampling decision. This ensures your traces will be complete.
33
+
34
+
</Alert>
35
+
36
+
```python
37
+
# Use the parent sampling decision if we have an incoming trace.
38
+
# Note: we strongly recommend respecting the parent sampling decision,
@@ -204,22 +221,23 @@ When the `traces_sampler` function is called, the Sentry SDK passes a `sampling
204
221
```python
205
222
{
206
223
"transaction_context": {
207
-
"name": str, # transaction title at creation time
208
-
"op": str, # short description of transaction type, like "http.request"
209
-
# other transaction data...
224
+
"name": str, # transaction title at creation time
225
+
"op": str, # short description of transaction type, like "http.request"
226
+
"data": Optional[Dict[str, Any]] # other transaction data
210
227
},
211
-
"parent_sampled": bool, # whether the parent transaction was sampled (if any)
212
-
"parent_sample_rate": float, # the sample rate used by the parent (if any)
213
-
# Custom context as passed to start_transaction
228
+
"parent_sampled ": Optional[bool] |None, # whether the parent transaction was sampled, `None` if no parent
229
+
"parent_sample_rate": Optional[float], # the sample rate used by the parent (if any)
230
+
"transaction_context": Optional[Dict[str, Any]], # custom context data
231
+
"custom_sampling_context": Optional[Dict[str, Any]] # additional custom data for sampling
214
232
}
215
233
```
216
234
217
-
The sampling context contains:
218
-
219
-
-`transaction_context`: Includes the transaction name, operation type, and other metadata
220
-
-`parent_sampled`: Whether the parent transaction was sampled (for distributed tracing)
221
-
-`parent_sample_rate`: The sample rate used in the parent transaction
222
-
- Any custom sampling context data passed to `start_transaction`
235
+
<b>Additional common types used in</b> `sampling_context`<b>:</b>
236
+
- str: for text values (names, operations, etc.)
237
+
- bool: for true/false values
238
+
- float: for decimal numbers (like sample rates)
239
+
- Dict[str, Any]: for dictionaries with string keys and any type of values
240
+
- Optional[Type]: for values that might be None
223
241
224
242
## Inheritance in Distributed Tracing
225
243
@@ -250,39 +268,20 @@ This approach ensures consistent sampling decisions across your entire distribut
250
268
When multiple sampling mechanisms could apply, Sentry follows this order of precedence:
251
269
252
270
1. If a sampling decision is passed to `start_transaction`, that decision is used
253
-
2. If `traces_sampler` is defined, its decision is used (can consider parent sampling)
254
-
3. If no `traces_sampler` but parent sampling is available, parent decision is used
271
+
2. If `traces_sampler` is defined, its decision is used. Although the `traces_sampler`can override the parent sampling decision, most users will want to ensure their `traces_sampler` respects the parent sampling decision
272
+
3. If no `traces_sampler`is defined, but there is a parent sampling decision from an incoming distributed trace, we use the parent sampling decision
255
273
4. If neither of the above, `traces_sample_rate` is used
256
-
5. If none of the above are set, no transactions are sampled (0%)
274
+
5. If none of the above are set, no transactions are sampled. This is equivalent to setting `traces_sample_rate=0.0`
257
275
258
276
## How Sampling Propagates in Distributed Traces
259
277
260
278
Sentry uses a "head-based" sampling approach:
261
279
262
280
- A sampling decision is made in the originating service (the "head")
263
-
- This decision is propagated to all downstream services via HTTP headers
281
+
- This decision is propagated to all downstream services
264
282
265
283
The two key headers are:
266
284
-`sentry-trace`: Contains trace ID, span ID, and sampling decision
267
285
-`baggage`: Contains additional trace metadata including sample rate
268
286
269
-
The Sentry Python SDK automatically attaches these headers to outgoing HTTP requests when using auto-instrumentation with libraries like `requests`, `urllib3`, or `httpx`. For other communication channels, you can manually propagate trace information:
# Add to your custom request (example using a message queue)
278
-
message = {
279
-
"data": "Your data here",
280
-
"metadata": {
281
-
"sentry_trace": sentry_trace_header,
282
-
"baggage": baggage_header
283
-
}
284
-
}
285
-
queue.send(json.dumps(message))
286
-
```
287
-
288
-
By implementing a thoughtful sampling strategy, you'll get the performance insights you need without overwhelming your systems or your Sentry quota.
287
+
The Sentry Python SDK automatically attaches these headers to outgoing HTTP requests when using auto-instrumentation with libraries like `requests`, `urllib3`, or `httpx`. For other communication channels, you can manually propagate trace information. Learn more about customizing tracing in [custom trace propagation](/platforms/python/tracing/distributed-tracing/custom-trace-propagation/).
0 commit comments