| Crates.io | axum-otel |
| lib.rs | axum-otel |
| version | 0.29.5 |
| created_at | 2025-06-02 16:57:15.121809+00 |
| updated_at | 2025-09-01 10:34:36.147258+00 |
| description | OpenTelemetry tracing for axum. |
| homepage | |
| repository | https://github.com/iamnivekx/axum-otel |
| max_upload_size | |
| id | 1698139 |
| size | 74,177 |
A structured logging middleware for Axum web framework that integrates with OpenTelemetry.
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"] }
use axum::{
routing::get,
Router,
};
use axum_otel::{AxumOtelOnFailure, AxumOtelOnResponse, AxumOtelSpanCreator};
use opentelemetry::sdk::trace::Config;
use opentelemetry_otlp::WithExportConfig;
use std::net::SocketAddr;
use tower_http::trace::TraceLayer;
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();
}
async fn handler() -> &'static str {
"Hello, World!"
}
Check out the examples directory for more usage examples:
For more detailed documentation, visit docs.rs.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under either of
at your option.