otel-rs
Modern, composable OpenTelemetry observability helpers for Rust.
Provides a single init() call that sets up traces, logs, and metrics export via OTLP, with dynamic subscriber layer composition, feature-gated transport selection, standard OTel env var support, and async-aware shutdown.
Quick Start
use OtelConfig;
async
Installation
Add to your Cargo.toml:
[]
= "0.1"
Feature Flags
| Feature | Default | Description |
|---|---|---|
tracing |
✅ | Distributed trace export |
logs |
✅ | Log export via OTLP |
metrics |
✅ | Metrics export via OTLP |
grpc |
✅ | gRPC transport (tonic) |
http |
❌ | HTTP/protobuf transport (reqwest) |
To use HTTP instead of gRPC:
[]
= { = "0.1", = false, = ["tracing", "logs", "metrics", "http"] }
Configuration
Composable Builder API
Configuration is split into composable sub-configs, each with its own builder:
use ;
use Duration;
let mut guard = builder
// Service identity
.service_name
.service_version
.environment
.namespace
// Exporter (endpoint, auth, protocol, timeout)
.exporter
// Tracing (sampling, batching)
.tracing
// Metrics (export interval)
.metrics
// Console & filtering
.log_level
.output_format
.allow_crate
.allow_crate
// Custom resource attributes
.attribute
.init
.await?;
Environment Variables
Standard OTel environment variables are respected as defaults. Builder values always take precedence.
| Variable | Effect |
|---|---|
OTEL_SERVICE_NAME |
Service name |
OTEL_EXPORTER_OTLP_ENDPOINT |
Collector endpoint |
OTEL_EXPORTER_OTLP_PROTOCOL |
grpc or http/protobuf |
OTEL_EXPORTER_OTLP_HEADERS |
Comma-separated key=value pairs |
OTEL_EXPORTER_OTLP_TIMEOUT |
Timeout in milliseconds |
OTEL_TRACES_SAMPLER |
always_on, always_off, traceidratio, parentbased_always_on |
OTEL_TRACES_SAMPLER_ARG |
Sampler argument (e.g., ratio 0.1) |
RUST_LOG |
Full override of log filtering |
Authentication
// Bearer token (Honeycomb, Grafana Cloud, etc.)
builder
.bearer_token
.build
// Basic auth
builder
.basic_auth
.build
// Custom headers
builder
.header
.header
.build
Disabling Features
builder
.service_name
.disable_tracing // No span export
.disable_metrics // No metrics export
.logging // No log export
.console_output // No stdout
.init
.await?;
Error Recording
Errors are automatically recorded to spans using extension traits and macros:
use ;
use Span;
async
Metrics
When metrics are enabled, the guard provides a Metrics handle:
use KeyValue;
let guard = builder
.service_name
.init
.await?;
if let Some = guard.metrics
Shutdown
OtelGuard supports both async and sync shutdown:
// Preferred: async shutdown with error reporting
guard.shutdown.await?;
// Fallback: Drop triggers sync best-effort shutdown
drop;
Always prefer the async path — it flushes pending telemetry and reports errors. The Drop impl is a safety net for cases where async shutdown isn't called.
Log Filtering
Uses an allow-list approach (default: all crates silenced):
builder
.log_level
.allow_crate // my_app=debug
.allow_crate // my_lib=debug
.custom_filter // Override for specific crate
.init
.await?;
For advanced filtering, use FilterBuilder directly:
use FilterBuilder;
let filter = new
.default_level
.allow
.allow_at
.directive
.build;
RUST_LOG takes full precedence when set.
License
Licensed under either of
at your option.