11import contextlib
22import inspect
3+ import math
34import os
45import re
56import sys
67from collections .abc import Mapping
78from datetime import timedelta
8- from decimal import ROUND_DOWN , Decimal , DefaultContext , localcontext
9+
910from functools import wraps
1011from random import Random
1112from 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
884879def _sample_rand_range (parent_sampled , sample_rate ):
0 commit comments