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

Always write to log on startup failure #1550

Merged
merged 2 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

file.path is currently default to "applicationinsights.log". for azure function, it doesn't have the write permission in the agent jar folder. can we make azure function or other attaches to default to "/var/log/applicationinsights.log"?

Copy link
Contributor

Choose a reason for hiding this comment

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

of course, we can always ask them to provide a value for this env var or declare it in the json config.
this env var will have a higher precedence over the json config, right? similar to other env vars' precedence.

Copy link
Member Author

Choose a reason for hiding this comment

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

this shouldn't happen for users, since they can't control the json file, and so can't mess it up accidentally and cause it to fail to start

also will still log to /tmp/applicationinsights.log now which is better than before (just not the more ideal /var/log/applicationinsights.log, trying to balance the amount of special cases we add to the code for different compute environments)


// 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 @@ -223,6 +224,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 @@ -255,7 +257,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");
Copy link
Contributor

Choose a reason for hiding this comment

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

before overriding, can we assertEquals the value from the applicationinsights.json?

Copy link
Member Author

Choose a reason for hiding this comment

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

the shouldParse test above does this


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