Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic error sampling Python #8314

Merged
merged 6 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/platform-includes/configuration/error-sampler/python.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
```python
import sentry_sdk

def my_sampler(_, hint):
exception_sampler_values = {
MyException: 0.5,
MyIgnoredException: 0.0, # or equivalently, False
}

try:
return exception_sampler_values[hint["exc_info"][0]]
except (IndexError, KeyError, TypeError):
return 1.0 # or equivalently, True

sentry_sdk.init(
# ...

error_sampler=my_sampler,
)
```
8 changes: 8 additions & 0 deletions src/platforms/common/configuration/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ Configures the sample rate for error events, in the range of `0.0` to `1.0`. The

</ConfigKey>

<ConfigKey name="error-sampler" supported={["python"]}>

Dynamically configures the sample rate for error events on a per-event basis. This configuration option accepts a function, which takes two parameters (the `event` and the `hint`), and which returns a boolean (indicating whether the event should be sent to Sentry) or a floating-point number between 0.0 and 1.0, inclusive (where the number indicates the probability the event is sent to Sentry; the SDK will randomly decide whether to send the event with the given probability).

If this configuration option is specified, the <PlatformIdentifier name="sample-rate" /> option is ignored.

</ConfigKey>

<ConfigKey name="max-breadcrumbs">

This variable controls the total amount of breadcrumbs that should be captured. This defaults to `100`, but you can set this to any number. However, you should be aware that Sentry has a [maximum payload size](https://develop.sentry.dev/sdk/envelopes/#size-limits) and any events exceeding that payload size will be dropped.
Expand Down
27 changes: 27 additions & 0 deletions src/platforms/common/configuration/sampling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ Changing the error sample rate requires re-deployment. In addition, setting an S

</Note>

<PlatformSection supported={["python"]}>

### Dynamically Sampling Error Events

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" />, which it uses to inform the sampling decision.

<Alert level="warning">

Your <PlatformIdentifier name="error-sampler" /> function **must return a valid value**. A valid value is either:

- A **floating-point number** between `0.0` and `1.0` (inclusive) indicating the probability an error gets sampled, **or**
- A **boolean** indicating whether or not to sample the error.

</Alert>

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:

<PlatformContent includePath="configuration/error-sampler" />

<Alert>

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.

</Alert>

</PlatformSection>

<PlatformSection notSupported={["elixir", "javascript.electron", "perl"]}>

## Sampling Transaction Events
Expand Down
Loading