Skip to content

ttys3/axum-otel-metrics

Repository files navigation

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 #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/

Related Projects

About

axum OpenTelemetry Metrics middleware with prometheus exporter

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 6