Skip to content

Commit

Permalink
Fixed local var not present when error in users error_sampler function (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
antonpirker authored Dec 13, 2023
1 parent 4deaa38 commit 4731312
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4731312

Please sign in to comment.