Skip to content

Commit 3246d38

Browse files
committed
move rounding to separate helper func
1 parent 6abb38c commit 3246d38

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

sentry_sdk/integrations/opentelemetry/sampler.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sentry_sdk
99
from sentry_sdk.tracing_utils import (
1010
_generate_sample_rand,
11+
_round_sample_rand,
1112
has_tracing_enabled,
1213
)
1314
from sentry_sdk.utils import is_valid_sample_rate, logger
@@ -86,10 +87,7 @@ def get_parent_sample_rand(parent_context, trace_id):
8687
if parent_sample_rand is None:
8788
return None
8889

89-
try:
90-
return Decimal(parent_sample_rand)
91-
except Exception:
92-
return None
90+
return _round_sample_rand(parent_sample_rand)
9391

9492
return None
9593

@@ -119,7 +117,9 @@ def dropped_result(parent_span_context, attributes, sample_rate=None, sample_ran
119117
trace_state = trace_state.update(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate))
120118

121119
if sample_rand is not None:
122-
trace_state = trace_state.update(TRACESTATE_SAMPLE_RAND_KEY, str(sample_rand))
120+
trace_state = trace_state.update(
121+
TRACESTATE_SAMPLE_RAND_KEY, f"{sample_rand:.6f}" # noqa: E231
122+
)
123123

124124
is_root_span = not (
125125
parent_span_context.is_valid and not parent_span_context.is_remote
@@ -169,7 +169,9 @@ def sampled_result(span_context, attributes, sample_rate=None, sample_rand=None)
169169
trace_state = trace_state.update(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate))
170170

171171
if sample_rand is not None:
172-
trace_state = trace_state.update(TRACESTATE_SAMPLE_RAND_KEY, str(sample_rand))
172+
trace_state = trace_state.update(
173+
TRACESTATE_SAMPLE_RAND_KEY, f"{sample_rand:.6f}" # noqa: E231
174+
)
173175

174176
return SamplingResult(
175177
Decision.RECORD_AND_SAMPLE,

sentry_sdk/tracing_utils.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -783,12 +783,7 @@ def _generate_sample_rand(
783783
while sample_rand >= upper:
784784
sample_rand = rng.uniform(lower, upper)
785785

786-
# Round down to exactly six decimal-digit precision.
787-
# Setting the context is needed to avoid an InvalidOperation exception
788-
# in case the user has changed the default precision.
789-
return Decimal(sample_rand).quantize(
790-
Decimal("0.000001"), rounding=ROUND_DOWN, context=Context(prec=6)
791-
)
786+
return _round_sample_rand(sample_rand)
792787

793788

794789
def _sample_rand_range(parent_sampled, sample_rate):
@@ -806,6 +801,21 @@ def _sample_rand_range(parent_sampled, sample_rate):
806801
return sample_rate, 1.0
807802

808803

804+
def _round_sample_rand(sample_rand):
805+
# type: (Decimal) -> Optional[Decimal]
806+
# Round down to exactly six decimal-digit precision.
807+
# Setting the context is needed to avoid an InvalidOperation exception
808+
# in case the user has changed the default precision.
809+
try:
810+
return Decimal(sample_rand).quantize(
811+
Decimal("0.000001"), rounding=ROUND_DOWN, context=Context(prec=6)
812+
)
813+
except Exception as ex:
814+
logger.debug(f"Failed to round sample_rand {sample_rand}: {ex}")
815+
816+
return None
817+
818+
809819
# Circular imports
810820
from sentry_sdk.tracing import (
811821
BAGGAGE_HEADER_NAME,

tests/integrations/opentelemetry/test_propagator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import decimal
21
from unittest.mock import MagicMock, patch
32

43
import pytest

0 commit comments

Comments
 (0)