Skip to content

Commit 030423c

Browse files
committed
Remove Decimal
1 parent 155e9aa commit 030423c

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

sentry_sdk/tracing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from decimal import Decimal
21
import uuid
32
import warnings
43
from datetime import datetime, timedelta, timezone
@@ -1223,7 +1222,7 @@ def _set_initial_sampling_decision(self, sampling_context):
12231222
return
12241223

12251224
# Now we roll the dice.
1226-
self.sampled = self._sample_rand < Decimal.from_float(self.sample_rate)
1225+
self.sampled = self._sample_rand < self.sample_rate
12271226

12281227
if self.sampled:
12291228
logger.debug(

sentry_sdk/tracing_utils.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import contextlib
22
import inspect
3+
import math
34
import os
45
import re
56
import sys
67
from collections.abc import Mapping
78
from datetime import timedelta
8-
from decimal import ROUND_DOWN, Decimal, DefaultContext, localcontext
9+
910
from functools import wraps
1011
from random import Random
1112
from urllib.parse import quote, unquote
@@ -501,7 +502,7 @@ def _fill_sample_rand(self):
501502
return
502503

503504
sample_rand = try_convert(
504-
Decimal, self.dynamic_sampling_context.get("sample_rand")
505+
float, self.dynamic_sampling_context.get("sample_rand")
505506
)
506507
if sample_rand is not None and 0 <= sample_rand < 1:
507508
# sample_rand is present and valid, so don't overwrite it
@@ -649,7 +650,7 @@ def populate_from_transaction(cls, transaction):
649650
options = client.options or {}
650651

651652
sentry_items["trace_id"] = transaction.trace_id
652-
sentry_items["sample_rand"] = str(transaction._sample_rand)
653+
sentry_items["sample_rand"] = f"{transaction._sample_rand:.6f}" # noqa: E231
653654

654655
if options.get("environment"):
655656
sentry_items["environment"] = options["environment"]
@@ -723,15 +724,15 @@ def strip_sentry_baggage(header):
723724
)
724725

725726
def _sample_rand(self):
726-
# type: () -> Optional[Decimal]
727+
# type: () -> Optional[float]
727728
"""Convenience method to get the sample_rand value from the sentry_items.
728729
729-
We validate the value and parse it as a Decimal before returning it. The value is considered
730-
valid if it is a Decimal in the range [0, 1).
730+
We validate the value and parse it as a float before returning it. The value is considered
731+
valid if it is a float in the range [0, 1).
731732
"""
732-
sample_rand = try_convert(Decimal, self.sentry_items.get("sample_rand"))
733+
sample_rand = try_convert(float, self.sentry_items.get("sample_rand"))
733734

734-
if sample_rand is not None and Decimal(0) <= sample_rand < Decimal(1):
735+
if sample_rand is not None and 0.0 <= sample_rand < 1.0:
735736
return sample_rand
736737

737738
return None
@@ -851,7 +852,7 @@ def _generate_sample_rand(
851852
*,
852853
interval=(0.0, 1.0), # type: tuple[float, float]
853854
):
854-
# type: (...) -> Decimal
855+
# type: (...) -> float
855856
"""Generate a sample_rand value from a trace ID.
856857
857858
The generated value will be pseudorandomly chosen from the provided
@@ -871,14 +872,8 @@ def _generate_sample_rand(
871872
sample_rand = rng.uniform(lower, upper)
872873

873874
# Round down to exactly six decimal-digit precision.
874-
# Setting the context is needed to avoid an InvalidOperation exception
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-
)
875+
# Using floor to ensure we always round down, similar to ROUND_DOWN
876+
return math.floor(sample_rand * 1000000) / 1000000
882877

883878

884879
def _sample_rand_range(parent_sampled, sample_rate):

0 commit comments

Comments
 (0)