Skip to content

Commit

Permalink
fix: make Optional label compatible with macro
Browse files Browse the repository at this point in the history
  • Loading branch information
xDarksome committed Jul 2, 2024
1 parent eb668b8 commit fa69002
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 16 deletions.
43 changes: 40 additions & 3 deletions crates/metrics/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
metrics::Label,
parking_lot::Mutex,
smallvec::SmallVec,
std::{borrow::Borrow, collections::HashMap, marker::PhantomData, sync::Arc},
std::{borrow::Borrow, collections::HashMap, sync::Arc},
};

pub type DynamicLabels = SmallVec<[Label; 4]>;
Expand Down Expand Up @@ -343,9 +343,33 @@ where
}
}

/// Makes any other label optional by accepting `Option` instead of the actual
/// Makes any other label optional by accepting [`Option`] instead of the actual
/// label value during the label resolution.
pub struct Optional<T>(PhantomData<T>);
pub struct Optional<T>(pub Option<T>);

impl<const NAME: LabelName, T> Optional<EnumLabel<NAME, T>> {
/// Creates a new [`Optional`] [`EnumLabel`].
pub fn new(v: Option<impl Into<T>>) -> Self {
Self(v.map(EnumLabel::new))
}
}

impl<const NAME: LabelName> Optional<BoolLabel<NAME>> {
/// Creates a new [`Optional`] [`BoolLabel`].
pub fn new(v: Option<bool>) -> Self {
Self(v.map(BoolLabel::new))
}
}

impl<const NAME: LabelName, T> Optional<StringLabel<NAME, T>> {
/// Creates a new [`Optional`] [`StringLabel`].
pub fn new<U: ?Sized>(v: Option<&U>) -> Optional<StringLabel<NAME, &U>>
where
T: Borrow<U>,
{
Optional(v.map(StringLabel::<NAME, T>::new))
}
}

impl<T, M> DynamicLabel<M> for Optional<T>
where
Expand Down Expand Up @@ -386,6 +410,19 @@ where
}
}

impl<T, U, M> ResolveLabels<(Optional<U>,)> for WithLabel<Optional<T>, M>
where
T: DynamicLabel<M>,
M: Metric,
WithLabel<T, M>: ResolveLabels<(U,), Target = M>,
{
type Target = M;

fn resolve_labels(&self, (label,): (Optional<U>,)) -> &M {
self.resolve_labels((label.0,))
}
}

// TODO: macro to autogenerate these

impl<L, M, A, B> ResolveLabels<(A, B)> for WithLabel<L, M>
Expand Down
2 changes: 1 addition & 1 deletion crates/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,4 @@ pub type LabeledFutureMetrics4<A, B, C, D> = Labeled4<FutureMetrics, A, B, C, D>

pub type OptionalEnumLabel<const NAME: LabelName, T> = Optional<EnumLabel<NAME, T>>;
pub type OptionalBoolLabel<const NAME: LabelName> = Optional<BoolLabel<NAME>>;
pub type OptionalStringLabel<const NAME: LabelName, T> = Optional<StringLabel<NAME, T>>;
pub type OptionalStringLabel<const NAME: LabelName, T = String> = Optional<StringLabel<NAME, T>>;
78 changes: 66 additions & 12 deletions crates/metrics/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@
///
/// Usage:
/// ```
/// use wc_metrics::{enum_ordinalize::Ordinalize, BoolLabel, EnumLabel, StringLabel, counter};
/// use wc_metrics::{
/// counter,
/// enum_ordinalize::Ordinalize,
/// BoolLabel,
/// EnumLabel,
/// OptionalBoolLabel,
/// OptionalEnumLabel,
/// OptionalStringLabel,
/// StringLabel,
/// };
///
/// #[derive(Clone, Copy, Debug, Ordinalize)]
/// enum MyEnum {
Expand Down Expand Up @@ -99,8 +108,12 @@
/// StringLabel<"b"> => s,
/// StringLabel<"c", u8> => &u,
/// BoolLabel<"d"> => b,
/// "e" => "1",
/// "f" => "2"
/// OptionalEnumLabel<"e", MyEnum> => Some(e),
/// OptionalStringLabel<"f"> => Some(s),
/// OptionalStringLabel<"g", u8> => Some(&u),
/// OptionalBoolLabel<"h"> => Some(b),
/// "i" => "1",
/// "j" => "2"
/// )
/// .increment(1);
/// ```
Expand All @@ -126,7 +139,16 @@ macro_rules! counter {
///
/// Usage:
/// ```
/// use wc_metrics::{enum_ordinalize::Ordinalize, BoolLabel, EnumLabel, StringLabel, gauge};
/// use wc_metrics::{
/// gauge,
/// enum_ordinalize::Ordinalize,
/// BoolLabel,
/// EnumLabel,
/// OptionalBoolLabel,
/// OptionalEnumLabel,
/// OptionalStringLabel,
/// StringLabel,
/// };
///
/// #[derive(Clone, Copy, Debug, Ordinalize)]
/// enum MyEnum {
Expand Down Expand Up @@ -212,8 +234,12 @@ macro_rules! counter {
/// StringLabel<"b"> => s,
/// StringLabel<"c", u8> => &u,
/// BoolLabel<"d"> => b,
/// "e" => "1",
/// "f" => "2"
/// OptionalEnumLabel<"e", MyEnum> => Some(e),
/// OptionalStringLabel<"f"> => Some(s),
/// OptionalStringLabel<"g", u8> => Some(&u),
/// OptionalBoolLabel<"h"> => Some(b),
/// "i" => "1",
/// "j" => "2"
/// )
/// .set(1);
/// ```
Expand All @@ -239,7 +265,16 @@ macro_rules! gauge {
///
/// Usage:
/// ```
/// use wc_metrics::{enum_ordinalize::Ordinalize, BoolLabel, EnumLabel, StringLabel, histogram};
/// use wc_metrics::{
/// histogram,
/// enum_ordinalize::Ordinalize,
/// BoolLabel,
/// EnumLabel,
/// OptionalBoolLabel,
/// OptionalEnumLabel,
/// OptionalStringLabel,
/// StringLabel,
/// };
///
/// #[derive(Clone, Copy, Debug, Ordinalize)]
/// enum MyEnum {
Expand Down Expand Up @@ -325,8 +360,12 @@ macro_rules! gauge {
/// StringLabel<"b"> => s,
/// StringLabel<"c", u8> => &u,
/// BoolLabel<"d"> => b,
/// "e" => "1",
/// "f" => "2"
/// OptionalEnumLabel<"e", MyEnum> => Some(e),
/// OptionalStringLabel<"f"> => Some(s),
/// OptionalStringLabel<"g", u8> => Some(&u),
/// OptionalBoolLabel<"h"> => Some(b),
/// "i" => "1",
/// "j" => "2"
/// )
/// .record(1);
/// ```
Expand All @@ -343,7 +382,18 @@ macro_rules! histogram {
/// Usage:
/// ```
/// use std::future::Future;
/// use wc_metrics::{enum_ordinalize::Ordinalize, BoolLabel, EnumLabel, StringLabel, future_metrics, FutureMetrics, FutureExt};
/// use wc_metrics::{
/// future_metrics,
/// enum_ordinalize::Ordinalize,
/// BoolLabel,
/// EnumLabel,
/// OptionalBoolLabel,
/// OptionalEnumLabel,
/// OptionalStringLabel,
/// StringLabel,
/// FutureExt,
/// FutureMetrics,
/// };
///
/// #[derive(Clone, Copy, Debug, Ordinalize)]
/// enum MyEnum {
Expand Down Expand Up @@ -399,8 +449,12 @@ macro_rules! histogram {
/// StringLabel<"b"> => s,
/// StringLabel<"c", u8> => &u,
/// BoolLabel<"d"> => b,
/// "e" => "1",
/// "f" => "2"
/// OptionalEnumLabel<"e", MyEnum> => Some(e),
/// OptionalStringLabel<"f"> => Some(s),
/// OptionalStringLabel<"g", u8> => Some(&u),
/// OptionalBoolLabel<"h"> => Some(b),
/// "i" => "1",
/// "j" => "2"
/// )));
/// ```
#[cfg(feature = "future")]
Expand Down

0 comments on commit fa69002

Please sign in to comment.