2016-03-25 13:15:36 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2016-04-28 16:30:32 -07:00
|
|
|
using System.Diagnostics;
|
2016-04-27 17:28:44 -07:00
|
|
|
using System.Threading.Tasks;
|
2016-03-25 13:15:36 -07:00
|
|
|
using Microsoft.ApplicationInsights;
|
2016-04-06 17:02:27 -07:00
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-04-28 16:30:32 -07:00
|
|
|
using Microsoft.DotNet.InternalAbstractions;
|
2016-03-25 13:15:36 -07:00
|
|
|
|
2016-04-06 17:02:27 -07:00
|
|
|
namespace Microsoft.DotNet.Cli
|
2016-03-25 13:15:36 -07:00
|
|
|
{
|
2016-03-31 16:37:40 -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;
|
2016-04-27 17:28:44 -07:00
|
|
|
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;
|
2016-04-27 17:28:44 -07:00
|
|
|
private Task _trackEventTask = null;
|
2016-03-25 13:15:36 -07:00
|
|
|
|
2016-04-27 17:28:44 -07:00
|
|
|
private int _sampleRate = 1;
|
|
|
|
private bool _isTestMachine = false;
|
|
|
|
|
|
|
|
private const int ReciprocalSampleRateValue = 1;
|
|
|
|
private const int ReciprocalSampleRateValueForTest = 1;
|
2016-03-30 17:21:31 -07:00
|
|
|
private const string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254";
|
2016-03-28 17:17:21 -07:00
|
|
|
private const string TelemetryOptout = "DOTNET_CLI_TELEMETRY_OPTOUT";
|
2016-04-27 17:28:44 -07:00
|
|
|
private const string TestMachineFlag = "TEST_MACHINE";
|
|
|
|
private const string TestMachine = "Test Machine";
|
2016-03-28 17:17:21 -07:00
|
|
|
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 17:28:44 -07:00
|
|
|
private const string ReciprocalSampleRate = "Reciprocal SampleRate";
|
2016-03-28 17:17:21 -07:00
|
|
|
|
2016-04-27 16:04:26 -07:00
|
|
|
public bool Enabled { get; }
|
|
|
|
|
2016-03-28 17:17:21 -07:00
|
|
|
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-31 16:37:40 -07:00
|
|
|
{
|
2016-03-25 13:15:36 -07:00
|
|
|
return;
|
2016-03-31 16:37:40 -07:00
|
|
|
}
|
2016-03-25 13:15:36 -07:00
|
|
|
|
2016-04-27 17:28:44 -07:00
|
|
|
_sampleRate = ReciprocalSampleRateValue;
|
|
|
|
_isTestMachine = Env.GetEnvironmentVariableAsBool(TestMachineFlag);
|
|
|
|
|
|
|
|
if(_isTestMachine)
|
|
|
|
{
|
|
|
|
_sampleRate = ReciprocalSampleRateValueForTest;
|
|
|
|
}
|
|
|
|
|
2016-05-02 14:00:18 -07:00
|
|
|
_isCollectingTelemetry = (Environment.TickCount % _sampleRate == 0);
|
2016-04-27 17:28:44 -07:00
|
|
|
if(!_isCollectingTelemetry)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-28 14:49:17 -07:00
|
|
|
_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");
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 14:00:18 -07:00
|
|
|
|
2016-04-28 14:49:17 -07:00
|
|
|
public void TrackEvent(string eventName, IList<string> properties, IDictionary<string, double> measurements)
|
|
|
|
{
|
|
|
|
if (!_isCollectingTelemetry)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-05-02 14:00:18 -07:00
|
|
|
|
2016-04-28 14:49:17 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_trackEventTask = _trackEventTask.ContinueWith(
|
2016-05-02 14:00:18 -07:00
|
|
|
x => TrackEventTask(eventName,
|
|
|
|
properties,
|
2016-04-28 14:49:17 -07:00
|
|
|
measurements)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch(Exception)
|
|
|
|
{
|
|
|
|
Debug.Fail("Exception during telemetry task continuation");
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 14:00:18 -07:00
|
|
|
|
2016-04-28 14:49:17 -07:00
|
|
|
private void InitializeTelemetry()
|
|
|
|
{
|
2016-04-27 17:28:44 -07:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 14:00:18 -07:00
|
|
|
|
2016-04-27 17:28:44 -07:00
|
|
|
public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
|
|
|
|
{
|
|
|
|
if (!Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!_isCollectingTelemetry)
|
|
|
|
{
|
|
|
|
return;
|
2016-05-02 14:00:18 -07:00
|
|
|
}
|
2016-03-25 13:15:36 -07:00
|
|
|
try
|
|
|
|
{
|
2016-04-27 16:04:26 -07:00
|
|
|
using (PerfTrace.Current.CaptureTiming())
|
|
|
|
{
|
2016-04-27 17:28:44 -07:00
|
|
|
_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
|
|
|
|
2016-04-27 17:28:44 -07:00
|
|
|
private void InitializeTelemetry()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_client = new TelemetryClient();
|
|
|
|
_client.InstrumentationKey = InstrumentationKey;
|
|
|
|
_client.Context.Session.Id = Guid.NewGuid().ToString();
|
2016-04-28 16:30:32 -07:00
|
|
|
|
2016-04-27 17:28:44 -07:00
|
|
|
var runtimeEnvironment = PlatformServices.Default.Runtime;
|
2016-04-28 16:30:32 -07:00
|
|
|
_client.Context.Device.OperatingSystem = RuntimeEnvironment.OperatingSystem;
|
2016-03-25 13:15:36 -07:00
|
|
|
|
2016-04-27 17:28:44 -07:00
|
|
|
_commonProperties = new Dictionary<string, string>();
|
2016-04-28 16:30:32 -07:00
|
|
|
_commonProperties.Add(OSVersion, RuntimeEnvironment.OperatingSystemVersion);
|
|
|
|
_commonProperties.Add(OSPlatform, RuntimeEnvironment.OperatingSystemPlatform.ToString());
|
|
|
|
_commonProperties.Add(RuntimeId, RuntimeEnvironment.GetRuntimeIdentifier());
|
2016-04-27 17:28:44 -07:00
|
|
|
_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;
|
|
|
|
}
|
2016-04-27 17:28:44 -07:00
|
|
|
catch(Exception)
|
2016-03-30 17:21:31 -07:00
|
|
|
{
|
2016-04-27 17:28:44 -07:00
|
|
|
_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");
|
2016-04-27 17:28:44 -07:00
|
|
|
return;
|
2016-03-30 17:21:31 -07:00
|
|
|
}
|
2016-03-25 13:15:36 -07:00
|
|
|
}
|
|
|
|
|
2016-04-27 17:28:44 -07:00
|
|
|
private void TrackEventTask(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
|
2016-03-25 13:15:36 -07:00
|
|
|
{
|
2016-04-27 17:28:44 -07:00
|
|
|
if(!_isInitialized)
|
2016-03-31 16:37:40 -07:00
|
|
|
{
|
2016-03-25 13:15:36 -07:00
|
|
|
return;
|
2016-03-31 16:37:40 -07:00
|
|
|
}
|
2016-03-25 13:15:36 -07:00
|
|
|
|
2016-04-27 17:28:44 -07:00
|
|
|
try
|
2016-03-25 13:15:36 -07:00
|
|
|
{
|
2016-04-27 17:28:44 -07:00
|
|
|
var eventMeasurements = GetEventMeasures(measurements);
|
|
|
|
var eventProperties = GetEventProperties(properties);
|
2016-04-27 16:04:26 -07:00
|
|
|
|
2016-04-27 17:28:44 -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-28 17:17:21 -07:00
|
|
|
}
|
2016-03-25 13:15:36 -07:00
|
|
|
|
2016-03-28 17:17:21 -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)
|
|
|
|
{
|
2016-03-31 16:37:40 -07:00
|
|
|
foreach (var measurement in measurements)
|
2016-03-25 13:15:36 -07:00
|
|
|
{
|
2016-03-31 16:37:40 -07:00
|
|
|
if (eventMeasurements.ContainsKey(measurement.Key))
|
|
|
|
{
|
|
|
|
eventMeasurements[measurement.Key] = measurement.Value;
|
|
|
|
}
|
2016-03-25 13:15:36 -07:00
|
|
|
else
|
2016-03-31 16:37:40 -07:00
|
|
|
{
|
|
|
|
eventMeasurements.Add(measurement.Key, measurement.Value);
|
|
|
|
}
|
2016-03-25 13:15:36 -07:00
|
|
|
}
|
|
|
|
}
|
2016-03-28 17:17:21 -07:00
|
|
|
return eventMeasurements;
|
|
|
|
}
|
2016-03-25 13:15:36 -07:00
|
|
|
|
2016-03-28 17:17:21 -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);
|
2016-03-31 16:37:40 -07:00
|
|
|
foreach (var property in properties)
|
2016-03-28 17:17:21 -07:00
|
|
|
{
|
2016-03-31 16:37:40 -07:00
|
|
|
if (eventProperties.ContainsKey(property.Key))
|
|
|
|
{
|
|
|
|
eventProperties[property.Key] = property.Value;
|
|
|
|
}
|
2016-03-28 17:17:21 -07:00
|
|
|
else
|
2016-03-31 16:37:40 -07:00
|
|
|
{
|
|
|
|
eventProperties.Add(property.Key, property.Value);
|
|
|
|
}
|
2016-03-28 17:17:21 -07:00
|
|
|
}
|
2016-04-04 16:18:47 -07:00
|
|
|
return eventProperties;
|
|
|
|
}
|
2016-05-02 14:00:18 -07:00
|
|
|
else
|
2016-04-04 16:18:47 -07:00
|
|
|
{
|
2016-05-02 14:00:18 -07:00
|
|
|
return _commonProperties;
|
2016-03-25 13:15:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|