axum OpenTelemetry metrics middleware
follow Semantic Conventions for HTTP Metrics
axum is an ergonomic and modular web framework built with Tokio, Tower, and Hyper
metrics will be recorded on the global opentelemetry meter:
requests_total
counter
requests_total
http_server_active_requests
gauge
The number of active HTTP requests
http_server_request_duration_seconds
histogram
http_server_request_duration_seconds_bucket
http_server_request_duration_seconds_sum
http_server_request_duration_seconds_count
http_server_request_size_bytes
histogram
http_server_request_size_bytes_bucket
http_server_request_size_bytes_sum
http_server_request_size_bytes_count
http_server_response_size_bytes_
histogram
http_server_response_size_bytes_bucket
http_server_response_size_bytes_sum
http_server_response_size_bytes_count
labels for requests_total
,
http_server_request_duration_seconds
, http_server_request_size_bytes
,
http_server_response_size_bytes
:
http_request_method
http_route
http_response_status_code
server_address
labels for http_server_active_requests
:
http_request_method
url_scheme
use axum_otel_metrics::HttpMetricsLayerBuilder;
let metrics = HttpMetricsLayerBuilder::new()
.build();
let app = Router::new()
.route("/", get(handler))
.route("/hello", get(handler))
.route("/world", get(handler))
// add the metrics middleware
.layer(metrics);
use axum_otel_metrics::HttpMetricsLayerBuilder;
#[derive(Clone)]
pub struct SharedState {
}
let state = SharedState {
};
let metrics = HttpMetricsLayerBuilder::new()
.build();
let app = Router::new()
.route("/", get(handler))
.route("/hello", get(handler))
.route("/world", get(handler))
// add the metrics middleware
.layer(metrics)
.with_state(state.clone());
https://opentelemetry.io/docs/instrumentation/rust/#status-and-releases
Traces | Metrics | Logs |
---|---|---|
Beta | Alpha | Alpha |
Push Metric Exporter https://opentelemetry.io/docs/reference/specification/metrics/sdk/#push-metric-exporter
Pull Metric Exporter https://opentelemetry.io/docs/reference/specification/metrics/sdk/#pull-metric-exporter
https://opentelemetry.io/docs/reference/specification/metrics/sdk_exporters/
In-memory https://opentelemetry.io/docs/reference/specification/metrics/sdk_exporters/in-memory/
Prometheus https://opentelemetry.io/docs/reference/specification/metrics/sdk_exporters/prometheus/
OTLP https://opentelemetry.io/docs/reference/specification/metrics/sdk_exporters/otlp/
Standard output https://opentelemetry.io/docs/reference/specification/metrics/sdk_exporters/stdout/
https://opentelemetry.io/docs/reference/specification/metrics/data-model/
https://github.com/nlopes/actix-web-prom
Actix-web middleware to expose Prometheus metrics
https://github.com/sd2k/rocket_prometheus
Prometheus fairing and handler for Rocket
https://github.com/Ptrskay3/axum-prometheus
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.