1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
use std::fmt;
use std::sync::Arc;
use serde::{Deserialize, Serialize};
use crate::metrics::{MetricNamespace, MetricType};
/// Optimized string represenation of a metric name.
///
/// The contained name does not need to be valid MRI, but it usually is.
///
/// The metric name can be efficiently cloned.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
#[serde(transparent)]
pub struct MetricName(Arc<str>);
impl MetricName {
/// Extracts the type from a well formed MRI.
///
/// If the contained metric name is not a well formed MRI this function returns `None`.
///
/// # Examples
///
/// ```
/// use relay_base_schema::metrics::{MetricName, MetricType};
///
/// let name = MetricName::from("cfoo");
/// assert!(name.try_type().is_none());
/// let name = MetricName::from("c:custom/foo@none");
/// assert_eq!(name.try_type(), Some(MetricType::Counter));
/// let name = MetricName::from("d:custom/foo@none");
/// assert_eq!(name.try_type(), Some(MetricType::Distribution));
/// let name = MetricName::from("s:custom/foo@none");
/// assert_eq!(name.try_type(), Some(MetricType::Set));
/// let name = MetricName::from("g:custom/foo@none");
/// assert_eq!(name.try_type(), Some(MetricType::Gauge));
/// ```
pub fn try_type(&self) -> Option<MetricType> {
match self.0.as_bytes().get(..2) {
Some(b"c:") => Some(MetricType::Counter),
Some(b"d:") => Some(MetricType::Distribution),
Some(b"s:") => Some(MetricType::Set),
Some(b"g:") => Some(MetricType::Gauge),
_ => None,
}
}
/// Extracts the namespace from a well formed MRI.
///
/// Returns [`MetricNamespace::Unsupported`] if the metric name is not a well formed MRI.
///
/// # Examples
///
/// ```
/// use relay_base_schema::metrics::{MetricName, MetricNamespace};
///
/// let name = MetricName::from("foo");
/// assert_eq!(name.namespace(), MetricNamespace::Unsupported);
/// let name = MetricName::from("c:custom_oops/foo@none");
/// assert_eq!(name.namespace(), MetricNamespace::Unsupported);
///
/// let name = MetricName::from("c:custom/foo@none");
/// assert_eq!(name.namespace(), MetricNamespace::Custom);
/// ```
pub fn namespace(&self) -> MetricNamespace {
self.try_namespace().unwrap_or(MetricNamespace::Unsupported)
}
/// Extracts the namespace from a well formed MRI.
///
/// If the contained metric name is not a well formed MRI this function returns `None`.
///
/// # Examples
///
/// ```
/// use relay_base_schema::metrics::{MetricName, MetricNamespace};
///
/// let name = MetricName::from("foo");
/// assert!(name.try_namespace().is_none());
/// let name = MetricName::from("c:custom_oops/foo@none");
/// assert!(name.try_namespace().is_none());
///
/// let name = MetricName::from("c:custom/foo@none");
/// assert_eq!(name.try_namespace(), Some(MetricNamespace::Custom));
/// ```
pub fn try_namespace(&self) -> Option<MetricNamespace> {
// A well formed MRI is always in the format `<type>:<namespace>/<name>[@<unit>]`,
// `<type>` is always a single ascii character.
//
// Skip the first two ascii characters and extract the namespace.
let maybe_namespace = self.0.get(2..)?.split('/').next()?;
MetricNamespace::all()
.into_iter()
.find(|namespace| maybe_namespace == namespace.as_str())
}
}
impl PartialEq<str> for MetricName {
fn eq(&self, other: &str) -> bool {
self.0.as_ref() == other
}
}
impl fmt::Display for MetricName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl From<String> for MetricName {
fn from(value: String) -> Self {
Self(value.into())
}
}
impl From<Arc<str>> for MetricName {
fn from(value: Arc<str>) -> Self {
Self(value)
}
}
impl From<&str> for MetricName {
fn from(value: &str) -> Self {
Self(value.into())
}
}
impl std::ops::Deref for MetricName {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl AsRef<str> for MetricName {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl std::borrow::Borrow<str> for MetricName {
fn borrow(&self) -> &str {
self.0.borrow()
}
}