use crate::app::config::Configuration;
use crate::counters;
use rocket::Build;
use rocket::Rocket;
use rocket_prometheus::PrometheusMetrics;

pub fn init(config: &Configuration) -> super::InfallibleResult<PrometheusMetrics> {
    if !config.enable_prometheus {
        return Ok(None);
    }

    let prometheus = PrometheusMetrics::new();

    counters::register_counters(prometheus.registry());

    Ok(Some(prometheus))
}

pub fn register(
    rocket: Rocket<Build>,
    _config: &Configuration,
    prometheus_fairing_and_metrics_routes: PrometheusMetrics,
) -> Rocket<Build> {
    rocket
        .attach(prometheus_fairing_and_metrics_routes.clone())
        .mount("/metrics", prometheus_fairing_and_metrics_routes)
}
