Crate datadog_opentelemetry

Crate datadog_opentelemetry 

Source
Expand description

§Datadog Opentelemetry

This library powers Distributed Tracing. It provides OpenTelemetry API and SDK compatibility with Datadog-specific features and optimizations.

§Usage

The datadog-opentelemetry crate provides an easy to use override for the rust opentelemetry-sdk.

§Installation

Add to you Cargo.toml

datadog-opentelemetry = { version = "0.2.1" }

§Tracing

To trace functions, you can either use the opentelemetry crate’s API or the tracing crate API with the tracing-opentelemetry bridge.

§Initialization

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 distributed tracing propagators globally.

§Tracing API
  • Requires tracing-subscriber and tracing
use opentelemetry::trace::TracerProvider;
use std::time::Duration;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

// 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");
§Opentelemetry API
  • requires opentelemetry
use std::time::Duration;

// 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

Configuration can be passed either:

  • Programmatically
use datadog_opentelemetry::configuration::Config;
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 DatadogTracingBuilder and configuration::ConfigBuilder

  • Through env variables
DD_SERVICE=my_service DD_ENV=prod cargo run

Or to pass options to the OpenTelemetry SDK TracerProviderBuilder

// Custom otel tracer sdk options
datadog_opentelemetry::tracing()
    .with_max_attributes_per_span(64)
    // Custom span processor
    .with_span_processor(MySpanProcessor)
    .init();

§Support

Modules§

configuration
Configuration for the Datadog tracing setup
log
Logging level

Structs§

DatadogTracingBuilder

Functions§

tracing
Initialize a new Datadog Tracing builder