use std::sync::OnceLock;
use axum::http::{StatusCode, header};
use axum::response::{IntoResponse, Response};
use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle};
use crate::error::ProxyError;
static HANDLE: OnceLock<Result<PrometheusHandle, String>> = OnceLock::new();
pub fn install() -> Result<PrometheusHandle, ProxyError> {
HANDLE
.get_or_init(|| {
PrometheusBuilder::new()
.install_recorder()
.map_err(|e| e.to_string())
})
.clone()
.map_err(ProxyError::Internal)
}
pub async fn handler() -> Response {
match install() {
Ok(handle) => (
StatusCode::OK,
[(header::CONTENT_TYPE, "text/plain; version=0.0.4")],
handle.render(),
)
.into_response(),
Err(err) => err.into_response(),
}
}