From dc3d1ef197547637df4209e5eee633e12f8f234a Mon Sep 17 00:00:00 2001 From: Darrell Roberts Date: Sat, 16 May 2026 12:35:15 -0400 Subject: [PATCH] feat: Allow overriding the sampling rate. 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 --- CHANGELOG.md | 3 +++ sentry-core/src/client/mod.rs | 10 ++++++++-- sentry-core/src/clientoptions.rs | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1932164..5a1f4c2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 0.49.2 +- Added `override_sampling_rate` to `ClientOptions` to allow user defined function to override sampling rate when capturing an event. ([#1128](https://github.com/getsentry/sentry-rust/pull/1128)). + ## 0.49.1 ### Fixes diff --git a/sentry-core/src/client/mod.rs b/sentry-core/src/client/mod.rs index bc75919a..fa830eaf 100644 --- a/sentry-core/src/client/mod.rs +++ b/sentry-core/src/client/mod.rs @@ -397,8 +397,14 @@ impl Client { scope.update_session_from_event(&event); } - let sample_rate = event_sample_rate(&self.options.event_sampling_strategy); - if !self.sample_should_send(sample_rate) { + let sampling_rate = self + .options() + .override_sampling_rate + .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 } else { diff --git a/sentry-core/src/clientoptions.rs b/sentry-core/src/clientoptions.rs index a67658db..7961bc3e 100644 --- a/sentry-core/src/clientoptions.rs +++ b/sentry-core/src/clientoptions.rs @@ -12,6 +12,9 @@ use crate::{Integration, IntoDsn, TransportFactory}; /// Type alias for before event/breadcrumb handlers. pub type BeforeCallback = Arc Option + Send + Sync>; +/// Type alias for override sample rate callback. +pub type OverrideSamplingRateCallback = Arc) -> Option + Send + Sync>; + /// The Session Mode of the SDK. /// /// Depending on the use-case, the SDK can be set to two different session modes: @@ -220,6 +223,8 @@ pub struct ClientOptions { /// /// See [`before_breadcrumb`](method@ClientOptions::before_breadcrumb) for details. pub before_breadcrumb: Option>, + /// Callback allowing for setting sampling rate based on user defined function. + pub override_sampling_rate: Option, /// Callback that is executed for each Log being added. /// /// See [`before_send_log`](method@ClientOptions::before_send_log) for details. @@ -580,6 +585,19 @@ impl ClientOptions { } } + /// Sets the [callback](field@ClientOptions::override_sampling_rate) that is used to override the + /// sampling rate. + #[inline] + pub fn override_sampling_rate( + self, + override_func: impl Fn(&Event<'static>) -> Option + Send + Sync + 'static, + ) -> Self { + Self { + override_sampling_rate: Some(Arc::new(override_func)), + ..self + } + } + /// Sets the [callback](field@ClientOptions::before_send_log) that is executed before sending /// each log. #[cfg(feature = "logs")] @@ -860,6 +878,7 @@ impl Default for ClientOptions { before_send_log: None, enable_metrics: true, before_send_metric: None, + override_sampling_rate: None, } } }