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
56 changes: 42 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@
<artifactId>slf4j-simple</artifactId>
<version>2.0.16</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>6.0.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>6.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -143,38 +157,46 @@
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>module-info.class</exclude>
<exclude>META-INF/*</exclude>
<exclude>META-INF/versions/**</exclude>
<exclude>META-INF/services/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<!--
Remove some cruft from the final jar.
These patterns should NOT include **/META-INF/maven/**/pom.properties, which is
used to report our own dependencies, but we should remove the top-level metadata
of vendored packages because those could trigger unwanted framework checks.
-->
<excludes>
<exclude>module-info.class</exclude>
<exclude>META-INF/*</exclude>
<exclude>META-INF/versions/**</exclude>
<exclude>META-INF/maven/org.slf4j/**</exclude>
<exclude>META-INF/maven/**/pom.xml</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>org.slf4j</pattern>
<shadedPattern>datadogserverless.slf4j</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
Expand All @@ -189,6 +211,12 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
</plugin>
</plugins>
</build>
</project>
31 changes: 26 additions & 5 deletions src/main/java/com/datadog/ServerlessCompatAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.NOPLogger;

enum CloudEnvironment {
AZURE_FUNCTION,
Expand All @@ -19,7 +20,24 @@ enum CloudEnvironment {
}

public class ServerlessCompatAgent {
private static final Logger log = LoggerFactory.getLogger(ServerlessCompatAgent.class);
private static final String ddLogLevel = System.getenv().getOrDefault("DD_LOG_LEVEL", "INFO").toUpperCase();

private static final Logger log = initLogger(ddLogLevel);

private static Logger initLogger(String logLevel) {
if ("OFF".equals(logLevel)) {
return NOPLogger.NOP_LOGGER;
} else {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel",
mapDdLogLevelToSlf4jLogLevel(logLevel));
return LoggerFactory.getLogger(ServerlessCompatAgent.class);
}
}

private static String mapDdLogLevelToSlf4jLogLevel(String ddLogLevel) {
return "CRITICAL".equals(ddLogLevel) ? "ERROR" : ddLogLevel;
}

private static final String os = System.getProperty("os.name").toLowerCase();
private static final String binaryPath = System.getenv("DD_SERVERLESS_COMPAT_PATH");

Expand Down Expand Up @@ -65,7 +83,8 @@ public static String getPackageVersion() {
}

public static boolean isAzureFlexWithoutDDAzureResourceGroup() {
return "FlexConsumption".equals(System.getenv("WEBSITE_SKU")) && System.getenv("DD_AZURE_RESOURCE_GROUP") == null;
return "FlexConsumption".equals(System.getenv("WEBSITE_SKU"))
&& System.getenv("DD_AZURE_RESOURCE_GROUP") == null;
}

public static void premain(String agentArgs, Instrumentation instrumentation) {
Expand Down Expand Up @@ -94,11 +113,13 @@ public static void premain(String agentArgs, Instrumentation instrumentation) {
return;
}

// Check for Azure Flex Consumption functions that don't have the DD_AZURE_RESOURCE_GROUP environment variable set
// Check for Azure Flex Consumption functions that don't have the
// DD_AZURE_RESOURCE_GROUP environment variable set
if (environment == CloudEnvironment.AZURE_FUNCTION && isAzureFlexWithoutDDAzureResourceGroup()) {
log.error("Azure function detected on flex consumption plan without DD_AZURE_RESOURCE_GROUP set. Please set the DD_AZURE_RESOURCE_GROUP environment variable to your resource group name in Azure app settings. Shutting down Datadog Serverless Compatibility Layer.");
log.error(
"Azure function detected on flex consumption plan without DD_AZURE_RESOURCE_GROUP set. Please set the DD_AZURE_RESOURCE_GROUP environment variable to your resource group name in Azure app settings. Shutting down Datadog Serverless Compatibility Layer.");
return;
}
}

try (InputStream inputStream = ServerlessCompatAgent.class.getClassLoader()
.getResourceAsStream(fileName)) {
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import com.datadog.ServerlessCompatAgent;
import java.lang.reflect.Method;
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"));
}
}
}