Skip to content

Commit bbb41a3

Browse files
authored
Make sure to use the default decimal context in our code (#4231)
Fixes #4213
1 parent 2c3776c commit bbb41a3

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

sentry_sdk/tracing.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from decimal import Decimal
12
import uuid
23
import warnings
34
from datetime import datetime, timedelta, timezone
@@ -1198,10 +1199,8 @@ def _set_initial_sampling_decision(self, sampling_context):
11981199
self.sampled = False
11991200
return
12001201

1201-
# Now we roll the dice. self._sample_rand is inclusive of 0, but not of 1,
1202-
# so strict < is safe here. In case sample_rate is a boolean, cast it
1203-
# to a float (True becomes 1.0 and False becomes 0.0)
1204-
self.sampled = self._sample_rand < self.sample_rate
1202+
# Now we roll the dice.
1203+
self.sampled = self._sample_rand < Decimal.from_float(self.sample_rate)
12051204

12061205
if self.sampled:
12071206
logger.debug(

sentry_sdk/tracing_utils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
from collections.abc import Mapping
77
from datetime import timedelta
8-
from decimal import ROUND_DOWN, Context, Decimal
8+
from decimal import ROUND_DOWN, Decimal, DefaultContext, localcontext
99
from functools import wraps
1010
from random import Random
1111
from urllib.parse import quote, unquote
@@ -872,10 +872,13 @@ def _generate_sample_rand(
872872

873873
# Round down to exactly six decimal-digit precision.
874874
# Setting the context is needed to avoid an InvalidOperation exception
875-
# in case the user has changed the default precision.
876-
return Decimal(sample_rand).quantize(
877-
Decimal("0.000001"), rounding=ROUND_DOWN, context=Context(prec=6)
878-
)
875+
# in case the user has changed the default precision or set traps.
876+
with localcontext(DefaultContext) as ctx:
877+
ctx.prec = 6
878+
return Decimal(sample_rand).quantize(
879+
Decimal("0.000001"),
880+
rounding=ROUND_DOWN,
881+
)
879882

880883

881884
def _sample_rand_range(parent_sampled, sample_rate):

tests/tracing/test_sample_rand.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import decimal
2+
from decimal import Inexact, FloatOperation
23
from unittest import mock
34

45
import pytest
@@ -58,14 +59,19 @@ def test_transaction_uses_incoming_sample_rand(
5859

5960
def test_decimal_context(sentry_init, capture_events):
6061
"""
61-
Ensure that having a decimal context with a precision below 6
62+
Ensure that having a user altered decimal context with a precision below 6
6263
does not cause an InvalidOperation exception.
6364
"""
6465
sentry_init(traces_sample_rate=1.0)
6566
events = capture_events()
6667

6768
old_prec = decimal.getcontext().prec
69+
old_inexact = decimal.getcontext().traps[Inexact]
70+
old_float_operation = decimal.getcontext().traps[FloatOperation]
71+
6872
decimal.getcontext().prec = 2
73+
decimal.getcontext().traps[Inexact] = True
74+
decimal.getcontext().traps[FloatOperation] = True
6975

7076
try:
7177
with mock.patch(
@@ -77,5 +83,7 @@ def test_decimal_context(sentry_init, capture_events):
7783
)
7884
finally:
7985
decimal.getcontext().prec = old_prec
86+
decimal.getcontext().traps[Inexact] = old_inexact
87+
decimal.getcontext().traps[FloatOperation] = old_float_operation
8088

8189
assert len(events) == 1

0 commit comments

Comments
 (0)