Skip to content

Commit

Permalink
ref(metrics): Refactor to match Python SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-auer committed Dec 11, 2023
1 parent 45a4ac1 commit d24c9b2
Show file tree
Hide file tree
Showing 5 changed files with 749 additions and 188 deletions.
46 changes: 43 additions & 3 deletions sentry-core/src/cadence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::sync::Arc;

use cadence::MetricSink;

use crate::metrics::{Metric, MetricType, MetricValue};
use crate::units::MetricUnit;
use crate::{Client, Hub};

/// A [`cadence`] compatible [`MetricSink`].
Expand All @@ -28,9 +30,12 @@ impl<S> MetricSink for SentryMetricSink<S>
where
S: MetricSink,
{
fn emit(&self, metric: &str) -> std::io::Result<usize> {
self.client.add_metric(metric);
self.sink.emit(metric)
fn emit(&self, string: &str) -> std::io::Result<usize> {
if let Some(metric) = parse_metric(string) {
self.client.add_metric(metric);
}

self.sink.emit(string)
}

fn flush(&self) -> std::io::Result<()> {
Expand All @@ -45,6 +50,41 @@ where
}
}

fn parse_metric(string: &str) -> Option<Metric> {
let mut components = string.split('|');

let (mri_str, value_str) = components.next()?.split_once(':')?;
let (name, unit) = match mri_str.split_once('@') {
Some((name, unit_str)) => (name, unit_str.parse().ok()?),
None => (mri_str, MetricUnit::None),
};

let ty = components.next().and_then(|s| s.parse().ok())?;
let value = match ty {
MetricType::Counter => MetricValue::Counter(value_str.parse().ok()?),
MetricType::Distribution => MetricValue::Distribution(value_str.parse().ok()?),
MetricType::Set => MetricValue::Set(value_str.parse().ok()?),
MetricType::Gauge => MetricValue::Gauge(value_str.parse().ok()?),
};

let mut builder = Metric::build(name.to_owned(), value).with_unit(unit);

for component in components {
if let Some('#') = component.chars().next() {
for pair in string.get(1..)?.split(',') {
let mut key_value = pair.splitn(2, ':');

let key = key_value.next()?.to_owned();
let value = key_value.next().unwrap_or_default().to_owned();

builder = builder.with_tag(key, value);
}
}
}

Some(builder.finish())
}

#[cfg(test)]
mod tests {
use cadence::{Counted, Distributed};
Expand Down
4 changes: 2 additions & 2 deletions sentry-core/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sentry_types::random_uuid;

use crate::constants::SDK_INFO;
#[cfg(feature = "UNSTABLE_metrics")]
use crate::metrics::MetricAggregator;
use crate::metrics::{self, MetricAggregator};
use crate::protocol::{ClientSdkInfo, Event};
use crate::session::SessionFlusher;
use crate::types::{Dsn, Uuid};
Expand Down Expand Up @@ -323,7 +323,7 @@ impl Client {
}

#[cfg(feature = "UNSTABLE_metrics")]
pub(crate) fn add_metric(&self, metric: &str) {
pub fn add_metric(&self, metric: metrics::Metric) {
if let Some(ref aggregator) = *self.metric_aggregator.read().unwrap() {
aggregator.add(metric)
}
Expand Down
2 changes: 2 additions & 0 deletions sentry-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ mod hub_impl;
mod metrics;
#[cfg(feature = "client")]
mod session;
#[cfg(all(feature = "client", feature = "UNSTABLE_metrics"))]
mod units;
#[cfg(all(feature = "client", feature = "UNSTABLE_cadence"))]
pub use crate::cadence::SentryMetricSink;
#[cfg(feature = "client")]
Expand Down
Loading

0 comments on commit d24c9b2

Please sign in to comment.