[][src]Crate tracing_opentelemetry

Tracing OpenTelemetry

tracing is a framework for instrumenting Rust programs to collect structured, event-based diagnostic information. This crate provides a layer that connects spans from multiple systems into a trace and emits them to OpenTelemetry-compatible distributed tracing systems for processing and visualization.

Special Fields

Fields with an otel. prefix are reserved for this crate and have specific meaning. They are treated as ordinary fields by other layers. The current special fields are:

  • otel.name: Override the span name sent to OpenTelemetry exporters. Setting this field is useful if you want to display non-static information in your span name.
  • otel.kind: Set the span kind to one of the supported OpenTelemetry span kinds. The value should be a string of any of the supported values: SERVER, CLIENT, PRODUCER, CONSUMER or INTERNAL. Other values are silently ignored.

Semantic Conventions

OpenTelemetry defines conventional names for attributes of common operations. These names can be assigned directly as fields, e.g. trace_span!("request", "otel.kind" = "client", "http.url" = ..), and they will be passed through to your configured OpenTelemetry exporter. You can find the full list of the operations and their expected field names in the semantic conventions spec.

Stability Status

The OpenTelemetry specification is currently in beta so some breaking may still occur on the path to 1.0. You can follow the changes via the spec repository to track progress toward stabilization.

Examples

use opentelemetry::{api::Provider, sdk};
use tracing::{error, span};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Registry;

// Create a new tracer
let tracer = sdk::Provider::default().get_tracer("service_name");

// Create a new OpenTelemetry tracing layer
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);

let subscriber = Registry::default().with(telemetry);

// Trace executed code
tracing::subscriber::with_default(subscriber, || {
    // Spans will be sent to the configured OpenTelemetry exporter
    let root = span!(tracing::Level::TRACE, "app_start", work_units = 2);
    let _enter = root.enter();

    error!("This event will be logged in the root span.");
});

Structs

OpenTelemetryLayer

An OpenTelemetry propagation layer for use in a project that uses tracing.

Traits

OpenTelemetrySpanExt

Utility functions to allow tracing Spans to accept and return OpenTelemetry Contexts.

Functions

layer

Construct a layer to track spans via OpenTelemetry.