diff --git a/README.md b/README.md index 96b02ab..1fbefef 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,20 @@ A collection of libraries and binaries used for instrumenting AWS Lambda Functions, Azure Functions, and Azure Spring Apps. +## Compat inventory runtime metadata + +With `DD_SERVERLESS_COMPAT_METADATA_ENABLED=true`, Compat wrappers pass the language +engine to the child binary using `DD_SERVERLESS_COMPAT_RUNTIME` (for example, +`python3.13.7`, not the wrapper package version). +This best-effort hint takes precedence over platform hints for the inventory `runtime` +field. Missing, empty, or `unknown` hints retain the existing platform-based detection for +older wrappers. Platform-owned environment variables are not overwritten. + +This does not change `DD_SERVERLESS_COMPAT_VERSION`, which identifies the wrapper +package. Release a binary with this reader before bundling the updated wrappers; +older binaries do not read the new hint. Customers do not need to set it. Setting +`DD_SERVERLESS_COMPAT_METADATA_ENABLED=false` disables inventory reporting. + ## Development Install the git pre-push hook (runs `cargo fmt --check` and `cargo clippy -D warnings` before each push): diff --git a/crates/metadata/src/platform/azure.rs b/crates/metadata/src/platform/azure.rs index 365dca9..c977b3f 100644 --- a/crates/metadata/src/platform/azure.rs +++ b/crates/metadata/src/platform/azure.rs @@ -29,6 +29,7 @@ pub(super) fn collect() -> Option { } fn collect_from(env: E) -> Option { + let runtime = super::wrapper_runtime(&env); let azure = AzureMetadata::new_function(env)?; let resource_name = known_value(azure.get_site_name())?.to_string(); let resource_id = known_value(azure.get_resource_id())?.to_string(); @@ -44,7 +45,9 @@ fn collect_from(env: E) -> Option { metadata.insert(field.into(), Value::String(value.to_string())); } } - if let Some(runtime) = runtime_with_version(azure.get_runtime(), azure.get_runtime_version()) { + if let Some(runtime) = + runtime.or_else(|| runtime_with_version(azure.get_runtime(), azure.get_runtime_version())) + { metadata.insert("runtime".into(), Value::String(runtime)); } @@ -179,4 +182,27 @@ mod tests { .expect("Azure metadata should be available"); assert_eq!(data.metadata["runtime"], "dotnet"); } + + #[test] + fn wrapper_runtime_falls_back_to_platform_detection() { + for (runtime, expected) in [ + (Some("python3.13.7"), "python3.13.7"), + (None, "python3.12"), + (Some(""), "python3.12"), + (Some("unknown"), "python3.12"), + ] { + let mut values = vec![ + ("FUNCTIONS_WORKER_RUNTIME", "python"), + ("FUNCTIONS_WORKER_RUNTIME_VERSION", "3.12"), + ("WEBSITE_OWNER_NAME", "abc123+my-rg-eastuswebspace"), + ("WEBSITE_RESOURCE_GROUP", "my-rg"), + ("WEBSITE_SITE_NAME", "my-func-app"), + ]; + if let Some(runtime) = runtime { + values.push(("DD_SERVERLESS_COMPAT_RUNTIME", runtime)); + } + let data = collect_from(FakeEnv::new(&values)).unwrap(); + assert_eq!(data.metadata["runtime"], expected); + } + } } diff --git a/crates/metadata/src/platform/gcp.rs b/crates/metadata/src/platform/gcp.rs index ae9cd0a..c193c73 100644 --- a/crates/metadata/src/platform/gcp.rs +++ b/crates/metadata/src/platform/gcp.rs @@ -102,6 +102,9 @@ fn first_env(env: &impl QueryEnv, names: &[&str]) -> Option { } fn detect_runtime(env: &impl QueryEnv) -> Option { + if let Some(runtime) = super::wrapper_runtime(env) { + return Some(runtime); + } for (runtime, variable) in [ ("node", "NODE_VERSION"), ("python", "PYTHON_VERSION"), @@ -308,8 +311,27 @@ mod tests { #[test] fn detects_runtime_from_platform_environment() { - let env = FakeEnv::new(&[("PYTHON_VERSION", "3.12.7")]); - assert_eq!(detect_runtime(&env), Some("python3.12.7".into())); + for (overrides, expected) in [ + (vec![], "python3.12.7"), + ( + vec![ + ("DD_SERVERLESS_COMPAT_RUNTIME", "python3.13.7"), + ("NODE_VERSION", "22.0.0"), + ], + "python3.13.7", + ), + (vec![("DD_SERVERLESS_COMPAT_RUNTIME", "")], "python3.12.7"), + ( + vec![("DD_SERVERLESS_COMPAT_RUNTIME", "unknown")], + "python3.12.7", + ), + ] { + let mut values = vec![("PYTHON_VERSION", "3.12.7")]; + values.extend(overrides); + let env = FakeEnv::new(&values); + assert_eq!(detect_runtime(&env).as_deref(), Some(expected)); + } + assert_eq!(detect_runtime(&FakeEnv::new(&[])), None); } #[tokio::test] diff --git a/crates/metadata/src/platform/mod.rs b/crates/metadata/src/platform/mod.rs index e23c2c4..3bfd572 100644 --- a/crates/metadata/src/platform/mod.rs +++ b/crates/metadata/src/platform/mod.rs @@ -4,6 +4,7 @@ mod azure; mod gcp; +use libdd_common::azure_app_services::QueryEnv; use libdd_trace_utils::trace_utils::EnvironmentType; use serde_json::{Map, Value}; @@ -14,6 +15,13 @@ pub(crate) struct PlatformData { pub metadata: Map, } +// Wrapper-supplied engine identity takes precedence over platform hints. +fn wrapper_runtime(env: &impl QueryEnv) -> Option { + env.get_var("DD_SERVERLESS_COMPAT_RUNTIME") + .map(|value| value.trim().to_owned()) + .filter(|value| !value.is_empty() && !value.eq_ignore_ascii_case("unknown")) +} + pub(crate) async fn collect(env_type: &EnvironmentType) -> Option { match env_type { EnvironmentType::AzureFunction => azure::collect(), @@ -21,3 +29,26 @@ pub(crate) async fn collect(env_type: &EnvironmentType) -> Option EnvironmentType::LambdaFunction | EnvironmentType::AzureSpringApp => None, } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_env::FakeEnv; + + #[test] + fn wrapper_runtime_is_an_optional_best_effort_hint() { + for (value, expected) in [ + ("python3.13.7", Some("python3.13.7")), + ("java21.0.1+12", Some("java21.0.1+12")), + (" python3.13.7 ", Some("python3.13.7")), + ("", None), + (" ", None), + ("unknown", None), + (" UNKNOWN ", None), + ] { + let env = FakeEnv::new(&[("DD_SERVERLESS_COMPAT_RUNTIME", value)]); + assert_eq!(wrapper_runtime(&env).as_deref(), expected); + } + assert_eq!(wrapper_runtime(&FakeEnv::new(&[])), None); + } +}