use tokio::time::Instant;

use lazy_static::*;
use prometheus::*;

lazy_static! {
    static ref REGISTRY: Registry = Registry::new();
    static ref ENCODER: TextEncoder = TextEncoder::new();
    pub static ref ROUTER_INDEX_WITH_PATH_HISTOGRAM: HistogramVec =
        register_histogram_vec_with_registry!(
            histogram_opts!(
                "fts_server_actions_duration_seconds",
                "fts-server's request timing with action and method"
            ),
            &["index_name", "action", "method"],
            REGISTRY,
        )
        .unwrap();
}

pub(crate) fn since(start: &Instant) -> f64 {
    start.elapsed().as_secs_f64()
}

pub(crate) fn index_metrics() -> String {
    let mut buffer = vec![];
    let metric_families = REGISTRY.gather();
    ENCODER.encode(&metric_families, &mut buffer).unwrap();

    // Output to the standard output.
    String::from_utf8(buffer).unwrap()
}
