From 235cdd7510e49468c1ab8de1c370ba472ee85562 Mon Sep 17 00:00:00 2001 From: Nori Zhang Date: Tue, 5 Nov 2024 17:19:43 +0800 Subject: [PATCH 1/3] telemetry --- .../agents/Microsoft.Azure.Agent/Telemetry.cs | 106 ++++++++++++------ 1 file changed, 69 insertions(+), 37 deletions(-) diff --git a/shell/agents/Microsoft.Azure.Agent/Telemetry.cs b/shell/agents/Microsoft.Azure.Agent/Telemetry.cs index 22466a9f..c41fe986 100644 --- a/shell/agents/Microsoft.Azure.Agent/Telemetry.cs +++ b/shell/agents/Microsoft.Azure.Agent/Telemetry.cs @@ -1,3 +1,4 @@ +using System.Runtime.InteropServices; using System.Text.Json; using Microsoft.ApplicationInsights; @@ -13,45 +14,15 @@ public class AzTrace /// internal static string InstallationId { get; private set; } + /// + /// OS platform the application is running on. + /// + internal static string Platform { get; private set; } + internal static void Initialize() { - InstallationId = null; - - string azCLIProfilePath, azPSHProfilePath; - string azureConfigDir = Environment.GetEnvironmentVariable("AZURE_CONFIG_DIR"); - string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - - if (string.IsNullOrEmpty(azureConfigDir)) - { - azCLIProfilePath = Path.Combine(userProfile, ".azure", "azureProfile.json"); - azPSHProfilePath = Path.Combine(userProfile, ".Azure", "AzureRmContextSettings.json"); - } - else - { - azCLIProfilePath = Path.Combine(azureConfigDir, "azureProfile.json"); - azPSHProfilePath = Path.Combine(azureConfigDir, "AzureRmContextSettings.json"); - } - - try - { - if (File.Exists(azCLIProfilePath)) - { - using var stream = File.OpenRead(azCLIProfilePath); - var jsonElement = JsonSerializer.Deserialize(stream); - InstallationId = jsonElement.GetProperty("installationId").GetString(); - } - else if (File.Exists(azPSHProfilePath)) - { - using var stream = File.OpenRead(azPSHProfilePath); - var jsonElement = JsonSerializer.Deserialize(stream); - InstallationId = jsonElement.GetProperty("Settings").GetProperty(nameof(InstallationId)).GetString(); - } - } - catch - { - // Something wrong when reading the config file. - InstallationId = null; - } + InstallationId = GetInstallationId(); + Platform = GetOSPlatform(); } /// @@ -157,6 +128,67 @@ internal static AzTrace Exception(object details) // Don't create an object when telemetry is disabled. return null; } + + private static string GetInstallationId() + { + string azCLIProfilePath, azPSHProfilePath; + string azureConfigDir = Environment.GetEnvironmentVariable("AZURE_CONFIG_DIR"); + string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + + if (string.IsNullOrEmpty(azureConfigDir)) + { + azCLIProfilePath = Path.Combine(userProfile, ".azure", "azureProfile.json"); + azPSHProfilePath = Path.Combine(userProfile, ".Azure", "AzureRmContextSettings.json"); + } + else + { + azCLIProfilePath = Path.Combine(azureConfigDir, "azureProfile.json"); + azPSHProfilePath = Path.Combine(azureConfigDir, "AzureRmContextSettings.json"); + } + + try + { + if (File.Exists(azCLIProfilePath)) + { + using var stream = File.OpenRead(azCLIProfilePath); + var jsonElement = JsonSerializer.Deserialize(stream); + return jsonElement.GetProperty("installationId").GetString(); + } + else if (File.Exists(azPSHProfilePath)) + { + using var stream = File.OpenRead(azPSHProfilePath); + var jsonElement = JsonSerializer.Deserialize(stream); + return jsonElement.GetProperty("Settings").GetProperty(nameof(InstallationId)).GetString(); + } + } + catch + { + // Something wrong when reading the config file. + return null; + } + + return null; + } + + private static string GetOSPlatform() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return "Windows"; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return "Linux"; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + return "macOS"; + } + else + { + return "Unknown"; + } + } } internal class Telemetry From 06e8b0a3ac48177f2de6da43abddd7980d73c535 Mon Sep 17 00:00:00 2001 From: Nori Zhang Date: Wed, 6 Nov 2024 14:35:51 +0800 Subject: [PATCH 2/3] add os platform --- shell/agents/Microsoft.Azure.Agent/Telemetry.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/agents/Microsoft.Azure.Agent/Telemetry.cs b/shell/agents/Microsoft.Azure.Agent/Telemetry.cs index c41fe986..fe7f22a7 100644 --- a/shell/agents/Microsoft.Azure.Agent/Telemetry.cs +++ b/shell/agents/Microsoft.Azure.Agent/Telemetry.cs @@ -231,6 +231,7 @@ private void LogTelemetry(AzTrace trace, Exception exception) ["EventType"] = trace.EventType, ["ShellCommand"] = trace.ShellCommand, ["Details"] = GetDetailedMessage(trace.Details), + ["OSPlatform"] = AzTrace.Platform }; if (exception is null) From fdeee92b9e22f54127c3c66e4398e222ff0b478d Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Wed, 6 Nov 2024 12:29:02 -0800 Subject: [PATCH 3/3] Minor update and remove dependency on 'RuntimeInformation' --- .../agents/Microsoft.Azure.Agent/Telemetry.cs | 128 +++++++++--------- 1 file changed, 63 insertions(+), 65 deletions(-) diff --git a/shell/agents/Microsoft.Azure.Agent/Telemetry.cs b/shell/agents/Microsoft.Azure.Agent/Telemetry.cs index fe7f22a7..d68111c2 100644 --- a/shell/agents/Microsoft.Azure.Agent/Telemetry.cs +++ b/shell/agents/Microsoft.Azure.Agent/Telemetry.cs @@ -1,4 +1,3 @@ -using System.Runtime.InteropServices; using System.Text.Json; using Microsoft.ApplicationInsights; @@ -17,12 +16,72 @@ public class AzTrace /// /// OS platform the application is running on. /// - internal static string Platform { get; private set; } + internal static string OSPlatform { get; private set; } internal static void Initialize() { InstallationId = GetInstallationId(); - Platform = GetOSPlatform(); + OSPlatform = GetOSPlatform(); + } + + private static string GetInstallationId() + { + string azCLIProfilePath, azPSHProfilePath; + string azureConfigDir = Environment.GetEnvironmentVariable("AZURE_CONFIG_DIR"); + string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + + if (string.IsNullOrEmpty(azureConfigDir)) + { + azCLIProfilePath = Path.Combine(userProfile, ".azure", "azureProfile.json"); + azPSHProfilePath = Path.Combine(userProfile, ".Azure", "AzureRmContextSettings.json"); + } + else + { + azCLIProfilePath = Path.Combine(azureConfigDir, "azureProfile.json"); + azPSHProfilePath = Path.Combine(azureConfigDir, "AzureRmContextSettings.json"); + } + + try + { + if (File.Exists(azCLIProfilePath)) + { + using var stream = File.OpenRead(azCLIProfilePath); + var jsonElement = JsonSerializer.Deserialize(stream); + return jsonElement.GetProperty("installationId").GetString(); + } + else if (File.Exists(azPSHProfilePath)) + { + using var stream = File.OpenRead(azPSHProfilePath); + var jsonElement = JsonSerializer.Deserialize(stream); + return jsonElement.GetProperty("Settings").GetProperty(nameof(InstallationId)).GetString(); + } + } + catch + { + // Something wrong when reading the config file. + } + + return null; + } + + private static string GetOSPlatform() + { + if (OperatingSystem.IsWindows()) + { + return "Windows"; + } + + if (OperatingSystem.IsLinux()) + { + return "Linux"; + } + + if (OperatingSystem.IsMacOS()) + { + return "macOS"; + } + + return "Unknown"; } /// @@ -128,67 +187,6 @@ internal static AzTrace Exception(object details) // Don't create an object when telemetry is disabled. return null; } - - private static string GetInstallationId() - { - string azCLIProfilePath, azPSHProfilePath; - string azureConfigDir = Environment.GetEnvironmentVariable("AZURE_CONFIG_DIR"); - string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - - if (string.IsNullOrEmpty(azureConfigDir)) - { - azCLIProfilePath = Path.Combine(userProfile, ".azure", "azureProfile.json"); - azPSHProfilePath = Path.Combine(userProfile, ".Azure", "AzureRmContextSettings.json"); - } - else - { - azCLIProfilePath = Path.Combine(azureConfigDir, "azureProfile.json"); - azPSHProfilePath = Path.Combine(azureConfigDir, "AzureRmContextSettings.json"); - } - - try - { - if (File.Exists(azCLIProfilePath)) - { - using var stream = File.OpenRead(azCLIProfilePath); - var jsonElement = JsonSerializer.Deserialize(stream); - return jsonElement.GetProperty("installationId").GetString(); - } - else if (File.Exists(azPSHProfilePath)) - { - using var stream = File.OpenRead(azPSHProfilePath); - var jsonElement = JsonSerializer.Deserialize(stream); - return jsonElement.GetProperty("Settings").GetProperty(nameof(InstallationId)).GetString(); - } - } - catch - { - // Something wrong when reading the config file. - return null; - } - - return null; - } - - private static string GetOSPlatform() - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return "Windows"; - } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - return "Linux"; - } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - return "macOS"; - } - else - { - return "Unknown"; - } - } } internal class Telemetry @@ -231,7 +229,7 @@ private void LogTelemetry(AzTrace trace, Exception exception) ["EventType"] = trace.EventType, ["ShellCommand"] = trace.ShellCommand, ["Details"] = GetDetailedMessage(trace.Details), - ["OSPlatform"] = AzTrace.Platform + ["OSPlatform"] = AzTrace.OSPlatform }; if (exception is null)