diff --git a/agent/runtime-attach/src/main/java/com/microsoft/applicationinsights/attach/ApplicationInsights.java b/agent/runtime-attach/src/main/java/com/microsoft/applicationinsights/attach/ApplicationInsights.java index a1acec8efea..8803a2a1f6a 100644 --- a/agent/runtime-attach/src/main/java/com/microsoft/applicationinsights/attach/ApplicationInsights.java +++ b/agent/runtime-attach/src/main/java/com/microsoft/applicationinsights/attach/ApplicationInsights.java @@ -5,15 +5,18 @@ import io.opentelemetry.contrib.attach.core.CoreRuntimeAttach; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Optional; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; +import javax.annotation.Nullable; /** This class allows you to attach the Application Insights agent for Java at runtime. */ public final class ApplicationInsights { @@ -52,7 +55,10 @@ public static void attach() { System.setProperty(RUNTIME_ATTACHED_ENABLED_PROPERTY, "true"); try { - Optional jsonConfig = findJsonConfig(); + Optional jsonConfig = findJsonConfigFromClasspath(); + if (!jsonConfig.isPresent()) { + jsonConfig = findJsonConfigFromFileSystem(); + } if (jsonConfig.isPresent()) { System.setProperty(RUNTIME_ATTACHED_JSON_PROPERTY, jsonConfig.get()); } @@ -66,7 +72,7 @@ public static void attach() { } } - private static Optional findJsonConfig() { + private static Optional findJsonConfigFromClasspath() { String fileName = findJsonConfigFile(); @@ -76,11 +82,23 @@ private static Optional findJsonConfig() { return Optional.empty(); } - String json = findJson(configContentAsInputStream); + String json = read(configContentAsInputStream); + return Optional.of(json); + } + + private static Optional findJsonConfigFromFileSystem() { + + InputStream configContentAsInputStream = findJsonConfigFromFileSystemAsStream(); + + if (configContentAsInputStream == null) { + return Optional.empty(); + } + + String json = read(configContentAsInputStream); return Optional.of(json); } - private static String findJson(InputStream configContentAsInputStream) { + private static String read(InputStream configContentAsInputStream) { try (InputStreamReader inputStreamReader = new InputStreamReader(configContentAsInputStream, StandardCharsets.UTF_8); BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) { @@ -91,6 +109,7 @@ private static String findJson(InputStream configContentAsInputStream) { } } + @Nullable private static InputStream findResourceAsStream(String fileName) { InputStream configContentAsInputStream = ApplicationInsights.class.getResourceAsStream("/" + fileName); @@ -100,6 +119,23 @@ private static InputStream findResourceAsStream(String fileName) { return configContentAsInputStream; } + @Nullable + private static InputStream findJsonConfigFromFileSystemAsStream() { + File defaultFile = new File("config/applicationinsights.json"); + if (!defaultFile.exists()) { + defaultFile = new File("applicationinsights.json"); + } + if (!defaultFile.exists()) { + return null; + } + try { + return Files.newInputStream(defaultFile.toPath()); + } catch (IOException e) { + throw new IllegalStateException( + "Unexpected issue during loading of JSON configuration file: " + e.getMessage()); + } + } + public static class ConfigurationException extends IllegalArgumentException { ConfigurationException(String message) { super(message);