| Crates.io | prometheus-axum-middleware |
| lib.rs | prometheus-axum-middleware |
| version | 0.1.1 |
| created_at | 2025-06-13 11:48:38.7252+00 |
| updated_at | 2025-06-13 11:48:38.7252+00 |
| description | An axum layer to add prometheus metrics |
| homepage | |
| repository | https://github.com/veo-technologies/prometheus-axum-middleware |
| max_upload_size | |
| id | 1711359 |
| size | 69,286 |
Middleware and utilities for integrating Prometheus metrics with Axum applications using the default Prometheus registry.
This crate is different from axum-prometheus since it uses directly the real prometheus implementation behind the scenes, while axum-prometheus uses metrics behind the scenes.
The advantage is that you will be easily able to add your own metrics to the same registry using the builtin prometheus macros / apis.
This crate also supports the prometheus remote write protocol, so you can push metrics to a Prometheus Pushgateway or remote write endpoint.
The output is encoded using protobuffers and compressed with snappy, using the builtin prometheus_reqwest_remote_write crate.
If you do not need the remote write support you can build the crate without the default features, this will remove most of the dependencies of the crate.
/metrics endpoint compatible with Prometheus scraping.These metrics can be renamed by specifying environmental variables in your environment or by using the set_prefix function before starting using them:
AXUM_HTTP_REQUESTS_TOTAL -> default value axum_http_request_totalAXUM_HTTP_REQUESTS_DURATION_SECONDS -> default value axum_http_requests_duration_secondsAXUM_HTTP_REQUESTS_PENDING -> default value axum_http_requests_pendingAXUM_HTTP_RESPONSE_BODY_SIZE -> default value axum_http_response_bodyUsing set_prefix with value myservice the names of the counters will be:
set_prefix - Set a prefix for all HTTP metrics (should be called before the first request).add_excluded_paths - Exclude a slice of paths from metrics collection (e.g., /healthcheck).PrometheusAxumLayer - Axum middleware to record Prometheus metrics for each HTTP request.render - Handler for the /metrics endpoint, returns all metrics in Prometheus text format.install_pusher - Periodically push metrics to a Prometheus Pushgateway or remote write endpoint.Add prometheus-axum-middleware to your Cargo.toml.
[dependencies]
prometheus-axum-middleware = "0.1.0"
use prometheus_axum_middleware::{set_prefix, add_excluded_paths, PrometheusAxumLayer, render};
use axum::{Router, routing};
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
// Set up metrics before starting your Axum app
set_prefix("myservice");
add_excluded_paths(&["/healthcheck"]);
let app = Router::new()
.route("/test_body_size", routing::get(async || "Hello, World!"))
.route("/metrics", routing::get(render))
.layer(PrometheusAxumLayer::new());
// Optionally, call `install_pusher` to push metrics to a remote endpoint
let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000)))
.await
.unwrap();
axum::serve(listener, app).await.unwrap()
}
This feature allows you to push metrics to a Prometheus Pushgateway using the remote write endpoint.
The data sent to this endpoint is encoded using protobuffers and compressed with snappy, using the builtin prometheus_reqwest_remote_write crate.
This feature is enabled by default and brings in several dependencies (reqwest, tracing, base64...). If you do not need the remote write support you can build the crate without the default features.