| Crates.io | tracing-otel-extra |
| lib.rs | tracing-otel-extra |
| version | 0.30.10 |
| created_at | 2025-06-09 05:52:35.389818+00 |
| updated_at | 2025-08-07 04:20:35.712316+00 |
| description | Bootstrap utilities for tracing and OpenTelemetry integration. |
| homepage | https://github.com/iamnivekx/tracing-otel-extra/tree/main/crates/tracing-otel |
| repository | https://github.com/iamnivekx/tracing-otel-extra/tree/main/crates/tracing-otel |
| max_upload_size | |
| id | 1705573 |
| size | 134,818 |
A tracing and OpenTelemetry integration utility library for Rust applications, providing easy-to-use configuration and initialization capabilities.
Add the dependency to your Cargo.toml:
[dependencies]
tracing-otel-extra = "0.1.0"
tracing = "0.1"
tokio = { version = "1.0", features = ["full"] }
use tracing_otel_extra::Logger;
use tracing::{info, warn};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Initialize with default configuration
let _guard = Logger::default().init()?;
info!("Application started");
warn!("This is a warning message");
// _guard automatically cleans up resources when it goes out of scope
Ok(())
}
use tracing_otel_extra::{Logger, LogFormat};
use opentelemetry::KeyValue;
use tracing::Level;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let _guard = Logger::new("my-awesome-service")
.with_format(LogFormat::Json) // Use JSON format
.with_level(Level::DEBUG) // Set log level
.with_ansi(false) // Disable ANSI colors
.with_sample_ratio(0.1) // 10% sampling rate
.with_metrics_interval(60) // 60-second metrics collection interval
.with_stdout_metrics(false) // Disable console metrics output
.with_attributes(vec![ // Add custom attributes
KeyValue::new("environment", "production"),
KeyValue::new("version", "1.2.3"),
])
.init()?;
// Your application code
tracing::info!(
user_id = 12345,
action = "login",
"User logged in successfully"
);
Ok(())
}
use tracing_otel_extra::init_logging;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let _guard = init_logging("legacy-service")?;
tracing::info!("Initialized using legacy API");
Ok(())
}
| 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 |
This library supports standard OpenTelemetry environment variables:
# OTLP export endpoint
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
export OTEL_EXPORTER_OTLP_PROTOCOL=grpc
# Log level (takes precedence over code configuration)
export RUST_LOG=debug
# Resource attributes
export OTEL_RESOURCE_ATTRIBUTES=service.name=my-service,service.version=1.0.0
Use with axum-otel to achieve complete web service observability:
use axum::{routing::get, Router};
use axum_otel::{AxumOtelSpanCreator, AxumOtelOnResponse, Level};
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;
use tracing_otel_extra::{Logger, LogFormat};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Initialize tracing
let _guard = Logger::new("web-service")
.with_format(LogFormat::Json)
.init()?;
// Build Axum application
let app = Router::new()
.route("/api/health", get(health_check))
.layer(
ServiceBuilder::new()
.layer(
TraceLayer::new_for_http()
.make_span_with(AxumOtelSpanCreator::new().level(Level::INFO))
.on_response(AxumOtelOnResponse::new().level(Level::INFO))
)
);
// Start server
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
axum::serve(listener, app).await?;
Ok(())
}
async fn health_check() -> &'static str {
"OK"
}
ProviderGuard implements the RAII pattern and automatically cleans up OpenTelemetry resources when the guard goes out of scope:
{
let _guard = Logger::new("temp-service").init()?;
// Use tracing
tracing::info!("Temporary service started");
} // <- guard automatically cleans up resources here
// Manual cleanup is also possible
let guard = Logger::new("manual-cleanup").init()?;
// ... use tracing
guard.shutdown()?; // Manual cleanup
Failed to connect to OTLP receiver
# Check endpoint configuration
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
# Quickly start Jaeger using Docker
docker run -d --name jaeger \
-p 16686:16686 \
-p 14250:14250 \
-p 4317:4317 \
jaegertracing/all-in-one:latest
Log level filtering not working
# Ensure environment variable is set correctly
export RUST_LOG=debug
# Or explicitly set level in code
.with_level(Level::DEBUG)
Metrics collection issues
// Adjust metrics collection interval
Logger::new("service")
.with_metrics_interval(10) // Collect every 10 seconds
.with_stdout_metrics(true) // Enable console output for debugging
This project is licensed under the MIT License - see the LICENSE file for details.
Issues and Pull Requests are welcome! Please ensure: