Skip to main content

opentelemetry-otlp

Version: 0.31.0 The opentelemetry-otlp crate exports telemetry data (logs, metrics, and traces) using the OpenTelemetry Protocol (OTLP) to compatible backends including OpenTelemetry Collector, Jaeger, Prometheus, and vendor-specific endpoints.

Installation

[dependencies]
opentelemetry-otlp = { version = "0.31", features = ["http-proto"] }

Feature Flags

Signals:
  • trace: Trace exporters (enabled by default)
  • metrics: Metrics exporters (enabled by default)
  • logs: Log exporters (enabled by default)
Transport - gRPC (Tonic):
  • grpc-tonic: Use Tonic for gRPC transport
  • gzip-tonic: gzip compression for Tonic
  • zstd-tonic: zstd compression for Tonic
  • tls-ring: TLS via rustls with ring crypto
  • tls-aws-lc: TLS via rustls with AWS-LC crypto
  • tls-roots: System trust roots via rustls-native-certs
  • tls-webpki-roots: Mozilla trust roots via webpki-roots
Transport - HTTP:
  • http-proto: HTTP with protobuf (enabled by default)
  • http-json: HTTP with JSON
  • gzip-http: gzip compression for HTTP
  • zstd-http: zstd compression for HTTP
  • reqwest-blocking-client: Reqwest blocking client (enabled by default)
  • reqwest-client: Reqwest async client
  • reqwest-rustls: Reqwest with rustls TLS
  • reqwest-rustls-webpki-roots: Reqwest with webpki roots
  • hyper-client: Hyper client

Quick Start

HTTP Export to Collector

use opentelemetry::global;
use opentelemetry::trace::Tracer;
use opentelemetry_otlp::{Protocol, WithExportConfig};

let exporter = opentelemetry_otlp::SpanExporter::builder()
    .with_http()
    .with_protocol(Protocol::HttpBinary)
    .build()?;

let provider = opentelemetry_sdk::trace::SdkTracerProvider::builder()
    .with_batch_exporter(exporter)
    .build();

global::set_tracer_provider(provider);

let tracer = global::tracer("my_tracer");
tracer.in_span("doing_work", |_cx| {
    // Your application logic
});

gRPC Export (Requires Tokio)

use opentelemetry::{global, trace::Tracer};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let exporter = opentelemetry_otlp::SpanExporter::builder()
        .with_tonic()
        .build()?;

    let provider = opentelemetry_sdk::trace::SdkTracerProvider::builder()
        .with_batch_exporter(exporter)
        .build();

    global::set_tracer_provider(provider);

    let tracer = global::tracer("my_tracer");
    tracer.in_span("doing_work", |_cx| {
        // Your application logic
    });

    Ok(())
}

Exporters

Span Exporter

SpanExporter
struct
Exports trace spans via OTLP
use opentelemetry_otlp::SpanExporter;

let exporter = SpanExporter::builder()
    .with_http()
    .with_endpoint("http://localhost:4318")
    .build()?;

Metric Exporter

MetricExporter
struct
Exports metrics via OTLP
use opentelemetry_otlp::MetricExporter;

let exporter = MetricExporter::builder()
    .with_http()
    .build()?;

let provider = opentelemetry_sdk::metrics::SdkMeterProvider::builder()
    .with_periodic_exporter(exporter)
    .build();

Log Exporter

LogExporter
struct
Exports log records via OTLP
use opentelemetry_otlp::LogExporter;

let exporter = LogExporter::builder()
    .with_http()
    .build()?;

let provider = opentelemetry_sdk::logs::SdkLoggerProvider::builder()
    .with_batch_exporter(exporter)
    .build();

Configuration

Protocol Selection

Protocol
enum
OTLP wire protocol format:
  • Grpc: gRPC transport
  • HttpBinary: HTTP with protobuf encoding
  • HttpJson: HTTP with JSON encoding

Compression

Compression
enum
Compression algorithm:
  • Gzip: gzip compression
  • Zstd: Zstandard compression
use opentelemetry_otlp::{Protocol, Compression, WithExportConfig};

let exporter = opentelemetry_otlp::SpanExporter::builder()
    .with_http()
    .with_protocol(Protocol::HttpBinary)
    .with_compression(Compression::Gzip)
    .with_timeout(std::time::Duration::from_secs(10))
    .build()?;

Environment Variables

The exporter respects standard OTLP environment variables:

General (All Signals)

  • OTEL_EXPORTER_OTLP_ENDPOINT: Target URL (default: http://localhost:4318 for HTTP, http://localhost:4317 for gRPC)
  • OTEL_EXPORTER_OTLP_PROTOCOL: Protocol (grpc, http/protobuf, http/json)
  • OTEL_EXPORTER_OTLP_TIMEOUT: Timeout in milliseconds (default: 10000)
  • OTEL_EXPORTER_OTLP_HEADERS: Request headers as key1=value1,key2=value2
  • OTEL_EXPORTER_OTLP_COMPRESSION: Compression algorithm (gzip, zstd)

Signal-Specific

  • OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: Trace-specific endpoint
  • OTEL_EXPORTER_OTLP_TRACES_HEADERS: Trace-specific headers
  • OTEL_EXPORTER_OTLP_TRACES_TIMEOUT: Trace-specific timeout
  • OTEL_EXPORTER_OTLP_TRACES_COMPRESSION: Trace-specific compression
  • OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: Metrics-specific endpoint
  • OTEL_EXPORTER_OTLP_METRICS_HEADERS: Metrics-specific headers
  • OTEL_EXPORTER_OTLP_METRICS_TIMEOUT: Metrics-specific timeout
  • OTEL_EXPORTER_OTLP_METRICS_COMPRESSION: Metrics-specific compression
  • OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE: Temporality (cumulative, delta, lowmemory)
  • OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: Logs-specific endpoint
  • OTEL_EXPORTER_OTLP_LOGS_HEADERS: Logs-specific headers
  • OTEL_EXPORTER_OTLP_LOGS_TIMEOUT: Logs-specific timeout
  • OTEL_EXPORTER_OTLP_LOGS_COMPRESSION: Logs-specific compression

OpenTelemetry Collector

docker run -p 4318:4318 -p 4317:4317 otel/opentelemetry-collector:latest

Jaeger (with OTLP support)

docker run -p 16686:16686 -p 4317:4317 \
  -e COLLECTOR_OTLP_ENABLED=true \
  jaegertracing/all-in-one:latest
View traces at http://localhost:16686

Prometheus (with OTLP receiver)

docker run -p 9090:9090 \
  -v ./prometheus.yml:/etc/prometheus/prometheus.yml \
  prom/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --web.enable-otlp-receiver
View metrics at http://localhost:9090

Documentation

Full API Documentation

View complete API reference on docs.rs

Minimum Rust Version

MSRV: 1.75.0

Build docs developers (and LLMs) love