dotnet-installer/src/Microsoft.DotNet.Cli.Utils/Telemetry.cs

109 lines
4 KiB
C#
Raw Normal View History

2016-03-25 13:15:36 -07:00
using System;
using System.Collections.Generic;
using Microsoft.ApplicationInsights;
using Microsoft.Extensions.PlatformAbstractions;
using System.Diagnostics;
2016-03-25 13:15:36 -07:00
namespace Microsoft.DotNet.Cli.Utils
{
public class Telemetry
{
private static bool _isInitialized = false;
private static TelemetryClient _client = null;
private static Dictionary<string, string> _commonProperties = null;
private static Dictionary<string, double> _commonMeasurements = null;
private const string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254";
private const string TelemetryOptout = "DOTNET_CLI_TELEMETRY_OPTOUT";
private const string OSVersion = "OS Version";
private const string OSPlatform = "OS Platform";
private const string RuntimeId = "Runtime Id";
private const string ProductVersion = "Product Version";
public Telemetry()
2016-03-25 13:15:36 -07:00
{
if (_isInitialized)
return;
bool optout = Env.GetEnvironmentVariableAsBool(TelemetryOptout);
2016-03-25 13:15:36 -07:00
if (optout)
2016-03-25 13:15:36 -07:00
return;
try
{
_client = new TelemetryClient();
_client.InstrumentationKey = InstrumentationKey;
2016-03-25 13:15:36 -07:00
_client.Context.Session.Id = Guid.NewGuid().ToString();
var runtimeEnvironment = PlatformServices.Default.Runtime;
_client.Context.Device.OperatingSystem = runtimeEnvironment.OperatingSystem;
_commonProperties = new Dictionary<string, string>();
_commonProperties.Add(OSVersion, runtimeEnvironment.OperatingSystemVersion);
_commonProperties.Add(OSPlatform, runtimeEnvironment.OperatingSystemPlatform.ToString());
_commonProperties.Add(RuntimeId, runtimeEnvironment.GetRuntimeIdentifier());
_commonProperties.Add(ProductVersion, Product.Version);
2016-03-25 13:15:36 -07:00
_commonMeasurements = new Dictionary<string, double>();
_isInitialized = true;
}
catch (Exception)
{
// we dont want to fail the tool if telemetry fais. We should be able to detect abnormalities from data
// at the server end
}
2016-03-25 13:15:36 -07:00
}
public void TrackCommand(string command, IDictionary<string, string> properties = null, IDictionary<string, double> measurements = null)
2016-03-25 13:15:36 -07:00
{
if (!_isInitialized)
return;
Dictionary<string, double> eventMeasurements = GetEventMeasures(measurements);
Dictionary<string, string> eventProperties = GetEventProperties(properties);
try
2016-03-25 13:15:36 -07:00
{
_client.TrackEvent(command, eventProperties, eventMeasurements);
_client.Flush();
2016-03-25 13:15:36 -07:00
}
catch (Exception) { }
}
2016-03-25 13:15:36 -07:00
private Dictionary<string, double> GetEventMeasures(IDictionary<string, double> measurements)
{
2016-03-25 13:15:36 -07:00
Dictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements);
if (measurements != null)
{
foreach (var m in measurements)
{
if (eventMeasurements.ContainsKey(m.Key))
eventMeasurements[m.Key] = m.Value;
else
eventMeasurements.Add(m.Key, m.Value);
}
}
return eventMeasurements;
}
2016-03-25 13:15:36 -07:00
private Dictionary<string, string> GetEventProperties(IDictionary<string, string> properties)
{
Dictionary<string, string> eventProperties = new Dictionary<string, string>(_commonProperties);
if (properties != null)
2016-03-25 13:15:36 -07:00
{
foreach (var p in properties)
{
if (eventProperties.ContainsKey(p.Key))
eventProperties[p.Key] = p.Value;
else
eventProperties.Add(p.Key, p.Value);
}
2016-03-25 13:15:36 -07:00
}
return eventProperties;
2016-03-25 13:15:36 -07:00
}
}
}