From 47313123d8c9b1dadce5460168d2ed849ee5730a Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Wed, 13 Dec 2023 12:39:23 +0100 Subject: [PATCH] Fixed local var not present when error in users error_sampler function (#2511) * Fixed local variable not present when error in users error_sampler function * Handling errors raised by error_sampler the same way as invalid sample rates --- sentry_sdk/client.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/client.py b/sentry_sdk/client.py index 8aad751470..aeaa8fa518 100644 --- a/sentry_sdk/client.py +++ b/sentry_sdk/client.py @@ -466,20 +466,28 @@ def _should_sample_error( hint, # type: Hint ): # type: (...) -> bool - sampler = self.options.get("error_sampler", None) + error_sampler = self.options.get("error_sampler", None) - if callable(sampler): + if callable(error_sampler): with capture_internal_exceptions(): - sample_rate = sampler(event, hint) + sample_rate = error_sampler(event, hint) else: sample_rate = self.options["sample_rate"] try: not_in_sample_rate = sample_rate < 1.0 and random.random() >= sample_rate + except NameError: + logger.warning( + "The provided error_sampler raised an error. Defaulting to sampling the event." + ) + + # If the error_sampler raised an error, we should sample the event, since the default behavior + # (when no sample_rate or error_sampler is provided) is to sample all events. + not_in_sample_rate = False except TypeError: parameter, verb = ( ("error_sampler", "returned") - if callable(sampler) + if callable(error_sampler) else ("sample_rate", "contains") ) logger.warning(