Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion datadog-opentelemetry/examples/propagator/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ async fn send_request(
) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let client = Client::builder(TokioExecutor::new()).build_http();

let cx = Context::current();
let cx = Context::current().with_baggage(vec![
KeyValue::new("request-id", "xyz-123"),
KeyValue::new("caller", "rust-propagator-example"),
]);

let mut req = hyper::Request::builder().uri(url);
global::get_text_map_propagator(|propagator| {
Expand Down
47 changes: 47 additions & 0 deletions datadog-opentelemetry/src/core/configuration/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,8 @@ pub enum TracePropagationStyle {
Datadog,
/// W3C Trace Context propagation format using `traceparent` and `tracestate` headers.
TraceContext,
/// W3C Baggage propagation format using the `baggage` header.
Baggage,
/// No propagation - trace context is not propagated.
None,
}
Expand Down Expand Up @@ -804,6 +806,7 @@ impl FromStr for TracePropagationStyle {
match s.trim().to_lowercase().as_str() {
"datadog" => Ok(TracePropagationStyle::Datadog),
"tracecontext" => Ok(TracePropagationStyle::TraceContext),
"baggage" => Ok(TracePropagationStyle::Baggage),
"none" => Ok(TracePropagationStyle::None),
_ => Err(format!("Unknown trace propagation style: '{s}'")),
}
Expand All @@ -815,6 +818,7 @@ impl Display for TracePropagationStyle {
let style = match self {
TracePropagationStyle::Datadog => "datadog",
TracePropagationStyle::TraceContext => "tracecontext",
TracePropagationStyle::Baggage => "baggage",
TracePropagationStyle::None => "none",
};
write!(f, "{style}")
Expand Down Expand Up @@ -1822,6 +1826,7 @@ fn default_config() -> Config {
Some(vec![
TracePropagationStyle::Datadog,
TracePropagationStyle::TraceContext,
TracePropagationStyle::Baggage,
]),
),
trace_propagation_style_extract: ConfigItem::new(
Expand Down Expand Up @@ -2726,6 +2731,7 @@ mod tests {
Some(vec![
TracePropagationStyle::Datadog,
TracePropagationStyle::TraceContext,
TracePropagationStyle::Baggage,
])
.as_deref()
);
Expand All @@ -2737,6 +2743,47 @@ mod tests {
assert!(config.trace_propagation_extract_first());
}

#[test]
fn test_propagation_style_baggage_parsed_from_env() {
// "baggage" is recognised as a valid style value (case-insensitive) in all three env vars.
let mut sources = CompositeSource::new();
sources.add_source(HashMapSource::from_iter(
[
("DD_TRACE_PROPAGATION_STYLE", "datadog,tracecontext,baggage"),
("DD_TRACE_PROPAGATION_STYLE_EXTRACT", "Baggage,datadog"),
("DD_TRACE_PROPAGATION_STYLE_INJECT", "BAGGAGE,tracecontext"),
],
ConfigSourceOrigin::EnvVar,
));
let config = Config::builder_with_sources(&sources).build();

assert_eq!(
config.trace_propagation_style(),
Some(vec![
TracePropagationStyle::Datadog,
TracePropagationStyle::TraceContext,
TracePropagationStyle::Baggage,
])
.as_deref()
);
assert_eq!(
config.trace_propagation_style_extract(),
Some(vec![
TracePropagationStyle::Baggage,
TracePropagationStyle::Datadog,
])
.as_deref()
);
assert_eq!(
config.trace_propagation_style_inject(),
Some(vec![
TracePropagationStyle::Baggage,
TracePropagationStyle::TraceContext,
])
.as_deref()
);
}

#[test]
fn test_stats_computation_enabled_config() {
let mut sources = CompositeSource::new();
Expand Down
21 changes: 21 additions & 0 deletions datadog-opentelemetry/src/propagation/baggage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

//! W3C Baggage propagation (`baggage` header).
//!
//! Actual extract/inject is performed by [`opentelemetry_sdk::propagation::BaggagePropagator`]
//! at the [`DatadogPropagator`](crate::text_map_propagator::DatadogPropagator) layer, which has
//! access to the OTel [`Context`](opentelemetry::Context) that carries baggage. This module
//! exposes the header key so the composite propagator can include it in its `fields()` list.

use std::sync::LazyLock;

/// The W3C `baggage` header name.
pub const BAGGAGE_KEY: &str = "baggage";

static BAGGAGE_HEADER_KEYS: LazyLock<[String; 1]> = LazyLock::new(|| [BAGGAGE_KEY.to_owned()]);

/// Returns the header keys used by the W3C baggage propagator.
pub fn keys() -> &'static [String] {
BAGGAGE_HEADER_KEYS.as_slice()
}
2 changes: 2 additions & 0 deletions datadog-opentelemetry/src/propagation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use config::{get_extractors, get_injectors};
use datadog::DATADOG_LAST_PARENT_ID_KEY;
use tracecontext::TRACESTATE_KEY;

/// W3C Baggage propagation (`baggage` header).
pub mod baggage;
pub mod carrier;
pub(crate) mod config;
pub mod context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::core::configuration::TracePropagationStyle;
use serde::{Deserialize, Deserializer};

use crate::propagation::{
baggage,
carrier::{Extractor, Injector},
context::{InjectSpanContext, SpanContext},
datadog, tracecontext, PropagationConfig, Propagator,
Expand All @@ -17,23 +18,26 @@ impl<C: PropagationConfig + ?Sized> Propagator<C> for TracePropagationStyle {
match self {
Self::Datadog => datadog::extract(carrier, config),
Self::TraceContext => tracecontext::extract(carrier),
_ => None,
// Baggage extraction operates on OTel Context and is handled by DatadogPropagator.
Self::Baggage | Self::None => None,
}
}

fn inject(&self, context: &mut InjectSpanContext, carrier: &mut dyn Injector, config: &C) {
match self {
Self::Datadog => datadog::inject(context, carrier, config),
Self::TraceContext => tracecontext::inject(context, carrier),
_ => {}
// Baggage injection operates on OTel Context and is handled by DatadogPropagator.
Self::Baggage | Self::None => {}
}
}

fn keys(&self) -> &[String] {
match self {
Self::Datadog => datadog::keys(),
Self::TraceContext => tracecontext::keys(),
_ => &NONE_KEYS,
Self::Baggage => baggage::keys(),
Self::None => &NONE_KEYS,
}
}
}
Expand Down
Loading
Loading