Skip to content

Commit 7e0635b

Browse files
feat(python): Update the Configuration > Sampling page for Python SDK 3.0 (#14275)
We've made some changes to sampling in the upcoming 3.0 version of the Python SDK. Creating a new 3.x version of the relevant pages. Also removed some `python.<platform>.mdx` snippets because as far as I can tell they can never be displayed anywhere (and they're now outdated). They'd only display if the Python docs supported the sub-SDK/platform view like e.g. JS does. Once 3.x is actually out of alpha, we can make the `__v3.x` page the default and make the base page into a `__v2.x`. --------- Co-authored-by: Shannon Anahata <[email protected]>
1 parent b205240 commit 7e0635b

File tree

21 files changed

+181
-176
lines changed

21 files changed

+181
-176
lines changed

docs/platforms/python/configuration/sampling.mdx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,27 @@ The information contained in the <PlatformIdentifier name="sampling-context" />
8383

8484
When using custom instrumentation to create a transaction, you can add data to the <PlatformIdentifier name="sampling-context" /> by passing it as an optional second argument to <PlatformIdentifier name="start-transaction" />. This is useful if there's data to which you want the sampler to have access but which you don't want to attach to the transaction as `tags` or `data`, such as information that's sensitive or that’s too large to send with the transaction. For example:
8585

86-
<PlatformContent includePath="performance/custom-sampling-context" />
86+
```python
87+
sentry_sdk.start_transaction(
88+
# kwargs passed to Transaction constructor - will be recorded on transaction
89+
name="GET /search",
90+
op="search",
91+
data={
92+
"query_params": {
93+
"animal": "dog",
94+
"type": "very good"
95+
}
96+
},
97+
# `custom_sampling_context` - won't be recorded
98+
custom_sampling_context={
99+
# PII
100+
"user_id": "12312012",
101+
# too big to send
102+
"search_results": { ... }
103+
}
104+
)
105+
```
106+
87107

88108
## Inheritance
89109

@@ -105,7 +125,12 @@ If you're using a <PlatformIdentifier name="traces-sample-rate" /> rather than a
105125

106126
If you know at transaction creation time whether or not you want the transaction sent to Sentry, you also have the option of passing a sampling decision directly to the transaction constructor (note, not in the <PlatformIdentifier name="custom-sampling-context" /> object). If you do that, the transaction won't be subject to the <PlatformIdentifier name="traces-sample-rate" />, nor will <PlatformIdentifier name="traces-sampler" /> be run, so you can count on the decision that's passed not to be overwritten.
107127

108-
<PlatformContent includePath="performance/force-sampling-decision" />
128+
```python
129+
sentry_sdk.start_transaction(
130+
name="GET /search",
131+
sampled=True
132+
)
133+
```
109134

110135
## Precedence
111136

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
title: Sampling
3+
description: "Learn how to configure the volume of error and transaction events sent to Sentry."
4+
sidebar_order: 60
5+
---
6+
7+
Adding Sentry to your app gives you a great deal of valuable information about errors and performance you wouldn't otherwise get. And lots of information is good -- as long as it's the right information, at a reasonable volume.
8+
9+
## Sampling Error Events
10+
11+
To send a representative sample of your errors to Sentry, set the <PlatformIdentifier name="sample-rate" /> option in your SDK configuration to a number between `0.0` (0% of errors sent) and `1.0` (100% of errors sent). This is a static rate, which will apply equally to all errors. For example, to sample 25% of your errors:
12+
13+
<PlatformContent includePath="configuration/sample-rate" />
14+
15+
The error sample rate defaults to `1.0`, meaning all errors are sent to Sentry.
16+
17+
Changing the error sample rate requires re-deployment. In addition, setting an SDK sample rate limits visibility into the source of events. Setting a [rate limit](/pricing/quotas/manage-event-stream-guide/#rate-limiting) for your project (which only drops events when volume is high) may better suit your needs.
18+
19+
### Dynamically Sampling Error Events
20+
21+
To sample error events dynamically, set the <PlatformIdentifier name="error-sampler" /> to a function that returns the desired sample rate for the event. The <PlatformIdentifier name="error-sampler" /> takes two arguments, <PlatformIdentifier name="event" /> and <PlatformIdentifier name="hint" />. `event` is the [Event](https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/_types.py) that will be sent to Sentry, `hint` includes Python's [sys.exc_info()](https://docs.python.org/3/library/sys.html#sys.exc_info) information in `hint["exc_info"]`.
22+
23+
<Alert>
24+
25+
Your <PlatformIdentifier name="error-sampler" /> function **must return a valid value**. A valid value is either:
26+
27+
- A **floating-point number** between `0.0` and `1.0` (inclusive) indicating the probability an error gets sampled, **or**
28+
- A **boolean** indicating whether or not to sample the error.
29+
30+
</Alert>
31+
32+
One potential use case for the <PlatformIdentifier name="error-sampler" /> is to apply different sample rates for different exception types. For instance, if you would like to sample some exception called `MyException` at 50%, discard all events of another exception called `MyIgnoredException`, and sample all other exception types at 100%, you could use the following code when initializing the SDK:
33+
34+
<PlatformContent includePath="configuration/error-sampler" />
35+
36+
<Alert>
37+
38+
You can define at most one of the <PlatformIdentifier name="error-sampler" /> and the <PlatformIdentifier name="sample-rate" />. If both are set, the <PlatformIdentifier name="error-sampler" /> will control sampling, and the <PlatformIdentifier name="sample-rate" /> will be ignored.
39+
40+
</Alert>
41+
42+
## Sampling Transaction Events
43+
44+
We recommend sampling your transactions for two reasons:
45+
46+
1. Capturing a single trace involves minimal overhead, but capturing traces for _every_ page load or _every_ API request may add an undesirable load to your system.
47+
2. Enabling sampling allows you to better manage the number of events sent to Sentry, so you can tailor your volume to your organization's needs.
48+
49+
Choose a sampling rate with the goal of finding a balance between performance and volume concerns with data accuracy. You don't want to collect _too_ much data, but you want to collect sufficient data from which to draw meaningful conclusions. If you’re not sure what rate to choose, start with a low value and gradually increase it as you learn more about your traffic patterns and volume.
50+
51+
## Configuring the Transaction Sample Rate
52+
53+
The Sentry SDKs have two configuration options to control the volume of transactions sent to Sentry, allowing you to take a representative sample:
54+
55+
1. Uniform sample rate (<PlatformIdentifier name="traces-sample-rate" />):
56+
- Provides an even cross-section of transactions, no matter where in your app or under what circumstances they occur.
57+
- Uses default [inheritance](#inheritance) and [precedence](#precedence) behavior
58+
2. Sampling function (<PlatformIdentifier name="traces-sampler" />) which:
59+
- Samples different transactions at different rates
60+
- <PlatformLink to="/configuration/filtering/">Filters</PlatformLink> out some
61+
transactions entirely
62+
- Modifies default [precedence](#precedence) and [inheritance](#inheritance) behavior
63+
64+
By default, none of these options are set, meaning no transactions will be sent to Sentry. You must set either one of the options to start sending transactions.
65+
66+
### Setting a Uniform Sample Rate
67+
68+
<PlatformContent includePath="performance/uniform-sample-rate" />
69+
70+
### Setting a Sampling Function
71+
72+
<PlatformContent includePath="performance/sampling-function-intro" />
73+
74+
## Sampling Context Data
75+
76+
### Default Sampling Context Data
77+
78+
The information contained in the <PlatformIdentifier name="sampling-context" /> object passed to the <PlatformIdentifier name="traces-sampler" /> when a transaction is created varies by integration.
79+
80+
<PlatformContent includePath="performance/default-sampling-context" />
81+
82+
### Custom Sampling Context Data
83+
84+
When using custom instrumentation to create a transaction, you can add data to the <PlatformIdentifier name="sampling-context" /> by providing additional `attributes` to <PlatformIdentifier name="start-span" />.
85+
86+
<PlatformContent includePath="performance/custom-sampling-context" />
87+
88+
All span attributes provided at span start are accessible via the <PlatformIdentifier name="sampling-context" /> and will also ultimately be sent to Sentry. If you want to exclude an attribute, you can filter it out in a <PlatformLink to="/configuration/filtering">`before_send`</PlatformLink>.
89+
90+
## Inheritance
91+
92+
Whatever a transaction's sampling decision, that decision will be passed to its child spans and from there to any transactions they subsequently cause in other services.
93+
94+
(See <PlatformLink to="/tracing/trace-propagation/">Distributed Tracing</PlatformLink> for more about how that propagation is done.)
95+
96+
If the transaction currently being created is one of those subsequent transactions (in other words, if it has a parent transaction), the upstream (parent) sampling decision will be included in the sampling context data. Your <PlatformIdentifier name="traces-sampler" /> can use this information to choose whether to inherit that decision. In most cases, inheritance is the right choice, to avoid breaking distributed traces. A broken trace will not include all your services.
97+
98+
<PlatformContent includePath="performance/always-inherit-sampling-decision">
99+
100+
In some SDKs, for convenience, the <PlatformIdentifier name="traces-sampler" /> function can return a boolean, so that a parent's decision can be returned directly if that's the desired behavior.
101+
102+
</PlatformContent>
103+
104+
If you're using a <PlatformIdentifier name="traces-sample-rate" /> rather than a <PlatformIdentifier name="traces-sampler" />, the decision will always be inherited. The provided <PlatformIdentifier name="traces-sample-rate" /> will only be used to generate a sample rate if there is no sampling decision coming in from upstream.
105+
106+
## Forcing a Sampling Decision
107+
108+
If you know at span creation time whether or not you want the transaction sent to Sentry, you also have the option of passing a sampling decision directly in the `start_span` API. If you do that, the transaction won't be subject to the <PlatformIdentifier name="traces-sample-rate" />, nor will <PlatformIdentifier name="traces-sampler" /> be run, so you can count on the decision that's passed not to be overwritten.
109+
110+
<PlatformContent includePath="performance/force-sampling-decision" />
111+
112+
## Precedence
113+
114+
There are multiple ways for a transaction to end up with a sampling decision.
115+
116+
- Random sampling according to a static sample rate set in <PlatformIdentifier name="traces-sample-rate" />
117+
- Random sampling according to a sample function rate returned by <PlatformIdentifier name="traces-sampler" />
118+
- Absolute decision (100% chance or 0% chance) returned by <PlatformIdentifier name="traces-sampler" />
119+
- If the transaction has a parent, inheriting its parent's sampling decision
120+
- Absolute decision passed to <PlatformIdentifier name="start-span" />
121+
122+
When there's the potential for more than one of these to come into play, the following precedence rules apply:
123+
124+
1. If a sampling decision is passed to <PlatformIdentifier name="start-transaction" />, that decision will be used, overriding everything else.
125+
2. If <PlatformIdentifier name="traces-sampler" /> is defined, its decision will be used. It can choose to keep or ignore any parent sampling decision, use the sampling context data to make its own decision, or choose a sample rate for the transaction. We advise against overriding the parent sampling decision because it will break distributed traces.
126+
3. If <PlatformIdentifier name="traces-sampler" /> is not defined, but there's a parent sampling decision, the parent sampling decision will be used.
127+
4. If <PlatformIdentifier name="traces-sampler" /> is not defined and there's no parent sampling decision, <PlatformIdentifier name="traces-sample-rate" /> will be used.

docs/platforms/python/profiling/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ sentry_sdk.init(
115115
# To set a uniform sample rate
116116
# Set profiles_sample_rate to 1.0 to profile 100%
117117
# of sampled transactions.
118-
# We recommend adjusting this value in production,
118+
# We recommend adjusting this value in production.
119119
profiles_sample_rate=1.0,
120120
# Alternatively, to control sampling dynamically
121121
profiles_sampler=profiles_sampler
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
```python
2-
sentry_sdk.start_transaction(
3-
# kwargs passed to Transaction constructor - will be recorded on transaction
2+
sentry_sdk.start_span(
43
name="GET /search",
54
op="search",
65
data={
7-
"query_params": {
8-
"animal": "dog",
9-
"type": "very good"
10-
}
6+
"query_params": {
7+
"animal": "dog",
8+
"type": "very good"
9+
}
1110
},
12-
# `custom_sampling_context` - won't be recorded
13-
custom_sampling_context={
14-
# PII
11+
# Attributes defined at root span (transaction) start will be accessible in traces_sampler
12+
# and they will be sent to Sentry unless filtered out manually.
13+
# Note that these can only be primitive types or a list of a single primitive
14+
# type.
15+
attributes={
1516
"user_id": "12312012",
16-
# too big to send
17-
"search_results": { ... }
17+
"foo": "bar",
1818
}
19-
);
19+
)
2020
```
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
For Python-based SDKs, it includes at least the following:
1+
For the Python SDK, it includes at least the following:
22

33
```python
44
{
5-
"transaction_context": {
6-
"name": <string> # transaction title at creation time
7-
"op": <string> # short description of transaction type, like "http.request"
8-
},
9-
"parent_sampled": <bool> # if this transaction has a parent, its sampling decision
10-
... # custom context as passed to `start_transaction`
5+
"transaction_context": {
6+
"name": <string> # transaction title at creation time
7+
"op": <string> # short description of transaction type, like "http.request"
8+
},
9+
"parent_sampled": <bool> # if this transaction has a parent, its sampling decision
10+
... # other attributes as passed to `start_span`
1111
}
1212
```

platform-includes/performance/default-sampling-context/python.aiohttp.mdx

Lines changed: 0 additions & 9 deletions
This file was deleted.

platform-includes/performance/default-sampling-context/python.asgi.mdx

Lines changed: 0 additions & 9 deletions
This file was deleted.

platform-includes/performance/default-sampling-context/python.aws-lambda.mdx

Lines changed: 0 additions & 10 deletions
This file was deleted.

platform-includes/performance/default-sampling-context/python.bottle.mdx

Lines changed: 0 additions & 12 deletions
This file was deleted.

platform-includes/performance/default-sampling-context/python.celery.mdx

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)