From b916cd081eeb9d8ddfb25243b229ebd38746b479 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Wed, 11 Sep 2024 11:13:23 +0200 Subject: [PATCH] [Python] Improved code snippet for `error_sampler` (#11314) --------- Co-authored-by: vivianyentran <20403606+vivianyentran@users.noreply.github.com> --- .../python/configuration/sampling.mdx | 10 +++----- .../configuration/error-sampler/python.mdx | 23 +++++++++++-------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/docs/platforms/python/configuration/sampling.mdx b/docs/platforms/python/configuration/sampling.mdx index b9313323be6ce..b1846ad87163d 100644 --- a/docs/platforms/python/configuration/sampling.mdx +++ b/docs/platforms/python/configuration/sampling.mdx @@ -14,17 +14,13 @@ To send a representative sample of your errors to Sentry, set the - -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. - - +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 to a function that returns the desired sample rate for the event. The takes two arguments, and , which it uses to inform the sampling decision. +To sample error events dynamically, set the to a function that returns the desired sample rate for the event. The takes two arguments, and . `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"]`. - + Your function **must return a valid value**. A valid value is either: diff --git a/platform-includes/configuration/error-sampler/python.mdx b/platform-includes/configuration/error-sampler/python.mdx index a327946cab907..9172030f400dc 100644 --- a/platform-includes/configuration/error-sampler/python.mdx +++ b/platform-includes/configuration/error-sampler/python.mdx @@ -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, ) ```