Skip to content

Commit 4731312

Browse files
authored
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
1 parent 4deaa38 commit 4731312

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

sentry_sdk/client.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,20 +466,28 @@ def _should_sample_error(
466466
hint, # type: Hint
467467
):
468468
# type: (...) -> bool
469-
sampler = self.options.get("error_sampler", None)
469+
error_sampler = self.options.get("error_sampler", None)
470470

471-
if callable(sampler):
471+
if callable(error_sampler):
472472
with capture_internal_exceptions():
473-
sample_rate = sampler(event, hint)
473+
sample_rate = error_sampler(event, hint)
474474
else:
475475
sample_rate = self.options["sample_rate"]
476476

477477
try:
478478
not_in_sample_rate = sample_rate < 1.0 and random.random() >= sample_rate
479+
except NameError:
480+
logger.warning(
481+
"The provided error_sampler raised an error. Defaulting to sampling the event."
482+
)
483+
484+
# If the error_sampler raised an error, we should sample the event, since the default behavior
485+
# (when no sample_rate or error_sampler is provided) is to sample all events.
486+
not_in_sample_rate = False
479487
except TypeError:
480488
parameter, verb = (
481489
("error_sampler", "returned")
482-
if callable(sampler)
490+
if callable(error_sampler)
483491
else ("sample_rate", "contains")
484492
)
485493
logger.warning(

0 commit comments

Comments
 (0)