tracing-otel
A tracing and OpenTelemetry integration utility library for Rust applications, providing easy-to-use configuration and initialization capabilities.
tracing-otel is the successor to tracing-otel-extra. It is a separate crates.io package, so applications must update both the Cargo dependency name and Rust imports; Cargo cannot migrate the package automatically. See the changelog for the migration steps.
Features
- Easy to Use - Simple configuration of tracing and OpenTelemetry through Builder pattern
- Multiple Output Formats - Support for Compact, Pretty, and JSON formats
- Flexible Configuration - Configurable sampling rates, log levels, metrics collection intervals, etc.
- Automatic Resource Cleanup - Automatic management of TracerProvider and MeterProvider through RAII pattern
- Built-in Metrics Support - Integrated OpenTelemetry metrics collection and export
- Environment Detection - Automatic detection of operating system and process information
- OTLP Export - Built-in OTLP protocol support, can directly export to Jaeger, OTEL Collector, etc.
- HTTP request spans (with
http+spanfeatures) -make_request_spanuses OpenTelemetry-aligned attribute names and supports framework-specific customization before parent context is applied; see thehttp::spanmodule andaxum-otelfor migration notes.
Crate Scope
tracing-otel contains shared tracing utilities:
fields,http,context, andspanfor HTTP tracing helpers.macrosfor runtime-configurable tracing macros.loggerandenvfor opinionated application bootstrap.
The logger feature intentionally initializes tracing, metrics, optional OpenTelemetry logs, console output, and optional file output. Applications that only need Axum middleware should depend on axum-otel; applications that only need provider-level OpenTelemetry setup can use otel-init.
Quick Start
Add the dependency to your Cargo.toml:
[]
= "0.33"
= "0.1"
= { = "1.0", = ["full"] }
Basic Usage
use Logger;
use ;
async
Advanced Configuration
use ;
use KeyValue;
use Level;
async
Legacy API (Backward Compatibility)
use init_logging;
async
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
service_name |
String |
Crate name | Service name for OpenTelemetry resource identification |
format |
LogFormat |
Compact |
Log output format: Compact, Pretty, Json |
ansi |
bool |
true |
Whether to enable ANSI color output |
level |
Level |
INFO |
Log level filtering |
sample_ratio |
f64 |
1.0 |
Trace sampling ratio (0.0-1.0) |
metrics_interval_secs |
u64 |
30 |
Metrics collection and export interval (seconds) |
attributes |
Vec<KeyValue> |
[] |
Custom OpenTelemetry attributes |
otel_logs_enabled |
bool |
false |
Whether to enable OpenTelemetry logs export |
Environment Variable Configuration
This library supports standard OpenTelemetry environment variables:
# OTLP export endpoint
# If no OTLP endpoint is configured, or it is configured as an empty string,
# tracing and metrics providers are still initialized locally so request spans
# can produce trace IDs, but nothing is exported and no connection to
# localhost:4317 is attempted.
#
# HTTP OTLP options:
# export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
# export OTEL_EXPORTER_OTLP_PROTOCOL=http/json
#
# Specify Protocol for Traces and Metrics:
# export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=http/protobuf
# export OTEL_EXPORTER_OTLP_METRICS_PROTOCOL=http/json
#
# Default behavior: when no protocol env vars are set, both traces and metrics use grpc.
# Log level (takes precedence over code configuration)
# Resource attributes
Integration with Axum
Use with axum-otel to achieve complete web service observability:
use ;
use ;
use ServiceBuilder;
use TraceLayer;
use ;
async
async
Resource Cleanup
LoggerGuard implements the RAII pattern and automatically shuts down OpenTelemetry providers, then releases non-blocking file writer guards to trigger their shutdown and flush path. The writer does not report the flush outcome:
// <- guard automatically cleans up resources here
// Manual cleanup is also possible
let guard = new.init?;
// ... use tracing
guard.shutdown?; // Manual cleanup
Requirements
- Rust Version: 1.70+
- Tokio Runtime: Requires tokio async runtime
- OTLP Receiver: Need to configure an OTLP-compatible receiver (such as Jaeger, OTEL Collector)
Troubleshooting
Common Issues
-
Failed to connect to OTLP receiver
# Check endpoint configuration # Quickly start Jaeger using Docker -
Log level filtering not working
# Ensure environment variable is set correctly # Or explicitly set level in code ) -
Metrics collection issues
// Adjust metrics collection interval new .with_metrics_interval // Collect every 10 seconds .with_stdout_metrics // Enable console output for debugging
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Issues and Pull Requests are welcome! Please ensure:
- Code passes all tests
- Add appropriate documentation
- Follow the project's code style
Related Projects
- axum-otel - OpenTelemetry middleware for Axum Web framework
- OpenTelemetry Rust - OpenTelemetry Rust implementation
- tracing - Structured logging framework for Rust