Expand description
OpenTelemetry instrumentation middleware for Tower-compatible HTTP servers (Axum, Hyper, Tonic, etc.).
The middleware produces both metrics and distributed tracing for incoming HTTP requests, following the OpenTelemetry HTTP semantic conventions.
§Layout
http::server— instruments incoming requests (SpanKind::Server) and extracts the trace context from request headers.http::extractors— pluggable route and attribute extractors used by the server layer.
§Metrics
http.server.request.duration— duration of HTTP server requests.http.server.active_requests— number of in-flight HTTP server requests.http.server.request.body.size— size of HTTP server request bodies.http.server.response.body.size— size of HTTP server response bodies.
§Tracing
A server span (SpanKind::Server) is created per request, with attributes such
as http.request.method, url.scheme, url.path, url.query,
user_agent.original, http.route, and http.response.status_code.
The value of a query parameter that can carry a credential, such as sig, is
replaced with REDACTED. Use
http::server::LayerBuilder::with_sensitive_query_parameters to name the
keys yourself.
§Quick start
With the default axum feature enabled, applying the middleware is a single
http::server::Layer::new call:
use axum::{routing::get, Router};
use opentelemetry_instrumentation_tower::http;
let app: Router = Router::new()
.route("/", get(root))
// Apply the OTel layer *after* the routes so that
// `AxumMatchedPathExtractor` can read the matched route template.
.layer(http::server::Layer::new());The layer reads the global TracerProvider and MeterProvider, so configure
those before constructing the layer.
§Customization
Use http::server::LayerBuilder to plug in custom extractors:
http::extractors::RouteExtractordecides how thehttp.routeattribute (and span name) is produced. Built-in choices:http::extractors::NoRouteExtractor,http::extractors::PathExtractor, [http::extractors::AxumMatchedPathExtractor] (requires theaxumfeature), orhttp::extractors::FnRouteExtractor.http::extractors::RequestAttributeExtractor/http::extractors::ResponseAttributeExtractorlet you attach additional attributes to spans and metrics. The default ishttp::extractors::NoOpExtractor.
See http::extractors::RouteExtractor for cardinality guidance — picking the
wrong extractor can blow up the cardinality of your metrics.
§Cargo features
axum(default-off) — enables [http::extractors::AxumMatchedPathExtractor] and makes it the default route extractor. Without this feature the default extractor ishttp::extractors::NoRouteExtractor(method-only span names, nohttp.routeattribute).
§Examples
Runnable end-to-end examples live in the examples/ directory of the
opentelemetry-rust-contrib repository.
Modules§
Structs§
- Error
- Error type for
opentelemetry_instrumentation_tower.