Struct open_metrics_client::metrics::family::Family[][src]

pub struct Family<S, M, C = fn() -> M> { /* fields omitted */ }
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 Encode derive macro to generate Encode implementation.

#[derive(Clone, Hash, PartialEq, Eq, Encode)]
struct Labels {
  method: Method,
};

#[derive(Clone, Hash, PartialEq, Eq, Encode)]
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

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))
});

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

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.