Skip to content

Fixed local var not present when error in users error_sampler function #2511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 13, 2023
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