pub fn opentelemetry_tracing_layer() -> TraceLayer<SharedClassifier<ServerErrorsAsFailures>, OtelMakeSpan, OtelOnRequest, OtelOnResponse, OtelOnBodyChunk, OtelOnEos, OtelOnFailure>Expand description
OpenTelemetry tracing middleware.
This returns a TraceLayer configured to use OpenTelemetry’s conventional span field
names.
§Span fields
The following fields will be set on the span:
http.client_ip: The client’s IP address. Requires usingRouter::into_make_service_with_connect_infohttp.flavor: The protocol version used (http 1.1, http 2.0, etc)http.host: The value of theHostheaderhttp.method: The request methodhttp.route: The matched routehttp.scheme: The URI scheme used (HTTPorHTTPS)http.status_code: The response status codehttp.target: The full request target including path and query parametershttp.user_agent: The value of theUser-Agentheaderotel.kind: Alwaysserverotel.status_code:OKif the response is success,ERRORif it is a 5xxtrace_id: The trace id as tracted via the remote span context.
§Example
use axum::{Router, routing::get, http::Request};
use axum_tracing_opentelemetry::opentelemetry_tracing_layer;
use std::net::SocketAddr;
use tower::ServiceBuilder;
let app = Router::new()
.route("/", get(|| async {}))
.layer(opentelemetry_tracing_layer());
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
// we must use `into_make_service_with_connect_info` for `opentelemetry_tracing_layer` to
// access the client ip
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
.await
.expect("server failed");§Complete example
See the “opentelemetry-jaeger” example for a complete setup that includes an OpenTelemetry pipeline sending traces to jaeger.