Skip to content

Make sure to use the default decimal context in our code#4231

Merged
antonpirker merged 3 commits into
masterfrom
antonpirker/fix/modified-decimal-context
Apr 23, 2025
Merged

Make sure to use the default decimal context in our code#4231
antonpirker merged 3 commits into
masterfrom
antonpirker/fix/modified-decimal-context

Conversation

@antonpirker

Copy link
Copy Markdown
Contributor

Fixes #4213

@antonpirker
antonpirker requested a review from a team as a code owner April 2, 2025 10:52
@codecov

codecov Bot commented Apr 2, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.32%. Comparing base (2c3776c) to head (dfb228e).
Report is 1 commits behind head on master.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4231   +/-   ##
=======================================
  Coverage   80.31%   80.32%           
=======================================
  Files         142      142           
  Lines       15930    15933    +3     
  Branches     2725     2725           
=======================================
+ Hits        12794    12798    +4     
  Misses       2262     2262           
+ Partials      874      873    -1     
Files with missing lines Coverage Δ
sentry_sdk/tracing.py 78.48% <100.00%> (+0.04%) ⬆️
sentry_sdk/tracing_utils.py 85.57% <100.00%> (+0.06%) ⬆️

... and 1 file with indirect coverage changes

Comment thread sentry_sdk/tracing.py
# to a float (True becomes 1.0 and False becomes 0.0)
self.sampled = self._sample_rand < self.sample_rate
# Now we roll the dice.
self.sampled = self._sample_rand < Decimal.from_float(self.sample_rate)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we need this? Just wondering if this can also potentially fail due to unforeseen circumstances 🙃

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

when people set the FloatOperation trap (like the user in the linked issue did) then the old comparison fails, because then comparing Decimals to floats is not allowed anymore. I guess we also could add this around the comparison:

 with localcontext(DefaultContext) as ctx:
     ctx.prec = 6
     self.sampled = self._sample_rand < self.sample_rate

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't really have a preference, but maybe wrapping stuff in a localcontext might be good, just to possibly avoid other unforeseen errors? I'm kinda afraid of the whole decimal API now so I'd make this as defensive as possible.

@szokeasaurusrex szokeasaurusrex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I added a small suggestion inline, but wanted to second what @sentrivana said about this whole Decimal API making me uneasy.

When I implemented sample_rand using Decimal, I thought it would make our lives easier by making it simple to ensure the correct precision for sample_rand.

However, now that multiple issues have come up related to using Decimal, I think it might have added more complexity than I realized, and perhaps we should consider revisiting the decision to use Decimal altogether (especially if another related issue comes up)

Decimal("0.000001"), rounding=ROUND_DOWN, context=Context(prec=6)
)
# in case the user has changed the default precision or set traps.
with localcontext(DefaultContext) as ctx:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Small readability nit, I would import decimal then change this to

Suggested change
with localcontext(DefaultContext) as ctx:
with decimal.localcontext(DefaultContext) as ctx:

Otherwise, from just reading this code, it is unclear that the localcontext is a decimal-related thing

@antonpirker

Copy link
Copy Markdown
Contributor Author

We will remove the Decimal code in a separate PR, so this fix is not needed anymore.

@antonpirker
antonpirker merged commit bbb41a3 into master Apr 23, 2025
@antonpirker
antonpirker deleted the antonpirker/fix/modified-decimal-context branch April 23, 2025 13:06
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.

Custom settings for decimal context broke the app

3 participants