otel-init
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.
otel-init is the successor to tracing-opentelemetry-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 OpenTelemetry initialization with OTLP exporter
- Configurable sampling and resource attributes
- Automatic cleanup with guard pattern
- Support for tracing, metrics, and logs
- Clean separation of concerns from other tracing libraries
OTLP Protocol Configuration
This crate respects the standard OpenTelemetry protocol environment variables:
# No OTLP endpoint configured, or configured as an empty string:
# providers are initialized locally, trace IDs are still created, and nothing is exported.
#
# Enable OTLP export for all signals:
# Or enable one signal only:
# Default protocol for all signals
# Specify Protocol for Traces, Metrics, and Logs:
Supported values: grpc, http/protobuf (or http/proto), http/json.
Default behavior: when no endpoint env vars are set, or endpoint env vars are empty, traces, metrics, and logs are local-only and do not attempt to connect to localhost:4317. When an endpoint is configured but no protocol env vars are set, traces, metrics, and logs all use grpc.
Installation
Add this to your Cargo.toml:
[]
= "0.33"
Quick Start
Basic Usage
use KeyValue;
use ;
async
With Tracing Subscriber
use KeyValue;
use Level;
use ;
use ;
async
Configuration
Sampling
Control the ratio of traces to sample (0.0 to 1.0):
// Sample 50% of traces
let tracer_provider = init_tracer_provider?;
// Sample all traces
let tracer_provider = init_tracer_provider?;
Metrics Collection
Configure the interval for metrics collection:
// Collect metrics every 60 seconds
let meter_provider = init_meter_provider?;
Resource Attributes
Add custom attributes to your service:
let resource = get_resource;
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
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Related Crates
- tracing-otel - HTTP, context, fields, and span utilities
- axum-otel - Axum web framework integration