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 1 commit
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,
)
```
24 changes: 24 additions & 0 deletions src/platforms/common/configuration/sampling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ Changing the error sample rate requires re-deployment. In addition, setting an S

</Note>

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

### Dynamically Sampling Error Events

It is also possible to sample error events dymanically, by setting the <PlatformIdentifier name="error-sampler" /> to a function that returns the desired sample rate for the event. The <PlatformIdentifier name="error-sampler" /> receives two arguments, the <PlatformIdentifier name="event" /> and the <PlatformIdentifier name="hint" />, when called, which it can inspect in order to inform the sampling decision.
szokeasaurusrex marked this conversation as resolved.
Show resolved Hide resolved

<Alert level="warning">

The <PlatformIdentifier name="error-sampler" /> function is responsible for returning a valid value. The only valid values are **floating-point numbers** between `0.0` and `1.0`, inclusive (to indicate the probability that an error gets sampled), and **booleans** (to indicate whether or not to sample the error).
szokeasaurusrex marked this conversation as resolved.
Show resolved Hide resolved

</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