trace_id
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.
π Features
- Zero-overhead trace ID generation - Optimized for high-performance applications
- Web framework integration - Currently supports Axum, with Actix Web planned
- Seamless Axum integration - Drop-in middleware with extractor support
- Automatic request correlation - Links all logs within a request lifecycle
- Header-based propagation - Supports
x-trace-idheader for distributed tracing - Thread-safe context management - Built on
tokio::task_local!for async safety - Tracing ecosystem integration - Automatic span attachment for structured logging
- Framework-agnostic core - Extensible architecture for future framework support
π¦ Installation
Add this to your Cargo.toml:
[]
= "0.1.1"
# For Axum integration
= { = "0.1.1", = ["axum"] }
π― Quick Start
Basic Axum Integration
use ;
use ;
use info;
async
async
Manual Trace ID Access
use get_trace_id;
async
π Documentation
Core Components
TraceId
A lightweight wrapper around a high-performance trace identifier that represents a unique trace ID compliant with W3C TraceContext specification.
use TraceId;
// Generate a new trace ID
let trace_id = new;
// Parse from string (with validation)
let trace_id = from_string_validated?;
// Convert to string
let id_string = trace_id.to_string;
TraceIdLayer
Axum middleware that automatically manages trace ID lifecycle:
- Extracts trace ID from
x-trace-idheader - Generates new ID if header is missing
- Adds trace ID to response headers
- Sets up tracing context for the request
use Router;
use TraceIdLayer;
let app = new
.route
.layer;
Context Management
Access the current trace ID from anywhere in your async call stack:
use get_trace_id;
async
π§ Advanced Usage
Proper Tracing Configuration
To see trace_id in your logs, you must properly configure your tracing subscriber:
use ;
// Basic configuration
init;
// Or more detailed configuration
registry
.with
.with
.init;
Without proper tracing subscriber configuration, the trace_id will not appear in your logs, even though it's correctly propagated through the context.
Custom Header Names (Planned Feature)
use TraceIdLayer;
// Use custom header name (planned for future release)
// let layer = TraceIdLayer::with_header("x-request-id");
Error Handling
use TraceId;
match from_string_validated
ποΈ Architecture
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β HTTP Request βββββΆβ TraceIdLayer βββββΆβ Your Handler β
β (x-trace-id) β β - Extract/Gen β β (TraceId) β
βββββββββββββββββββ β - Set Context β βββββββββββββββββββ
β - Add to Span β
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Tracing Context β
β - All logs β
β - Automatic ID β
ββββββββββββββββββββ
π¦ Examples
Check out the examples directory for comprehensive usage patterns. See EXAMPLES.md for detailed instructions on running and testing the examples.
Available examples:
- tracing_example.rs - Basic Axum integration with TraceId extractor
- tracing_configurations.rs - Different tracing subscriber configurations
π§ͺ Testing
# Run all tests
# Run with Axum features
# Run benchmarks
# Check code quality
π Performance
Optimized for high-throughput applications:
- ID Generation: ~50ns per ID
- String Validation: ~100ns per validation
- Context Access: ~10ns per access
- Memory Overhead: 16 bytes per trace ID
Run benchmarks with cargo bench to see performance on your system.
π€ Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
π License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE or http://opensource.org/licenses/MIT)
at your option.
π Related Projects
- tracing - Structured logging and diagnostics
- axum - Ergonomic web framework (currently supported)
- actix-web - Powerful web framework (planned support)