Skip to main content

max / makenotwork

764 B · 20 lines History Blame Raw
1 use axum::{extract::State, response::IntoResponse};
2 use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle};
3
4 pub fn init() -> PrometheusHandle {
5 PrometheusBuilder::new().install_recorder().expect("install prometheus recorder")
6 }
7
8 pub async fn render(State(handle): State<PrometheusHandle>) -> impl IntoResponse {
9 handle.render()
10 }
11
12 /// A render handle that does NOT install the global recorder — the global is a
13 /// process-wide singleton, so tests (many `AppState`s per process) must not
14 /// call [`init`]. Metrics emitted via the macros are dropped under this handle,
15 /// which is fine for tests that only exercise routes.
16 #[cfg(test)]
17 pub fn test_handle() -> PrometheusHandle {
18 PrometheusBuilder::new().build_recorder().handle()
19 }
20