Crate axum_otel

Crate axum_otel 

Source
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

See the examples directory for complete examples.

Structs§

AxumOtelOnFailure
An implementor of OnFailure which records the failure status code.
AxumOtelOnResponse
An implementor of OnResponse which records the response status code and latency.
AxumOtelSpanCreator
An implementor of MakeSpan which creates tracing spans populated with information about the request received by an axum web server.
Level
Describes the level of verbosity of a span or event.