Skip to content

Commit fd9783f

Browse files
committed
wip
1 parent 45a4ac1 commit fd9783f

File tree

5 files changed

+805
-181
lines changed

5 files changed

+805
-181
lines changed

sentry-core/src/cadence.rs

Lines changed: 45 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::{MetricType, MetricValue};
6+
use crate::units::MetricUnit;
57
use crate::{Client, Hub};
68

79
/// A [`cadence`] compatible [`MetricSink`].
@@ -28,9 +30,9 @@ 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+
parse_and_add(string, &self.client); // Ignore error for now
35+
self.sink.emit(string)
3436
}
3537

3638
fn flush(&self) -> std::io::Result<()> {
@@ -45,6 +47,46 @@ where
4547
}
4648
}
4749

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

sentry-core/src/client.rs

Lines changed: 9 additions & 3 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,9 +323,15 @@ impl Client {
323323
}
324324

325325
#[cfg(feature = "UNSTABLE_metrics")]
326-
pub(crate) fn add_metric(&self, metric: &str) {
326+
pub(crate) fn add_metric(
327+
&self,
328+
name: impl Into<metrics::MetricStr>,
329+
value: metrics::MetricValue,
330+
) -> metrics::MetricBuilder<'_> {
327331
if let Some(ref aggregator) = *self.metric_aggregator.read().unwrap() {
328-
aggregator.add(metric)
332+
aggregator.add(name, value)
333+
} else {
334+
metrics::MetricBuilder::dangling()
329335
}
330336
}
331337

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)