pub struct Family<S, M, C = fn() -> M> { /* private fields */ }Expand description
Representation of the OpenMetrics MetricFamily data type.
A Family is a set of metrics with the same name, help text and
type, differentiated by their label values thus spanning a multidimensional
space.
§Generic over the label set
A Family is generic over the label type. For convenience one might
choose a Vec<(String, String)>, for performance and/or type safety one might
define a custom type.
§Examples
§Family with Vec<(String, String)> for convenience
let family = Family::<Vec<(String, String)>, Counter>::default();
// Record a single HTTP GET request.
family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc();
§Family with custom type for performance and/or type safety
Using EncodeLabelSet and EncodeLabelValue derive macro to generate
EncodeLabelSet for structs and
EncodeLabelValue for enums.
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
struct Labels {
method: Method,
};
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelValue)]
enum Method {
GET,
PUT,
};
let family = Family::<Labels, Counter>::default();
// Record a single HTTP GET request.
family.get_or_create(&Labels { method: Method::GET }).inc();Implementations§
Source§impl<S, M, C> Family<S, M, C>
impl<S, M, C> Family<S, M, C>
Sourcepub fn new_with_constructor(constructor: C) -> Family<S, M, C>
pub fn new_with_constructor(constructor: C) -> Family<S, M, C>
Create a metric family using a custom constructor to construct new metrics.
When calling Family::get_or_create a Family needs to be able to
construct a new metric in case none exists for the given label set. In
most cases, e.g. for Counter
Family can just use the Default::default implementation for the
metric type. For metric types such as
Histogram one might want
Family to construct a
Histogram with custom buckets
(see example below). For such case one can use this method. For more
involved constructors see MetricConstructor.
Family::<Vec<(String, String)>, Histogram>::new_with_constructor(|| {
Histogram::new(exponential_buckets(1.0, 2.0, 10))
});Source§impl<S, M, C> Family<S, M, C>
impl<S, M, C> Family<S, M, C>
Sourcepub fn get_or_create_owned(&self, label_set: &S) -> M
pub fn get_or_create_owned(&self, label_set: &S) -> M
Access a metric with the given label set, creating it if one does not yet exist.
let family = Family::<Vec<(String, String)>, Counter>::default();
// Will create and return the metric with label `method="GET"` when first called.
family.get_or_create_owned(&vec![("method".to_owned(), "GET".to_owned())]).inc();
// Will return a clone of the existing metric on all subsequent calls.
family.get_or_create_owned(&vec![("method".to_owned(), "GET".to_owned())]).inc();Callers wishing to avoid a clone of the metric M can call Family::get_or_create() to
return a reference to the metric instead.
Source§impl<S, M, C> Family<S, M, C>
impl<S, M, C> Family<S, M, C>
Sourcepub fn get_or_create(
&self,
label_set: &S,
) -> MappedRwLockReadGuard<'_, RawRwLock, M>
pub fn get_or_create( &self, label_set: &S, ) -> MappedRwLockReadGuard<'_, RawRwLock, M>
Access a metric with the given label set, creating it if one does not yet exist.
let family = Family::<Vec<(String, String)>, Counter>::default();
// Will create the metric with label `method="GET"` on first call and
// return a reference.
family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc();
// Will return a reference to the existing metric on all subsequent
// calls.
family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc();NB: This method can cause deadlocks if multiple metrics within this family are read at
once. Use Family::get_or_create_owned() if you would like to avoid this by cloning the
metric M.
Sourcepub fn get(
&self,
label_set: &S,
) -> Option<MappedRwLockReadGuard<'_, RawRwLock, M>>
pub fn get( &self, label_set: &S, ) -> Option<MappedRwLockReadGuard<'_, RawRwLock, M>>
Access a metric with the given label set, returning None if one does not yet exist.
let family = Family::<Vec<(String, String)>, Counter>::default();
if let Some(metric) = family.get(&vec![("method".to_owned(), "GET".to_owned())]) {
metric.inc();
};Sourcepub fn remove(&self, label_set: &S) -> bool
pub fn remove(&self, label_set: &S) -> bool
Remove a label set from the metric family.
Returns a bool indicating if a label set was removed or not.
let family = Family::<Vec<(String, String)>, Counter>::default();
// Will create the metric with label `method="GET"` on first call and
// return a reference.
family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc();
// Will return `true`, indicating that the `method="GET"` label set was
// removed.
assert!(family.remove(&vec![("method".to_owned(), "GET".to_owned())]));Sourcepub fn clear(&self)
pub fn clear(&self)
Clear all label sets from the metric family.
let family = Family::<Vec<(String, String)>, Counter>::default();
// Will create the metric with label `method="GET"` on first call and
// return a reference.
family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc();
// Clear the family of all label sets.
family.clear();Trait Implementations§
Source§impl<S, M, C> EncodeMetric for Family<S, M, C>where
S: Clone + Hash + Eq + EncodeLabelSet,
M: EncodeMetric + TypedMetric,
C: MetricConstructor<M>,
impl<S, M, C> EncodeMetric for Family<S, M, C>where
S: Clone + Hash + Eq + EncodeLabelSet,
M: EncodeMetric + TypedMetric,
C: MetricConstructor<M>,
Source§impl<S, M, C> TypedMetric for Family<S, M, C>where
M: TypedMetric,
impl<S, M, C> TypedMetric for Family<S, M, C>where
M: TypedMetric,
Source§const TYPE: MetricType = <M as TypedMetric>::TYPE
const TYPE: MetricType = <M as TypedMetric>::TYPE
Auto Trait Implementations§
impl<S, M, C> Freeze for Family<S, M, C>where
C: Freeze,
impl<S, M, C = fn() -> M> !RefUnwindSafe for Family<S, M, C>
impl<S, M, C> Send for Family<S, M, C>
impl<S, M, C> Sync for Family<S, M, C>
impl<S, M, C> Unpin for Family<S, M, C>where
C: Unpin,
impl<S, M, C> UnsafeUnpin for Family<S, M, C>where
C: UnsafeUnpin,
impl<S, M, C = fn() -> M> !UnwindSafe for Family<S, M, C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more