Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
add a method for getting a metric from props
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Nov 8, 2023
1 parent 5e76c62 commit 61722b3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
20 changes: 20 additions & 0 deletions core/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ impl<'k> ToValue for Key<'k> {
}
}

impl<'v> Value<'v> {
pub fn to_key(&self) -> Option<Key<'v>> {
#[cfg(feature = "alloc")]
{
self.to_str().map(Key::new_cow_ref)
}
#[cfg(not(feature = "alloc"))]
{
self.to_borrowed_str().map(Key::new_ref)
}
}
}

pub trait ToKey {
fn to_key(&self) -> Key;
}
Expand Down Expand Up @@ -249,6 +262,13 @@ mod alloc_support {
}

impl<'k> Key<'k> {
pub fn new_cow_ref(key: Cow<'k, str>) -> Self {
match key {
Cow::Borrowed(key) => Key::new_ref(key),
Cow::Owned(key) => Key::new_owned(key),
}
}

pub fn to_cow(&self) -> Cow<'static, str> {
match self.value_static {
Some(key) => Cow::Borrowed(key),
Expand Down
24 changes: 24 additions & 0 deletions core/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl<'m, V: ToValue> Props for Metric<'m, V> {
#[non_exhaustive]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum MetricKind {
Point,
Counter,
}

Expand All @@ -72,6 +73,7 @@ impl fmt::Debug for MetricKind {
impl fmt::Display for MetricKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
MetricKind::Point => "point",
MetricKind::Counter => "counter",
})
}
Expand All @@ -81,6 +83,10 @@ impl FromStr for MetricKind {
type Err = ParseMetricKindError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.eq_ignore_ascii_case("point") {
return Ok(MetricKind::Point);
}

if s.eq_ignore_ascii_case("counter") {
return Ok(MetricKind::Counter);
}
Expand All @@ -105,3 +111,21 @@ impl<'v> Value<'v> {
.or_else(|| self.parse())
}
}

#[cfg(test)]
mod tests {
use crate::well_known::WellKnown;

use super::*;

#[test]
fn metric_well_known() {
let metric = Metric::new(Key::new("metric"), MetricKind::Point, Value::from(1usize));

let well_known = WellKnown::metric(&metric).unwrap();

assert_eq!("metric", well_known.name());
assert_eq!(MetricKind::Point, well_known.kind());
assert_eq!(1, well_known.value().to_usize().unwrap());
}
}
14 changes: 14 additions & 0 deletions core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ impl<'v> Value<'v> {
self.visit(&mut visitor);
visitor.0
}

pub fn to_borrowed_str(&self) -> Option<&'v str> {
self.0.to_borrowed_str()
}

pub fn to_usize(&self) -> Option<usize> {
self.0.to_u64()?.try_into().ok()
}
}

pub trait Visitor<'v> {
Expand Down Expand Up @@ -187,13 +195,19 @@ impl<'v> From<usize> for Value<'v> {
mod alloc_support {
use super::*;

use alloc::borrow::Cow;

#[derive(Clone)]
pub struct OwnedValue(value_bag::OwnedValueBag);

impl<'v> Value<'v> {
pub fn to_owned(&self) -> OwnedValue {
OwnedValue(self.0.to_owned())
}

pub fn to_str(&self) -> Option<Cow<'v, str>> {
self.0.to_str()
}
}

impl OwnedValue {
Expand Down
15 changes: 12 additions & 3 deletions core/src/well_known.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
id::{SpanId, TraceId},
key::Key,
level::Level,
metrics::MetricKind,
metrics::{Metric, MetricKind},
props::Props,
value::Value,
};
Expand Down Expand Up @@ -41,8 +42,16 @@ pub trait WellKnown: Props {
self.get(ERR_KEY)
}

fn metric_name(&self) -> Option<Value> {
self.get(METRIC_NAME_KEY)
fn metric(&self) -> Option<Metric<Value>> {
Some(Metric::new(
self.metric_name()?,
self.metric_kind()?,
self.metric_value()?,
))
}

fn metric_name(&self) -> Option<Key> {
self.get(METRIC_NAME_KEY)?.to_key()
}

fn metric_kind(&self) -> Option<MetricKind> {
Expand Down

0 comments on commit 61722b3

Please sign in to comment.