8 releases (breaking)

0.8.0 Jun 17, 2021
0.7.0 May 10, 2021
0.6.0 Mar 24, 2021
0.5.0 Jan 22, 2021
0.1.0 Aug 14, 2020

#144 in Debugging

Download history 3051/week @ 2021-04-07 1568/week @ 2021-04-14 2047/week @ 2021-04-21 2290/week @ 2021-04-28 3085/week @ 2021-05-05 2521/week @ 2021-05-12 3394/week @ 2021-05-19 3352/week @ 2021-05-26 2754/week @ 2021-06-02 2932/week @ 2021-06-09 2957/week @ 2021-06-16 3021/week @ 2021-06-23 2708/week @ 2021-06-30 2788/week @ 2021-07-07 2377/week @ 2021-07-14 2160/week @ 2021-07-21

10,866 downloads per month
Used in 2 crates

Apache-2.0

1MB
23K SLoC

OpenTelemetry — An observability framework for cloud-native software.

OpenTelemetry Collector Rust Exporter

OTLP integration for applications instrumented with OpenTelemetry.

Crates.io: opentelemetry-otlp Documentation LICENSE GitHub Actions CI Gitter chat

Documentation | Chat

Overview

OpenTelemetry is a collection of tools, APIs, and SDKs used to instrument, generate, collect, and export telemetry data (metrics, logs, and traces) for analysis in order to understand your software's performance and behavior.

This crate provides an exporter for sending trace and metric data in the OTLP format to the OpenTelemetry collector. The OpenTelemetry Collector offers a vendor-agnostic implementation on how to receive, process, and export telemetry data. In addition, it removes the need to run, operate, and maintain multiple agents/collectors in order to support open-source telemetry data formats (e.g. Jaeger, Prometheus, etc.) sending to multiple open-source or commercial back-ends.

Quickstart

First make sure you have a running version of the opentelemetry collector you want to send data to:

$ docker run -p 4317:4317 otel/opentelemetry-collector-dev:latest

Then install a new pipeline with the recommended defaults to start exporting telemetry:

use opentelemetry::trace::Tracer;

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
    // use tonic as grpc layer here.
    // If you want to use grpcio. enable `grpc-sys` feature and use with_grpcio function here.
    let tracer = opentelemetry_otlp::new_pipeline().with_tonic().install_simple()?;

    tracer.in_span("doing_work", |cx| {
        // Traced app logic here...
   });

    Ok(())
}

Performance

For optimal performance, a batch exporter is recommended as the simple exporter will export each span synchronously on drop. You can enable the [rt-tokio], [rt-tokio-current-thread] or [rt-async-std] features and specify a runtime on the pipeline builder to have a batch exporter configured for you automatically.

[dependencies]
opentelemetry = { version = "*", features = ["async-std"] }
opentelemetry-otlp = { version = "*", features = ["grpc-sys"] }
let tracer = opentelemetry_otlp::new_pipeline()
    .install_batch(opentelemetry::runtime::AsyncStd)?;

Kitchen Sink Full Configuration

Example showing how to override all configuration options. See the OtlpPipelineBuilder docs for details of each option.

use opentelemetry::{KeyValue, Tracer};
use opentelemetry::sdk::{trace, IdGenerator, Resource, Sampler};
use opentelemetry_otlp::{Protocol};
use std::time::Duration;
use tonic::{
    metadata::*,
    transport::{Certificate, ClientTlsConfig},
};

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
    let cert = std::fs::read_to_string("ca.pem")?;

    let mut map = MetadataMap::with_capacity(3);

    map.insert("x-host", "example.com".parse().unwrap());
    map.insert("x-number", "123".parse().unwrap());
    map.insert_bin("trace-proto-bin", MetadataValue::from_bytes(b"[binary data]"));

    let tracer = opentelemetry_otlp::new_pipeline()
        .with_endpoint("http://localhost:4317")
        .with_protocol(Protocol::Grpc)
        .with_timeout(Duration::from_secs(3))
        .with_trace_config(
            trace::config()
                .with_sampler(Sampler::AlwaysOn)
                .with_id_generator(IdGenerator::default())
                .with_max_events_per_span(64)
                .with_max_attributes_per_span(16)
                .with_max_events_per_span(16)
                .with_resource(Resource::new(vec![KeyValue::new("service.name", "example")])),
        )
        .with_tonic()
        .with_tls_config(ClientTlsConfig::new()
            .ca_certificate(Certificate::from_pem(&cert))
            .domain_name("example.com".to_string())
        )
        .with_metadata(map)
        .install_batch(opentelemetry::runtime::Tokio)?;

    tracer.in_span("doing_work", |cx| {
        // Traced app logic here...
    });

    Ok(())
}

Grpc libraries comparison

Multiple gRPC transport layers are available. tonic is the default gRPC transport layer and is enabled by default. grpcio is optional.

gRPC transport layer hyperium/tonic tikv/grpc-rs
Feature --features=default --features=grpc-sys
gRPC library tonic grpcio
Transport hyperium/hyper (Rust) grpc/grpc (C++ binding)
TLS support yes yes
TLS library rustls OpenSSL
TLS optional yes yes
Supported .proto generator prost prost, protobuf

Dependencies

~6–16MB
~316K SLoC