From 2d3bbaa29e6ac221570fee087b7b5a2d0148fe49 Mon Sep 17 00:00:00 2001 From: Nina Rei Date: Wed, 4 Nov 2026 16:00:26 -0500 Subject: [PATCH 1/3] fix: forward the loaded java runtime to Compat metadata --- README.md | 8 ++++++++ .../java/com/datadog/ServerlessCompatAgent.java | 14 ++++++++++++-- .../com/datadog/ServerlessCompatAgentTest.java | 13 +++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c9442b4..cc056a9 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,14 @@ Datadog library for Java to enable tracing and custom metric submission from [Azure Functions](https://azure.microsoft.com/en-us/products/functions) and [Google Cloud Run Functions (1st gen)](https://cloud.google.com/functions/1stgendocs/concepts/overview). +## Inventory version metadata + +With `DD_SERVERLESS_COMPAT_METADATA_ENABLED=true`, inventory reports the wrapper package as +`serverless_compat_runtime_version`, the embedded binary release as +`serverless_compat_binary_version`, and the language engine in `runtime`. +`DD_SERVERLESS_COMPAT_PATH` selects a custom binary but does not change the wrapper package version. +The binary must supply its own embedded version; an unversioned binary must not inherit the wrapper version. + # Getting Started - [Install Serverless Monitoring for Azure Functions](https://docs.datadoghq.com/serverless/azure_functions/?code-lang=java) diff --git a/src/main/java/com/datadog/ServerlessCompatAgent.java b/src/main/java/com/datadog/ServerlessCompatAgent.java index 97abbae..12e5be6 100644 --- a/src/main/java/com/datadog/ServerlessCompatAgent.java +++ b/src/main/java/com/datadog/ServerlessCompatAgent.java @@ -169,12 +169,22 @@ public static void premain(String agentArgs, Instrumentation instrumentation) { String packageVersion = getPackageVersion(); log.debug("Found package version {}", packageVersion); - ProcessBuilder processBuilder = new ProcessBuilder(executableFile.getAbsolutePath()); - processBuilder.environment().put("DD_SERVERLESS_COMPAT_VERSION", packageVersion); + ProcessBuilder processBuilder = createProcessBuilder(executableFile, packageVersion, environment); processBuilder.inheritIO(); processBuilder.start(); } catch (Exception e) { log.error("Exception when starting {}", fileName, e); } } + + static ProcessBuilder createProcessBuilder(File executable, String packageVersion, CloudEnvironment environment) { + ProcessBuilder processBuilder = new ProcessBuilder(executable.getAbsolutePath()); + Map childEnvironment = processBuilder.environment(); + childEnvironment.put("DD_SERVERLESS_COMPAT_VERSION", packageVersion); + childEnvironment.put("JAVA_VERSION", System.getProperty("java.version")); + if (environment == CloudEnvironment.AZURE_FUNCTION) { + childEnvironment.put("FUNCTIONS_WORKER_RUNTIME_VERSION", System.getProperty("java.version")); + } + return processBuilder; + } } diff --git a/src/test/java/com/datadog/ServerlessCompatAgentTest.java b/src/test/java/com/datadog/ServerlessCompatAgentTest.java index 8f20a84..8d71fe4 100644 --- a/src/test/java/com/datadog/ServerlessCompatAgentTest.java +++ b/src/test/java/com/datadog/ServerlessCompatAgentTest.java @@ -1,6 +1,7 @@ package com.datadog; import java.lang.reflect.Method; +import java.io.File; import java.util.HashMap; import java.util.Map; import org.junit.jupiter.api.Test; @@ -13,6 +14,18 @@ class ServerlessCompatAgentTest { + @Test + void childReceivesWrapperAndEngineVersionsForCustomBinary() { + File executable = new File("/custom/compat"); + ProcessBuilder child = ServerlessCompatAgent.createProcessBuilder( + executable, "0.25.0", CloudEnvironment.AZURE_FUNCTION); + assertEquals(executable.getAbsolutePath(), child.command().get(0)); + assertEquals("0.25.0", child.environment().get("DD_SERVERLESS_COMPAT_VERSION")); + assertEquals(System.getProperty("java.version"), child.environment().get("JAVA_VERSION")); + assertEquals(System.getProperty("java.version"), child.environment().get("FUNCTIONS_WORKER_RUNTIME_VERSION")); + assertEquals(System.getenv("DD_SERVICE"), child.environment().get("DD_SERVICE")); + } + @ParameterizedTest @CsvSource({ "TRACE, TRACE", From bc3f6475393a94a34b4f9c4b8c2e07e3d1a8d22e Mon Sep 17 00:00:00 2001 From: Nina Rei Date: Mon, 9 Nov 2026 11:47:24 -0500 Subject: [PATCH 2/3] fix: use a combined Compat runtime metadata hint --- README.md | 5 +++ .../com/datadog/ServerlessCompatAgent.java | 12 ++++--- .../datadog/ServerlessCompatAgentTest.java | 31 +++++++++++++++++-- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index cc056a9..8c36e86 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,11 @@ With `DD_SERVERLESS_COMPAT_METADATA_ENABLED=true`, inventory reports the wrapper `DD_SERVERLESS_COMPAT_PATH` selects a custom binary but does not change the wrapper package version. The binary must supply its own embedded version; an unversioned binary must not inherit the wrapper version. +The wrapper passes a combined language and version hint (for example, `python3.13.7`) +through `DD_SERVERLESS_COMPAT_RUNTIME` +to the child binary without overwriting platform-owned version variables. This requires a +binary that supports this contract; older binaries retain their platform-based runtime detection. + # Getting Started - [Install Serverless Monitoring for Azure Functions](https://docs.datadoghq.com/serverless/azure_functions/?code-lang=java) diff --git a/src/main/java/com/datadog/ServerlessCompatAgent.java b/src/main/java/com/datadog/ServerlessCompatAgent.java index 12e5be6..eef1526 100644 --- a/src/main/java/com/datadog/ServerlessCompatAgent.java +++ b/src/main/java/com/datadog/ServerlessCompatAgent.java @@ -169,7 +169,7 @@ public static void premain(String agentArgs, Instrumentation instrumentation) { String packageVersion = getPackageVersion(); log.debug("Found package version {}", packageVersion); - ProcessBuilder processBuilder = createProcessBuilder(executableFile, packageVersion, environment); + ProcessBuilder processBuilder = createProcessBuilder(executableFile, packageVersion); processBuilder.inheritIO(); processBuilder.start(); } catch (Exception e) { @@ -177,13 +177,15 @@ public static void premain(String agentArgs, Instrumentation instrumentation) { } } - static ProcessBuilder createProcessBuilder(File executable, String packageVersion, CloudEnvironment environment) { + static ProcessBuilder createProcessBuilder(File executable, String packageVersion) { ProcessBuilder processBuilder = new ProcessBuilder(executable.getAbsolutePath()); Map childEnvironment = processBuilder.environment(); childEnvironment.put("DD_SERVERLESS_COMPAT_VERSION", packageVersion); - childEnvironment.put("JAVA_VERSION", System.getProperty("java.version")); - if (environment == CloudEnvironment.AZURE_FUNCTION) { - childEnvironment.put("FUNCTIONS_WORKER_RUNTIME_VERSION", System.getProperty("java.version")); + String javaVersion = System.getProperty("java.version"); + if (javaVersion != null && !javaVersion.trim().isEmpty()) { + childEnvironment.put("DD_SERVERLESS_COMPAT_RUNTIME", "java" + javaVersion.trim()); + } else { + childEnvironment.remove("DD_SERVERLESS_COMPAT_RUNTIME"); } return processBuilder; } diff --git a/src/test/java/com/datadog/ServerlessCompatAgentTest.java b/src/test/java/com/datadog/ServerlessCompatAgentTest.java index 8d71fe4..a1926f5 100644 --- a/src/test/java/com/datadog/ServerlessCompatAgentTest.java +++ b/src/test/java/com/datadog/ServerlessCompatAgentTest.java @@ -17,15 +17,40 @@ class ServerlessCompatAgentTest { @Test void childReceivesWrapperAndEngineVersionsForCustomBinary() { File executable = new File("/custom/compat"); + String inheritedWorkerVersion = System.getenv("FUNCTIONS_WORKER_RUNTIME_VERSION"); ProcessBuilder child = ServerlessCompatAgent.createProcessBuilder( - executable, "0.25.0", CloudEnvironment.AZURE_FUNCTION); + executable, "0.25.0"); assertEquals(executable.getAbsolutePath(), child.command().get(0)); assertEquals("0.25.0", child.environment().get("DD_SERVERLESS_COMPAT_VERSION")); - assertEquals(System.getProperty("java.version"), child.environment().get("JAVA_VERSION")); - assertEquals(System.getProperty("java.version"), child.environment().get("FUNCTIONS_WORKER_RUNTIME_VERSION")); + assertEquals("java" + System.getProperty("java.version"), child.environment().get("DD_SERVERLESS_COMPAT_RUNTIME")); + assertEquals(System.getenv("JAVA_VERSION"), child.environment().get("JAVA_VERSION")); + assertEquals(inheritedWorkerVersion, child.environment().get("FUNCTIONS_WORKER_RUNTIME_VERSION")); + assertEquals(inheritedWorkerVersion, System.getenv("FUNCTIONS_WORKER_RUNTIME_VERSION")); assertEquals(System.getenv("DD_SERVICE"), child.environment().get("DD_SERVICE")); } + @Test + void missingJavaVersionDoesNotSupplyRuntimeHint() { + String originalVersion = System.getProperty("java.version"); + try { + for (String version : new String[] {null, "", " "}) { + if (version == null) { + System.clearProperty("java.version"); + } else { + System.setProperty("java.version", version); + } + ProcessBuilder child = ServerlessCompatAgent.createProcessBuilder(new File("/custom/compat"), "0.25.0"); + assertFalse(child.environment().containsKey("DD_SERVERLESS_COMPAT_RUNTIME")); + } + } finally { + if (originalVersion == null) { + System.clearProperty("java.version"); + } else { + System.setProperty("java.version", originalVersion); + } + } + } + @ParameterizedTest @CsvSource({ "TRACE, TRACE", From 5b0c2ab3fe51b1a32ec9bd064228eca05ea27673 Mon Sep 17 00:00:00 2001 From: Nina Rei Date: Tue, 10 Nov 2026 16:41:06 -0500 Subject: [PATCH 3/3] test: remove artificial runtime version case and internal README details --- README.md | 13 ----------- .../datadog/ServerlessCompatAgentTest.java | 22 ------------------- 2 files changed, 35 deletions(-) diff --git a/README.md b/README.md index 8c36e86..c9442b4 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,6 @@ Datadog library for Java to enable tracing and custom metric submission from [Azure Functions](https://azure.microsoft.com/en-us/products/functions) and [Google Cloud Run Functions (1st gen)](https://cloud.google.com/functions/1stgendocs/concepts/overview). -## Inventory version metadata - -With `DD_SERVERLESS_COMPAT_METADATA_ENABLED=true`, inventory reports the wrapper package as -`serverless_compat_runtime_version`, the embedded binary release as -`serverless_compat_binary_version`, and the language engine in `runtime`. -`DD_SERVERLESS_COMPAT_PATH` selects a custom binary but does not change the wrapper package version. -The binary must supply its own embedded version; an unversioned binary must not inherit the wrapper version. - -The wrapper passes a combined language and version hint (for example, `python3.13.7`) -through `DD_SERVERLESS_COMPAT_RUNTIME` -to the child binary without overwriting platform-owned version variables. This requires a -binary that supports this contract; older binaries retain their platform-based runtime detection. - # Getting Started - [Install Serverless Monitoring for Azure Functions](https://docs.datadoghq.com/serverless/azure_functions/?code-lang=java) diff --git a/src/test/java/com/datadog/ServerlessCompatAgentTest.java b/src/test/java/com/datadog/ServerlessCompatAgentTest.java index a1926f5..9bc81a7 100644 --- a/src/test/java/com/datadog/ServerlessCompatAgentTest.java +++ b/src/test/java/com/datadog/ServerlessCompatAgentTest.java @@ -29,28 +29,6 @@ void childReceivesWrapperAndEngineVersionsForCustomBinary() { assertEquals(System.getenv("DD_SERVICE"), child.environment().get("DD_SERVICE")); } - @Test - void missingJavaVersionDoesNotSupplyRuntimeHint() { - String originalVersion = System.getProperty("java.version"); - try { - for (String version : new String[] {null, "", " "}) { - if (version == null) { - System.clearProperty("java.version"); - } else { - System.setProperty("java.version", version); - } - ProcessBuilder child = ServerlessCompatAgent.createProcessBuilder(new File("/custom/compat"), "0.25.0"); - assertFalse(child.environment().containsKey("DD_SERVERLESS_COMPAT_RUNTIME")); - } - } finally { - if (originalVersion == null) { - System.clearProperty("java.version"); - } else { - System.setProperty("java.version", originalVersion); - } - } - } - @ParameterizedTest @CsvSource({ "TRACE, TRACE",