Struct prometheus_client::metrics::family::Family
source · 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 struct
s and
EncodeLabelValue
for enum
s.
#[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: Clone + Hash + Eq, M, C> Family<S, M, C>
impl<S: Clone + Hash + Eq, M, C> Family<S, M, C>
sourcepub fn new_with_constructor(constructor: C) -> Self
pub fn new_with_constructor(constructor: C) -> Self
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: Clone + Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C>
impl<S: Clone + Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C>
sourcepub fn get_or_create(&self, label_set: &S) -> MappedRwLockReadGuard<'_, M>
pub fn get_or_create(&self, label_set: &S) -> MappedRwLockReadGuard<'_, 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();
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§fn encode(&self, encoder: MetricEncoder<'_>) -> Result<(), Error>
fn encode(&self, encoder: MetricEncoder<'_>) -> Result<(), Error>
source§fn metric_type(&self) -> MetricType
fn metric_type(&self) -> MetricType
source§impl<S, M: TypedMetric, C> TypedMetric for Family<S, M, C>
impl<S, M: TypedMetric, C> TypedMetric for Family<S, M, C>
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 = 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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)