diff --git a/CHANGELOG.md b/CHANGELOG.md index 49350fb9..421d917d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Deprecations - Deprecated `ClientOptions::enable_logs`. The option is now a no-op; logs are always enabled. To stop sending logs, stop calling the logging APIs or disable the log-capturing integrations (`tracing`, `log`, or `slog`) ([#1299](https://github.com/getsentry/sentry-rust/pull/1299)). +- Deprecated `ClientOptions::enable_metrics`. The option is now a no-op; metrics are always enabled. To stop sending metrics, stop calling the metrics APIs ([#1300](https://github.com/getsentry/sentry-rust/pull/1300)). ## 0.49.1 diff --git a/sentry-core/src/client/mod.rs b/sentry-core/src/client/mod.rs index b9917750..706a5ecd 100644 --- a/sentry-core/src/client/mod.rs +++ b/sentry-core/src/client/mod.rs @@ -117,11 +117,7 @@ impl Clone for Client { let logs_batcher = RwLock::new(Some(Batcher::new(envelope_sender.clone()))); #[cfg(feature = "metrics")] - let metrics_batcher = RwLock::new( - self.options - .enable_metrics - .then(|| Batcher::new(envelope_sender.clone())), - ); + let metrics_batcher = RwLock::new(Some(Batcher::new(envelope_sender.clone()))); Client { options: self.options.clone(), @@ -177,13 +173,19 @@ impl Client { /// Panics in debug builds if the deprecated no-op field /// [`enable_logs`](field@ClientOptions::enable_logs) is set to `false`. pub fn with_options(mut options: ClientOptions) -> Client { - #[expect(deprecated, reason = "need to check deprecated field")] + #[expect(deprecated, reason = "need to check deprecated fields")] { debug_assert!( options.enable_logs, "invalid initialization options: the Sentry SDK no longer supports disabling logs \ via the `enable_logs` field: {options:?}" ); + + debug_assert!( + options.enable_metrics, + "invalid initialization options: the Sentry SDK no longer supports disabling \ + metrics via the `enable_metrics` field: {options:?}" + ) } // Create the main hub eagerly to avoid problems with the background thread @@ -216,11 +218,7 @@ impl Client { let logs_batcher = RwLock::new(Some(Batcher::new(envelope_sender.clone()))); #[cfg(feature = "metrics")] - let metrics_batcher = RwLock::new( - options - .enable_metrics - .then(|| Batcher::new(envelope_sender.clone())), - ); + let metrics_batcher = RwLock::new(Some(Batcher::new(envelope_sender.clone()))); let client = Client { options, @@ -601,11 +599,6 @@ impl Client { /// Captures a metric and sends it to Sentry. #[cfg(feature = "metrics")] pub fn capture_metric(&self, metric: M, scope: &Scope) { - if !self.options.enable_metrics { - // Skip preparing the metric if we don't send it anyways. - return; - } - if let Some(metric) = self.prepare_metric(metric, scope) { if let Some(batcher) = self .metrics_batcher diff --git a/sentry-core/src/clientoptions.rs b/sentry-core/src/clientoptions.rs index 2104e755..d8005086 100644 --- a/sentry-core/src/clientoptions.rs +++ b/sentry-core/src/clientoptions.rs @@ -251,9 +251,11 @@ pub struct ClientOptions { /// `false`. The panic occurs at initialization-time. #[deprecated = "this option is a deprecated no-op"] pub enable_logs: bool, - /// Whether metric capture APIs should capture metrics. + /// Deprecated no-op. /// - /// See [`enable_metrics`](method@ClientOptions::enable_metrics) for details. + /// In debug builds, we panic if the Sentry client is initialized with this option set to + /// `false`. The panic occurs at initialization-time. + #[deprecated = "this option is a deprecated no-op"] pub enable_metrics: bool, /// Callback that is executed for each [`Metric`] before sending. /// @@ -698,12 +700,19 @@ impl ClientOptions { } } - /// Enables or disables [metric capture APIs](field@ClientOptions::enable_metrics). + /// This function is effectively a no-op, as it sets the deprecated, no-op field + /// [`enable_metrics`](field@ClientOptions::enable_metrics). + /// + /// To stop sending metrics, simply remove any calls to our metrics APIs. We only capture + /// logs if you explicitly call the relevant APIs. /// - /// The `metrics` feature is required to capture metrics. Defaults to `true`. + /// In debug builds, we panic if the Sentry client is initialized with this option set to + /// `false`. The panic occurs at initialization-time. + #[deprecated = "this function sets a no-op option"] #[inline] pub fn enable_metrics(self, enable_metrics: bool) -> Self { Self { + #[expect(deprecated, reason = "need to set deprecated field")] enable_metrics, ..self } @@ -832,7 +841,11 @@ impl fmt::Debug for ClientOptions { &self.enable_logs, ) .field("before_send_log", &before_send_log) - .field("enable_metrics", &self.enable_metrics) + .field( + "enable_metrics", + #[expect(deprecated, reason = "still need to debug-log this field")] + &self.enable_metrics, + ) .field("before_send_metric", &before_send_metric) .field("org_id", &self.org_id) .field("strict_trace_continuation", &self.strict_trace_continuation) @@ -874,6 +887,7 @@ impl Default for ClientOptions { #[expect(deprecated, reason = "still need to set deprecated fields")] enable_logs: true, before_send_log: None, + #[expect(deprecated, reason = "still need to set deprecated fields")] enable_metrics: true, before_send_metric: None, } diff --git a/sentry-core/tests/metrics.rs b/sentry-core/tests/metrics.rs index 65679121..740043c5 100644 --- a/sentry-core/tests/metrics.rs +++ b/sentry-core/tests/metrics.rs @@ -10,68 +10,6 @@ use sentry_core::{metrics, test}; use sentry_core::{ClientOptions, TransactionContext}; use sentry_types::protocol::v7::{Envelope, LogAttribute, Metric, User}; -/// Test that metrics are sent when metrics are enabled. -#[test] -fn sent_when_enabled() { - let options = ClientOptions::new().enable_metrics(true); - - let mut envelopes = - test::with_captured_envelopes_options(|| metrics::counter("test", 1).capture(), options); - - assert_eq!(envelopes.len(), 1, "expected exactly one envelope"); - - let envelope = envelopes.pop().unwrap(); - - let mut items = envelope.into_items(); - let Some(item) = items.next() else { - panic!("Expected at least one item"); - }; - - assert!(items.next().is_none(), "Expected only one item"); - - let EnvelopeItem::ItemContainer(ItemContainer::Metrics(mut metrics)) = item else { - panic!("Envelope item has unexpected structure"); - }; - - assert_eq!(metrics.len(), 1, "Expected exactly one metric"); - - let metric = metrics.pop().unwrap(); - assert!(matches!(metric, Metric { - r#type: MetricType::Counter, - name, - value: 1.0, - .. - } if name == "test")); -} - -/// Test that metrics are sent by default. -#[test] -fn metrics_enabled_by_default() { - let options = ClientOptions::default(); - - let envelopes = - test::with_captured_envelopes_options(|| metrics::counter("test", 1).capture(), options); - assert_eq!( - envelopes.len(), - 1, - "expected exactly one envelope when metrics are enabled by default" - ) -} - -/// Test that metrics are disabled (not sent) when disabled in the -/// [`ClientOptions`]. -#[test] -fn metrics_disabled_when_configured() { - let options = ClientOptions::new().enable_metrics(false); - - let envelopes = - test::with_captured_envelopes_options(|| metrics::counter("test", 1).capture(), options); - assert!( - envelopes.is_empty(), - "no envelopes should be captured when metrics disabled" - ) -} - /// Test that no metrics are captured by a no-op call with /// metrics enabled #[test]