From 45a4ac16167e8a360d33ec24bf5b291e57a33120 Mon Sep 17 00:00:00 2001 From: Jan Michael Auer Date: Mon, 11 Dec 2023 13:26:01 +0100 Subject: [PATCH] ref(metrics): Minor code-level changes --- sentry-core/src/cadence.rs | 10 ++++------ sentry-core/src/metrics.rs | 13 +++++++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/sentry-core/src/cadence.rs b/sentry-core/src/cadence.rs index 10304ac3..691026fd 100644 --- a/sentry-core/src/cadence.rs +++ b/sentry-core/src/cadence.rs @@ -17,12 +17,10 @@ pub struct SentryMetricSink { impl SentryMetricSink { /// Creates a new [`SentryMetricSink`], wrapping the given [`MetricSink`]. pub fn try_new(sink: S) -> Result { - let hub = Hub::current(); - let Some(client) = hub.client() else { - return Err(sink); - }; - - Ok(Self { client, sink }) + match Hub::current().client() { + Some(client) => Ok(Self { client, sink }), + None => Err(sink), + } } } diff --git a/sentry-core/src/metrics.rs b/sentry-core/src/metrics.rs index 682ab695..cad640ba 100644 --- a/sentry-core/src/metrics.rs +++ b/sentry-core/src/metrics.rs @@ -9,6 +9,9 @@ use sentry_types::protocol::latest::{Envelope, EnvelopeItem}; use crate::client::TransportArc; +const BUCKET_INTERVAL: Duration = Duration::from_secs(10); +const FLUSH_INTERVAL: Duration = Duration::from_secs(10); + #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] enum MetricType { Counter, @@ -56,12 +59,14 @@ struct GaugeValue { sum: f64, count: u64, } + enum BucketValue { Counter(f64), Distribution(Vec), Set(BTreeSet), Gauge(GaugeValue), } + impl BucketValue { fn distribution(val: f64) -> BucketValue { Self::Distribution(vec![val]) @@ -89,7 +94,9 @@ impl BucketValue { (BucketValue::Distribution(d1), BucketValue::Distribution(d2)) => { d1.extend(d2); } - (BucketValue::Set(s1), BucketValue::Set(s2)) => s1.extend(s2), + (BucketValue::Set(s1), BucketValue::Set(s2)) => { + s1.extend(s2); + } (BucketValue::Gauge(g1), BucketValue::Gauge(g2)) => { g1.last = g2.last; g1.min = g1.min.min(g2.min); @@ -99,6 +106,7 @@ impl BucketValue { } _ => return Err(()), } + Ok(()) } } @@ -124,9 +132,6 @@ pub struct MetricAggregator { handle: Option>, } -const BUCKET_INTERVAL: Duration = Duration::from_secs(10); -const FLUSH_INTERVAL: Duration = Duration::from_secs(10); - impl MetricAggregator { pub fn new(transport: TransportArc) -> Self { let (sender, receiver) = mpsc::sync_channel(30);