Skip to content
Merged
Changes from 2 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
107 changes: 70 additions & 37 deletions shell/agents/Microsoft.Azure.Agent/Telemetry.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.InteropServices;
using System.Text.Json;

using Microsoft.ApplicationInsights;
Expand All @@ -13,45 +14,15 @@ public class AzTrace
/// </summary>
internal static string InstallationId { get; private set; }

/// <summary>
/// OS platform the application is running on.
/// </summary>
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<JsonElement>(stream);
InstallationId = jsonElement.GetProperty("installationId").GetString();
}
else if (File.Exists(azPSHProfilePath))
{
using var stream = File.OpenRead(azPSHProfilePath);
var jsonElement = JsonSerializer.Deserialize<JsonElement>(stream);
InstallationId = jsonElement.GetProperty("Settings").GetProperty(nameof(InstallationId)).GetString();
}
}
catch
{
// Something wrong when reading the config file.
InstallationId = null;
}
InstallationId = GetInstallationId();
Platform = GetOSPlatform();
}

/// <summary>
Expand Down Expand Up @@ -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<JsonElement>(stream);
return jsonElement.GetProperty("installationId").GetString();
}
else if (File.Exists(azPSHProfilePath))
{
using var stream = File.OpenRead(azPSHProfilePath);
var jsonElement = JsonSerializer.Deserialize<JsonElement>(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
Expand Down Expand Up @@ -199,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)
Expand Down