axum_otel/
lib.rs

1#![deny(unsafe_code)]
2#![warn(
3    missing_docs,
4    missing_debug_implementations,
5    missing_copy_implementations,
6    trivial_casts,
7    trivial_numeric_casts,
8    unused_import_braces,
9    unused_qualifications
10)]
11#![doc(html_root_url = "https://docs.rs/axum-otel/latest")]
12#![macro_use]
13#![allow(unused_imports)]
14
15//! OpenTelemetry tracing for axum based on tower-http.
16//!
17//! This crate provides a middleware for Axum web framework that automatically instruments HTTP requests
18//! and responses, and adds OpenTelemetry tracing to the request and response spans.
19//!
20//! ## Features
21//!
22//! - Automatic request and response tracing
23//! - OpenTelemetry integration
24//! - Request ID tracking
25//! - Customizable span attributes
26//! - Error tracking
27//!
28//! ## Usage
29//!
30//! ```rust
31//! use axum::{
32//!     routing::get,
33//!     Router,
34//! };
35//! use axum_otel::{AxumOtelOnFailure, AxumOtelOnResponse, AxumOtelSpanCreator, Level};
36//! use tower_http::trace::TraceLayer;
37//!
38//! async fn handler() -> &'static str {
39//!     "Hello, world!"
40//! }
41//!
42//! // Build our application with a route
43//! let app: Router<()> = Router::new()
44//!     .route("/", get(handler))
45//!     .layer(
46//!         TraceLayer::new_for_http()
47//!             .make_span_with(AxumOtelSpanCreator::new().level(Level::INFO))
48//!             .on_response(AxumOtelOnResponse::new().level(Level::INFO))
49//!             .on_failure(AxumOtelOnFailure::new()),
50//!     );
51//! ```
52//!
53//! ## Components
54//!
55//! - [`AxumOtelSpanCreator`] - Creates spans for each request with relevant HTTP information
56//! - [`AxumOtelOnResponse`] - Records response status and latency
57//! - [`AxumOtelOnFailure`] - Handles error cases and updates span status
58//!
59//! See the [examples](https://github.com/iamnivekx/axum-otel/tree/main/examples) directory for complete examples.
60//!
61mod make_span;
62mod on_failure;
63mod on_response;
64
65// Exports for the tower-http::trace::TraceLayer based middleware
66pub use make_span::AxumOtelSpanCreator;
67pub use on_failure::AxumOtelOnFailure;
68pub use on_response::AxumOtelOnResponse;
69
70// Re-export the Level enum from tracing crate
71pub use tracing::Level;