This library powers Distributed Tracing. It provides OpenTelemetry API and SDK compatibility with Datadog-specific features and optimizations.
The datadog-opentelemetry crate provides an easy to use override for the rust opentelemetry-sdk.
Add to you Cargo.toml
datadog-opentelemetry = { version = "0.2.1" }To trace functions, you can either use the opentelemetry crate's API or the tracing crate API with the tracing-opentelemetry bridge.
The following examples will read datadog and opentelemetry configuration from environment variables and other available sources, initialize and set up the tracer provider and the text distributed tracing propagators globally.
- Requires
tracing-subscriberandtracing
use opentelemetry::trace::TracerProvider;
use std::time::Duration;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
fn main() {
// This picks up env var configuration and other datadog configuration sources
let tracer_provider = datadog_opentelemetry::tracing()
.init();
tracing_subscriber::registry()
.with(tracing_opentelemetry::layer().with_tracer(tracer_provider.tracer("my_application_name")))
.init();
tracer_provider.shutdown_with_timeout(Duration::from_secs(1)).expect("tracer shutdown error");
}- requires
opentelemetry
use std::time::Duration;
fn main() {
// This picks up env var configuration and other datadog configuration sources
let tracer_provider = datadog_opentelemetry::tracing()
.init();
// Your code
// Now use standard OpenTelemetry APIs
use opentelemetry::global;
use opentelemetry::trace::Tracer;
let tracer = global::tracer("my-service");
let span = tracer.start("my-operation");
// ... do work ...
// Shutdown the tracer to flush the remaining data
tracer_provider.shutdown_with_timeout(Duration::from_secs(1)).expect("tracer shutdown error");
}Configuration can be passed either:
- Programmatically
let config = datadog_opentelemetry::configuration::Config::builder()
.set_service("my_service".to_string())
.set_env("prod".to_string())
.build();
let tracer_provider = datadog_opentelemetry::tracing()
.with_config(config)
// this also accepts options for the Opentelemetry SDK builder
.with_max_attributes_per_span(64)
.init();For advanced usage and configuration information, check out the library documentation.
- Through env variables
DD_SERVICE=my_service DD_ENV=prod cargo runThis API call also be used to pass options to the OpenTelemetry SDK TracerProviderBuilder
impl opentelemetry_sdk::trace::SpanProcessor for MySpanProcessor { ... }
// Custom otel tracer sdk options
datadog_opentelemetry::tracing()
.with_max_attributes_per_span(64)
// Custom span processor
.with_span_processor(MySpanProcessor)
.init();- MSRV: 1.84
- opentelemetry version: 0.31
tracing-opentelemetryversion: 0.32