Skip to content

Commit b916cd0

Browse files
[Python] Improved code snippet for error_sampler (#11314)
--------- Co-authored-by: vivianyentran <[email protected]>
1 parent 014af4f commit b916cd0

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

docs/platforms/python/configuration/sampling.mdx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@ To send a representative sample of your errors to Sentry, set the <PlatformIdent
1414

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

17-
<Note>
18-
19-
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.
20-
21-
</Note>
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.
2218

2319
### Dynamically Sampling Error Events
2420

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

27-
<Alert level="warning">
23+
<Alert>
2824

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
```python
22
import sentry_sdk
3+
from sentry_sdk.types import Event, Hint
34

4-
def my_sampler(_, hint):
5-
exception_sampler_values = {
6-
MyException: 0.5,
7-
MyIgnoredException: 0.0, # or equivalently, False
8-
}
95

10-
try:
11-
return exception_sampler_values[hint["exc_info"][0]]
12-
except (IndexError, KeyError, TypeError):
13-
return 1.0 # or equivalently, True
6+
def my_error_sampler(event: Event, hint: Hint) -> float:
7+
error_class = hint["exc_info"][0]
8+
9+
if error_class == MyException:
10+
return 0.5
11+
elif error_class == MyIgnoredException:
12+
return 0
13+
14+
# All the other errors
15+
return 1.0
16+
1417

1518
sentry_sdk.init(
1619
# ...
1720

18-
error_sampler=my_sampler,
21+
error_sampler=my_error_sampler,
1922
)
2023
```

0 commit comments

Comments
 (0)