Reducing Telemetry performance delay
Added telemetry timeout Added SHA256 hashed arguments Added Continous Integration flag Added variable rate sampling Removed ExitCode from telemetry
This commit is contained in:
parent
4f1dbeba0e
commit
4b905ae2bd
2 changed files with 95 additions and 37 deletions
|
@ -132,6 +132,8 @@ namespace Microsoft.DotNet.Cli
|
|||
command = "help";
|
||||
}
|
||||
|
||||
telemetryClient.TrackEvent(command, null, null);
|
||||
|
||||
int exitCode;
|
||||
Func<string[], int> builtIn;
|
||||
if (s_builtIns.TryGetValue(command, out builtIn))
|
||||
|
@ -147,13 +149,6 @@ namespace Microsoft.DotNet.Cli
|
|||
exitCode = result.ExitCode;
|
||||
}
|
||||
|
||||
telemetryClient.TrackEvent(
|
||||
command,
|
||||
null,
|
||||
new Dictionary<string, double>
|
||||
{
|
||||
["ExitCode"] = exitCode
|
||||
});
|
||||
|
||||
return exitCode;
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.InternalAbstractions;
|
||||
|
@ -10,17 +11,27 @@ namespace Microsoft.DotNet.Cli
|
|||
public class Telemetry : ITelemetry
|
||||
{
|
||||
private bool _isInitialized = false;
|
||||
private bool _isCollectingTelemetry = false;
|
||||
private TelemetryClient _client = null;
|
||||
|
||||
private Dictionary<string, string> _commonProperties = null;
|
||||
private Dictionary<string, double> _commonMeasurements = null;
|
||||
private Task _trackEventTask = null;
|
||||
|
||||
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";
|
||||
|
||||
public bool Enabled { get; }
|
||||
|
||||
|
@ -33,26 +44,27 @@ namespace Microsoft.DotNet.Cli
|
|||
return;
|
||||
}
|
||||
|
||||
_sampleRate = ReciprocalSampleRateValue;
|
||||
_isTestMachine = Env.GetEnvironmentVariableAsBool(TestMachineFlag);
|
||||
|
||||
if(_isTestMachine)
|
||||
{
|
||||
_sampleRate = ReciprocalSampleRateValueForTest;
|
||||
}
|
||||
|
||||
_isCollectingTelemetry = (Environment.TickCount % _sampleRate == 0);
|
||||
if(!_isCollectingTelemetry)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using (PerfTrace.Current.CaptureTiming())
|
||||
{
|
||||
_client = new TelemetryClient();
|
||||
_client.InstrumentationKey = InstrumentationKey;
|
||||
_client.Context.Session.Id = Guid.NewGuid().ToString();
|
||||
|
||||
|
||||
_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);
|
||||
_commonMeasurements = new Dictionary<string, double>();
|
||||
//initialize in task to offload to parallel thread
|
||||
_trackEventTask = Task.Factory.StartNew(() => InitializeTelemetry());
|
||||
}
|
||||
|
||||
_isInitialized = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
@ -61,32 +73,83 @@ namespace Microsoft.DotNet.Cli
|
|||
Debug.Fail("Exception during telemetry initialization");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
|
||||
{
|
||||
if (!_isInitialized)
|
||||
if (!Enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!_isCollectingTelemetry)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
using (PerfTrace.Current.CaptureTiming())
|
||||
{
|
||||
_trackEventTask = _trackEventTask.ContinueWith(
|
||||
x => TrackEventTask(eventName, properties, measurements)
|
||||
);
|
||||
}
|
||||
}
|
||||
catch(Exception)
|
||||
{
|
||||
Debug.Fail("Exception during telemetry task continuation");
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
_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>();
|
||||
_isInitialized = true;
|
||||
}
|
||||
catch(Exception)
|
||||
{
|
||||
_isInitialized = false;
|
||||
// we dont want to fail the tool if telemetry fails.
|
||||
Debug.Fail("Exception during telemetry initialization");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void TrackEventTask(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
|
||||
{
|
||||
if(!_isInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
using (PerfTrace.Current.CaptureTiming())
|
||||
try
|
||||
{
|
||||
Dictionary<string, double> eventMeasurements = GetEventMeasures(measurements);
|
||||
Dictionary<string, string> eventProperties = GetEventProperties(properties);
|
||||
var eventMeasurements = GetEventMeasures(measurements);
|
||||
var eventProperties = GetEventProperties(properties);
|
||||
|
||||
try
|
||||
{
|
||||
_client.TrackEvent(eventName, eventProperties, eventMeasurements);
|
||||
_client.Flush();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Debug.Fail("Exception during TrackEvent");
|
||||
}
|
||||
_client.TrackEvent(eventName, eventProperties, eventMeasurements);
|
||||
_client.Flush();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Debug.Fail("Exception during TrackEvent");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Dictionary<string, double> GetEventMeasures(IDictionary<string, double> measurements)
|
||||
{
|
||||
Dictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements);
|
||||
|
|
Loading…
Reference in a new issue