Merge pull request #2724 from adamgorMSFT/adamgor/telemetry-perf-improvements

Improving Telemetry performance
This commit is contained in:
Piotr Puszkiewicz 2016-05-03 15:14:58 -07:00
commit 99193a3a91
3 changed files with 64 additions and 41 deletions

View file

@ -135,6 +135,8 @@ namespace Microsoft.DotNet.Cli
command = "help"; command = "help";
} }
telemetryClient.TrackEvent(command, null, null);
int exitCode; int exitCode;
Func<string[], int> builtIn; Func<string[], int> builtIn;
if (s_builtIns.TryGetValue(command, out builtIn)) if (s_builtIns.TryGetValue(command, out builtIn))
@ -150,13 +152,6 @@ namespace Microsoft.DotNet.Cli
exitCode = result.ExitCode; exitCode = result.ExitCode;
} }
telemetryClient.TrackEvent(
command,
null,
new Dictionary<string, double>
{
["ExitCode"] = exitCode
});
return exitCode; return exitCode;

View file

@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights;
using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.InternalAbstractions; using Microsoft.DotNet.InternalAbstractions;
@ -17,13 +18,18 @@ namespace Microsoft.DotNet.Cli
private Dictionary<string, string> _commonProperties = null; private Dictionary<string, string> _commonProperties = null;
private Dictionary<string, double> _commonMeasurements = null; private Dictionary<string, double> _commonMeasurements = null;
private Task _trackEventTask = null;
private string _telemetryProfile;
private const string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254"; private const string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254";
private const string TelemetryOptout = "DOTNET_CLI_TELEMETRY_OPTOUT"; private const string TelemetryOptout = "DOTNET_CLI_TELEMETRY_OPTOUT";
private const string TelemetryProfileEnvironmentVariable = "DOTNET_CLI_TELEMETRY_PROFILE";
private const string OSVersion = "OS Version"; private const string OSVersion = "OS Version";
private const string OSPlatform = "OS Platform"; private const string OSPlatform = "OS Platform";
private const string RuntimeId = "Runtime Id"; private const string RuntimeId = "Runtime Id";
private const string ProductVersion = "Product Version"; private const string ProductVersion = "Product Version";
private const string TelemetryProfile = "Telemetry Profile";
public bool Enabled { get; } public bool Enabled { get; }
@ -36,60 +42,73 @@ namespace Microsoft.DotNet.Cli
return; return;
} }
_telemetryProfile = Environment.GetEnvironmentVariable(TelemetryProfileEnvironmentVariable);
//initialize in task to offload to parallel thread
_trackEventTask = Task.Factory.StartNew(() => InitializeTelemetry());
}
public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
{
if (!Enabled)
{
return;
}
//continue task in existing parallel thread
_trackEventTask = _trackEventTask.ContinueWith(
x => TrackEventTask(eventName, properties, measurements)
);
}
private void InitializeTelemetry()
{
try try
{ {
using (PerfTrace.Current.CaptureTiming()) _client = new TelemetryClient();
{ _client.InstrumentationKey = InstrumentationKey;
_client = new TelemetryClient(); _client.Context.Session.Id = Guid.NewGuid().ToString();
_client.InstrumentationKey = InstrumentationKey;
_client.Context.Session.Id = Guid.NewGuid().ToString();
_client.Context.Device.OperatingSystem = RuntimeEnvironment.OperatingSystem; _client.Context.Device.OperatingSystem = RuntimeEnvironment.OperatingSystem;
_commonProperties = new Dictionary<string, string>(); _commonProperties = new Dictionary<string, string>();
_commonProperties.Add(OSVersion, RuntimeEnvironment.OperatingSystemVersion); _commonProperties.Add(OSVersion, RuntimeEnvironment.OperatingSystemVersion);
_commonProperties.Add(OSPlatform, RuntimeEnvironment.OperatingSystemPlatform.ToString()); _commonProperties.Add(OSPlatform, RuntimeEnvironment.OperatingSystemPlatform.ToString());
_commonProperties.Add(RuntimeId, RuntimeEnvironment.GetRuntimeIdentifier()); _commonProperties.Add(RuntimeId, RuntimeEnvironment.GetRuntimeIdentifier());
_commonProperties.Add(ProductVersion, Product.Version); _commonProperties.Add(ProductVersion, Product.Version);
_commonMeasurements = new Dictionary<string, double>(); _commonProperties.Add(TelemetryProfile, _telemetryProfile);
} _commonMeasurements = new Dictionary<string, double>();
_isInitialized = true; _isInitialized = true;
} }
catch (Exception) catch (Exception)
{ {
// we dont want to fail the tool if telemetry fais. We should be able to detect abnormalities from data _isInitialized = false;
// at the server end // we dont want to fail the tool if telemetry fails.
Debug.Fail("Exception during telemetry initialization"); Debug.Fail("Exception during telemetry initialization");
} }
} }
public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements) private void TrackEventTask(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
{ {
if (!_isInitialized) if (!_isInitialized)
{ {
return; return;
} }
using (PerfTrace.Current.CaptureTiming()) try
{ {
Dictionary<string, double> eventMeasurements = GetEventMeasures(measurements); var eventProperties = GetEventProperties(properties);
Dictionary<string, string> eventProperties = GetEventProperties(properties); var eventMeasurements = GetEventMeasures(measurements);
try _client.TrackEvent(eventName, eventProperties, eventMeasurements);
{ _client.Flush();
_client.TrackEvent(eventName, eventProperties, eventMeasurements); }
_client.Flush(); catch (Exception)
} {
catch (Exception) Debug.Fail("Exception during TrackEventTask");
{
Debug.Fail("Exception during TrackEvent");
}
} }
} }
private Dictionary<string, double> GetEventMeasures(IDictionary<string, double> measurements) private Dictionary<string, double> GetEventMeasures(IDictionary<string, double> measurements)
{ {
Dictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements); Dictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements);

View file

@ -3,6 +3,7 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using FluentAssertions; using FluentAssertions;
using Microsoft.DotNet.InternalAbstractions; using Microsoft.DotNet.InternalAbstractions;
using Microsoft.DotNet.ProjectModel; using Microsoft.DotNet.ProjectModel;
@ -14,13 +15,13 @@ namespace Microsoft.Dotnet.Tools.Test.Tests
{ {
public class GivenThatWeWantToUseDotnetTestE2EInDesignTimeForMultipleTFms : TestBase public class GivenThatWeWantToUseDotnetTestE2EInDesignTimeForMultipleTFms : TestBase
{ {
private readonly string _projectFilePath; private string _projectFilePath;
private readonly string _netCoreAppOutputPath; private string _netCoreAppOutputPath;
private readonly string _net451OutputPath; private string _net451OutputPath;
public GivenThatWeWantToUseDotnetTestE2EInDesignTimeForMultipleTFms() private void Setup([CallerMemberName] string callingMethod = "")
{ {
var testInstance = TestAssetsManager.CreateTestInstance(Path.Combine("ProjectsWithTests", "MultipleFrameworkProject")); var testInstance = TestAssetsManager.CreateTestInstance(Path.Combine("ProjectsWithTests", "MultipleFrameworkProject"), callingMethod);
_projectFilePath = Path.Combine(testInstance.TestRoot, "project.json"); _projectFilePath = Path.Combine(testInstance.TestRoot, "project.json");
var contexts = ProjectContext.CreateContextForEachFramework( var contexts = ProjectContext.CreateContextForEachFramework(
@ -51,6 +52,8 @@ namespace Microsoft.Dotnet.Tools.Test.Tests
[WindowsOnlyFact] [WindowsOnlyFact]
public void It_discovers_tests_for_the_ProjectWithTestsWithNetCoreApp() public void It_discovers_tests_for_the_ProjectWithTestsWithNetCoreApp()
{ {
Setup();
using (var adapter = new Adapter("TestDiscovery.Start")) using (var adapter = new Adapter("TestDiscovery.Start"))
{ {
adapter.Listen(); adapter.Listen();
@ -68,6 +71,8 @@ namespace Microsoft.Dotnet.Tools.Test.Tests
[WindowsOnlyFact] [WindowsOnlyFact]
public void It_discovers_tests_for_the_ProjectWithTestsWithNet451() public void It_discovers_tests_for_the_ProjectWithTestsWithNet451()
{ {
Setup();
using (var adapter = new Adapter("TestDiscovery.Start")) using (var adapter = new Adapter("TestDiscovery.Start"))
{ {
adapter.Listen(); adapter.Listen();
@ -86,6 +91,8 @@ namespace Microsoft.Dotnet.Tools.Test.Tests
[Fact] [Fact]
public void It_runs_tests_for_netcoreapp10() public void It_runs_tests_for_netcoreapp10()
{ {
Setup();
using (var adapter = new Adapter("TestExecution.GetTestRunnerProcessStartInfo")) using (var adapter = new Adapter("TestExecution.GetTestRunnerProcessStartInfo"))
{ {
adapter.Listen(); adapter.Listen();
@ -105,6 +112,8 @@ namespace Microsoft.Dotnet.Tools.Test.Tests
[WindowsOnlyFact] [WindowsOnlyFact]
public void It_runs_tests_for_net451() public void It_runs_tests_for_net451()
{ {
Setup();
using (var adapter = new Adapter("TestExecution.GetTestRunnerProcessStartInfo")) using (var adapter = new Adapter("TestExecution.GetTestRunnerProcessStartInfo"))
{ {
adapter.Listen(); adapter.Listen();