tracing-opentelemetry-extra

Crates.iotracing-opentelemetry-extra
lib.rstracing-opentelemetry-extra
version0.30.10
created_at2025-06-21 09:59:03.849594+00
updated_at2025-08-07 04:20:21.869217+00
descriptionBootstrap utilities for tracing and OpenTelemetry integration.
homepagehttps://github.com/iamnivekx/tracing-otel-extra/tree/main/crates/tracing-opentelemetry
repositoryhttps://github.com/iamnivekx/tracing-otel-extra/tree/main/crates/tracing-opentelemetry
max_upload_size
id1720681
size61,093
Nivek Huang (iamnivekx)

documentation

https://docs.rs/tracing-otel-extra

README

tracing-opentelemetry-extra

Reference: This crate is mainly organized based on the official tracing-opentelemetry OTLP example.

This crate provides enhanced OpenTelemetry integration for tracing applications. It's based on the tracing-opentelemetry examples and provides a clean, easy-to-use API for setting up OpenTelemetry tracing and metrics.

Features

  • Easy OpenTelemetry initialization with OTLP exporter
  • Configurable sampling and resource attributes
  • Automatic cleanup with guard pattern
  • Support for both tracing and metrics
  • Clean separation of concerns from other tracing libraries

Installation

Add this to your Cargo.toml:

[dependencies]
tracing-opentelemetry-extra = "0.30.x"

Quick Start

Basic Usage

use opentelemetry::KeyValue;
use tracing_opentelemetry_extra::{get_resource, init_tracer_provider, init_meter_provider, OtelGuard};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Create resource with service name and attributes
    let resource = get_resource(
        "my-service",
        &[
            KeyValue::new("environment", "production"),
            KeyValue::new("version", "1.0.0"),
        ],
    );

    // Initialize providers
    let tracer_provider = init_tracer_provider(&resource, 1.0)?;
    let meter_provider = init_meter_provider(&resource, 30)?;

    // initialize tracing subscriber with otel layers

    // Create guard for automatic cleanup
    let _guard = OtelGuard::new(Some(tracer_provider), Some(meter_provider));

    // Your application code here...
    tracing::info!("Application started");

    // Cleanup is handled automatically when the guard is dropped
    Ok(())
}

With Tracing Subscriber

use opentelemetry::KeyValue;
use tracing_opentelemetry_extra::{get_resource, init_tracer_provider, init_meter_provider, init_tracing_subscriber, OtelGuard};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Create resource
    let service_name = "my-service";
    let resource = get_resource(service_name, &[KeyValue::new("environment", "production")]);
    
    // Initialize providers
    let tracer_provider = init_tracer_provider(&resource, 1.0)?;
    let meter_provider = init_meter_provider(&resource, 30)?;

    // Set up tracing subscriber
    let env_filter = init_env_filter(&Level::INFO);
    
    // Create guard for cleanup
    let _guard = init_tracing_subscriber(
        service_name,
        env_filter,
        vec![Box::new(tracing_subscriber::fmt::layer())],
        tracer_provider,
        meter_provider,
    )?;

    // Your application code here...
    tracing::info!("Application started with OpenTelemetry");

    Ok(())
}

Configuration

Sampling

Control the ratio of traces to sample (0.0 to 1.0):

// Sample 50% of traces
let tracer_provider = init_tracer_provider(&resource, 0.5)?;

// Sample all traces
let tracer_provider = init_tracer_provider(&resource, 1.0)?;

Metrics Collection

Configure the interval for metrics collection:

// Collect metrics every 60 seconds
let meter_provider = init_meter_provider(&resource, 60)?;

Resource Attributes

Add custom attributes to your service:

let resource = get_resource(
    "my-service",
    &[
        KeyValue::new("environment", "production"),
        KeyValue::new("version", "1.0.0"),
        KeyValue::new("region", "us-west-2"),
    ],
);

Features

  • subscriber (default): Enables tracing-subscriber integration

Examples

See the examples directory for more detailed usage examples.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under either of

at your option.

Related Crates

Commit count: 60

cargo fmt