use std::ops::{Deref, DerefMut};
use metrics_exporter_prometheus::{Matcher, PrometheusBuilder, PrometheusHandle};
use crate::contracts::{Application, Service};
pub struct Metrics(PrometheusHandle);
impl Service for Metrics {
fn register<A: Application + ?Sized>() -> Self
where
Self: Sized,
{
const EXPONENTIAL_SECONDS: &[f64] = &[
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
];
let recorder = PrometheusBuilder::new()
.set_buckets_for_metric(
Matcher::Full("http_requests_duration_seconds".to_string()),
EXPONENTIAL_SECONDS,
)
.unwrap()
.install_recorder()
.unwrap();
Self(recorder)
}
}
impl Deref for Metrics {
type Target = PrometheusHandle;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Metrics {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}