Skip to main content

opentelemetry

Version: 0.31.0 The opentelemetry crate is the core API for OpenTelemetry in Rust. It provides the foundational interfaces and types for instrumenting applications with telemetry data, including traces, metrics, and logs.
This crate provides API definitions only (no-op implementations). To actually collect and export telemetry, you need to use opentelemetry-sdk along with exporters like opentelemetry-otlp.

Installation

[dependencies]
opentelemetry = "0.31"

Feature Flags

  • trace: Includes the trace API (enabled by default)
  • metrics: Includes the metrics API (enabled by default)
  • logs: Includes the logs bridge API (enabled by default)
  • internal-logs: Enables internal logging via tracing (enabled by default)

What’s Included

Context API

Manage and propagate execution context across async boundaries.
use opentelemetry::Context;

let cx = Context::current();

Tracing API

Create distributed traces to track request flows:
use opentelemetry::{global, trace::{Tracer, Span}, KeyValue};

let tracer = global::tracer("my_service");
let mut span = tracer.start("my_span");
span.set_attribute(KeyValue::new("http.client_ip", "83.164.160.102"));
span.end();

Metrics API

Record measurements about your service:
use opentelemetry::{global, KeyValue};

let meter = global::meter("my_service");
let counter = meter.u64_counter("request.count").build();
counter.add(1, &[KeyValue::new("http.client_ip", "83.164.160.102")]);

Logs Bridge API

The Logs Bridge API is not intended for direct use by application developers. Use log appenders like opentelemetry-appender-log or opentelemetry-appender-tracing instead.

Propagators API

Share context across service boundaries using propagation standards like W3C Trace Context and Baggage.
use opentelemetry::global;

let propagator = opentelemetry::propagation::TraceContextPropagator::new();
global::set_text_map_propagator(propagator);

Core Types

Context
struct
Carries execution-scoped values across API boundaries
KeyValue
struct
Key-value pair for attributes on spans, metrics, and logs
Value
enum
Supported attribute value types: Bool, Int, Double, String, Array

Trace Types

trace::Tracer
trait
Creates and manages spans
trace::Span
trait
Represents a single operation within a trace
trace::TracerProvider
trait
Creates Tracer instances
TraceId
struct
16-byte unique identifier for a trace
SpanId
struct
8-byte unique identifier for a span

Metrics Types

metrics::Meter
trait
Creates metric instruments
metrics::Counter
trait
Monotonically increasing metric
metrics::Histogram
trait
Records distribution of values
metrics::Gauge
trait
Records current value
metrics::UpDownCounter
trait
Counter that can increase or decrease

Modules

  • global - Global API for tracer, meter, and propagator registration
  • trace - Distributed tracing API
  • metrics - Metrics recording API
  • logs - Logs bridge API
  • context - Context management
  • baggage - Cross-cutting concerns propagation
  • propagation - Context propagation formats

Documentation

Full API Documentation

View complete API reference on docs.rs

Minimum Rust Version

MSRV: 1.75.0

Build docs developers (and LLMs) love