Skip to main content

opentelemetry-sdk

Version: 0.31.0 The opentelemetry-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)
Async Runtime Support:
  • rt-tokio: Use Tokio’s multi-thread runtime
  • rt-tokio-current-thread: Use Tokio’s current-thread runtime
  • experimental_async_runtime: Enable async runtime trait
Advanced Features:
  • jaeger_remote_sampler: Remote sampling from Jaeger
  • spec_unstable_metrics_views: Metrics views (experimental)
  • experimental_metrics_custom_reader: Custom metric readers
  • experimental_logs_batch_log_processor_with_async_runtime: Async batch log processor

Quick Start

Tracing

Metrics

Logs

Key Components

Trace SDK

struct
Main entry point for tracing. Creates Tracer instances and manages span processors.
struct
Batches spans before exporting (recommended for production)
struct
Exports each span immediately (useful for debugging)
enum
Controls which spans to record:
  • AlwaysOn: Sample all spans
  • AlwaysOff: Sample no spans
  • TraceIdRatioBased: Sample based on trace ID
  • ParentBased: Defer to parent span’s sampling decision

Metrics SDK

struct
Creates Meter instances and manages metric readers
struct
Periodically exports metrics at a fixed interval (default: 60 seconds)
struct
Exports metrics on-demand (useful for pull-based exporters like Prometheus)

Logs SDK

struct
Creates Logger instances and manages log processors
struct
Batches log records before exporting
struct
Exports each log record immediately

Resource Detection

struct
Immutable set of attributes describing the entity producing telemetry

Propagation

The SDK includes propagator implementations:
struct
W3C Trace Context propagation format
struct
W3C Baggage propagation format

Environment Variables

The SDK respects OpenTelemetry environment variables:

General

  • OTEL_SERVICE_NAME: Service name
  • OTEL_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 for traceidratio)
  • 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)

Documentation

Full API Documentation

View complete API reference on docs.rs

Minimum Rust Version

MSRV: 1.75.0