Sampling for Python
Learn how to configure the volume of error events and traces sent to Sentry using the Python SDK.
Adding Sentry to your app gives you a great deal of very 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.
This page describes sampling in version 3.x of the Python SDK. For 2.x, switch to 2.x in the version selector.
To send a representative sample of your errors to Sentry, set the sample_rate option in your SDK configuration to a number between 0 (0% of errors sent) and 1 (100% of errors sent). This is a static rate, which will apply equally to all errors. For example, to sample 25% of your errors:
import sentry_sdk
sentry_sdk.init(
# ...
sample_rate=0.25,
)
import sentry_sdk
sentry_sdk.init(
# ...
sample_rate=0.25,
)
The error sample rate defaults to 1.0, meaning all errors are sent to Sentry.
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 for your project (which only drops events when volume is high) may better suit your needs.
To sample error events dynamically, set the error_sampler to a function that returns the desired sample rate for the event. The error_sampler takes two arguments, event and hint. event is the Event that will be sent to Sentry, hint includes Python's sys.exc_info() information in hint["exc_info"].
Your error_sampler function must return a valid value. A valid value is either:
- A floating-point number between
0.0and1.0(inclusive) indicating the probability an error gets sampled, or - A boolean indicating whether or not to sample the error.
One potential use case for the 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:
import sentry_sdk
from sentry_sdk.types import Event, Hint
def error_sampler(event: Event, hint: Hint) -> float:
error_class = hint["exc_info"][0]
if error_class == MyException:
return 0.5
elif error_class == MyIgnoredException:
return 0
# All the other errors
return 1.0
sentry_sdk.init(
# ...
error_sampler=error_sampler,
)
import sentry_sdk
from sentry_sdk.types import Event, Hint
def error_sampler(event: Event, hint: Hint) -> float:
error_class = hint["exc_info"][0]
if error_class == MyException:
return 0.5
elif error_class == MyIgnoredException:
return 0
# All the other errors
return 1.0
sentry_sdk.init(
# ...
error_sampler=error_sampler,
)
You can define at most one of the error_sampler and the sample_rate. If both are set, the error_sampler will control sampling, and the sample_rate will be ignored.
We recommend sampling your traces for two reasons:
- 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.
- Enabling sampling allows you to better manage the number of spans sent to Sentry, so you can tailor your volume to your organization's needs.
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.
The SDK makes the sampling decision when a service span starts, and child spans and downstream services inherit it. There are two options to control it:
- A uniform sample rate (
traces_sample_rate), which samples the same share of traces everywhere in your app. - A sampling function (
traces_sampler), which lets you sample different service spans at different rates, filter some out entirely, and decide whether to inherit an incoming sampling decision.
By default, neither option is set, meaning no spans will be sent to Sentry. For example, to sample 25% of your traces:
sentry_sdk.init(
# ...
traces_sample_rate=0.25,
)
sentry_sdk.init(
# ...
traces_sample_rate=0.25,
)
See Configure Sampling for how to write a sampling function, what's in the sampling context, how to add custom sampling context, and how sampling decisions are inherited across distributed traces.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").