Skip to content

DataDog/dd-trace-rs

Crates.io Documentation Documentation (master) Apache licensed

dd-trace-rs

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 text 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};

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");
}

Opentelemetry API

  • 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

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 run

This 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();

Support

About

Datadog APM for Rust

Resources

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE
Unknown
license-tool.toml

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages