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

Download history 596/week @ 2025-03-18 916/week @ 2025-03-25 1502/week @ 2025-04-01 1226/week @ 2025-04-08 987/week @ 2025-04-15 3667/week @ 2025-04-22 3959/week @ 2025-04-29 2491/week @ 2025-05-06 1494/week @ 2025-05-13 1494/week @ 2025-05-20 1045/week @ 2025-05-27 570/week @ 2025-06-03 569/week @ 2025-06-10 846/week @ 2025-06-17 811/week @ 2025-06-24 449/week @ 2025-07-01

2,740 downloads per month
Used in 2 crates

MIT license

29KB
393 lines

axum-otel-metrics

Build status Crates.io Documentation

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 or OTEL_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/

where is prometheus exporter?

Metrics Data Model

https://opentelemetry.io/docs/reference/specification/metrics/data-model/

Dependencies

~7–16MB
~188K SLoC