Crate tracing_otel_extra

Crate tracing_otel_extra 

Source
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 tracing
  • logger: Basic logging functionality with configurable formats
  • env: Environment-based logging configuration
  • context: Trace context utilities
  • fields: Common tracing fields and attributes
  • http: HTTP request/response tracing
  • span: 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
}

Modules§

extract