37 lines
997 B
Rust
37 lines
997 B
Rust
|
use axum::{routing::get, Router};
|
||
|
use lazy_static::lazy_static;
|
||
|
use log::warn;
|
||
|
use prometheus::{IntCounterVec, Opts, Registry};
|
||
|
|
||
|
lazy_static! {
|
||
|
pub static ref REGISTRY: Registry = Registry::new();
|
||
|
pub static ref REQUEST_COUNTER: IntCounterVec =
|
||
|
IntCounterVec::new(Opts::new("requests", "Requests"), &["method", "status", "route"])
|
||
|
.unwrap();
|
||
|
}
|
||
|
|
||
|
pub fn init_metrics() {
|
||
|
REGISTRY.register(Box::new(REQUEST_COUNTER.clone())).unwrap();
|
||
|
}
|
||
|
|
||
|
pub async fn serve() {
|
||
|
let app = Router::new().route("/metrics", get(metrics));
|
||
|
|
||
|
let listener = tokio::net::TcpListener::bind("localhost:31756").await.unwrap();
|
||
|
axum::serve(listener, app).await.unwrap();
|
||
|
}
|
||
|
|
||
|
async fn metrics() -> String {
|
||
|
let encoder = prometheus::TextEncoder::new();
|
||
|
let res_custom = encoder.encode_to_string(®ISTRY.gather());
|
||
|
|
||
|
match res_custom {
|
||
|
Ok(s) => s,
|
||
|
Err(e) => {
|
||
|
warn!("Error encoding metrics: {:?}", e);
|
||
|
|
||
|
String::new()
|
||
|
}
|
||
|
}
|
||
|
}
|