Skip to content
Merged
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
28 changes: 27 additions & 1 deletion crates/metadata/src/platform/azure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub(super) fn collect() -> Option<PlatformData> {
}

fn collect_from<E: QueryEnv>(env: E) -> Option<PlatformData> {
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();
Expand All @@ -44,7 +45,9 @@ fn collect_from<E: QueryEnv>(env: E) -> Option<PlatformData> {
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));
}

Expand Down Expand Up @@ -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);
}
}
}
26 changes: 24 additions & 2 deletions crates/metadata/src/platform/gcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ fn first_env(env: &impl QueryEnv, names: &[&str]) -> Option<String> {
}

fn detect_runtime(env: &impl QueryEnv) -> Option<String> {
if let Some(runtime) = super::wrapper_runtime(env) {
return Some(runtime);
}
for (runtime, variable) in [
("node", "NODE_VERSION"),
("python", "PYTHON_VERSION"),
Expand Down Expand Up @@ -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]
Expand Down
31 changes: 31 additions & 0 deletions crates/metadata/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -14,10 +15,40 @@ pub(crate) struct PlatformData {
pub metadata: Map<String, Value>,
}

// Wrapper-supplied engine identity takes precedence over platform hints.
fn wrapper_runtime(env: &impl QueryEnv) -> Option<String> {
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<PlatformData> {
match env_type {
EnvironmentType::AzureFunction => azure::collect(),
EnvironmentType::CloudFunction => gcp::collect().await,
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);
}
}
Loading