Skip to content

Commit 9263c03

Browse files
committed
Fix in how to get the installation id in AzTrace
1 parent 85d9bf7 commit 9263c03

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

shell/agents/Microsoft.Azure.Agent/DataRetriever.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ private AzCLICommand QueryForMetadata(string azCommand)
598598
Dictionary<string, string> details = new()
599599
{
600600
["Command"] = azCommand,
601-
["Message"] = $"AzCLI metadata query and process raised an exception"
601+
["Message"] = "AzCLI metadata query and process raised an exception."
602602
};
603603
Telemetry.Trace(AzTrace.Exception(response: null, details), e);
604604
}

shell/agents/Microsoft.Azure.Agent/Telemetry.cs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,42 @@ namespace Microsoft.Azure.Agent;
88

99
public class AzTrace
1010
{
11-
private static readonly string s_installationId;
12-
static AzTrace()
11+
/// <summary>
12+
/// Installation id from the Azure CLI installation.
13+
/// </summary>
14+
internal static string InstallationId { get; private set; }
15+
16+
internal static void Initialize()
1317
{
14-
string azureConfigDir = Environment.GetEnvironmentVariable("AZURE_CONFIG_DIR");
15-
string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
16-
string userProfilePath = string.IsNullOrEmpty(azureConfigDir) ? Path.Combine(userProfile, ".Azure", "azureProfile.json") : azureConfigDir;
18+
InstallationId = null;
1719

18-
JsonElement array;
19-
s_installationId = null;
20+
string azureConfigDir = Environment.GetEnvironmentVariable("AZURE_CONFIG_DIR")
21+
?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".azure");
22+
string azCLIProfilePath = Path.Combine(azureConfigDir, "azureProfile.json");
23+
string azPSHProfilePath = Path.Combine(azureConfigDir, "AzureRmContextSettings.json");
2024

21-
if (File.Exists(userProfilePath))
25+
try
2226
{
23-
using var jsonStream = new FileStream(userProfilePath, FileMode.Open, FileAccess.Read);
24-
array = JsonSerializer.Deserialize<JsonElement>(jsonStream);
25-
s_installationId = array.GetProperty("installationId").GetString();
26-
}
27-
else
28-
{
29-
try
27+
if (File.Exists(azCLIProfilePath))
3028
{
31-
userProfilePath = string.IsNullOrEmpty(azureConfigDir) ? Path.Combine(userProfile, ".Azure", "AzureRmContextSettings.json") : azureConfigDir;
32-
using var jsonStream = new FileStream(userProfilePath, FileMode.Open, FileAccess.Read);
33-
array = JsonSerializer.Deserialize<JsonElement>(jsonStream);
34-
s_installationId = array.GetProperty("Settings").GetProperty("InstallationId").GetString();
29+
using var stream = File.OpenRead(azCLIProfilePath);
30+
var jsonElement = JsonSerializer.Deserialize<JsonElement>(stream);
31+
InstallationId = jsonElement.GetProperty("installationId").GetString();
3532
}
36-
catch
33+
else if (File.Exists(azPSHProfilePath))
3734
{
38-
// If finally no installation id found, just return null.
39-
s_installationId = null;
35+
using var stream = File.OpenRead(azPSHProfilePath);
36+
var jsonElement = JsonSerializer.Deserialize<JsonElement>(stream);
37+
InstallationId = jsonElement.GetProperty("Settings").GetProperty(nameof(InstallationId)).GetString();
4038
}
4139
}
40+
catch
41+
{
42+
// Something wrong when reading the config file.
43+
InstallationId = null;
44+
}
4245
}
4346

44-
internal AzTrace()
45-
{
46-
InstallationId = s_installationId;
47-
}
48-
49-
/// <summary>
50-
/// Installation id from the Azure CLI installation.
51-
/// </summary>
52-
internal string InstallationId { get; }
53-
5447
/// <summary>
5548
/// Topic name of the response from Azure Copilot.
5649
/// </summary>
@@ -174,7 +167,7 @@ private void LogTelemetry(AzTrace trace, Exception e = null)
174167
{
175168
["QueryId"] = trace.QueryId,
176169
["ConversationId"] = trace.ConversationId,
177-
["InstallationId"] = trace.InstallationId,
170+
["InstallationId"] = AzTrace.InstallationId,
178171
["TopicName"] = trace.TopicName,
179172
["EventType"] = trace.EventType,
180173
["ShellCommand"] = trace.ShellCommand,
@@ -209,7 +202,11 @@ private static string GetDetailedMessage(object details)
209202
/// <summary>
210203
/// Initialize telemetry client.
211204
/// </summary>
212-
internal static void Initialize() => s_singleton ??= new Telemetry();
205+
internal static void Initialize()
206+
{
207+
s_singleton ??= new Telemetry();
208+
AzTrace.Initialize();
209+
}
213210

214211
/// <summary>
215212
/// Trace a telemetry metric.

0 commit comments

Comments
 (0)