Skip to content

Commit deb4dbc

Browse files
committed
Support finding applicationinsights.json in current dir when using runtime attach
1 parent 241de9a commit deb4dbc

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

agent/runtime-attach/src/main/java/com/microsoft/applicationinsights/attach/ApplicationInsights.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
import io.opentelemetry.contrib.attach.core.CoreRuntimeAttach;
77
import java.io.BufferedReader;
8+
import java.io.File;
89
import java.io.IOException;
910
import java.io.InputStream;
1011
import java.io.InputStreamReader;
1112
import java.nio.charset.StandardCharsets;
13+
import java.nio.file.Files;
1214
import java.util.Optional;
1315
import java.util.Properties;
1416
import java.util.logging.Level;
@@ -52,7 +54,10 @@ public static void attach() {
5254
System.setProperty(RUNTIME_ATTACHED_ENABLED_PROPERTY, "true");
5355

5456
try {
55-
Optional<String> jsonConfig = findJsonConfig();
57+
Optional<String> jsonConfig = findJsonConfigFromClasspath();
58+
if (!jsonConfig.isPresent()) {
59+
jsonConfig = findJsonConfigFromDefaultFileInCurrentDir();
60+
}
5661
if (jsonConfig.isPresent()) {
5762
System.setProperty(RUNTIME_ATTACHED_JSON_PROPERTY, jsonConfig.get());
5863
}
@@ -66,7 +71,7 @@ public static void attach() {
6671
}
6772
}
6873

69-
private static Optional<String> findJsonConfig() {
74+
private static Optional<String> findJsonConfigFromClasspath() {
7075

7176
String fileName = findJsonConfigFile();
7277

@@ -80,6 +85,18 @@ private static Optional<String> findJsonConfig() {
8085
return Optional.of(json);
8186
}
8287

88+
private static Optional<String> findJsonConfigFromDefaultFileInCurrentDir() {
89+
90+
InputStream configContentAsInputStream = findDefaultFileInCurrentDirAsStream();
91+
92+
if (configContentAsInputStream == null) {
93+
return Optional.empty();
94+
}
95+
96+
String json = findJson(configContentAsInputStream);
97+
return Optional.of(json);
98+
}
99+
83100
private static String findJson(InputStream configContentAsInputStream) {
84101
try (InputStreamReader inputStreamReader =
85102
new InputStreamReader(configContentAsInputStream, StandardCharsets.UTF_8);
@@ -100,6 +117,19 @@ private static InputStream findResourceAsStream(String fileName) {
100117
return configContentAsInputStream;
101118
}
102119

120+
private static InputStream findDefaultFileInCurrentDirAsStream() {
121+
File defaultFile = new File("applicationinsights.json");
122+
if (!defaultFile.exists()) {
123+
return null;
124+
}
125+
try {
126+
return Files.newInputStream(defaultFile.toPath());
127+
} catch (IOException e) {
128+
throw new IllegalStateException(
129+
"Unexpected issue during loading of JSON configuration file: " + e.getMessage());
130+
}
131+
}
132+
103133
public static class ConfigurationException extends IllegalArgumentException {
104134
ConfigurationException(String message) {
105135
super(message);

0 commit comments

Comments
 (0)