a2a_protocol_client/otel_baggage.rs
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! W3C Baggage on outbound calls, and `OTEL_PROPAGATORS` (audit C-10; ADR
5//! 0038 ยง1).
6//!
7//! **Opt-in, by key.** Baggage is a data channel to whoever the next hop is,
8//! and an A2A peer is often another organisation's agent: forwarding every
9//! entry would hand it whatever an upstream service put there (`user.id`,
10//! `tenant`, a session). So nothing is sent until
11//! [`ClientConfig::with_baggage_propagation`] names the keys that may cross
12//! ([`BaggagePolicy::keys`]), or, knowingly, all of them
13//! ([`BaggagePolicy::All`]). What is sent is bounded by the W3C limits (64
14//! members, 8192 bytes, whole members only) and is the baggage of the
15//! OpenTelemetry context the call runs in.
16//!
17//! **`OTEL_PROPAGATORS`.** Read once, when the [`ClientConfig`] is made
18//! (default `tracecontext,baggage`): without `tracecontext` the call writes no
19//! `traceparent` of its own, without `baggage` no `baggage`; `none` turns off
20//! both. A name this crate does not carry is warned about when the client is
21//! built and ignored; a list of only such names gets the default.
22//!
23//! Needs the `otel` feature, a `tracing-opentelemetry` layer recording, and a
24//! transport with per-request headers (JSON-RPC, HTTP+JSON, gRPC), as the
25//! `traceparent` does.
26
27pub use a2a_protocol_types::trace_context::baggage::{BaggagePolicy, Propagators};
28
29use crate::config::ClientConfig;
30
31impl ClientConfig {
32 /// Which baggage entries this client sends; [`BaggagePolicy::Off`] by
33 /// default. See the [module](crate::otel_baggage) for why it is opt-in.
34 #[must_use]
35 pub fn with_baggage_propagation(mut self, policy: BaggagePolicy) -> Self {
36 self.baggage = policy;
37 self
38 }
39
40 /// Which propagators the calls use, instead of what `OTEL_PROPAGATORS`
41 /// said when this config was made.
42 #[must_use]
43 pub fn with_propagators(mut self, propagators: Propagators) -> Self {
44 self.propagators = propagators;
45 self
46 }
47}
48
49/// Warns, once per client built, of each `OTEL_PROPAGATORS` name this crate
50/// does not carry (decision F3: a value that cannot be used warns, and the
51/// rest of the list, or the default, applies).
52pub(crate) fn warn_unknown(propagators: &Propagators) {
53 for _name in &propagators.unknown {
54 trace_warn!(
55 propagator = %_name,
56 "OTEL_PROPAGATORS names a propagator this client does not carry; ignored \
57 (it carries tracecontext and baggage)"
58 );
59 }
60}
61
62/// Writes the entries of the call span's baggage that `policy` lets out as
63/// the call's `baggage` header, unless the caller set one (in any case).
64#[cfg(feature = "otel")]
65pub(crate) fn propagate(
66 span: &tracing::Span,
67 carries_headers: bool,
68 policy: &BaggagePolicy,
69 headers: &mut std::collections::HashMap<String, String>,
70) {
71 use a2a_protocol_types::trace_context::baggage::{BAGGAGE_HEADER, encode};
72 use opentelemetry::baggage::BaggageExt as _;
73 use tracing_opentelemetry::OpenTelemetrySpanExt as _;
74 // `Off` needs no check of its own: it allows no key, so `encode` below
75 // writes nothing.
76 if !carries_headers
77 || span.is_disabled()
78 || headers
79 .keys()
80 .any(|k| k.eq_ignore_ascii_case(BAGGAGE_HEADER))
81 {
82 return;
83 }
84 let context = span.context();
85 let baggage = context.baggage();
86 let members: Vec<(&str, &str, Option<&str>)> = baggage
87 .iter()
88 .map(|(key, (value, metadata))| (key.as_str(), value.as_str(), Some(metadata.as_str())))
89 .collect();
90 if let Some(header) = encode(members, policy) {
91 headers.insert(BAGGAGE_HEADER.to_owned(), header);
92 }
93}
94
95#[cfg(test)]
96mod tests;