Skip to content

Commit

Permalink
[Python] Improved code snippet for error_sampler (#11314)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: vivianyentran <[email protected]>
  • Loading branch information
antonpirker and vivianyentran committed Sep 11, 2024
1 parent 014af4f commit b916cd0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
10 changes: 3 additions & 7 deletions docs/platforms/python/configuration/sampling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ To send a representative sample of your errors to Sentry, set the <PlatformIdent

The error sample rate defaults to `1.0`, meaning all errors are sent to Sentry.

<Note>

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.

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

### 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.
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"]`.

<Alert level="warning">
<Alert>

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

Expand Down
23 changes: 13 additions & 10 deletions platform-includes/configuration/error-sampler/python.mdx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
```python
import sentry_sdk
from sentry_sdk.types import Event, Hint

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
def my_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=my_sampler,
error_sampler=my_error_sampler,
)
```

0 comments on commit b916cd0

Please sign in to comment.