Skip to content

feat: Allow overriding the sampling rate. - #1284

Open
darrell-roberts wants to merge 1 commit into
getsentry:masterfrom
darrell-roberts:darrell/push-nnsnopqlnrqq
Open

feat: Allow overriding the sampling rate.#1284
darrell-roberts wants to merge 1 commit into
getsentry:masterfrom
darrell-roberts:darrell/push-nnsnopqlnrqq

Conversation

@darrell-roberts

Copy link
Copy Markdown

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

@darrell-roberts
darrell-roberts requested a review from a team as a code owner August 8, 2026 16:52

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread sentry-core/src/clientoptions.rs
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
@darrell-roberts
darrell-roberts force-pushed the darrell/push-nnsnopqlnrqq branch from fe9a608 to dc3d1ef Compare August 8, 2026 17:07
Comment on lines +403 to 409
.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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

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.

1 participant