Skip to content
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

Ability to set the configuration file from the classpath of runtime attachment #2581

Merged
merged 10 commits into from
Oct 12, 2022
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