use std::sync::Arc;
#[cfg(test)]
pub(crate) use gadk_agent::agent::telemetry::{OTEL_SEMCONV_1_36, SCHEMA_VERSION_OPT_IN_ENV_VAR};
pub(crate) use gadk_agent::agent::telemetry::{env_provider as provider, use_legacy_schema};
use gadk_platform::env::EnvProvider;
use gadk_platform::{go_to_lower, go_to_upper};
use parking_lot::Mutex;
pub(crate) const CAPTURE_MESSAGE_CONTENT_ENV_VAR: &str =
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ContentCaptureMode {
None,
EventOnly,
SpanOnly,
SpanAndEvent,
}
static CAPTURE_MODE: Mutex<Option<ContentCaptureMode>> = Mutex::new(None);
pub fn set_env_provider(env: Arc<dyn EnvProvider>) {
gadk_agent::agent::telemetry::set_env_provider(env);
*CAPTURE_MODE.lock() = None;
}
fn getenv(key: &str) -> String {
provider().var(key).unwrap_or_default()
}
pub(crate) fn parse_content_capture_mode(s: &str) -> ContentCaptureMode {
match go_to_upper(s.trim()).as_str() {
"EVENT_ONLY" => return ContentCaptureMode::EventOnly,
"SPAN_ONLY" => return ContentCaptureMode::SpanOnly,
"SPAN_AND_EVENT" => return ContentCaptureMode::SpanAndEvent,
_ => {}
}
if evals_to_true(s) {
return ContentCaptureMode::EventOnly;
}
ContentCaptureMode::None
}
pub(crate) fn evals_to_true(s: &str) -> bool {
let u = go_to_lower(s.trim());
u == "1" || u == "true"
}
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn apply_env() {
*CAPTURE_MODE.lock() = Some(parse_content_capture_mode(&getenv(
CAPTURE_MESSAGE_CONTENT_ENV_VAR,
)));
}
pub(crate) fn capture_mode() -> ContentCaptureMode {
let mut cached = CAPTURE_MODE.lock();
*cached
.get_or_insert_with(|| parse_content_capture_mode(&getenv(CAPTURE_MESSAGE_CONTENT_ENV_VAR)))
}
pub(crate) fn capture_message_content() -> bool {
matches!(
capture_mode(),
ContentCaptureMode::EventOnly | ContentCaptureMode::SpanAndEvent
)
}
pub(crate) fn capture_content_on_spans() -> bool {
matches!(
capture_mode(),
ContentCaptureMode::SpanOnly | ContentCaptureMode::SpanAndEvent
)
}
#[cfg(test)]
pub(crate) struct TestEnv {
_lock: parking_lot::MutexGuard<'static, ()>,
}
#[cfg(test)]
pub(crate) fn test_env(pairs: &[(&str, &str)]) -> TestEnv {
let lock = crate::testutil::telemetry::ENV_LOCK.lock();
set_env_provider(Arc::new(gadk_platform::env::MapEnv(
pairs
.iter()
.map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
.collect(),
)));
TestEnv { _lock: lock }
}
#[cfg(test)]
impl Drop for TestEnv {
fn drop(&mut self) {
set_env_provider(Arc::new(gadk_platform::env::MapEnv::default()));
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use std::collections::HashMap;
use gadk_platform::env::MapEnv;
use super::*;
use crate::testutil::telemetry::ENV_LOCK;
fn env(pairs: &[(&str, &str)]) -> Arc<dyn EnvProvider> {
Arc::new(MapEnv(
pairs
.iter()
.map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
.collect::<HashMap<_, _>>(),
))
}
#[test]
fn parse_rules_follow_go() {
use ContentCaptureMode::*;
for (s, want) in [
("EVENT_ONLY", EventOnly),
(" event_only ", EventOnly),
("SPAN_ONLY", SpanOnly),
("span_and_event", SpanAndEvent),
("1", EventOnly),
("TRUE", EventOnly),
(" true\n", EventOnly),
("yes", None),
("", None),
("0", None),
] {
assert_eq!(parse_content_capture_mode(s), want, "{s:?}");
}
}
#[test]
fn capture_mode_is_cached_until_the_provider_changes() {
let _g = ENV_LOCK.lock();
set_env_provider(env(&[(CAPTURE_MESSAGE_CONTENT_ENV_VAR, "SPAN_AND_EVENT")]));
assert!(capture_message_content());
assert!(capture_content_on_spans());
gadk_agent::agent::telemetry::set_env_provider(env(&[]));
assert_eq!(capture_mode(), ContentCaptureMode::SpanAndEvent);
apply_env();
assert_eq!(capture_mode(), ContentCaptureMode::None);
set_env_provider(env(&[(CAPTURE_MESSAGE_CONTENT_ENV_VAR, "true")]));
assert!(capture_message_content());
assert!(!capture_content_on_spans());
set_env_provider(Arc::new(MapEnv::default()));
assert_eq!(capture_mode(), ContentCaptureMode::None);
}
#[test]
fn schema_opt_in_is_read_on_every_call() {
let _g = ENV_LOCK.lock();
set_env_provider(env(&[(
SCHEMA_VERSION_OPT_IN_ENV_VAR,
" OTEL_SEMCONV_1_36 ",
)]));
assert!(use_legacy_schema());
gadk_agent::agent::telemetry::set_env_provider(env(&[(
SCHEMA_VERSION_OPT_IN_ENV_VAR,
"1",
)]));
assert!(use_legacy_schema());
gadk_agent::agent::telemetry::set_env_provider(env(&[(
SCHEMA_VERSION_OPT_IN_ENV_VAR,
"2",
)]));
assert!(!use_legacy_schema());
set_env_provider(Arc::new(MapEnv::default()));
assert!(!use_legacy_schema());
}
}