Skip to content

Commit

Permalink
Merge pull request #12 from unoplatform/dev/jela/assembly
Browse files Browse the repository at this point in the history
fix!: Rename to TelemetryClient, add OSArchitecture, remove static fields, mandatory assembly for version
  • Loading branch information
jeromelaban authored Dec 13, 2024
2 parents d2c1724 + b36ae23 commit 9b3eb77
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
Expand All @@ -21,7 +22,7 @@ namespace Uno.DevTools.Telemetry
{
public class Telemetry
{
internal static string? CurrentSessionId;
private string? _currentSessionId;
private TelemetryClient? _client;
private Dictionary<string, string>? _commonProperties;
private Dictionary<string, double>? _commonMeasurements;
Expand All @@ -32,6 +33,7 @@ public class Telemetry
private PersistenceChannel.PersistenceChannel? _persistenceChannel;
private string _instrumentationKey;
private string _eventNamePrefix;
private readonly Assembly _versionAssembly;
private readonly Func<string>? _currentDirectoryProvider;
private const string TelemetryOptout = "UNO_PLATFORM_TELEMETRY_OPTOUT";

Expand All @@ -45,9 +47,11 @@ public class Telemetry
/// <param name="currentDirectoryProvider">A delegate that can provide the value to be hashed in the "Current Path Hash" custom dimension </param>
/// <param name="blockThreadInitialization">Block the execution of the constructor until the telemetry is initialized</param>
/// <param name="sessionId">Defines the session ID for this instance</param>
/// <param name="versionAssembly">The assembly to use to get the version to report in telemetry</param>
public Telemetry(
string instrumentationKey,
string eventNamePrefix,
Assembly versionAssembly,
string? sessionId = null,
bool blockThreadInitialization = false,
Func<bool?>? enabledProvider = null,
Expand All @@ -56,6 +60,7 @@ public Telemetry(
_instrumentationKey = instrumentationKey;
_currentDirectoryProvider = currentDirectoryProvider;
_eventNamePrefix = eventNamePrefix;
_versionAssembly = versionAssembly;

if (bool.TryParse(Environment.GetEnvironmentVariable(TelemetryOptout), out var telemetryOptOut))
{
Expand All @@ -72,7 +77,7 @@ public Telemetry(
}

// Store the session ID in a static field so that it can be reused
CurrentSessionId = sessionId ?? Guid.NewGuid().ToString();
_currentSessionId = sessionId ?? Guid.NewGuid().ToString();
if (blockThreadInitialization)
{
InitializeTelemetry();
Expand Down Expand Up @@ -154,14 +159,14 @@ private void InitializeTelemetry()

_persistenceChannel.SendingInterval = TimeSpan.FromMilliseconds(1);

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

_telemetryConfig = new TelemetryConfiguration { InstrumentationKey = _instrumentationKey };
_client = new TelemetryClient(_telemetryConfig);
_client.InstrumentationKey = _instrumentationKey;
_client.Context.User.Id = _commonProperties[TelemetryCommonProperties.MachineId];
_client.Context.Session.Id = CurrentSessionId;
_client.Context.Session.Id = _currentSessionId;
_client.Context.Device.OperatingSystem = RuntimeEnvironment.OperatingSystem;
}
catch (Exception e)
Expand Down
8 changes: 7 additions & 1 deletion src/Uno.DevTools.Telemetry/TelemetryCommonProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ internal class TelemetryCommonProperties
{
public TelemetryCommonProperties(
string storageDirectoryPath,
Assembly versionAssembly,
Func<string>? getCurrentDirectory = null)
{
_getCurrentDirectory = getCurrentDirectory ?? Directory.GetCurrentDirectory;
_storageDirectoryPath = storageDirectoryPath;
_versionAssembly = versionAssembly;
}

private Func<string> _getCurrentDirectory;
private string _storageDirectoryPath;
private readonly Assembly _versionAssembly;

public const string OSVersion = "OS Version";
public const string OSPlatform = "OS Platform";
public const string OSArchitecture = "OS Architecture";
public const string OutputRedirected = "Output Redirected";
public const string RuntimeId = "Runtime Id";
public const string MachineId = "Machine ID";
Expand All @@ -58,6 +63,7 @@ public Dictionary<string, string> GetTelemetryCommonProperties()
{
{ OSVersion, RuntimeEnvironment.OperatingSystemVersion },
{ OSPlatform, RuntimeEnvironment.OperatingSystemPlatform.ToString() },
{ OSArchitecture, RuntimeInformation.OSArchitecture.ToString() },
{ OutputRedirected, Console.IsOutputRedirected.ToString() },
{ RuntimeId, RuntimeEnvironment.GetRuntimeIdentifier() },
{ ProductVersion, GetProductVersion() },
Expand Down Expand Up @@ -155,7 +161,7 @@ select nic.GetPhysicalAddress().ToString()

private string GetProductVersion()
{
if (this.GetType().Assembly.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute)).FirstOrDefault() is AssemblyInformationalVersionAttribute attribute)
if (_versionAssembly.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute)).FirstOrDefault() is AssemblyInformationalVersionAttribute attribute)
{
return attribute.InformationalVersion;
}
Expand Down

0 comments on commit 9b3eb77

Please sign in to comment.