From 2796cef9a10082d84ca38d3f11320ef9be0fd2cf Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Fri, 27 Oct 2023 10:52:49 +0200 Subject: [PATCH] Dynamic error sampling Python (#8314) * Dynamic error sampling Python * Apply suggestions from code review Co-authored-by: Shana Matthews * Remove extra closing parenthesis * Added error-sampler to basic options * Bullet points for valid values * [getsentry/action-github-commit] Auto commit --------- Co-authored-by: Shana Matthews Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com> --- .../configuration/error-sampler/python.mdx | 20 ++++++++++++++ .../common/configuration/options.mdx | 8 ++++++ .../common/configuration/sampling.mdx | 27 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 src/platform-includes/configuration/error-sampler/python.mdx diff --git a/src/platform-includes/configuration/error-sampler/python.mdx b/src/platform-includes/configuration/error-sampler/python.mdx new file mode 100644 index 0000000000000..a327946cab907 --- /dev/null +++ b/src/platform-includes/configuration/error-sampler/python.mdx @@ -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, +) +``` diff --git a/src/platforms/common/configuration/options.mdx b/src/platforms/common/configuration/options.mdx index 834143766dbb1..503bd3c76a2cf 100644 --- a/src/platforms/common/configuration/options.mdx +++ b/src/platforms/common/configuration/options.mdx @@ -192,6 +192,14 @@ Configures the sample rate for error events, in the range of `0.0` to `1.0`. The + + +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 option is ignored. + + + 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. diff --git a/src/platforms/common/configuration/sampling.mdx b/src/platforms/common/configuration/sampling.mdx index e06fbf539d7a8..80e5b9db3fb27 100644 --- a/src/platforms/common/configuration/sampling.mdx +++ b/src/platforms/common/configuration/sampling.mdx @@ -41,6 +41,33 @@ Changing the error sample rate requires re-deployment. In addition, setting an S + + +### 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. + + + +Your 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. + + + +One potential use case for the 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: + + + + + +You can define at most one of the and the . If both are set, the will control sampling, and the will be ignored. + + + + + ## Sampling Transaction Events