dotnet-installer/src/dotnet/Telemetry.cs

135 lines
4.7 KiB
C#
Raw Normal View History

2016-03-25 13:15:36 -07:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
2016-03-25 13:15:36 -07:00
using Microsoft.ApplicationInsights;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.InternalAbstractions;
2016-03-25 13:15:36 -07:00
namespace Microsoft.DotNet.Cli
2016-03-25 13:15:36 -07:00
{
public class Telemetry : ITelemetry
2016-03-25 13:15:36 -07:00
{
2016-04-04 16:18:47 -07:00
private bool _isInitialized = false;
private TelemetryClient _client = null;
2016-03-25 13:15:36 -07:00
2016-04-04 16:18:47 -07:00
private Dictionary<string, string> _commonProperties = null;
private Dictionary<string, double> _commonMeasurements = null;
2016-03-25 13:15:36 -07:00
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";
2016-04-27 16:04:26 -07:00
public bool Enabled { get; }
public Telemetry()
2016-03-25 13:15:36 -07:00
{
2016-04-27 16:04:26 -07:00
Enabled = !Env.GetEnvironmentVariableAsBool(TelemetryOptout);
2016-03-25 13:15:36 -07:00
2016-04-27 16:04:26 -07:00
if (!Enabled)
{
2016-03-25 13:15:36 -07:00
return;
}
2016-03-25 13:15:36 -07:00
try
{
2016-04-27 16:04:26 -07:00
using (PerfTrace.Current.CaptureTiming())
{
_client = new TelemetryClient();
_client.InstrumentationKey = InstrumentationKey;
_client.Context.Session.Id = Guid.NewGuid().ToString();
2016-03-25 13:15:36 -07:00
_client.Context.Device.OperatingSystem = RuntimeEnvironment.OperatingSystem;
2016-03-25 13:15:36 -07:00
2016-04-27 16:04:26 -07:00
_commonProperties = new Dictionary<string, string>();
_commonProperties.Add(OSVersion, RuntimeEnvironment.OperatingSystemVersion);
_commonProperties.Add(OSPlatform, RuntimeEnvironment.OperatingSystemPlatform.ToString());
_commonProperties.Add(RuntimeId, RuntimeEnvironment.GetRuntimeIdentifier());
2016-04-27 16:04:26 -07:00
_commonProperties.Add(ProductVersion, Product.Version);
_commonMeasurements = new Dictionary<string, double>();
}
2016-03-25 13:15:36 -07:00
_isInitialized = true;
}
catch (Exception)
{
2016-04-27 16:04:26 -07:00
// we dont want to fail the tool if telemetry fais. We should be able to detect abnormalities from data
// at the server end
2016-04-04 16:18:47 -07:00
Debug.Fail("Exception during telemetry initialization");
}
2016-03-25 13:15:36 -07:00
}
public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
2016-03-25 13:15:36 -07:00
{
if (!_isInitialized)
{
2016-03-25 13:15:36 -07:00
return;
}
2016-03-25 13:15:36 -07:00
2016-04-27 16:04:26 -07:00
using (PerfTrace.Current.CaptureTiming())
2016-03-25 13:15:36 -07:00
{
2016-04-27 16:04:26 -07:00
Dictionary<string, double> eventMeasurements = GetEventMeasures(measurements);
Dictionary<string, string> eventProperties = GetEventProperties(properties);
try
{
_client.TrackEvent(eventName, eventProperties, eventMeasurements);
_client.Flush();
}
catch (Exception)
{
Debug.Fail("Exception during TrackEvent");
}
2016-04-04 16:18:47 -07:00
}
}
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 measurement in measurements)
2016-03-25 13:15:36 -07:00
{
if (eventMeasurements.ContainsKey(measurement.Key))
{
eventMeasurements[measurement.Key] = measurement.Value;
}
2016-03-25 13:15:36 -07:00
else
{
eventMeasurements.Add(measurement.Key, measurement.Value);
}
2016-03-25 13:15:36 -07:00
}
}
return eventMeasurements;
}
2016-03-25 13:15:36 -07:00
private Dictionary<string, string> GetEventProperties(IDictionary<string, string> properties)
{
if (properties != null)
2016-03-25 13:15:36 -07:00
{
2016-04-04 16:18:47 -07:00
var eventProperties = new Dictionary<string, string>(_commonProperties);
foreach (var property in properties)
{
if (eventProperties.ContainsKey(property.Key))
{
eventProperties[property.Key] = property.Value;
}
else
{
eventProperties.Add(property.Key, property.Value);
}
}
2016-04-04 16:18:47 -07:00
return eventProperties;
}
else
{
return _commonProperties;
2016-03-25 13:15:36 -07:00
}
}
}
}