Trait open_metrics_client::metrics::family::MetricConstructor[][src]

pub trait MetricConstructor<M> {
    fn new_metric(&self) -> M;
}
Expand description

A constructor for creating new metrics in a Family when calling Family::get_or_create. Such constructor is provided via Family::new_with_constructor.

This is mostly used when creating histograms using constructors that need to capture variables.

struct CustomBuilder {
    buckets: Vec<f64>,
}

impl MetricConstructor<Histogram> for CustomBuilder {
    fn new_metric(&self) -> Histogram {
        // When a new histogram is created, this function will be called.
        Histogram::new(self.buckets.iter().cloned())
    }
}

let custom_builder = CustomBuilder { buckets: vec![0.0, 10.0, 100.0] };
let metric = Family::<(), Histogram, CustomBuilder>::new_with_constructor(custom_builder);

Required methods

Implementors

In cases in which the explicit type of the metric is not required, it is posible to directly provide a closure even if it captures variables.

let custom_buckets = vec![0.0, 10.0, 100.0];
let metric = Family::<(), Histogram, _>::new_with_constructor(|| {
    Histogram::new(custom_buckets.clone().into_iter())
});