Trait prometheus_client::metrics::family::MetricConstructor
source · [−]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
fn new_metric(&self) -> M
Implementors
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())
});