nixfleet_control_plane/server/routes/
metrics.rs

1//! `/metrics` - Prometheus text format. mTLS-protected like `/v1/*`;
2//! operator Prometheus scrapes with the same agent identity it presents to
3//! `/v1/hosts`. Returns 404 when the `metrics` feature is off.
4
5use axum::http::StatusCode;
6use axum::response::Response;
7
8#[cfg(feature = "metrics")]
9use axum::response::IntoResponse;
10
11#[cfg(feature = "metrics")]
12use crate::metrics::{install_recorder, record_build_info};
13
14/// `GET /metrics` - render counter state. 404 when `metrics` feature off.
15/// `record_build_info()` is called on every request so `cp_build_info` is
16/// always present in the scrape output even when no other events have fired.
17#[cfg(feature = "metrics")]
18pub(in crate::server) async fn metrics_handler() -> Result<Response, StatusCode> {
19    record_build_info();
20    let body = install_recorder().render();
21    Ok(([("content-type", "text/plain; version=0.0.4")], body).into_response())
22}
23
24#[cfg(not(feature = "metrics"))]
25pub(in crate::server) async fn metrics_handler() -> Result<Response, StatusCode> {
26    Err(StatusCode::NOT_FOUND)
27}