Expand description
§Tracing Extra
This crate provides common utilities for initializing tracing and OpenTelemetry.
§Features
The crate is organized into several feature flags:
otel: OpenTelemetry integration for distributed tracinglogger: Basic logging functionality with configurable formatsenv: Environment-based logging configurationcontext: Trace context utilitiesfields: Common tracing fields and attributeshttp: HTTP request/response tracingspan: Span creation and management utilities
§Examples
Basic usage with configuration builder:
ⓘ
use tracing_otel_extra::{Logger, LogFormat};
use opentelemetry::KeyValue;
#[tokio::main]
async fn main() {
let _guard = Logger::new("my-service")
.with_format(LogFormat::Json)
.with_ansi(false)
.with_sample_ratio(0.1)
.with_attributes(vec![
KeyValue::new("environment", "production"),
KeyValue::new("version", "1.0.0"),
])
.init()
.expect("Failed to initialize tracing");
// Your application code here
// Cleanup is handled automatically when the guard is dropped
}Using environment-based configuration:
ⓘ
use tracing_otel_extra::init_logging_from_env;
#[tokio::main]
async fn main() {
// Configure through environment variables:
// LOG_SERVICE_NAME=my-service
// LOG_FORMAT=json
// LOG_SAMPLE_RATIO=0.1
let _guard = init_logging_from_env(None)
.expect("Failed to initialize tracing from environment");
// Your application code here
}