Skip to main content

adk_telemetry/
config.rs

1//! Runtime configuration for semantic convention emission.
2//!
3//! Controls opt-in behaviors that cannot be gated at compile time, such as capturing the
4//! conversation on GenAI spans and events.
5
6use std::sync::atomic::{AtomicU8, Ordering};
7
8/// The environment variable the GenAI conventions name for content capture, read when the
9/// process has not set a mode with [`set_capture_content`]. Values, compared without regard to
10/// case: `NO_CONTENT` (the default), `SPAN_ONLY`, `EVENT_ONLY`, `SPAN_AND_EVENT`; anything
11/// else reads as `NO_CONTENT`.
12pub const CAPTURE_CONTENT_ENV: &str = "OTEL_INSTRUMENTATION_GENAI_CAPTURE_CONTENT";
13
14/// Where GenAI instrumentation records the conversation: the system instructions, input and
15/// output messages, tool definitions, and tool call arguments and results.
16///
17/// Off by default: prompts, answers and tool payloads can hold personal data and secrets.
18///
19/// # Example
20///
21/// ```
22/// use adk_telemetry::config::CaptureContent;
23///
24/// assert_eq!(CaptureContent::parse("span_and_event"), CaptureContent::SpanAndEvent);
25/// assert_eq!(CaptureContent::parse("yes"), CaptureContent::NoContent);
26/// assert!(CaptureContent::SpanOnly.on_spans() && !CaptureContent::SpanOnly.on_events());
27/// ```
28#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
29#[non_exhaustive]
30pub enum CaptureContent {
31    /// No content is recorded.
32    #[default]
33    NoContent,
34    /// Content is recorded as attributes of the GenAI spans.
35    SpanOnly,
36    /// Content is recorded on the `gen_ai.client.inference.operation.details` event.
37    EventOnly,
38    /// Both.
39    SpanAndEvent,
40}
41
42impl CaptureContent {
43    /// The mode `value` names, as [`CAPTURE_CONTENT_ENV`] spells it.
44    pub fn parse(value: &str) -> Self {
45        match value.trim().to_ascii_uppercase().as_str() {
46            "SPAN_ONLY" => Self::SpanOnly,
47            "EVENT_ONLY" => Self::EventOnly,
48            "SPAN_AND_EVENT" => Self::SpanAndEvent,
49            _ => Self::NoContent,
50        }
51    }
52
53    /// Whether content goes on span attributes.
54    pub fn on_spans(self) -> bool {
55        matches!(self, Self::SpanOnly | Self::SpanAndEvent)
56    }
57
58    /// Whether content goes on the `gen_ai.client.inference.operation.details` event.
59    pub fn on_events(self) -> bool {
60        matches!(self, Self::EventOnly | Self::SpanAndEvent)
61    }
62
63    fn code(self) -> u8 {
64        match self {
65            Self::NoContent => 1,
66            Self::SpanOnly => 2,
67            Self::EventOnly => 3,
68            Self::SpanAndEvent => 4,
69        }
70    }
71}
72
73/// No mode set in process: read the environment.
74const UNSET: u8 = 0;
75
76static CAPTURE: AtomicU8 = AtomicU8::new(UNSET);
77
78/// Sets the content capture mode for this process, overriding [`CAPTURE_CONTENT_ENV`].
79///
80/// # Example
81///
82/// ```
83/// use adk_telemetry::config::{CaptureContent, capture_content, set_capture_content};
84///
85/// set_capture_content(CaptureContent::SpanOnly);
86/// assert_eq!(capture_content(), CaptureContent::SpanOnly);
87/// set_capture_content(CaptureContent::NoContent);
88/// assert_eq!(capture_content(), CaptureContent::NoContent);
89/// ```
90pub fn set_capture_content(mode: CaptureContent) {
91    CAPTURE.store(mode.code(), Ordering::Relaxed);
92}
93
94/// The content capture mode: the one [`set_capture_content`] set, or else
95/// [`CAPTURE_CONTENT_ENV`]'s.
96pub fn capture_content() -> CaptureContent {
97    match CAPTURE.load(Ordering::Relaxed) {
98        1 => CaptureContent::NoContent,
99        2 => CaptureContent::SpanOnly,
100        3 => CaptureContent::EventOnly,
101        4 => CaptureContent::SpanAndEvent,
102        _ => std::env::var(CAPTURE_CONTENT_ENV)
103            .map(|value| CaptureContent::parse(&value))
104            .unwrap_or_default(),
105    }
106}
107
108/// Runtime configuration for semantic convention emission.
109///
110/// # Example
111/// ```
112/// use adk_telemetry::config::{CaptureContent, SemconvConfig};
113///
114/// // Default: content capture disabled
115/// let config = SemconvConfig::default();
116/// assert_eq!(config.capture_content, CaptureContent::NoContent);
117///
118/// // Record the conversation on spans while debugging
119/// let debug_config = SemconvConfig { capture_content: CaptureContent::SpanOnly };
120/// ```
121#[derive(Debug, Clone, Default)]
122pub struct SemconvConfig {
123    /// Where the conversation is recorded; [`CaptureContent::NoContent`] by default.
124    pub capture_content: CaptureContent,
125}
126
127impl SemconvConfig {
128    /// The process's setting: [`capture_content`].
129    pub fn from_process() -> Self {
130        Self { capture_content: capture_content() }
131    }
132
133    /// Applies this configuration to the process ([`set_capture_content`]).
134    pub fn install(&self) {
135        set_capture_content(self.capture_content);
136    }
137}
138
139#[cfg(test)]
140mod tests {
141    use super::*;
142
143    #[test]
144    fn test_default_config_disables_content_capture() {
145        assert_eq!(SemconvConfig::default().capture_content, CaptureContent::NoContent);
146    }
147
148    #[test]
149    fn modes_parse_as_the_conventions_spell_them() {
150        for (value, mode) in [
151            ("NO_CONTENT", CaptureContent::NoContent),
152            ("span_only", CaptureContent::SpanOnly),
153            ("Event_Only", CaptureContent::EventOnly),
154            (" SPAN_AND_EVENT ", CaptureContent::SpanAndEvent),
155            ("true", CaptureContent::NoContent),
156            ("", CaptureContent::NoContent),
157        ] {
158            assert_eq!(CaptureContent::parse(value), mode, "{value}");
159        }
160    }
161}