opentelemetry-sdk
Version: 0.31.0 Theopentelemetry-sdk crate provides the official SDK implementation for OpenTelemetry in Rust. It implements the processing pipeline for traces, metrics, and logs, including batching, sampling, and resource detection.
Installation
Feature Flags
Core Features:trace: Trace SDK (enabled by default)metrics: Metrics SDK (enabled by default)logs: Logs SDK (enabled by default)internal-logs: Internal SDK logging (enabled by default)
rt-tokio: Use Tokio’s multi-thread runtimert-tokio-current-thread: Use Tokio’s current-thread runtimeexperimental_async_runtime: Enable async runtime trait
jaeger_remote_sampler: Remote sampling from Jaegerspec_unstable_metrics_views: Metrics views (experimental)experimental_metrics_custom_reader: Custom metric readersexperimental_logs_batch_log_processor_with_async_runtime: Async batch log processor
Quick Start
Tracing
Metrics
Logs
Key Components
Trace SDK
Main entry point for tracing. Creates
Tracer instances and manages span processors.Batches spans before exporting (recommended for production)
Exports each span immediately (useful for debugging)
Controls which spans to record:
AlwaysOn: Sample all spansAlwaysOff: Sample no spansTraceIdRatioBased: Sample based on trace IDParentBased: Defer to parent span’s sampling decision
Metrics SDK
Creates
Meter instances and manages metric readersPeriodically exports metrics at a fixed interval (default: 60 seconds)
Exports metrics on-demand (useful for pull-based exporters like Prometheus)
Logs SDK
Creates
Logger instances and manages log processorsBatches log records before exporting
Exports each log record immediately
Resource Detection
Immutable set of attributes describing the entity producing telemetry
Propagation
The SDK includes propagator implementations:W3C Trace Context propagation format
W3C Baggage propagation format
Environment Variables
The SDK respects OpenTelemetry environment variables:General
OTEL_SERVICE_NAME: Service nameOTEL_RESOURCE_ATTRIBUTES: Resource attributes as key-value pairs
Trace
OTEL_TRACES_SAMPLER: Sampler type (always_on,always_off,traceidratio,parentbased_always_on, etc.)OTEL_TRACES_SAMPLER_ARG: Sampler argument (e.g., ratio fortraceidratio)OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: Max attributes per span (default: 128)OTEL_SPAN_EVENT_COUNT_LIMIT: Max events per span (default: 128)OTEL_BSP_SCHEDULE_DELAY: Batch span processor export interval in ms (default: 5000)OTEL_BSP_MAX_QUEUE_SIZE: Max queue size (default: 2048)OTEL_BSP_MAX_EXPORT_BATCH_SIZE: Max batch size (default: 512)
Metrics
OTEL_METRIC_EXPORT_INTERVAL: Export interval in ms (default: 60000)
Logs
OTEL_BLRP_SCHEDULE_DELAY: Batch log processor export interval in ms (default: 1000)OTEL_BLRP_MAX_QUEUE_SIZE: Max queue size (default: 2048)OTEL_BLRP_MAX_EXPORT_BATCH_SIZE: Max batch size (default: 512)
Related Crates
- opentelemetry - Core API
- opentelemetry-otlp - OTLP exporter
- opentelemetry-stdout - Stdout exporter
Documentation
Full API Documentation
View complete API reference on docs.rs