Skip to content
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

is_valid_sample_rate may be too strict #1386

Closed
timharsch opened this issue Apr 5, 2022 · 4 comments
Closed

is_valid_sample_rate may be too strict #1386

timharsch opened this issue Apr 5, 2022 · 4 comments
Labels
Good first issue Good for newcomers Type: Bug Something isn't working

Comments

@timharsch
Copy link

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.5.6

Steps to Reproduce

One day sentry stopped sending transactions to sentry.io. After much digging I found the issue to be here:

if not is_valid_sample_rate(sample_rate):

We have a custom traces_sampler function that reads in from a simple JSON file and sets the sample rate based on the endpoint. An example of our JSON:

{
  "^$": 0.05,
  "default_sample_rate": 1.0
}

We limit our root path /, because it gets polled by our load balancer once a minute. And the endpoint is blindingly simple and uninteresting, so we don't want to track all events and blow through our quota.

The problem came about when I switch from the json package to parse the json, to the json_encoder package, which generally is more robust json parser. The issue is that the package json will create a type of float when it deserializes the json, json_encoder will create a type of Decimal

Expected Result

  • sample_rate with type Decimal should be sufficient for a return type of the traces_sampler function
  • sentry documentation should state the exact allowable types for the traces_sampler function

Actual Result

transactions will be skipped if the type returned by traces_sampler function is not of type float.

@antonpirker antonpirker added Type: Bug Something isn't working Status: Backlog Good first issue Good for newcomers and removed Status: Untriaged labels Apr 6, 2022
@antonpirker
Copy link
Member

Hey @timharsch !

Thanks for writing in. A really nice solution you have there for having dynamic sample rates for your endpoints.

I think there would be an easy fix by just adding a check for decimal here:
https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/tracing_utils.py#L183

You can supply a pull request with a fix, if you need this change fast.

I have put this in the internal backlog at low priority so it will take quite some time until we can tackle this, because we have a lot on our plates.

@timharsch
Copy link
Author

Hmm.. not sure why you are suggesting a change in the record_sql_queries function. It doesn't seem to relate.

I believe the fix should be to go from this code on line 132

if not isinstance(rate, Real) or math.isnan(rate):

if not isinstance(rate, Real) or math.isnan(rate):
to
if not isinstance(rate, Real) or not isinstance(rate, Decimal) or math.isnan(rate):

The float cast on line 141 should cast the Decimal value to float.

Arvind2222 added a commit to Arvind2222/sentry-python that referenced this issue Oct 11, 2022
* Fix is_valid_sample_rate to allow check on `Decimal`
@Arvind2222
Copy link
Contributor

@timharsch - Can you please review - #1672

@sl0thentr0py
Copy link
Member

fixed by #1672

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good first issue Good for newcomers Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants