#logging-tracing #open-telemetry #axum #logging

axum-otel

OpenTelemetry tracing for axum based on tower-http

15 releases

0.31.0 Nov 18, 2025
0.30.10 Aug 7, 2025
0.30.9-alpha.2 Jul 5, 2025
0.30.8 Jun 26, 2025
0.29.4 Jun 4, 2025

#242 in Debugging

Download history 494/week @ 2025-08-29 364/week @ 2025-09-05 504/week @ 2025-09-12 562/week @ 2025-09-19 352/week @ 2025-09-26 302/week @ 2025-10-03 307/week @ 2025-10-10 260/week @ 2025-10-17 247/week @ 2025-10-24 331/week @ 2025-10-31 557/week @ 2025-11-07 807/week @ 2025-11-14 823/week @ 2025-11-21 981/week @ 2025-11-28 914/week @ 2025-12-05 397/week @ 2025-12-12

3,221 downloads per month

MIT/Apache

105KB
1.5K SLoC

axum-otel

A structured logging middleware for Axum web framework that integrates with OpenTelemetry.

Features

  • Structured logging middleware for Axum
  • OpenTelemetry integration
  • Request tracing
  • Metrics collection
  • Customizable span attributes

Installation

Add this to your Cargo.toml:

[dependencies]
axum-otel = "0.29.0"
axum = { version = "0.8", features = ["macros"] }
tower-http = { version = "0.6.5", features = ["trace"] }
opentelemetry = { version = "0.29.0", features = ["metrics"] }
opentelemetry_sdk = { version = "0.29.0", features = ["rt-tokio"] }
opentelemetry-otlp = { version = "0.29.0", features = ["metrics", "grpc-tonic"] }

Quick Start

use axum::{
    routing::get,
    Router,
};
use axum_otel::{AxumOtelOnFailure, AxumOtelOnResponse, AxumOtelSpanCreator};
use opentelemetry::sdk::trace::Config;
use opentelemetry_otlp::{WithExportConfig, Protocol};
use std::net::SocketAddr;
use tower_http::trace::TraceLayer;
use tracing::Level;

async fn handler() -> &'static str {
    "Hello, world!"
}

#[tokio::main]
async fn main() {
    // Initialize OpenTelemetry
    let tracer = opentelemetry_otlp::new_pipeline()
        .tracing()
        .with_exporter(
            opentelemetry_otlp::new_exporter()
                .tonic()
                .with_endpoint("http://localhost:4317")
                .with_protocol(Protocol::Grpc)
        )
        .with_trace_config(Config::default())
        .install_batch(opentelemetry::runtime::Tokio)
        .expect("Failed to initialize OpenTelemetry");

    // Build our application with a route
    let app = Router::new()
        .route("/", get(handler))
        .layer(
            TraceLayer::new_for_http()
                .make_span_with(AxumOtelSpanCreator::new().level(Level::INFO))
                .on_response(AxumOtelOnResponse::new().level(Level::INFO))
                .on_failure(AxumOtelOnFailure::new()),
        );

    // Run it
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    tracing::debug!("listening on {}", addr);
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

Examples

Check out the examples directory for more usage examples:

Documentation

For more detailed documentation, visit docs.rs.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under either of

at your option.

Dependencies

~11–17MB
~231K SLoC