use axum::extract::State;
use axum::response::IntoResponse;
use crate::NodeDelegate;
pub async fn metrics<S: NodeDelegate>(State(ctx): State<S>) -> axum::response::Result<impl IntoResponse> {
let mut buf = String::new();
let mut encode_to_buffer = |mfs: &[_]| {
if let Err(e) = prometheus::TextEncoder.encode_utf8(mfs, &mut buf) {
log::error!("could not encode custom metrics: {e}");
}
};
encode_to_buffer(&ctx.gather_metrics());
encode_to_buffer(&prometheus::gather());
Ok(buf)
}
pub fn router<S>() -> axum::Router<S>
where
S: NodeDelegate + Clone + 'static,
{
use axum::routing::get;
axum::Router::new().route("/", get(metrics::<S>))
}