Skip to main content

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 spans
  • always_off - Sample no spans
  • traceidratio - Sample based on trace ID (requires OTEL_TRACES_SAMPLER_ARG = ratio)
  • parentbased_always_on - Use parent decision, otherwise always sample
  • parentbased_always_off - Use parent decision, otherwise never sample
  • parentbased_traceidratio - Use parent decision, otherwise ratio-based

Propagator configuration

Supported propagators:
  • tracecontext - W3C Trace Context
  • baggage - W3C Baggage
  • b3 - B3 single header
  • b3multi - B3 multi header
  • jaeger - Jaeger propagation
  • xray - AWS X-Ray
  • ottrace - OpenTracing

Feature flags

opentelemetry crate

opentelemetry-sdk crate

Experimental features:

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):
  1. Programmatic configuration - Values set in code
  2. Environment variables - Runtime configuration
  3. Default values - Built-in defaults

Best practices

Use environment variables for deployment - Different environments (dev, staging, prod) can have different configurations without code changes.
Validate configuration - Check that environment variables are set correctly before running in production.
Secure sensitive values - Don’t commit API keys or tokens to version control. Use secret management systems.
Feature flags reduce binary size - Only enable the features you need to minimize dependencies and compile time.

Configuration by environment

Development

Staging

Production

Resource detection

Configure resource attributes

Batch processing

Tune batch processor settings