pub trait PrometheusMetrics {
// Provided methods
fn get_exponential_seconds() -> Vec<f64> { ... }
fn get_prometheus_handle() -> PrometheusHandle { ... }
fn get_prometheus_app() -> Router { ... }
fn get_metrics_path() -> &'static str { ... }
fn get_metrics_addr() -> String { ... }
fn start_metrics_server<'async_trait>(
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: Send + 'async_trait { ... }
}
Expand description
based on axum/examples/prometheus-metrics/src/main.rs
use axum::{Router, ServiceExt, routing::get, middleware};
use axum_restful::utils::{PrometheusMetrics, track_metrics};
struct Metrics;
impl PrometheusMetrics for Metrics {}
tokio::spawn(async {
Metrics::start_metrics_server().await;
});
let app = Router::new()
.route("/hello", get(|| async {"hello"}))
.route_layer(middleware::from_fn(track_metrics));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
and then you can visit http://127.0.0.1:3000/hello to get response from axum server,
next the visis metrics is recorded in prometheus metrics,
the default prometheus metrics is http://0.0.0.0:3001/metrics.
ip and port can modified by PrometheusMetrics::get_metrics_addr
url path can modified by PrometheusMetrics::get_metrics_path
Provided Methods§
fn get_exponential_seconds() -> Vec<f64>
fn get_prometheus_handle() -> PrometheusHandle
fn get_prometheus_app() -> Router
fn get_metrics_path() -> &'static str
fn get_metrics_addr() -> String
fn start_metrics_server<'async_trait>(
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: Send + 'async_trait,
Object Safety§
This trait is not object safe.