36 releases (18 breaking)

0.21.1 Sep 23, 2024
0.19.1 Aug 31, 2024
0.19.0 Jun 30, 2024
0.18.0 Mar 9, 2024
0.2.1 Jun 11, 2022

#333 in Debugging

Download history 12385/week @ 2024-07-16 16504/week @ 2024-07-23 49668/week @ 2024-07-30 42147/week @ 2024-08-06 50735/week @ 2024-08-13 65667/week @ 2024-08-20 33170/week @ 2024-08-27 40924/week @ 2024-09-03 41312/week @ 2024-09-10 46204/week @ 2024-09-17 47794/week @ 2024-09-24 54398/week @ 2024-10-01 39176/week @ 2024-10-08 52075/week @ 2024-10-15 46483/week @ 2024-10-22 45572/week @ 2024-10-29

190,370 downloads per month
Used in 8 crates (7 directly)

CC0 license

51KB
722 lines

axum-tracing-opentelemetry

crates license crate version

Project Status: Active – The project has reached a stable, usable state and is being actively developed.

Middlewares to integrate axum + tracing + opentelemetry.

  • Read OpenTelemetry header from incoming request
  • Start a new trace if no trace found in the incoming request
  • Trace is attached into tracing'span
  • OpenTelemetry Span is created on close of the tracing's span (behavior from [tracing-opentelemetry])

For examples, you can look at the examples folder.

//...
use axum_tracing_opentelemetry::opentelemetry_tracing_layer;

#[tokio::main]
async fn main() -> Result<(), axum::BoxError> {
    // very opinionated init of tracing, look as is source to make your own
    init_tracing_opentelemetry::tracing_subscriber_ext::init_subscribers()?;

    let app = app();
    // run it
    let addr = &"0.0.0.0:3000".parse::<SocketAddr>()?;
    tracing::warn!("listening on {}", addr);
    let listener = tokio::net::TcpListener::bind(addr).await?;
    axum::serve(listener, app.into_make_service())
        //FIXME .with_graceful_shutdown(shutdown_signal())
        .await?;
    Ok(())
}

fn app() -> Router {
    Router::new()
        .route("/", get(index)) // request processed inside span
        // include trace context as header into the response
        .layer(OtelInResponseLayer::default())
        //start OpenTelemetry trace on incoming request
        .layer(OtelAxumLayer::default())
        .route("/health", get(health)) // request processed without span / trace
}

async fn shutdown_signal() {
    //...
    opentelemetry::global::shutdown_tracer_provider();
}

For more info about how to initialize, you can look at crate init-tracing-opentelemetry or tracing-opentelemetry.

Changelog - History

CHANGELOG.md

Dependencies

~8.5MB
~146K SLoC