Skip to content

Commit

Permalink
ref(metrics): Allow MRI to be owned (#2763)
Browse files Browse the repository at this point in the history
This allows us to pass a MRI as an owned structure and allows for less
conversions between String <-> MRI down the line. This will also make it
possible to implement Deserialize for the MRI
  • Loading branch information
Dav1dde authored Nov 24, 2023
1 parent 068d837 commit f2be998
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions relay-metrics/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ fn parse_gauge(string: &str) -> Option<GaugeValue> {
/// - (optional) The unit. If missing, it is defaulted to "none".
///
/// The metric type is never part of this string and must be supplied separately.
fn parse_mri(string: &str, ty: MetricType) -> Option<MetricResourceIdentifier> {
fn parse_mri(string: &str, ty: MetricType) -> Option<MetricResourceIdentifier<'_>> {
let (name_and_namespace, unit) = protocol::parse_name_unit(string)?;

let (raw_namespace, name) = name_and_namespace
Expand All @@ -416,7 +416,7 @@ fn parse_mri(string: &str, ty: MetricType) -> Option<MetricResourceIdentifier> {

Some(MetricResourceIdentifier {
ty,
name,
name: name.into(),
namespace: raw_namespace.parse().ok()?,
unit,
})
Expand Down
16 changes: 13 additions & 3 deletions relay-metrics/src/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::error::Error;
use std::fmt;
use std::hash::Hasher as _;
use std::{borrow::Cow, error::Error};

use hash32::{FnvHasher, Hasher as _};

Expand Down Expand Up @@ -234,7 +234,7 @@ pub struct MetricResourceIdentifier<'a> {
pub namespace: MetricNamespace,

/// The display name of the metric in the allowed character set.
pub name: &'a str,
pub name: Cow<'a, str>,

/// The verbatim unit name of the metric value.
///
Expand All @@ -255,10 +255,20 @@ impl<'a> MetricResourceIdentifier<'a> {
Ok(Self {
ty,
namespace: raw_namespace.parse()?,
name,
name: Cow::Borrowed(name),
unit,
})
}

/// Converts the MRI into an owned version with a static lifetime.
pub fn into_owned(self) -> MetricResourceIdentifier<'static> {
MetricResourceIdentifier {
ty: self.ty,
namespace: self.namespace,
name: Cow::Owned(self.name.into_owned()),
unit: self.unit,
}
}
}

impl<'a> fmt::Display for MetricResourceIdentifier<'a> {
Expand Down
2 changes: 1 addition & 1 deletion relay-server/src/metrics_extraction/sessions/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl IntoMetric for SessionMetric {
let mri = MetricResourceIdentifier {
ty: value.ty(),
namespace: MetricNamespace::Sessions,
name: &name,
name: name.into(),
unit: MetricUnit::None,
};

Expand Down
2 changes: 1 addition & 1 deletion relay-server/src/metrics_extraction/transactions/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl IntoMetric for TransactionMetric {
let mri = MetricResourceIdentifier {
ty: value.ty(),
namespace,
name: &name,
name,
unit,
};

Expand Down
2 changes: 1 addition & 1 deletion relay-server/src/utils/metrics_rate_limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<Q: AsRef<Vec<Quota>>> MetricsLimiter<Q> {
_ => 0,
};

let has_profile = matches!(mri.name, "usage" | "duration")
let has_profile = matches!(mri.name.as_ref(), "usage" | "duration")
&& metric.tag(PROFILE_TAG) == Some("true");

Some((count, has_profile))
Expand Down

0 comments on commit f2be998

Please sign in to comment.