Skip to content

Use float for sample rand #4677

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

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft

Use float for sample rand #4677

wants to merge 18 commits into from

Conversation

sentrivana
Copy link
Contributor

@sentrivana sentrivana commented Aug 6, 2025

Closes #4270

Copy link

codecov bot commented Aug 6, 2025

Codecov Report

❌ Patch coverage is 83.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.05%. Comparing base (46eb82c) to head (e46a07e).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
sentry_sdk/utils.py 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4677      +/-   ##
==========================================
- Coverage   85.09%   85.05%   -0.04%     
==========================================
  Files         156      156              
  Lines       15869    15867       -2     
  Branches     2689     2689              
==========================================
- Hits        13503    13495       -8     
- Misses       1585     1587       +2     
- Partials      781      785       +4     
Files with missing lines Coverage Δ
sentry_sdk/integrations/asgi.py 90.71% <100.00%> (+0.06%) ⬆️
sentry_sdk/tracing.py 85.77% <100.00%> (-0.45%) ⬇️
sentry_sdk/tracing_utils.py 87.40% <100.00%> (-0.87%) ⬇️
sentry_sdk/utils.py 88.32% <50.00%> (-0.20%) ⬇️

... and 1 file with indirect coverage changes

@sentrivana sentrivana changed the title Remove Decimal Use float for sample rand Aug 6, 2025
@sentrivana sentrivana marked this pull request as ready for review August 7, 2025 11:14
@sentrivana sentrivana requested a review from a team as a code owner August 7, 2025 11:14
@sentrivana sentrivana changed the base branch from ivana/random-perf-improvements to master August 7, 2025 14:30
@sentrivana sentrivana marked this pull request as draft August 12, 2025 11:31
)
sample_rand_scaled = rng.randrange(int(lower * 1000000), int(upper * 1000000))

return sample_rand_scaled / 1000000
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Sampling Context Propagation Fails for Tiny Rates

The _generate_sample_rand function's integer scaling for randrange arguments (int(value * 1e6)) can cause the start and stop values to become equal for very narrow intervals (less than 1e-6). This leads randrange to raise a ValueError ("empty range"). This error is caught in PropagationContext._fill_sample_rand, which then silently skips setting the sample_rand. As a result, dynamic sampling context propagation fails for traces with small sample_rates (e.g., when parent_sampled=True with a tiny sample_rate), leading to inconsistent sampling decisions across services.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move away from Decimal
1 participant