pub struct GenericMetricLayer<'a, T, M> { /* private fields */ }Expand description
The tower middleware layer for recording http metrics with different exporters.
Implementations§
Source§impl<'a, T, M> GenericMetricLayer<'a, T, M>where
M: MakeDefaultHandle<Out = T>,
impl<'a, T, M> GenericMetricLayer<'a, T, M>where
M: MakeDefaultHandle<Out = T>,
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new tower middleware that can be used to track metrics.
By default, this will not “install” the exporter which sets it as the
global recorder for all metrics calls.
If you’re using Prometheus, here you can use metrics_exporter_prometheus::PrometheusBuilder
to build your own customized metrics exporter.
This middleware is using the following constants for identifying different HTTP metrics:
In terms of setup, the most important one is AXUM_HTTP_REQUESTS_DURATION_SECONDS, which is a histogram metric
used for request latency. You may set customized buckets tailored for your used case here.
§Example
use axum::{routing::get, Router};
use axum_prometheus::{AXUM_HTTP_REQUESTS_DURATION_SECONDS, utils::SECONDS_DURATION_BUCKETS, PrometheusMetricLayer};
use metrics_exporter_prometheus::{Matcher, PrometheusBuilder};
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
let metric_layer = PrometheusMetricLayer::new();
// This is the default if you use `PrometheusMetricLayer::pair`.
let metric_handle = PrometheusBuilder::new()
.set_buckets_for_metric(
Matcher::Full(AXUM_HTTP_REQUESTS_DURATION_SECONDS.to_string()),
SECONDS_DURATION_BUCKETS,
)
.unwrap()
.install_recorder()
.unwrap();
let app = Router::<()>::new()
.route("/fast", get(|| async {}))
.route(
"/slow",
get(|| async {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}),
)
.route("/metrics", get(|| async move { metric_handle.render() }))
.layer(metric_layer);
// Run the server as usual:
// let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000)))
// .await
// .unwrap();
// axum::serve(listener, app).await.unwrap()
}Sourcepub fn enable_response_body_size(&mut self)
pub fn enable_response_body_size(&mut self)
Enable tracking response body sizes.
Sourcepub fn pair_from(m: M) -> (Self, T)
pub fn pair_from(m: M) -> (Self, T)
Crate a new tower middleware and a default exporter from the provided value of the passed in argument.
This function is useful when additional data needs to be injected into MakeDefaultHandle::make_default_handle.
§Example
use axum_prometheus::{GenericMetricLayer, MakeDefaultHandle};
struct Recorder { host: String }
impl MakeDefaultHandle for Recorder {
type Out = ();
fn make_default_handle(self) -> Self::Out {
// Perform the initialization. `self` is passed in by value.
todo!();
}
}
fn main() {
let (metric_layer, metric_handle) = GenericMetricLayer::pair_from(
Recorder { host: "0.0.0.0".to_string() }
);
}Source§impl<'a, T, M> GenericMetricLayer<'a, T, M>where
M: MakeDefaultHandle<Out = T> + Default,
impl<'a, T, M> GenericMetricLayer<'a, T, M>where
M: MakeDefaultHandle<Out = T> + Default,
Sourcepub fn pair() -> (Self, T)
pub fn pair() -> (Self, T)
Crate a new tower middleware and a default global Prometheus exporter with sensible defaults.
If used with a custom exporter that’s different from Prometheus, the exporter struct
must implement MakeDefaultHandle + Default.
§Example
use axum::{routing::get, Router};
use axum_prometheus::PrometheusMetricLayer;
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
let (metric_layer, metric_handle) = PrometheusMetricLayer::pair();
let app = Router::<()>::new()
.route("/fast", get(|| async {}))
.route(
"/slow",
get(|| async {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}),
)
.route("/metrics", get(|| async move { metric_handle.render() }))
.layer(metric_layer);
// Run the server as usual:
// let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000)))
// .await
// .unwrap();
// axum::serve(listener, app).await.unwrap()
}