dotnet-installer/src/dotnet/Telemetry.cs

233 lines
8.1 KiB
C#
Raw Normal View History

2016-03-25 13:15:36 -07:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
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 bool _isCollectingTelemetry = false;
2016-04-04 16:18:47 -07:00
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;
private Task _trackEventTask = null;
2016-03-25 13:15:36 -07:00
private int _sampleRate = 1;
private bool _isTestMachine = false;
private const int ReciprocalSampleRateValue = 1;
private const int ReciprocalSampleRateValueForTest = 1;
private const string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254";
private const string TelemetryOptout = "DOTNET_CLI_TELEMETRY_OPTOUT";
private const string TestMachineFlag = "TEST_MACHINE";
private const string TestMachine = "Test Machine";
private const string OSVersion = "OS Version";
private const string OSPlatform = "OS Platform";
private const string RuntimeId = "Runtime Id";
private const string ProductVersion = "Product Version";
private const string ReciprocalSampleRate = "Reciprocal SampleRate";
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
_sampleRate = ReciprocalSampleRateValue;
_isTestMachine = Env.GetEnvironmentVariableAsBool(TestMachineFlag);
if(_isTestMachine)
{
_sampleRate = ReciprocalSampleRateValueForTest;
}
_isCollectingTelemetry = (Environment.TickCount % _sampleRate == 0);
if(!_isCollectingTelemetry)
{
return;
}
_isCollectingTelemetry = true;
try
{
//initialize in task to offload to parallel thread
_trackEventTask = Task.Factory.StartNew(() => InitializeTelemetry());
}
catch(Exception)
{
Debug.Fail("Exception during telemetry task initialization");
}
}
public void TrackEvent(string eventName, IList<string> properties, IDictionary<string, double> measurements)
{
if (!_isCollectingTelemetry)
{
return;
}
try
{
_trackEventTask = _trackEventTask.ContinueWith(
x => TrackEventTask(eventName,
properties,
measurements)
);
}
catch(Exception)
{
Debug.Fail("Exception during telemetry task continuation");
}
}
private void InitializeTelemetry()
{
try
{
using (PerfTrace.Current.CaptureTiming())
{
//initialize in task to offload to parallel thread
_trackEventTask = Task.Factory.StartNew(() => InitializeTelemetry());
}
}
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
Debug.Fail("Exception during telemetry initialization");
}
}
public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
{
if (!Enabled)
{
return;
}
if (!_isCollectingTelemetry)
{
return;
}
2016-03-25 13:15:36 -07:00
try
{
2016-04-27 16:04:26 -07:00
using (PerfTrace.Current.CaptureTiming())
{
_trackEventTask = _trackEventTask.ContinueWith(
x => TrackEventTask(eventName, properties, measurements)
);
}
}
catch(Exception)
{
Debug.Fail("Exception during telemetry task continuation");
}
}
2016-03-25 13:15:36 -07:00
private void InitializeTelemetry()
{
try
{
_client = new TelemetryClient();
_client.InstrumentationKey = InstrumentationKey;
_client.Context.Session.Id = Guid.NewGuid().ToString();
var runtimeEnvironment = PlatformServices.Default.Runtime;
_client.Context.Device.OperatingSystem = RuntimeEnvironment.OperatingSystem;
2016-03-25 13:15:36 -07:00
_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);
_commonProperties.Add(TestMachine, _isTestMachine.ToString());
_commonProperties.Add(ReciprocalSampleRate, _sampleRate.ToString());
_commonMeasurements = new Dictionary<string, double>();
2016-03-25 13:15:36 -07:00
_isInitialized = true;
}
catch(Exception)
{
_isInitialized = false;
// we dont want to fail the tool if telemetry fails.
2016-04-04 16:18:47 -07:00
Debug.Fail("Exception during telemetry initialization");
return;
}
2016-03-25 13:15:36 -07:00
}
private void TrackEventTask(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
try
2016-03-25 13:15:36 -07:00
{
var eventMeasurements = GetEventMeasures(measurements);
var eventProperties = GetEventProperties(properties);
2016-04-27 16:04:26 -07:00
_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
}
}
}
}