55
66import io .opentelemetry .contrib .attach .core .CoreRuntimeAttach ;
77import java .io .BufferedReader ;
8+ import java .io .File ;
89import java .io .IOException ;
910import java .io .InputStream ;
1011import java .io .InputStreamReader ;
1112import java .nio .charset .StandardCharsets ;
13+ import java .nio .file .Files ;
1214import java .util .Optional ;
1315import java .util .Properties ;
1416import 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