Expand description
OpenTelemetry tracing for axum based on tower-http.
This crate provides a middleware for Axum web framework that automatically instruments HTTP requests and responses, and adds OpenTelemetry tracing to the request and response spans.
§Features
- Automatic request and response tracing
- OpenTelemetry integration
- Request ID tracking
- Customizable span attributes
- Error tracking
§Usage
use axum::{
routing::get,
Router,
};
use axum_otel::{AxumOtelOnFailure, AxumOtelOnResponse, AxumOtelSpanCreator, Level};
use tower_http::trace::TraceLayer;
async fn handler() -> &'static str {
"Hello, world!"
}
// Build our application with a route
let app: Router<()> = 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()),
);§Components
AxumOtelSpanCreator- Creates spans for each request with relevant HTTP informationAxumOtelOnResponse- Records response status and latencyAxumOtelOnFailure- Handles error cases and updates span status
See the examples directory for complete examples.
Structs§
- Axum
Otel OnFailure - An implementor of
OnFailurewhich records the failure status code. - Axum
Otel OnResponse - An implementor of
OnResponsewhich records the response status code and latency. - Axum
Otel Span Creator - An implementor of
MakeSpanwhich createstracingspans populated with information about the request received by anaxumweb server. - Level
- Describes the level of verbosity of a span or event.