axum-tracing-opentelemetry
Middlewares and tools 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
For examples, you can look at:
- the examples folder
- davidB/sandbox_axum_observability: Sandbox to experiment axum and observability. This example shows also propagation of the trace between tracing span and service (via reqwest).
//...
use axum_tracing_opentelemetry::{
opentelemetry_tracing_layer,
// optional tools to init tracer (require features)
CollectorKind, init_tracer
};
fn init_tracing() {
let otel_tracer = init_tracer(CollectorKind::Otlp).expect("setup of Tracer");
let otel_layer = tracing_opentelemetry::layer().with_tracer(otel_tracer);
let subscriber = tracing_subscriber::registry()
//...
.with(otel_layer);
tracing::subscriber::set_global_default(subscriber).unwrap();
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
init_tracing();
let app = app();
// run it
let addr = &"0.0.0.0:3000".parse::<SocketAddr>()?;
tracing::warn!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.with_graceful_shutdown(shutdown_signal())
.await?;
Ok(())
}
fn app() -> Router {
// build our application with a route
Router::new()
.route("/health", get(health))
// opentelemetry_tracing_layer setup `TraceLayer`, that is provided by tower-http so you have to add that as a dependency.
.layer(opentelemetry_tracing_layer())
}
async fn shutdown_signal() {
//...
opentelemetry::global::shutdown_tracer_provider();
}
History
0.1.0
- Code originally created at part of axum-extra Add OpenTelemetry middleware by davidpdrsn · Pull Request #769 · tokio-rs/axum
- Code copied and modified as part of davidB/sandbox_axum_observability: Sandbox to experiment axum and observability
- Published as a standalone crate with OK from original author