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 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: Clone + Hash + Eq, M, C> Family<S, M, C>

source

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>

source

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();
source

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())]));
source

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: Clone> Clone for Family<S, M, C>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<S: Debug, M: Debug, C> Debug for Family<S, M, C>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<S: Clone + Hash + Eq, M: Default> Default for Family<S, M>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<S, M, C> EncodeMetric for Family<S, M, C>

source§

fn encode(&self, encoder: MetricEncoder<'_>) -> Result<(), Error>

Encode the given instance in the OpenMetrics text encoding.
source§

fn metric_type(&self) -> MetricType

The OpenMetrics metric type of the instance.
source§

impl<S, M: TypedMetric, C> TypedMetric for Family<S, M, C>

source§

const TYPE: MetricType = <M as TypedMetric>::TYPE

The OpenMetrics metric 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>
where C: Send, S: Send + Sync, M: Send + Sync,

§

impl<S, M, C> Sync for Family<S, M, C>
where C: Sync, S: Send + Sync, M: Send + Sync,

§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Metric for T
where T: EncodeMetric + Send + Sync + Debug + 'static,