Skip to content

Commit

Permalink
Use a default value for the node if we can't create the telemetry.uui…
Browse files Browse the repository at this point in the history
…d file.

This handles the case where we have a read-only file system and want to continue to capture telemetry.
  • Loading branch information
JamesWTruher committed May 11, 2022
1 parent 93df840 commit fdefa4f
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/System.Management.Automation/utils/Telemetry.cs
Expand Up @@ -95,6 +95,9 @@ public static class ApplicationInsightsTelemetry
// private const string _psCoreTelemetryKey = "ee4b2115-d347-47b0-adb6-b19c2c763808"; // Production
private const string _psCoreTelemetryKey = "d26a5ef4-d608-452c-a6b8-a4a55935f70d"; // V7 Preview 3

// In the event there is a problem in creating the node identifier file, use the default identifier.
private static readonly Guid _defaultNodeIdentifier = new Guid("2f998828-3f4a-4741-bf50-d11c6be42f50");

// Use "anonymous" as the string to return when you can't report a name
private const string _anonymous = "anonymous";

Expand Down Expand Up @@ -845,35 +848,39 @@ private static bool TryCreateUniqueIdentifierAndFile(string telemetryFilePath, o

// The directory may not exist, so attempt to create it
// CreateDirectory will simply return the directory if exists
bool attemptFileCreation = true;
try
{
Directory.CreateDirectory(Path.GetDirectoryName(telemetryFilePath));
}
catch
{
// send a telemetry indicating a problem with the cache dir
// it's likely something is seriously wrong so we should at least report it.
// We don't want to provide reasons here, that's not the point, but we
// would like to know if we're having a generalized problem which we can trace statistically
CanSendTelemetry = false;
s_telemetryClient.GetMetric(_telemetryFailure, "Detail").TrackValue(1, "cachedir");
return false;
// There was a problem in creating the directory for the file, do not attempt to create the file.
// We don't send telemetry here because there are valid reasons for the directory to not exist
// and not be able to be created.
attemptFileCreation = false;
}

// Create and save the new identifier, and if there's a problem, disable telemetry
try
{
id = Guid.NewGuid();
File.WriteAllBytes(telemetryFilePath, id.ToByteArray());
return true;
}
catch
// If we were able to create the directory, try to create the file,
// if this fails we will send telemetry to indicate this and then use the default identifier.
if (attemptFileCreation)
{
// another bit of telemetry to notify us about a problem with saving the unique id.
s_telemetryClient.GetMetric(_telemetryFailure, "Detail").TrackValue(1, "saveuuid");
try
{
id = Guid.NewGuid();
File.WriteAllBytes(telemetryFilePath, id.ToByteArray());
return true;
}
catch
{
// another bit of telemetry to notify us about a problem with saving the unique id.
s_telemetryClient.GetMetric(_telemetryFailure, "Detail").TrackValue(1, "saveuuid");
}
}

return false;
// all attempts to create an identifier have failed, so use the default node id.
id = _defaultNodeIdentifier;
return true;
}

/// <summary>
Expand Down

0 comments on commit fdefa4f

Please sign in to comment.