-
Notifications
You must be signed in to change notification settings - Fork 0
[SVLS-8757] Improve cloud environment detection logic #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
@@ -16,6 +18,7 @@ enum CloudEnvironment { | |
| AZURE_FUNCTION, | ||
| AZURE_SPRING_APP, | ||
| GOOGLE_CLOUD_RUN_FUNCTION_1ST_GEN, | ||
| GOOGLE_CLOUD_RUN_FUNCTION_2ND_GEN, | ||
| UNKNOWN | ||
| } | ||
|
|
||
|
|
@@ -50,23 +53,39 @@ public static boolean isLinux() { | |
| } | ||
|
|
||
| public static CloudEnvironment getEnvironment() { | ||
| Map<String, String> env = System.getenv(); | ||
| return getEnvironment(System.getenv()); | ||
| } | ||
|
|
||
| static CloudEnvironment getEnvironment(Map<String, String> env) { | ||
| List<CloudEnvironment> detected = new ArrayList<>(); | ||
|
|
||
| if (env.get("FUNCTIONS_EXTENSION_VERSION") != null && | ||
| env.get("FUNCTIONS_WORKER_RUNTIME") != null) { | ||
| return CloudEnvironment.AZURE_FUNCTION; | ||
| if (env.get("FUNCTIONS_EXTENSION_VERSION") != null | ||
| && env.get("FUNCTIONS_WORKER_RUNTIME") != null) { | ||
| detected.add(CloudEnvironment.AZURE_FUNCTION); | ||
| } | ||
|
|
||
| if (env.get("ASCSVCRT_SPRING__APPLICATION__NAME") != null) { | ||
| return CloudEnvironment.AZURE_SPRING_APP; | ||
| detected.add(CloudEnvironment.AZURE_SPRING_APP); | ||
| } | ||
|
Comment on lines
55
to
69
|
||
|
|
||
| if (env.get("FUNCTION_NAME") != null && | ||
| env.get("GCP_PROJECT") != null) { | ||
| return CloudEnvironment.GOOGLE_CLOUD_RUN_FUNCTION_1ST_GEN; | ||
| if (env.get("FUNCTION_NAME") != null && env.get("GCP_PROJECT") != null) { | ||
| // Set by Google Cloud Functions for older runtimes | ||
| detected.add(CloudEnvironment.GOOGLE_CLOUD_RUN_FUNCTION_1ST_GEN); | ||
| } else if (env.get("K_SERVICE") != null && env.get("FUNCTION_TARGET") != null) { | ||
| // Set by Google Cloud Functions for newer runtimes | ||
| detected.add(CloudEnvironment.GOOGLE_CLOUD_RUN_FUNCTION_2ND_GEN); | ||
| } | ||
|
|
||
| if (detected.isEmpty()) { | ||
| return CloudEnvironment.UNKNOWN; | ||
| } | ||
| if (detected.size() > 1) { | ||
| log.error("Multiple cloud environments detected: {}", detected); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it'd make sense to track gen2/unknown uses of the compat layer, similarly to the telemetry we are thinking about adding for serverless-init? https://datadoghq.atlassian.net/browse/SVLS-8970 (possibility is a separate question)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be interesting if we are able to track that! I can bring it up in the next standup or ask Piyali |
||
| return CloudEnvironment.UNKNOWN; | ||
| } | ||
|
|
||
| return CloudEnvironment.UNKNOWN; | ||
| CloudEnvironment environment = detected.get(0); | ||
| return environment; | ||
| } | ||
|
|
||
| public static String getPackageVersion() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package com.datadog; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.helpers.NOPLogger; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class ServerlessCompatAgentTest { | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "TRACE, TRACE", | ||
| "DEBUG, DEBUG", | ||
| "INFO, INFO", | ||
| "WARN, WARN", | ||
| "ERROR, ERROR", | ||
| "CRITICAL, ERROR", | ||
| "OFF, null" | ||
| }) | ||
| void testInitLogger(String ddLogLevel, String expectedSlf4jLevel) throws Exception { | ||
| Method initLoggerMethod = ServerlessCompatAgent.class.getDeclaredMethod("initLogger", String.class); | ||
| initLoggerMethod.setAccessible(true); | ||
| Logger logger = (Logger) initLoggerMethod.invoke(null, ddLogLevel); | ||
|
|
||
| if ("OFF".equals(ddLogLevel)) { | ||
| assertTrue(logger instanceof NOPLogger); | ||
| } else { | ||
| assertEquals(expectedSlf4jLevel, | ||
| System.getProperty("org.slf4j.simpleLogger.defaultLogLevel")); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void getEnvironment_returnsUnknownWhenNoVarsSet() { | ||
| assertEquals(CloudEnvironment.UNKNOWN, | ||
| ServerlessCompatAgent.getEnvironment(new HashMap<>())); | ||
| } | ||
|
|
||
| @Test | ||
| void getEnvironment_detectsAzureFunction() { | ||
| Map<String, String> env = new HashMap<>(); | ||
| env.put("FUNCTIONS_EXTENSION_VERSION", "~4"); | ||
| env.put("FUNCTIONS_WORKER_RUNTIME", "java"); | ||
| assertEquals(CloudEnvironment.AZURE_FUNCTION, | ||
| ServerlessCompatAgent.getEnvironment(env)); | ||
| } | ||
|
|
||
| @Test | ||
| void getEnvironment_requiresBothAzureFunctionVars() { | ||
| Map<String, String> env = new HashMap<>(); | ||
| env.put("FUNCTIONS_EXTENSION_VERSION", "~4"); | ||
| assertEquals(CloudEnvironment.UNKNOWN, | ||
| ServerlessCompatAgent.getEnvironment(env)); | ||
| } | ||
|
|
||
| @Test | ||
| void getEnvironment_detectsAzureSpringApp() { | ||
| Map<String, String> env = new HashMap<>(); | ||
| env.put("ASCSVCRT_SPRING__APPLICATION__NAME", "my-app"); | ||
| assertEquals(CloudEnvironment.AZURE_SPRING_APP, | ||
| ServerlessCompatAgent.getEnvironment(env)); | ||
| } | ||
|
|
||
| @Test | ||
| void getEnvironment_detectsGcpOlderRuntimes() { | ||
| Map<String, String> env = new HashMap<>(); | ||
| env.put("FUNCTION_NAME", "my-function"); | ||
| env.put("GCP_PROJECT", "my-project"); | ||
| assertEquals(CloudEnvironment.GOOGLE_CLOUD_RUN_FUNCTION_1ST_GEN, | ||
| ServerlessCompatAgent.getEnvironment(env)); | ||
| } | ||
|
|
||
| @Test | ||
| void getEnvironment_requiresBothGcpOlderRuntimesVars() { | ||
| Map<String, String> env = new HashMap<>(); | ||
| env.put("FUNCTION_NAME", "my-function"); | ||
| assertEquals(CloudEnvironment.UNKNOWN, | ||
| ServerlessCompatAgent.getEnvironment(env)); | ||
| } | ||
|
|
||
| @Test | ||
| void getEnvironment_detectsGcpNewerRuntimes() { | ||
| Map<String, String> env = new HashMap<>(); | ||
| env.put("K_SERVICE", "my-service"); | ||
| env.put("FUNCTION_TARGET", "handler"); | ||
| assertEquals(CloudEnvironment.GOOGLE_CLOUD_RUN_FUNCTION_2ND_GEN, | ||
| ServerlessCompatAgent.getEnvironment(env)); | ||
| } | ||
|
|
||
| @Test | ||
| void getEnvironment_returnsUnknownWhenMultipleEnvironmentsDetected() { | ||
| Map<String, String> env = new HashMap<>(); | ||
| env.put("FUNCTIONS_EXTENSION_VERSION", "~4"); | ||
| env.put("FUNCTIONS_WORKER_RUNTIME", "java"); | ||
| env.put("FUNCTION_NAME", "my-function"); | ||
| env.put("GCP_PROJECT", "my-project"); | ||
| assertEquals(CloudEnvironment.UNKNOWN, | ||
| ServerlessCompatAgent.getEnvironment(env)); | ||
| } | ||
|
|
||
| @Test | ||
| void getEnvironment_bareFunctionNameDoesNotTriggerGcpDetection() { | ||
| Map<String, String> env = new HashMap<>(); | ||
| env.put("FUNCTION_NAME", "my-function"); | ||
| env.put("FUNCTIONS_EXTENSION_VERSION", "~4"); | ||
| env.put("FUNCTIONS_WORKER_RUNTIME", "java"); | ||
| assertEquals(CloudEnvironment.AZURE_FUNCTION, | ||
| ServerlessCompatAgent.getEnvironment(env)); | ||
| } | ||
| } |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getEnvironment()behavior changed significantly (multi-detection handling + new Google signature), but there are no unit tests covering these branches. Since the repo already has JUnit tests forServerlessCompatAgent, consider extracting the detection logic into a package-private helper that accepts an envMap<String,String>so tests can cover each detection path and the "multiple environments" case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added unit tests in c359922