-
Notifications
You must be signed in to change notification settings - Fork 208
Support finding applicationinsights.json in current dir when using runtime attach #3990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
deb4dbc
Support finding applicationinsights.json in current dir when using ru…
trask 68e07d5
and config dir
trask 765ccd1
up
trask b2ecd8e
spotless
trask 482b199
Merge remote-tracking branch 'origin/main' into runtime-attach-curr-dir
trask 2c56c2b
ConfigurationException
trask 996a541
check file system first
trask File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<String> jsonConfig = findJsonConfig(); | ||
| Optional<String> 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<String> findJsonConfig() { | ||
| private static Optional<String> findJsonConfigFromClasspath() { | ||
|
|
||
| String fileName = findJsonConfigFile(); | ||
|
|
||
|
|
@@ -76,11 +82,23 @@ private static Optional<String> findJsonConfig() { | |
| return Optional.empty(); | ||
| } | ||
|
|
||
| String json = findJson(configContentAsInputStream); | ||
| String json = read(configContentAsInputStream); | ||
| return Optional.of(json); | ||
| } | ||
|
|
||
| private static Optional<String> 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); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's assume the user adds a JSON configuration file in
src/main/resourcesand creates a Spring Boot executable JAR.The user may want to use a configuration different from the default one in the classpath without creating a new JAR file.
It may be useful to check the configuration from the file system first.