pub trait MetricConstructor<M> {
    // Required method
    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§

source

fn new_metric(&self) -> M

Create a new instance of the metric type.

Implementors§

source§

impl<M, F: Fn() -> M> MetricConstructor<M> for F

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 = [0.0, 10.0, 100.0];
let metric = Family::<(), Histogram, _>::new_with_constructor(|| {
    Histogram::new(custom_buckets.into_iter())
});