dd-trace-rs
This library powers Distributed Tracing, metrics and logging. 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
= { = "0.3.1" }
Creating traces, metrics and logs
Tracing
To trace functions, you can either use the opentelemetry crate's
API or the tracing crate
API with the tracing-opentelemetry
bridge.
Metrics
To collect metrics, use the opentelemetry crate's
Metrics API. For more
details, see the
Datadog OpenTelemetry Rust documentation.
Logging
- Enable with the
logsfeature of this crate
To collect logs, you can use the log crate with the
opentelemetry_appender_log. For more details, see the
Datadog OpenTelemetry Rust documentation.
Library initialization
The following examples will read datadog and opentelemetry configuration from environment variables and other available sources, initialize and set up the global providers for tracing, logging or metrics
Tracing API
Requires
use TracerProvider;
use Duration;
use ;
// This picks up env var configuration and other datadog configuration sources
let tracer_provider = tracing.init;
registry
.with
.init;
tracer_provider
.shutdown_with_timeout
.expect;
Opentelemetry trace API
Requires
opentelemetrywith thetracefeature enabled
use Duration;
// This picks up env var configuration and other datadog configuration sources
let tracer_provider = tracing.init;
// Your code
// Now use standard OpenTelemetry APIs
use global;
use Tracer;
let tracer = tracer;
let span = tracer.start;
// ... do work ...
// Shutdown the tracer to flush the remaining data
tracer_provider
.shutdown_with_timeout
.expect;
Opentelemetry metrics API
Requires
- the
metricsfeature of this crate to be enabled opentelemetrywith themetricsfeature enabledtokio
The metrics provider MUST be initialized within a tokio context
// Initialize metrics with default configuration
let meter_provider = metrics.init;
// Use standard OpenTelemetry Metrics APIs
use global;
use Counter;
use KeyValue;
let meter = meter;
let counter: = meter.u64_counter.build;
counter.add;
// Shutdown to flush remaining metrics
meter_provider.shutdown.unwrap;
Log API
Requires
- the
logsfeature of this crate to be enabled logopentelemetry-appender-logtokio
The logger provider MUST be initialized within a tokio context
// Initialize logs with default configuration
let logger_provider = logs.init;
let otel_log_appender = new;
set_boxed_logger.unwrap;
// Before ending the program shutdown to flush remaining logs to the collector
logger_provider.shutdown;
For more details, see the Datadog OpenTelemetry Rust documentation.
Configuration
Configuration can be passed either:
- Programmatically
use Config;
let config = builder
.set_service
.set_env
.build;
let tracer_provider = tracing
.with_config
// this also accepts options for the Opentelemetry SDK builder
.with_max_attributes_per_span
.init;
let metrics_provider = metrics
.with_config
.init;
let logging_provider = logs
.with_config
.init;
For advanced usage and configuration information, check out [DatadogTracingBuilder],
[configuration::ConfigBuilder] and the
library documentation.
- Through env variables
DD_SERVICE=my_service DD_ENV=prod
Configuration of the OpenTelemetry SDK TracerProviderBuilder can be done when initializing the library
;
// Custom otel tracer sdk options
tracing
.with_max_attributes_per_span
// Custom span processor
.with_span_processor
.init;
Support
-
MSRV: 1.84
-
opentelemetryversion: 0.31 -
tracing-opentelemetryversion: 0.32 -
opentelemetry-appender-logversion 0.31 -
logversion 0.4
Features
metricsenabled the metrics providermetrics-grpcenabled the metrics provider, with GRPC OTLP exportmetrics-httpenabled the metrics provider, with HTTP OTLP exportlogsenabled the log providerlogs-grpcenabled the log provider, with GRPC OTLP exportlogs-httpenabled the log provider, with HTTP OTLP export