Skip to content

Commit

Permalink
Always write to log on startup failure (#1550)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Mar 12, 2021
1 parent c85fa18 commit 93657fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
package com.microsoft.applicationinsights.agent.internal.wasbootstrap;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.instrument.Instrumentation;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Locale;

Expand Down Expand Up @@ -134,19 +138,34 @@ private static void logErrorMessage(Logger startupLogger, String message, boolea
try {
// IF the startupLogger failed to be initialized due to configuration syntax error, try initializing it here
Path agentPath = new File(bootstrapURL.toURI()).toPath();
startupLogger = configureLogging(new SelfDiagnostics(), agentPath);
SelfDiagnostics selfDiagnostics = new SelfDiagnostics();
selfDiagnostics.file.path = ConfigurationBuilder.overlayWithEnvVar(
ConfigurationBuilder.APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_FILE_PATH, selfDiagnostics.file.path);
startupLogger = configureLogging(selfDiagnostics, agentPath);
if (isFriendlyException) {
startupLogger.error(message);
} else {
startupLogger.error(message, t);
}
} catch (Throwable e) {
// If the startupLogger still have some issues being initialized, just print the error stack trace
} catch (Throwable ignored) {
// this is a last resort in cases where the JVM doesn't have write permission to the directory where the agent lives
// and the user has not specified APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_FILE_PATH

// If the startupLogger still have some issues being initialized, print the error stack trace to stderr
if (isFriendlyException) {
System.err.println(message);
} else {
t.printStackTrace();
}
// and write to a temp file because some environments do not have (easy) access to stderr
String tmpDir = System.getProperty("java.io.tmpdir");
File file = new File(tmpDir, "applicationinsights.log");
try {
Writer out = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
out.write(message);
out.close();
} catch (Throwable ignored2) {
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class ConfigurationBuilder {
private static final String APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL = "APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL";

private static final String APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_LEVEL = "APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_LEVEL";
public static final String APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_FILE_PATH = "APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_FILE_PATH";

private static final String APPLICATIONINSIGHTS_PREVIEW_OTEL_API_SUPPORT = "APPLICATIONINSIGHTS_PREVIEW_OTEL_API_SUPPORT";

Expand Down Expand Up @@ -218,6 +219,7 @@ public static void overlayEnvVars(Configuration config) throws IOException {
config.sampling.percentage = overlayWithEnvVar(APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE, config.sampling.percentage);

config.selfDiagnostics.level = overlayWithEnvVar(APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_LEVEL, config.selfDiagnostics.level);
config.selfDiagnostics.file.path = overlayWithEnvVar(APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_FILE_PATH, config.selfDiagnostics.file.path);

config.preview.openTelemetryApiSupport = overlayWithEnvVar(APPLICATIONINSIGHTS_PREVIEW_OTEL_API_SUPPORT, config.preview.openTelemetryApiSupport);

Expand Down Expand Up @@ -250,7 +252,7 @@ private static String getWebsiteSiteNameEnvVar() {
return value;
}

static String overlayWithEnvVar(String name, String defaultValue) {
public static String overlayWithEnvVar(String name, String defaultValue) {
String value = getEnvVar(name);
return value != null ? value : defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ public void shouldOverrideSelfDiagnosticsLevel() throws IOException {
assertEquals("DEBUG", configuration.selfDiagnostics.level);
}

@Test
public void shouldOverrideSelfDiagnosticsFilePath() throws IOException {
envVars.set("APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_FILE_PATH", "/tmp/ai.log");

Configuration configuration = loadConfiguration();
ConfigurationBuilder.overlayEnvVars(configuration);

assertEquals("/tmp/ai.log", configuration.selfDiagnostics.file.path);
}

@Test
public void shouldOverridePreviewOtelApiSupport() throws IOException {
envVars.set("APPLICATIONINSIGHTS_PREVIEW_OTEL_API_SUPPORT", "true");
Expand Down

0 comments on commit 93657fd

Please sign in to comment.