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

Sdk name improvements #1551

Merged
merged 1 commit 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 @@ -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;

Expand All @@ -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"))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 for the dedicated plan, azure functions is using app service's prefix. this will fix it :)

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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private StatusFile() {

// visible for testing
static boolean shouldWrite() {
return enabled && DiagnosticsHelper.isAppServiceCodeless();
return enabled && DiagnosticsHelper.isAppSvcAttachForLoggingPurposes();
}

public static <T> void putValueAndWrite(String key, T value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down