32 releases (16 breaking)

0.18.1 Apr 24, 2024
0.18.0 Mar 9, 2024
0.17.1 Feb 24, 2024
0.16.0 Dec 30, 2023
0.2.1 Jun 11, 2022

#371 in Profiling

Download history 9959/week @ 2024-01-28 12041/week @ 2024-02-04 14188/week @ 2024-02-11 9299/week @ 2024-02-18 16804/week @ 2024-02-25 10291/week @ 2024-03-03 14564/week @ 2024-03-10 12463/week @ 2024-03-17 16828/week @ 2024-03-24 17475/week @ 2024-03-31 8519/week @ 2024-04-07 11280/week @ 2024-04-14 12111/week @ 2024-04-21 12676/week @ 2024-04-28 10657/week @ 2024-05-05 14036/week @ 2024-05-12

49,732 downloads per month
Used in 8 crates (7 directly)

CC0 license

49KB
711 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

~11MB
~193K SLoC