17 releases (11 breaking)
0.12.0 | Jun 20, 2025 |
---|---|
0.10.0 | Feb 13, 2025 |
0.9.1 | Dec 10, 2024 |
0.9.0-alpha.2 | Aug 4, 2024 |
0.2.0 | Nov 25, 2022 |
#68 in HTTP server
2,740 downloads per month
Used in 2 crates
29KB
393 lines
axum-otel-metrics
axum OpenTelemetry metrics middleware with OTLP exporter
follows Semantic Conventions for HTTP Metrics
axum is an ergonomic and modular web framework built with Tokio, Tower, and Hyper
Note: Prometheus exporter is removed now
Development of the Prometheus exporter has been discontinued. See the related issue. This crate depends on the unmaintained
protobuf
crate and has unresolved security vulnerabilities. Version 0.29 will be the final release.For Prometheus integration, we strongly recommend using the [OTLP] exporter instead. Prometheus natively supports OTLP, providing a more stable and actively maintained solution. for more details please check https://github.com/open-telemetry/opentelemetry-rust/blob/opentelemetry-0.30.0/opentelemetry-prometheus/README.md and https://github.com/ttys3/axum-otel-metrics/issues/176
Usage
Uses the OTLP Exporter to send metrics to OpenTelemetry collector.
You can configure it via environment variables:
OTEL_EXPORTER_OTLP_ENDPOINT
orOTEL_EXPORTER_OTLP_METRICS_ENDPOINT
OTEL_EXPORTER_OTLP_ENDPOINT
default value:
- gRPC:
http://localhost:4317
- HTTP:
http://localhost:4318
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
default value:
- gRPC:
http://localhost:4317
- HTTP:
http://localhost:4318/v1/metrics
For more details, see https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/#endpoint-configuration
use axum_otel_metrics::HttpMetricsLayerBuilder;
use axum::{response::Html, routing::get, Router};
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider, Temporality};
use opentelemetry::global;
let exporter = opentelemetry_otlp::MetricExporter::builder()
.with_http()
.with_temporality(Temporality::default())
.build()
.unwrap();
let reader = PeriodicReader::builder(exporter)
.with_interval(std::time::Duration::from_secs(30))
.build();
let provider = SdkMeterProvider::builder()
.with_reader(reader)
.build();
// TODO: ensure defer run `provider.shutdown()?;`
global::set_meter_provider(provider.clone());
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);
async fn handler() -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}
Exported Metrics
The following metrics are exported following OpenTelemetry semantic conventions:
http.server.active_requests (UpDownCounter)
- The number of active HTTP requests
- Labels:
http.request.method
,url.scheme
http.server.request.duration (Histogram)
- The HTTP request latencies in seconds
- Labels:
http.request.method
,http.route
,http.response.status_code
,server.address
http.server.request.body.size (Histogram)
- The HTTP request sizes in bytes
- Labels:
http.request.method
,http.route
,http.response.status_code
,server.address
http.server.response.body.size (Histogram)
- The HTTP response sizes in bytes
- Labels:
http.request.method
,http.route
,http.response.status_code
,server.address
OpenTelemetry Rust Instrumentation Status and Releases
https://opentelemetry.io/docs/instrumentation/rust/#status-and-releases
Traces | Metrics | Logs |
---|---|---|
Beta | Beta | Beta |
OpenTelemetry Metrics Exporter
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
exporters
https://opentelemetry.io/docs/reference/specification/metrics/sdk_exporters/
- In-memory https://opentelemetry.io/docs/reference/specification/metrics/sdk_exporters/in-memory/
- OTLP https://opentelemetry.io/docs/reference/specification/metrics/sdk_exporters/otlp/
- Standard output https://opentelemetry.io/docs/reference/specification/metrics/sdk_exporters/stdout/
where is prometheus exporter?
Metrics Data Model
https://opentelemetry.io/docs/reference/specification/metrics/data-model/
Related Projects
- 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
Dependencies
~7–16MB
~188K SLoC