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,9 @@
/** This class allows you to attach the Application Insights agent for Java at runtime. */
public final class ApplicationInsights {

public static final String APPLICATIONINSIGHTS_RUNTIMEATTACH_CONFIGURATION_FILE =
jeanbisutti marked this conversation as resolved.
Show resolved Hide resolved
"applicationinsights.runtimeattach.configuration.file";
jeanbisutti marked this conversation as resolved.
Show resolved Hide resolved

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

private static final String RUNTIME_ATTACHED_ENABLED_PROPERTY =
Expand Down Expand Up @@ -53,8 +56,9 @@ public static void attach() {

private static Optional<String> findJsonConfig() {

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

Choose a reason for hiding this comment

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

can we update smoke test for runtime attach to test this?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could, but we would have to add the following kind of code to the current test:

  public static void main(String[] args) {
    System.setProperty("applicationinsights.runtime-attach.configuration.file", "applicationinsights-test.json");
    ApplicationInsights.attach();
    SpringApplication.run(SpringBootApp.class, args);
  }

The users would instead add a system property with -Dapplicationinsights.runtime-attach.configuration.file=applicationinsights-test.json. So, the test would not represent the typical way of configuring the file name. And so, the test would not document the standard way of configuring the file name.

Another option could be to rewrite a test to be able to add -Dapplicationinsights.runtime-attach.configuration.file=applicationinsights-test.json. However, this would require a significant time for an added code pretty simple.

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe add a todo for now? it's not required for this PR.

Copy link
Member

Choose a reason for hiding this comment

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

is there some other form of test coverage that may be simpler (but still useful)?

Copy link
Member Author

Choose a reason for hiding this comment

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

An approach could be to change the visibility of some methods to be able to test them. However, I don't recommend doing this. The major drawback of this approach is that we are testing implementation details, not a user use case. So, since the test is tied to implementation details, refactoring (changing the code without changing the behavior) could break it (because we remove a method, for example), adding a cost to test maintenance and slowing down and discouraging future refactoring.

Copy link
Member

Choose a reason for hiding this comment

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

I created a work item for the smoke test and we can discuss if/how to implement it in the future

if (configContentAsInputStream == null) {
return Optional.empty();
}
Expand All @@ -69,6 +73,15 @@ private static Optional<String> findJsonConfig() {
}
}

private static String findJsonConfigFile() {
String fileFromProperty =
System.getProperty(APPLICATIONINSIGHTS_RUNTIMEATTACH_CONFIGURATION_FILE);
trask marked this conversation as resolved.
Show resolved Hide resolved
if (fileFromProperty != null) {
trask marked this conversation as resolved.
Show resolved Hide resolved
return fileFromProperty;
}
return "applicationinsights.json";
}

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