Skip to content
Merged
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
40 changes: 38 additions & 2 deletions src/main/java/com/datadog/ServerlessCompatAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

enum CloudEnvironment {
AZURE_FUNCTION,
AZURE_SPRING_APP,
GOOGLE_CLOUD_RUN_FUNCTION_1ST_GEN,
UNKNOWN
}

public class ServerlessCompatAgent {
private static final Logger log = LoggerFactory.getLogger(ServerlessCompatAgent.class);
private static final String os = System.getProperty("os.name").toLowerCase();
Expand All @@ -23,13 +31,32 @@ public static boolean isLinux() {
return os.contains("linux");
}

public static CloudEnvironment getEnvironment() {
Map<String, String> env = System.getenv();

if (env.get("FUNCTIONS_EXTENSION_VERSION") != null &&
env.get("FUNCTIONS_WORKER_RUNTIME") != null) {
return CloudEnvironment.AZURE_FUNCTION;
}

if (env.get("ASCSVCRT_SPRING__APPLICATION__NAME") != null) {
return CloudEnvironment.AZURE_SPRING_APP;
}

if (env.get("FUNCTION_NAME") != null &&
env.get("GCP_PROJECT") != null) {
return CloudEnvironment.GOOGLE_CLOUD_RUN_FUNCTION_1ST_GEN;
}

return CloudEnvironment.UNKNOWN;
}

public static String setPackageVersion() {
String packageVersion;

try {
packageVersion = ServerlessCompatAgent.class.getPackage().getImplementationVersion();
}
catch (Exception e) {
} catch (Exception e) {
log.error("Unable to identify package version", e);
packageVersion = "unknown";
}
Expand All @@ -38,6 +65,15 @@ public static String setPackageVersion() {
}

public static void premain(String agentArgs, Instrumentation instrumentation) {
CloudEnvironment environment = getEnvironment();
log.debug("Environment detected: {}", environment);

if (environment == CloudEnvironment.UNKNOWN) {
log.error("{} environment detected, will not start the Datadog Serverless Compatibility Layer",
environment);
return;
}

final String fileName;
final String tempDirPath;

Expand Down