| Crates.io | trace_id |
| lib.rs | trace_id |
| version | 0.1.1 |
| created_at | 2025-08-10 19:59:16.883478+00 |
| updated_at | 2025-08-11 04:24:08.053283+00 |
| description | A lightweight, high-performance tracing ID generation and propagation library for Rust, with out-of-the-box support for Axum. |
| homepage | https://github.com/OpenMindOpenWorld/trace_id |
| repository | https://github.com/OpenMindOpenWorld/trace_id |
| max_upload_size | |
| id | 1789252 |
| size | 137,436 |
A lightweight, high-performance trace ID library for Rust applications, designed for seamless integration with web frameworks and the tracing ecosystem. Currently supports Axum with Actix Web support planned for future releases.
x-trace-id header for distributed tracingtokio::task_local! for async safetyAdd this to your Cargo.toml:
[dependencies]
trace_id = "0.1.1"
# For Axum integration
trace_id = { version = "0.1.1", features = ["axum"] }
use axum::{routing::get, Router};
use trace_id::{TraceId, TraceIdLayer};
use tracing::info;
async fn handler(trace_id: TraceId) -> String {
info!("Processing request"); // Automatically includes trace_id
format!("Hello! Trace ID: {}", trace_id)
}
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(handler))
.layer(TraceIdLayer::new());
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
use trace_id::get_trace_id;
async fn business_logic() {
let trace_id = get_trace_id();
// Use trace_id for external system integration
external_api_call(&trace_id).await;
tracing::info!("Business logic completed");
}
TraceIdA lightweight wrapper around a high-performance trace identifier that represents a unique trace ID compliant with W3C TraceContext specification.
use trace_id::TraceId;
// Generate a new trace ID
let trace_id = TraceId::new();
// Parse from string (with validation)
let trace_id = TraceId::from_string_validated("550e8400-e29b-41d4-a716-446655440000")?;
// Convert to string
let id_string = trace_id.to_string();
TraceIdLayerAxum middleware that automatically manages trace ID lifecycle:
x-trace-id headeruse axum::Router;
use trace_id::TraceIdLayer;
let app = Router::new()
.route("/api/users", get(get_users))
.layer(TraceIdLayer::new());
Access the current trace ID from anywhere in your async call stack:
use trace_id::get_trace_id;
async fn deep_function() {
let trace_id = get_trace_id();
tracing::info!(trace_id = %trace_id, "Deep in the call stack");
}
To see trace_id in your logs, you must properly configure your tracing subscriber:
use tracing_subscriber::{EnvFilter, fmt, prelude::*};
// Basic configuration
tracing_subscriber::fmt::init();
// Or more detailed configuration
tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_default_env())
.init();
Without proper tracing subscriber configuration, the trace_id will not appear in your logs, even though it's correctly propagated through the context.
use trace_id::TraceIdLayer;
// Use custom header name (planned for future release)
// let layer = TraceIdLayer::with_header("x-request-id");
use trace_id::TraceId;
match TraceId::from_string_validated("invalid-trace-id") {
Some(trace_id) => println!("Valid: {}", trace_id),
None => println!("Invalid trace ID format"),
}
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β HTTP Request βββββΆβ TraceIdLayer βββββΆβ Your Handler β
β (x-trace-id) β β - Extract/Gen β β (TraceId) β
βββββββββββββββββββ β - Set Context β βββββββββββββββββββ
β - Add to Span β
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Tracing Context β
β - All logs β
β - Automatic ID β
ββββββββββββββββββββ
Check out the examples directory for comprehensive usage patterns. See EXAMPLES.md for detailed instructions on running and testing the examples.
Available examples:
# Run all tests
cargo test
# Run with Axum features
cargo test --features axum
# Run benchmarks
cargo bench
# Check code quality
cargo clippy --all-features -- -D warnings
cargo fmt --check
Optimized for high-throughput applications:
Run benchmarks with cargo bench to see performance on your system.
We welcome contributions! Please see our Contributing Guide for details.
git clone https://github.com/OpenMindOpenWorld/trace_id.git
cd trace_id
cargo test --all-features
This project is licensed under either of
at your option.