Skip to content

Commit c6758de

Browse files
authored
STDOUT All Functions Diagnostics Logs (#2108)
* Enable etw and status file on function * Fix spotless * Add debugging logs * Fix npe * Use sout * Update version number * Fix spotless * Fix spotless * Fix spotless * Remove it * Update codeowners * Set diagnostics logger to debug level * Add console appender to diagnostic logger * Revert previous change * Cleanup stdout * More cleanup * Use console on linux consumption plan only * Use csv format as the linux diagnostic logging * Fix spotlessApply * Suppress stdout * Add prefix * Update prefix and add newline * Clean up logs * Remove suppressWarning for stdout * Fix sdkVersion with prefix * Don't write functions logs to json file * Refactor * Fix errorProne * Address feedback * Add a todo
1 parent 51fe9c7 commit c6758de

File tree

7 files changed

+126
-33
lines changed

7 files changed

+126
-33
lines changed

agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/DiagnosticsHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private DiagnosticsHelper() {}
5353

5454
public static void setAgentJarFile(Path agentPath) {
5555
if (Files.exists(agentPath.resolveSibling("appsvc.codeless"))) {
56+
// TODO we can remove this check after the new functions model is deployed.
5657
if ("java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
5758
rpIntegrationChar = 'f';
5859
} else {

agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/etw/EtwAppender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public EtwAppender() {
5454
@Override
5555
public void start() {
5656
IpaVerbose event = new IpaVerbose(proto);
57-
event.setMessageFormat("EtwProvider initialized sucessfully.");
57+
event.setMessageFormat("EtwProvider initialized successfully.");
5858
try {
5959
this.etwProvider.writeEvent(event);
6060
} catch (LinkageError | ApplicationInsightsEtwException e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* ApplicationInsights-Java
3+
* Copyright (c) Microsoft Corporation
4+
* All rights reserved.
5+
*
6+
* MIT License
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
8+
* software and associated documentation files (the ""Software""), to deal in the Software
9+
* without restriction, including without limitation the rights to use, copy, modify, merge,
10+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
11+
* persons to whom the Software is furnished to do so, subject to the following conditions:
12+
* The above copyright notice and this permission notice shall be included in all copies or
13+
* substantial portions of the Software.
14+
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16+
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17+
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
* DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
package com.microsoft.applicationinsights.agent.bootstrap.diagnostics.log;
23+
24+
import ch.qos.logback.classic.PatternLayout;
25+
import ch.qos.logback.classic.spi.ILoggingEvent;
26+
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.ApplicationMetadataFactory;
27+
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.DiagnosticsHelper;
28+
29+
public class ApplicationInsightsCsvLayout extends PatternLayout {
30+
31+
private static final String PREFIX = "LanguageWorkerConsoleLogMS_APPLICATION_INSIGHTS_LOGS";
32+
33+
private static final ApplicationMetadataFactory applicationMetadataFactory =
34+
DiagnosticsHelper.getMetadataFactory();
35+
private final String qualifiedSdkVersion;
36+
37+
public ApplicationInsightsCsvLayout(String qualifiedSdkVersion) {
38+
this.qualifiedSdkVersion = qualifiedSdkVersion;
39+
}
40+
41+
@Override
42+
public String doLayout(ILoggingEvent event) {
43+
StringBuilder stringBuilder = new StringBuilder();
44+
stringBuilder.append(PREFIX);
45+
stringBuilder.append(" ");
46+
stringBuilder.append(event.getTimeStamp());
47+
stringBuilder.append(",");
48+
stringBuilder.append(event.getLevel().toString());
49+
stringBuilder.append(",");
50+
stringBuilder.append(event.getLoggerName());
51+
stringBuilder.append(",");
52+
stringBuilder.append("\"");
53+
stringBuilder.append(event.getFormattedMessage());
54+
stringBuilder.append("\"");
55+
stringBuilder.append(",");
56+
stringBuilder.append(applicationMetadataFactory.getSiteName().getValue());
57+
stringBuilder.append(",");
58+
stringBuilder.append(applicationMetadataFactory.getInstrumentationKey().getValue());
59+
stringBuilder.append(",");
60+
stringBuilder.append(qualifiedSdkVersion);
61+
stringBuilder.append(",");
62+
stringBuilder.append("java");
63+
stringBuilder.append(System.getProperty("line.separator"));
64+
65+
return stringBuilder.toString();
66+
}
67+
}

agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/status/StatusFile.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ private static boolean shouldWrite() {
154154
if (writable) {
155155
return true;
156156
}
157+
157158
// read-only app services, want to log warning once in this case
158159
if (startupLogger != null && !alreadyLogged.getAndSet(true)) {
159160
startupLogger.info(

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/AiComponentInstaller.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424
import static java.util.concurrent.TimeUnit.SECONDS;
2525

2626
import com.microsoft.applicationinsights.agent.bootstrap.BytecodeUtil;
27-
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.DiagnosticsHelper;
2827
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.SdkVersionFinder;
2928
import com.microsoft.applicationinsights.agent.internal.common.FriendlyException;
3029
import com.microsoft.applicationinsights.agent.internal.common.LocalFileSystemUtils;
31-
import com.microsoft.applicationinsights.agent.internal.common.PropertyHelper;
3230
import com.microsoft.applicationinsights.agent.internal.common.Strings;
3331
import com.microsoft.applicationinsights.agent.internal.common.SystemInformation;
3432
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration;
@@ -65,7 +63,6 @@
6563
import java.util.List;
6664
import java.util.concurrent.CountDownLatch;
6765
import java.util.stream.Collectors;
68-
import org.checkerframework.checker.nullness.qual.Nullable;
6966
import org.slf4j.Logger;
7067
import org.slf4j.LoggerFactory;
7168

@@ -96,12 +93,6 @@ static AppIdSupplier beforeAgent(Instrumentation instrumentation) {
9693
}
9794

9895
private static AppIdSupplier start(Instrumentation instrumentation) {
99-
100-
String codelessSdkNamePrefix = getCodelessSdkNamePrefix();
101-
if (codelessSdkNamePrefix != null) {
102-
PropertyHelper.setSdkNamePrefix(codelessSdkNamePrefix);
103-
}
104-
10596
File javaTmpDir = new File(System.getProperty("java.io.tmpdir"));
10697
boolean readOnlyFileSystem = false;
10798
if (javaTmpDir.canRead() && !javaTmpDir.canWrite()) {
@@ -274,25 +265,6 @@ private static ServiceProfilerServiceConfig formServiceProfilerConfig(
274265
tempDirectory);
275266
}
276267

277-
@Nullable
278-
private static String getCodelessSdkNamePrefix() {
279-
if (!DiagnosticsHelper.isRpIntegration()) {
280-
return null;
281-
}
282-
StringBuilder sdkNamePrefix = new StringBuilder(4);
283-
sdkNamePrefix.append(DiagnosticsHelper.rpIntegrationChar());
284-
if (SystemInformation.isWindows()) {
285-
sdkNamePrefix.append("w");
286-
} else if (SystemInformation.isUnix()) {
287-
sdkNamePrefix.append("l");
288-
} else {
289-
startupLogger.warn("could not detect os: {}", System.getProperty("os.name"));
290-
sdkNamePrefix.append("u");
291-
}
292-
sdkNamePrefix.append("r_"); // "r" is for "recommended"
293-
return sdkNamePrefix.toString();
294-
}
295-
296268
private static boolean hasConnectionStringOrInstrumentationKey(Configuration config) {
297269
return !Strings.isNullOrEmpty(config.connectionString);
298270
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/LoggingConfigurator.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@
3838
import ch.qos.logback.core.util.FileSize;
3939
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.DiagnosticsHelper;
4040
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.etw.EtwAppender;
41+
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.log.ApplicationInsightsCsvLayout;
4142
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.log.ApplicationInsightsDiagnosticsLogFilter;
4243
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.log.ApplicationInsightsJsonLayout;
4344
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.log.MoshiJsonFormatter;
45+
import com.microsoft.applicationinsights.agent.internal.common.PropertyHelper;
4446
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration;
4547
import com.microsoft.applicationinsights.agent.internal.logbackpatch.FixedWindowRollingPolicy;
4648
import java.nio.file.Path;
@@ -73,7 +75,9 @@ void configure() {
7375
loggerContext.getLogger(ROOT_LOGGER_NAME).detachAndStopAllAppenders();
7476

7577
if (DiagnosticsHelper.useAppSvcRpIntegrationLogging()) {
76-
configureAppSvcs();
78+
configureAppSvc();
79+
} else if (DiagnosticsHelper.useFunctionsRpIntegrationLogging()) {
80+
configureFunctions();
7781
} else if (destination == null || destination.equalsIgnoreCase("file+console")) {
7882
configureFileAndConsole();
7983
} else if (destination.equalsIgnoreCase("file")) {
@@ -85,7 +89,7 @@ void configure() {
8589
}
8690
}
8791

88-
private void configureAppSvcs() {
92+
private void configureAppSvc() {
8993
Logger rootLogger = loggerContext.getLogger(ROOT_LOGGER_NAME);
9094
rootLogger.addAppender(configureFileAppender());
9195
rootLogger.addAppender(configureConsoleAppender());
@@ -124,11 +128,26 @@ private void configureAppSvcs() {
124128
loggingLevelConfigurator.initLoggerLevels(loggerContext);
125129
}
126130

131+
private void configureFunctions() {
132+
Logger rootLogger = loggerContext.getLogger(ROOT_LOGGER_NAME);
133+
rootLogger.addAppender(configureConsoleAppender());
134+
Logger diagnosticLogger = loggerContext.getLogger(DiagnosticsHelper.DIAGNOSTICS_LOGGER_NAME);
135+
diagnosticLogger.setLevel(Level.INFO);
136+
diagnosticLogger.setAdditive(false);
137+
Appender<ILoggingEvent> appender = configureConsoleAppender();
138+
diagnosticLogger.addAppender(appender);
139+
140+
// errors reported by other loggers should also go to diagnostic log
141+
// (level filter for these is applied in ApplicationInsightsDiagnosticsLogFilter)
142+
rootLogger.addAppender(appender);
143+
144+
loggingLevelConfigurator.initLoggerLevels(loggerContext);
145+
}
146+
127147
private void configureFileAndConsole() {
128148
Logger rootLogger = loggerContext.getLogger(ROOT_LOGGER_NAME);
129149
rootLogger.addAppender(configureFileAppender());
130150
rootLogger.addAppender(configureConsoleAppender());
131-
132151
loggingLevelConfigurator.initLoggerLevels(loggerContext);
133152
// these messages are specifically designed for attach
134153
loggerContext.getLogger("applicationinsights.extension.diagnostics").setLevel(Level.OFF);
@@ -200,7 +219,13 @@ private Appender<ILoggingEvent> configureConsoleAppender() {
200219
appender.setContext(loggerContext);
201220
appender.setName("CONSOLE");
202221

203-
appender.setEncoder(createEncoder());
222+
// format Functions diagnostic log as comma separated
223+
if (DiagnosticsHelper.useFunctionsRpIntegrationLogging()) {
224+
appender.setLayout(
225+
new ApplicationInsightsCsvLayout(PropertyHelper.getQualifiedSdkVersionString()));
226+
} else {
227+
appender.setEncoder(createEncoder());
228+
}
204229
appender.start();
205230

206231
return appender;

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/MainEntryPoint.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.SdkVersionFinder;
2727
import com.microsoft.applicationinsights.agent.bootstrap.diagnostics.status.StatusFile;
2828
import com.microsoft.applicationinsights.agent.internal.common.FriendlyException;
29+
import com.microsoft.applicationinsights.agent.internal.common.PropertyHelper;
30+
import com.microsoft.applicationinsights.agent.internal.common.SystemInformation;
2931
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration;
3032
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration.SelfDiagnostics;
3133
import com.microsoft.applicationinsights.agent.internal.configuration.ConfigurationBuilder;
@@ -88,6 +90,11 @@ public static void start(Instrumentation instrumentation, File javaagentFile) {
8890
// configuration is only read this early in order to extract logging configuration
8991
rpConfiguration = RpConfigurationBuilder.create(agentPath);
9092
configuration = ConfigurationBuilder.create(agentPath, rpConfiguration);
93+
94+
String codelessSdkNamePrefix = getCodelessSdkNamePrefix();
95+
if (codelessSdkNamePrefix != null) {
96+
PropertyHelper.setSdkNamePrefix(codelessSdkNamePrefix);
97+
}
9198
startupLogger = configureLogging(configuration.selfDiagnostics, agentPath);
9299
StatusFile.startupLogger = startupLogger;
93100
ConfigurationBuilder.logConfigurationWarnMessages();
@@ -214,4 +221,24 @@ private static Logger configureLogging(SelfDiagnostics selfDiagnostics, Path age
214221
new LoggingConfigurator(selfDiagnostics, agentPath).configure();
215222
return LoggerFactory.getLogger("com.microsoft.applicationinsights.agent");
216223
}
224+
225+
@Nullable
226+
private static String getCodelessSdkNamePrefix() {
227+
if (!DiagnosticsHelper.isRpIntegration()) {
228+
return null;
229+
}
230+
StringBuilder sdkNamePrefix = new StringBuilder(4);
231+
sdkNamePrefix.append(DiagnosticsHelper.rpIntegrationChar());
232+
if (SystemInformation.isWindows()) {
233+
sdkNamePrefix.append("w");
234+
} else if (SystemInformation.isUnix()) {
235+
sdkNamePrefix.append("l");
236+
} else {
237+
LoggerFactory.getLogger("com.microsoft.applicationinsights.agent")
238+
.warn("could not detect os: {}", System.getProperty("os.name"));
239+
sdkNamePrefix.append("u");
240+
}
241+
sdkNamePrefix.append("r_"); // "r" is for "recommended"
242+
return sdkNamePrefix.toString();
243+
}
217244
}

0 commit comments

Comments
 (0)