feat: Allow overriding the sampling rate. - #1284
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit fe9a608. Configure here.
Allow for a user defined function for setting the sampling rate. Similar to Python SDK https://docs.sentry.io/platforms/python/sampling/#dynamically-sampling-error-events
fe9a608 to
dc3d1ef
Compare
| .as_ref() | ||
| .and_then(|f| f(&event)) | ||
| .unwrap_or_else(|| event_sample_rate(&self.options.event_sampling_strategy)); | ||
|
|
||
| if !self.sample_should_send(sampling_rate) { | ||
| self.record_lost_event(ClientReportReason::SampleRate); | ||
| None |
There was a problem hiding this comment.
Bug: The override_sampling_rate callback lacks validation for NaN values, causing sample_should_send to always return false and silently drop all events.
Severity: MEDIUM
Suggested Fix
Validate the f32 value returned from the override_sampling_rate callback. One option is to panic if the value is invalid (e.g., NaN, not in the [0.0, 1.0] range), which would be consistent with other sampling rate setters like sample_rate(). Alternatively, clamp invalid values to a safe default (like 0.0) and document the behavior.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: sentry-core/src/client/mod.rs#L403-L409
Potential issue: The `override_sampling_rate` callback does not validate the `f32` value
it returns. If a user's callback returns a `NaN` value (e.g., through a division by
zero), this value is passed to the `sample_should_send` function. Due to IEEE 754
floating-point semantics, all comparisons with `NaN` (`>= 1.0`, `<= 0.0`, and `< rate`)
evaluate to `false`. This causes `sample_should_send` to always return `false`,
resulting in all events being silently dropped. This behavior is inconsistent with other
sampling rate setters in the SDK which panic on invalid values.
Did we get this right? 👍 / 👎 to inform future reviews.

Allow for a user defined function for setting the sampling rate. Similar to Python SDK https://docs.sentry.io/platforms/python/sampling/#dynamically-sampling-error-events
Description
Allow defining a function that overrides the sampling rate. For example you have a sampling rate set below 100% but you have certain events that should always be 100%. This allows for a user defined function to provide a sample rate override otherwise falls back to the defined sampling rate.
Issues
#1127