Skip to content

Commit

Permalink
Ability to set the configuration file from the classpath of runtime a…
Browse files Browse the repository at this point in the history
…ttachment (#2581)

Fix #2576

Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
  • Loading branch information
jeanbisutti and trask committed Oct 12, 2022
1 parent c385f5f commit f19e3f5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
/** This class allows you to attach the Application Insights agent for Java at runtime. */
public final class ApplicationInsights {

/**
* This property allows configuring an Application Insights json file. It can be helpful to get an
* Application Insights json file by Spring profile.
*/
public static final String APPLICATIONINSIGHTS_RUNTIME_ATTACH_CONFIGURATION_FILE =
"applicationinsights.runtime-attach.configuration.classpath.file";

private static final Logger logger = Logger.getLogger(ApplicationInsights.class.getName());

private static final String RUNTIME_ATTACHED_ENABLED_PROPERTY =
Expand All @@ -30,6 +37,9 @@ private ApplicationInsights() {}
/**
* Attach the Application Insights agent for Java to the current JVM. The attachment must be
* requested at the beginning of the main method.
*
* @throws ConfigurationException If the file given by the
* applicationinsights.runtime-attach.configuration.classpath.file property was not found
*/
public static void attach() {

Expand All @@ -53,22 +63,55 @@ public static void attach() {

private static Optional<String> findJsonConfig() {

InputStream configContentAsInputStream =
ApplicationInsights.class.getResourceAsStream("/applicationinsights.json");
String fileName = findJsonConfigFile();

InputStream configContentAsInputStream = findResourceAsStream(fileName);

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);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
String json = bufferedReader.lines().collect(Collectors.joining(""));
return Optional.of(json);
return bufferedReader.lines().collect(Collectors.joining(""));
} catch (IOException e) {
throw new IllegalStateException(
"Unexpected issue during loading of JSON configuration file: " + e.getMessage());
}
}

private static InputStream findResourceAsStream(String fileName) {
InputStream configContentAsInputStream =
ApplicationInsights.class.getResourceAsStream("/" + fileName);
if (configContentAsInputStream == null && isJsonFileConfiguredWithProperty()) {
throw new ConfigurationException(fileName + " not found on the class path");
}
return configContentAsInputStream;
}

public static class ConfigurationException extends IllegalArgumentException {
ConfigurationException(String message) {
super(message);
}
}

private static String findJsonConfigFile() {
if (isJsonFileConfiguredWithProperty()) {
return System.getProperty(APPLICATIONINSIGHTS_RUNTIME_ATTACH_CONFIGURATION_FILE);
}
return "applicationinsights.json";
}

private static boolean isJsonFileConfiguredWithProperty() {
return System.getProperty(APPLICATIONINSIGHTS_RUNTIME_ATTACH_CONFIGURATION_FILE) != null;
}

private static boolean agentIsAttached() {
try {
Class.forName("io.opentelemetry.javaagent.OpenTelemetryAgent", false, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package com.microsoft.applicationinsights.smoketestapp;

import com.microsoft.applicationinsights.attach.ApplicationInsights;
import java.io.IOException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
Expand All @@ -13,7 +12,7 @@
@SpringBootApplication
public class SpringBootApp extends SpringBootServletInitializer {

public static void main(String[] args) throws IOException {
public static void main(String[] args) {
ApplicationInsights.attach();
SpringApplication.run(SpringBootApp.class, args);
}
Expand Down

0 comments on commit f19e3f5

Please sign in to comment.