From a145354e95fd3874f373c538b80d748685e20e85 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Thu, 11 Mar 2021 20:46:35 -0800 Subject: [PATCH] Sdk name improvements --- .../diagnostics/DiagnosticsHelper.java | 49 ++++++++----------- .../diagnostics/status/StatusFile.java | 2 +- .../diagnostics/DiagnosticsTestHelper.java | 6 +-- ...tionInsightsDiagnosticsLogFilterTests.java | 2 +- .../diagnostics/status/StatusFileTests.java | 10 ++-- .../agent/internal/AiComponentInstaller.java | 11 ++--- .../internal/wasbootstrap/MainEntryPoint.java | 2 +- .../configuration/ConfigurationBuilder.java | 5 -- 8 files changed, 34 insertions(+), 53 deletions(-) diff --git a/agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/DiagnosticsHelper.java b/agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/DiagnosticsHelper.java index f9245ac92c9..833fc74f717 100644 --- a/agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/DiagnosticsHelper.java +++ b/agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/DiagnosticsHelper.java @@ -25,11 +25,9 @@ private DiagnosticsHelper() { } public static final String IPA_ETW_PROVIDER_ENABLED_ENV_VAR = "APPLICATIONINSIGHTS_EXTENSION_ETW_PROVIDER_ENABLED"; // visible for testing - static volatile boolean appServiceCodeless; + static volatile boolean appSvcAttachForLoggingPurposes; - private static volatile boolean aksCodeless; - - private static volatile boolean functionsCodeless; + private static volatile char attachChar; private static final boolean isWindows; @@ -46,47 +44,40 @@ private DiagnosticsHelper() { } public static void setAgentJarFile(Path agentPath) { if (Files.exists(agentPath.resolveSibling("appsvc.codeless"))) { - appServiceCodeless = true; + appSvcAttachForLoggingPurposes = true; + if ("java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) { + attachChar = 'f'; + } else { + attachChar = 'a'; + } } else if (Files.exists(agentPath.resolveSibling("aks.codeless"))) { - aksCodeless = true; + attachChar = 'k'; } else if (Files.exists(agentPath.resolveSibling("functions.codeless"))) { - functionsCodeless = true; + attachChar = 'f'; + } else if (Files.exists(agentPath.resolveSibling("springcloud.codeless"))) { + attachChar = 's'; } } - public static boolean isAppServiceCodeless() { - return appServiceCodeless; - } - - public static boolean isAksCodeless() { - return aksCodeless; + public static boolean isAnyAttach() { + return attachChar != 0; } - public static boolean isFunctionsCodeless() { - return functionsCodeless; + // returns 0 if not attach + public static char attachChar() { + return attachChar; } - public static boolean isAnyCodelessAttach() { - return appServiceCodeless || aksCodeless || functionsCodeless; + // this also applies to Azure Functions running on App Services + public static boolean isAppSvcAttachForLoggingPurposes() { + return appSvcAttachForLoggingPurposes; } public static ApplicationMetadataFactory getMetadataFactory() { return METADATA_FACTORY; } - public static String getCodelessResourceType() { - if (appServiceCodeless) { - return "appsvc"; - } else if (aksCodeless) { - return "aks"; - } else if (functionsCodeless) { - return "functions"; - } - return null; - } - public static boolean isOsWindows() { return isWindows; } - } diff --git a/agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/status/StatusFile.java b/agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/status/StatusFile.java index a958263c7c6..44ff403f96e 100644 --- a/agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/status/StatusFile.java +++ b/agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/status/StatusFile.java @@ -125,7 +125,7 @@ private StatusFile() { // visible for testing static boolean shouldWrite() { - return enabled && DiagnosticsHelper.isAppServiceCodeless(); + return enabled && DiagnosticsHelper.isAppSvcAttachForLoggingPurposes(); } public static void putValueAndWrite(String key, T value) { diff --git a/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/DiagnosticsTestHelper.java b/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/DiagnosticsTestHelper.java index 8cf133ccaf3..5ad5c26a025 100644 --- a/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/DiagnosticsTestHelper.java +++ b/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/DiagnosticsTestHelper.java @@ -4,12 +4,12 @@ public class DiagnosticsTestHelper { private DiagnosticsTestHelper() { } - public static void setIsAppServiceCodeless(boolean appServiceCodeless) { - DiagnosticsHelper.appServiceCodeless = appServiceCodeless; + public static void setIsAppSvcAttachForLoggingPurposes(boolean appSvcAttachForLoggingPurposes) { + DiagnosticsHelper.appSvcAttachForLoggingPurposes = appSvcAttachForLoggingPurposes; } public static void reset() { - setIsAppServiceCodeless(false); + setIsAppSvcAttachForLoggingPurposes(false); } } diff --git a/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/log/ApplicationInsightsDiagnosticsLogFilterTests.java b/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/log/ApplicationInsightsDiagnosticsLogFilterTests.java index 2aaa7bbdd5a..bc6faa3d5a2 100644 --- a/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/log/ApplicationInsightsDiagnosticsLogFilterTests.java +++ b/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/log/ApplicationInsightsDiagnosticsLogFilterTests.java @@ -17,7 +17,7 @@ public class ApplicationInsightsDiagnosticsLogFilterTests { @Before public void setup() { - DiagnosticsTestHelper.setIsAppServiceCodeless(true); + DiagnosticsTestHelper.setIsAppSvcAttachForLoggingPurposes(true); filter = new ApplicationInsightsDiagnosticsLogFilter(); mockEvent = mock(ILoggingEvent.class); when(mockEvent.getLevel()).thenReturn(Level.ERROR); diff --git a/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/status/StatusFileTests.java b/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/status/StatusFileTests.java index 4e2369751ee..465ad4e3d89 100644 --- a/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/status/StatusFileTests.java +++ b/agent/agent-bootstrap/src/test/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/status/StatusFileTests.java @@ -129,13 +129,13 @@ public void doesNotWriteIfEnabledEnvVarIsFalse() throws Exception { @Test public void ifEnabledVarHasInvalidValueThenItIsEnabled() throws Exception { envVars.set(StatusFile.STATUS_FILE_ENABLED_ENV_VAR, "42"); - DiagnosticsTestHelper.setIsAppServiceCodeless(true); + DiagnosticsTestHelper.setIsAppSvcAttachForLoggingPurposes(true); runWriteFileTest(true); } @Test public void writesCorrectFile() throws Exception { - DiagnosticsTestHelper.setIsAppServiceCodeless(true); + DiagnosticsTestHelper.setIsAppSvcAttachForLoggingPurposes(true); runWriteFileTest(true); } @@ -171,7 +171,7 @@ Map parseJsonFile(File tempFolder) throws java.io.IOException { @Test public void doesNotWriteIfNotAppService() throws Exception { - DiagnosticsTestHelper.setIsAppServiceCodeless(false); // just to be sure + DiagnosticsTestHelper.setIsAppSvcAttachForLoggingPurposes(false); // just to be sure final File tempFolder = this.tempFolder.newFolder(); StatusFile.directory = tempFolder.getAbsolutePath(); @@ -189,7 +189,7 @@ public void doesNotWriteIfNotAppService() throws Exception { public void putValueAndWriteOverwritesCurrentFile() throws Exception { final String key = "write-test"; try { - DiagnosticsTestHelper.setIsAppServiceCodeless(true); + DiagnosticsTestHelper.setIsAppSvcAttachForLoggingPurposes(true); final File tempFolder = this.tempFolder.newFolder(); @@ -210,7 +210,7 @@ public void putValueAndWriteOverwritesCurrentFile() throws Exception { assertMapHasExpectedInformation(map, key, value); } finally { - DiagnosticsTestHelper.setIsAppServiceCodeless(false); + DiagnosticsTestHelper.setIsAppSvcAttachForLoggingPurposes(false); StatusFile.CONSTANT_VALUES.remove(key); } } diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/AiComponentInstaller.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/AiComponentInstaller.java index 5f35fbc2348..c8bb2f26f36 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/AiComponentInstaller.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/AiComponentInstaller.java @@ -196,16 +196,11 @@ private static void validateProcessorConfiguration(Configuration config) throws @Nullable private static String getCodelessSdkNamePrefix() { - StringBuilder sdkNamePrefix = new StringBuilder(4); - if (DiagnosticsHelper.isAppServiceCodeless()) { - sdkNamePrefix.append("a"); - } else if (DiagnosticsHelper.isAksCodeless()) { - sdkNamePrefix.append("k"); - } else if (DiagnosticsHelper.isFunctionsCodeless()) { - sdkNamePrefix.append("f"); - } else { + if (!DiagnosticsHelper.isAnyAttach()) { return null; } + StringBuilder sdkNamePrefix = new StringBuilder(4); + sdkNamePrefix.append(DiagnosticsHelper.attachChar()); if (SystemInformation.INSTANCE.isWindows()) { sdkNamePrefix.append("w"); } else if (SystemInformation.INSTANCE.isUnix()) { diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/wasbootstrap/MainEntryPoint.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/wasbootstrap/MainEntryPoint.java index 6a2126531be..0a2a09188c0 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/wasbootstrap/MainEntryPoint.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/wasbootstrap/MainEntryPoint.java @@ -154,7 +154,7 @@ private static void logErrorMessage(Logger startupLogger, String message, boolea private static Logger configureLogging(SelfDiagnostics selfDiagnostics, Path agentPath) { String logbackXml; String destination = selfDiagnostics.destination; - if (DiagnosticsHelper.isAppServiceCodeless()) { + if (DiagnosticsHelper.isAppSvcAttachForLoggingPurposes()) { // User-accessible IPA log file. Enabled by default. if ("false".equalsIgnoreCase(System.getenv(DiagnosticsHelper.IPA_LOG_FILE_ENABLED_ENV_VAR))) { System.setProperty("ai.config.appender.user-logdir.location", ""); diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/wasbootstrap/configuration/ConfigurationBuilder.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/wasbootstrap/configuration/ConfigurationBuilder.java index 6ac9787097f..f3ed41d3497 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/wasbootstrap/configuration/ConfigurationBuilder.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/wasbootstrap/configuration/ConfigurationBuilder.java @@ -160,11 +160,6 @@ private static Configuration loadConfigurationFile(Path agentJarPath) throws IOE return getConfigurationFromEnvVar(configurationContent, true); } - if (DiagnosticsHelper.isAnyCodelessAttach()) { - // codeless attach only supports configuration via environment variables (for now at least) - return new Configuration(); - } - String configPathStr = getConfigPath(); if (configPathStr != null) { Path configPath = agentJarPath.resolveSibling(configPathStr);