Skip to content

Commit

Permalink
fix: Adjust telemetry persistence
Browse files Browse the repository at this point in the history
(cherry picked from commit cd59234)
  • Loading branch information
jeromelaban authored and mergify[bot] committed Jun 13, 2024
1 parent 49aaf12 commit 657104e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class Telemetry
private Dictionary<string, double> _commonMeasurements;
private TelemetryConfiguration _telemetryConfig;
private Task _trackEventTask;
private string _storageDirectoryPath;
private string _settingsStorageDirectoryPath;
private PersistenceChannel.PersistenceChannel _persistenceChannel;
private const string InstrumentationKey = "9a44058e-1913-4721-a979-9582ab8bedce";
private const string TelemetryOptout = "UNO_PLATFORM_TELEMETRY_OPTOUT";
Expand Down Expand Up @@ -120,12 +122,24 @@ private void InitializeTelemetry()
{
try
{
_storageDirectoryPath = Path.Combine(Path.GetTempPath(), ".uno", "telemetry");

// Store the settings on in the user profile for linux
if (RuntimeEnvironment.OperatingSystemPlatform == Platform.Linux)
{
_settingsStorageDirectoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".uno", "telemetry");
}
else
{
_settingsStorageDirectoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Uno Platform", "telemetry");
}

_persistenceChannel = new PersistenceChannel.PersistenceChannel(
storageDirectoryPath: Path.Combine(Path.GetTempPath(), ".uno", "telemetry"));
storageDirectoryPath: _storageDirectoryPath);

_persistenceChannel.SendingInterval = TimeSpan.FromMilliseconds(1);

_commonProperties = new TelemetryCommonProperties().GetTelemetryCommonProperties();
_commonProperties = new TelemetryCommonProperties(_settingsStorageDirectoryPath).GetTelemetryCommonProperties();
_commonMeasurements = new Dictionary<string, double>();

_telemetryConfig = new TelemetryConfiguration { InstrumentationKey = InstrumentationKey };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ namespace Uno.UI.SourceGenerators.Telemetry
internal class TelemetryCommonProperties
{
public TelemetryCommonProperties(
string storageDirectoryPath,
Func<string> getCurrentDirectory = null)
{
_getCurrentDirectory = getCurrentDirectory ?? Directory.GetCurrentDirectory;
_storageDirectoryPath = storageDirectoryPath;
}

private Func<string> _getCurrentDirectory;

private string _storageDirectoryPath;
public const string OSVersion = "OS Version";
public const string OSPlatform = "OS Platform";
public const string OutputRedirected = "Output Redirected";
Expand Down Expand Up @@ -60,6 +62,17 @@ public TelemetryCommonProperties(

private string GetMachineId()
{
var machineHashPath = Path.Combine(_storageDirectoryPath, ".machinehash");

if (File.Exists(machineHashPath))
{
if (File.ReadAllText(machineHashPath) is { Length: 32 /* hash */ or 36 /* guid */ } readHash)
{
return readHash;
}
}

string hash = null;
try
{
var macAddr =
Expand All @@ -69,14 +82,24 @@ from nic in NetworkInterface.GetAllNetworkInterfaces()
select nic.GetPhysicalAddress().ToString()
).FirstOrDefault();

return HashBuilder.Build(macAddr);
hash = HashBuilder.Build(macAddr);

if (!Directory.Exists(Path.GetDirectoryName(machineHashPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(machineHashPath));
}

File.WriteAllText(machineHashPath, hash);
}
catch (Exception e)
{
Debug.Fail($"Failed to get Mac address: {e}");

return Guid.NewGuid().ToString();
// if the hash was set, but the write failed, let's continue.
hash ??= Guid.NewGuid().ToString();
}

return hash;
}

private string GetProductVersion()
Expand Down

0 comments on commit 657104e

Please sign in to comment.