Overview
OpenTelemetry Rust can be configured through:- Environment variables (runtime configuration)
- Cargo feature flags (compile-time configuration)
- Programmatic configuration (in code)
Environment variables
General configuration
OTLP exporter configuration
Signal-specific endpoints
Batch processor configuration
Spans:
Logs:
Trace configuration
Sampler types
always_on- Sample all spansalways_off- Sample no spanstraceidratio- Sample based on trace ID (requiresOTEL_TRACES_SAMPLER_ARG= ratio)parentbased_always_on- Use parent decision, otherwise always sampleparentbased_always_off- Use parent decision, otherwise never sampleparentbased_traceidratio- Use parent decision, otherwise ratio-based
Propagator configuration
Supported propagators:
tracecontext- W3C Trace Contextbaggage- W3C Baggageb3- B3 single headerb3multi- B3 multi headerjaeger- Jaeger propagationxray- AWS X-Rayottrace- OpenTracing
Feature flags
opentelemetry crate
opentelemetry-sdk crate
opentelemetry-otlp crate
Complete configuration example
Environment file (.env)
Cargo.toml
Application code
Override precedence
Configuration is resolved in this order (highest to lowest priority):- Programmatic configuration - Values set in code
- Environment variables - Runtime configuration
- Default values - Built-in defaults
Best practices
Validate configuration - Check that environment variables are set correctly before running in production.
Feature flags reduce binary size - Only enable the features you need to minimize dependencies and compile time.
Configuration by environment
Development
Staging
Production
Related
Resource detection
Configure resource attributes
Batch processing
Tune batch processor settings