Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()) {
Copy link
Member

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/resources and 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.

jsonConfig = findJsonConfigFromFileSystem();
}
if (jsonConfig.isPresent()) {
System.setProperty(RUNTIME_ATTACHED_JSON_PROPERTY, jsonConfig.get());
}
Expand All @@ -66,7 +72,7 @@ public static void attach() {
}
}

private static Optional<String> findJsonConfig() {
private static Optional<String> findJsonConfigFromClasspath() {

String fileName = findJsonConfigFile();

Expand All @@ -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)) {
Expand All @@ -91,6 +109,7 @@ private static String findJson(InputStream configContentAsInputStream) {
}
}

@Nullable
private static InputStream findResourceAsStream(String fileName) {
InputStream configContentAsInputStream =
ApplicationInsights.class.getResourceAsStream("/" + fileName);
Expand All @@ -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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Unexpected issue during loading of JSON configuration file: " + e.getMessage());
}
}

public static class ConfigurationException extends IllegalArgumentException {
ConfigurationException(String message) {
super(message);
Expand Down
Loading