Skip to main content

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

§HTTP span attributes

Field names follow the OpenTelemetry HTTP traces semantic conventions where applicable. If you upgrade from older releases, update queries and dashboards:

Previous attributeReplacement
http.hostserver.address
http.user_agentuser_agent.original
http.client_ipclient.address

http.response.status_code is recorded as an integer when the response is sent (AxumOtelOnResponse).

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.