Axum-Prometheus
A middleware to collect HTTP metrics for Axum applications.
axum-prometheus relies on metrics.rs and its ecosystem to collect and export metrics - for instance for Prometheus, metrics_exporter_prometheus is used as a backend to interact with Prometheus.
Metrics
By default, three HTTP metrics are tracked
axum_http_requests_total(labels: endpoint, method, status): the total number of HTTP requests handled (counter)axum_http_requests_duration_seconds(labels: endpoint, method, status): the request duration for all HTTP requests handled (histogram)axum_http_requests_pending(labels: endpoint, method): the number of currently in-flight requests (gauge)
This crate also allows to track response body sizes as a histogram — see PrometheusMetricLayerBuilder::enable_response_body_size.
Renaming Metrics
These metrics can be renamed by specifying environmental variables at compile time:
AXUM_HTTP_REQUESTS_TOTALAXUM_HTTP_REQUESTS_DURATION_SECONDSAXUM_HTTP_REQUESTS_PENDINGAXUM_HTTP_RESPONSE_BODY_SIZE(if body size tracking is enabled)
These environmental variables can be set in your .cargo/config.toml since Cargo 1.56:
[]
= "my_app_requests_total"
= "my_app_requests_duration_seconds"
= "my_app_requests_pending"
= "my_app_response_body_size"
..or optionally use PrometheusMetricLayerBuilder::with_prefix function.
Compatibility
| Axum Version | Crate Version |
|---|---|
0.5 |
0.1 |
0.6 |
0.2, 0.3, 0.4 |
0.7 |
0.5, 0.6, 0.7 |
0.8 |
0.8, 0.9, 0.10 |
MSRV
This crate's current MSRV is 1.75.
Usage
For more elaborate use-cases, see the builder example.
Add axum-prometheus to your Cargo.toml.
[]
= "0.10.0"
Then you instantiate the prometheus middleware:
use ;
use ;
use PrometheusMetricLayer;
async
Note that the /metrics endpoint is not automatically exposed, so you need to add that as a route manually.
Calling the /metrics endpoint will expose your metrics:
axum_http_requests_total{method="GET",endpoint="/metrics",status="200"} 5
axum_http_requests_pending{method="GET",endpoint="/metrics"} 1
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="0.005"} 4
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="0.01"} 4
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="0.025"} 4
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="0.05"} 4
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="0.1"} 4
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="0.25"} 4
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="0.5"} 4
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="1"} 4
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="2.5"} 4
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="5"} 4
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="10"} 4
axum_http_requests_duration_seconds_bucket{method="GET",status="200",endpoint="/metrics",le="+Inf"} 4
axum_http_requests_duration_seconds_sum{method="GET",status="200",endpoint="/metrics"} 0.001997171
axum_http_requests_duration_seconds_count{method="GET",status="200",endpoint="/metrics"} 4
Let's note that since metrics-exporter-prometheus = "0.13" that crate introduced the push-gateway default feature, that
requires openssl support. The axum_prometheus crate does not rely on, nor enable this feature by default — if you need it,
you may enable it through the "push-gateway" feature in axum_prometheus.
Prometheus push gateway feature
This crate currently has no higher level API for the push-gateway feature. If you plan to use it, enable the push-gateway feature in axum-prometheus, use BaseMetricLayer, and setup your recorder manually, similar to the base-metric-layer-example.
Using a different exporter than Prometheus
This crate may be used with other exporters than Prometheus. First, disable the default features:
= { = "0.10.0", = false }
Then implement the MakeDefaultHandle for the provider you'd like to use. For StatsD:
use StatsdBuilder;
use ;
// The custom StatsD exporter struct. It may take fields as well.
// In order to use this with `axum_prometheus`, we must implement `MakeDefaultHandle`.
It's also possible to use GenericMetricLayer::pair, however it's only callable if the recorder struct implements Default as well.
use StatsdBuilder;
use ;
This crate is similar to (and takes inspiration from) actix-web-prom and rocket_prometheus,
and also builds on top of davidpdrsn's earlier work with LifeCycleHooks in tower-http.