Skip to content

Commit cdd36e4

Browse files
committed
fix: Move statsd parsing to metrics module
1 parent c164983 commit cdd36e4

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

sentry-core/src/cadence.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ where
3131
S: MetricSink,
3232
{
3333
fn emit(&self, string: &str) -> std::io::Result<usize> {
34-
if let Some(metric) = parse_metric(string) {
34+
if let Ok(metric) = Metric::parse_statsd(string) {
3535
self.client.add_metric(metric);
3636
}
3737

@@ -50,41 +50,6 @@ where
5050
}
5151
}
5252

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 component.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-
8853
#[cfg(test)]
8954
mod tests {
9055
use cadence::{Counted, Distributed};

sentry-core/src/metrics.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ impl Metric {
299299
MetricBuilder { metric }
300300
}
301301

302+
pub fn parse_statsd(string: &str) -> Result<Self, ParseMetricError> {
303+
parse_metric_opt(string).ok_or(ParseMetricError(()))
304+
}
305+
302306
pub fn incr(name: impl Into<MetricStr>) -> MetricBuilder {
303307
Self::build(name, MetricValue::Counter(1.0))
304308
}
@@ -353,6 +357,52 @@ impl MetricBuilder {
353357
}
354358
}
355359

360+
#[derive(Debug)]
361+
pub struct ParseMetricError(());
362+
363+
impl std::error::Error for ParseMetricError {}
364+
365+
impl fmt::Display for ParseMetricError {
366+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
367+
f.write_str("invalid metric string")
368+
}
369+
}
370+
371+
fn parse_metric_opt(string: &str) -> Option<Metric> {
372+
let mut components = string.split('|');
373+
374+
let (mri_str, value_str) = components.next()?.split_once(':')?;
375+
let (name, unit) = match mri_str.split_once('@') {
376+
Some((name, unit_str)) => (name, unit_str.parse().ok()?),
377+
None => (mri_str, MetricUnit::None),
378+
};
379+
380+
let ty = components.next().and_then(|s| s.parse().ok())?;
381+
let value = match ty {
382+
MetricType::Counter => MetricValue::Counter(value_str.parse().ok()?),
383+
MetricType::Distribution => MetricValue::Distribution(value_str.parse().ok()?),
384+
MetricType::Set => MetricValue::Set(value_str.parse().ok()?),
385+
MetricType::Gauge => MetricValue::Gauge(value_str.parse().ok()?),
386+
};
387+
388+
let mut builder = Metric::build(name.to_owned(), value).with_unit(unit);
389+
390+
for component in components {
391+
if let Some('#') = component.chars().next() {
392+
for pair in component.get(1..)?.split(',') {
393+
let mut key_value = pair.splitn(2, ':');
394+
395+
let key = key_value.next()?.to_owned();
396+
let value = key_value.next().unwrap_or_default().to_owned();
397+
398+
builder = builder.with_tag(key, value);
399+
}
400+
}
401+
}
402+
403+
Some(builder.finish())
404+
}
405+
356406
pub struct MetricAggregator {
357407
inner: Arc<Mutex<AggregatorInner>>,
358408
handle: Option<JoinHandle<()>>,

0 commit comments

Comments
 (0)