Skip to content

Commit d24c9b2

Browse files
committed
ref(metrics): Refactor to match Python SDK
1 parent 45a4ac1 commit d24c9b2

File tree

5 files changed

+749
-188
lines changed

5 files changed

+749
-188
lines changed

sentry-core/src/cadence.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::sync::Arc;
22

33
use cadence::MetricSink;
44

5+
use crate::metrics::{Metric, MetricType, MetricValue};
6+
use crate::units::MetricUnit;
57
use crate::{Client, Hub};
68

79
/// A [`cadence`] compatible [`MetricSink`].
@@ -28,9 +30,12 @@ impl<S> MetricSink for SentryMetricSink<S>
2830
where
2931
S: MetricSink,
3032
{
31-
fn emit(&self, metric: &str) -> std::io::Result<usize> {
32-
self.client.add_metric(metric);
33-
self.sink.emit(metric)
33+
fn emit(&self, string: &str) -> std::io::Result<usize> {
34+
if let Some(metric) = parse_metric(string) {
35+
self.client.add_metric(metric);
36+
}
37+
38+
self.sink.emit(string)
3439
}
3540

3641
fn flush(&self) -> std::io::Result<()> {
@@ -45,6 +50,41 @@ where
4550
}
4651
}
4752

53+
fn parse_metric(string: &str) -> Option<Metric> {
54+
let mut components = string.split('|');
55+
56+
let (mri_str, value_str) = components.next()?.split_once(':')?;
57+
let (name, unit) = match mri_str.split_once('@') {
58+
Some((name, unit_str)) => (name, unit_str.parse().ok()?),
59+
None => (mri_str, MetricUnit::None),
60+
};
61+
62+
let ty = components.next().and_then(|s| s.parse().ok())?;
63+
let value = match ty {
64+
MetricType::Counter => MetricValue::Counter(value_str.parse().ok()?),
65+
MetricType::Distribution => MetricValue::Distribution(value_str.parse().ok()?),
66+
MetricType::Set => MetricValue::Set(value_str.parse().ok()?),
67+
MetricType::Gauge => MetricValue::Gauge(value_str.parse().ok()?),
68+
};
69+
70+
let mut builder = Metric::build(name.to_owned(), value).with_unit(unit);
71+
72+
for component in components {
73+
if let Some('#') = component.chars().next() {
74+
for pair in string.get(1..)?.split(',') {
75+
let mut key_value = pair.splitn(2, ':');
76+
77+
let key = key_value.next()?.to_owned();
78+
let value = key_value.next().unwrap_or_default().to_owned();
79+
80+
builder = builder.with_tag(key, value);
81+
}
82+
}
83+
}
84+
85+
Some(builder.finish())
86+
}
87+
4888
#[cfg(test)]
4989
mod tests {
5090
use cadence::{Counted, Distributed};

sentry-core/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use sentry_types::random_uuid;
1212

1313
use crate::constants::SDK_INFO;
1414
#[cfg(feature = "UNSTABLE_metrics")]
15-
use crate::metrics::MetricAggregator;
15+
use crate::metrics::{self, MetricAggregator};
1616
use crate::protocol::{ClientSdkInfo, Event};
1717
use crate::session::SessionFlusher;
1818
use crate::types::{Dsn, Uuid};
@@ -323,7 +323,7 @@ impl Client {
323323
}
324324

325325
#[cfg(feature = "UNSTABLE_metrics")]
326-
pub(crate) fn add_metric(&self, metric: &str) {
326+
pub fn add_metric(&self, metric: metrics::Metric) {
327327
if let Some(ref aggregator) = *self.metric_aggregator.read().unwrap() {
328328
aggregator.add(metric)
329329
}

sentry-core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ mod hub_impl;
147147
mod metrics;
148148
#[cfg(feature = "client")]
149149
mod session;
150+
#[cfg(all(feature = "client", feature = "UNSTABLE_metrics"))]
151+
mod units;
150152
#[cfg(all(feature = "client", feature = "UNSTABLE_cadence"))]
151153
pub use crate::cadence::SentryMetricSink;
152154
#[cfg(feature = "client")]

0 commit comments

Comments
 (0)