From deb4dbc94ecf61364fccb482e4515ce8a7b15784 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Wed, 18 Dec 2024 14:17:13 -0800 Subject: [PATCH 1/4] Support finding applicationinsights.json in current dir when using runtime attach --- .../attach/ApplicationInsights.java | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) 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..19ca94d14a2 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,10 +5,12 @@ 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; @@ -52,7 +54,10 @@ public static void attach() { System.setProperty(RUNTIME_ATTACHED_ENABLED_PROPERTY, "true"); try { - Optional jsonConfig = findJsonConfig(); + Optional jsonConfig = findJsonConfigFromClasspath(); + if (!jsonConfig.isPresent()) { + jsonConfig = findJsonConfigFromDefaultFileInCurrentDir(); + } if (jsonConfig.isPresent()) { System.setProperty(RUNTIME_ATTACHED_JSON_PROPERTY, jsonConfig.get()); } @@ -66,7 +71,7 @@ public static void attach() { } } - private static Optional findJsonConfig() { + private static Optional findJsonConfigFromClasspath() { String fileName = findJsonConfigFile(); @@ -80,6 +85,18 @@ private static Optional findJsonConfig() { return Optional.of(json); } + private static Optional findJsonConfigFromDefaultFileInCurrentDir() { + + InputStream configContentAsInputStream = findDefaultFileInCurrentDirAsStream(); + + if (configContentAsInputStream == null) { + return Optional.empty(); + } + + String json = findJson(configContentAsInputStream); + return Optional.of(json); + } + private static String findJson(InputStream configContentAsInputStream) { try (InputStreamReader inputStreamReader = new InputStreamReader(configContentAsInputStream, StandardCharsets.UTF_8); @@ -100,6 +117,19 @@ private static InputStream findResourceAsStream(String fileName) { return configContentAsInputStream; } + private static InputStream findDefaultFileInCurrentDirAsStream() { + File 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); From 68e07d5111ffb74e9fcbce10a0bc020b511c5a90 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Thu, 19 Dec 2024 15:11:46 -0800 Subject: [PATCH 2/4] and config dir --- .../applicationinsights/attach/ApplicationInsights.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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 19ca94d14a2..bd9cee0b50a 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 @@ -56,7 +56,7 @@ public static void attach() { try { Optional jsonConfig = findJsonConfigFromClasspath(); if (!jsonConfig.isPresent()) { - jsonConfig = findJsonConfigFromDefaultFileInCurrentDir(); + jsonConfig = findJsonConfigFromFileSystem(); } if (jsonConfig.isPresent()) { System.setProperty(RUNTIME_ATTACHED_JSON_PROPERTY, jsonConfig.get()); @@ -85,7 +85,7 @@ private static Optional findJsonConfigFromClasspath() { return Optional.of(json); } - private static Optional findJsonConfigFromDefaultFileInCurrentDir() { + private static Optional findJsonConfigFromFileSystem() { InputStream configContentAsInputStream = findDefaultFileInCurrentDirAsStream(); @@ -118,7 +118,10 @@ private static InputStream findResourceAsStream(String fileName) { } private static InputStream findDefaultFileInCurrentDirAsStream() { - File defaultFile = new File("applicationinsights.json"); + File defaultFile = new File("config/applicationinsights.json"); + if (!defaultFile.exists()) { + defaultFile = new File("applicationinsights.json"); + } if (!defaultFile.exists()) { return null; } From 765ccd10f457c2b224c8825a57b0c530c0ed243f Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Thu, 19 Dec 2024 16:00:48 -0800 Subject: [PATCH 3/4] up --- .../attach/ApplicationInsights.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) 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 bd9cee0b50a..48a227c070d 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 @@ -4,6 +4,7 @@ package com.microsoft.applicationinsights.attach; import io.opentelemetry.contrib.attach.core.CoreRuntimeAttach; +import javax.annotation.Nullable; import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -81,23 +82,23 @@ private static Optional findJsonConfigFromClasspath() { return Optional.empty(); } - String json = findJson(configContentAsInputStream); + String json = read(configContentAsInputStream); return Optional.of(json); } private static Optional findJsonConfigFromFileSystem() { - InputStream configContentAsInputStream = findDefaultFileInCurrentDirAsStream(); + InputStream configContentAsInputStream = findJsonConfigFromFileSystemAsStream(); if (configContentAsInputStream == null) { return Optional.empty(); } - String json = findJson(configContentAsInputStream); + 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)) { @@ -108,6 +109,7 @@ private static String findJson(InputStream configContentAsInputStream) { } } + @Nullable private static InputStream findResourceAsStream(String fileName) { InputStream configContentAsInputStream = ApplicationInsights.class.getResourceAsStream("/" + fileName); @@ -117,7 +119,8 @@ private static InputStream findResourceAsStream(String fileName) { return configContentAsInputStream; } - private static InputStream findDefaultFileInCurrentDirAsStream() { + @Nullable + private static InputStream findJsonConfigFromFileSystemAsStream() { File defaultFile = new File("config/applicationinsights.json"); if (!defaultFile.exists()) { defaultFile = new File("applicationinsights.json"); From b2ecd8e74cf190d43cfac4ad8056337b86ce359c Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 20 Dec 2024 12:04:04 -0800 Subject: [PATCH 4/4] spotless --- .../applicationinsights/attach/ApplicationInsights.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 48a227c070d..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 @@ -4,7 +4,6 @@ package com.microsoft.applicationinsights.attach; import io.opentelemetry.contrib.attach.core.CoreRuntimeAttach; -import javax.annotation.Nullable; import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -17,6 +16,7 @@ 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 {