dotnet-installer/src/dotnet/Telemetry.cs

165 lines
5.9 KiB
C#
Raw Normal View History

2016-05-02 15:31:33 -07:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
2016-03-25 13:15:36 -07:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
2016-03-25 13:15:36 -07:00
using Microsoft.ApplicationInsights;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Configurer;
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 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 const string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254";
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 OSPlatform = "OS Platform";
private const string RuntimeId = "Runtime Id";
private const string ProductVersion = "Product Version";
private const string TelemetryProfile = "Telemetry Profile";
2016-04-27 16:04:26 -07:00
public bool Enabled { get; }
public Telemetry () : this(null) { }
public Telemetry(INuGetCacheSentinel sentinel)
2016-03-25 13:15:36 -07:00
{
Enabled = !Env.GetEnvironmentVariableAsBool(TelemetryOptout) && PermissionExists(sentinel);
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
//initialize in task to offload to parallel thread
_trackEventTask = Task.Factory.StartNew(() => InitializeTelemetry());
}
2016-05-02 14:00:18 -07:00
private bool PermissionExists(INuGetCacheSentinel sentinel)
{
if (sentinel == null)
{
return false;
}
return sentinel.Exists();
}
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)
);
}
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();
_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(TelemetryProfile, Environment.GetEnvironmentVariable(TelemetryProfileEnvironmentVariable));
_commonMeasurements = new Dictionary<string, double>();
2016-03-25 13:15:36 -07:00
}
catch (Exception)
{
_client = null;
// 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-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 (_client == null)
{
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 eventProperties = GetEventProperties(properties);
2016-05-02 14:12:48 -07:00
var eventMeasurements = GetEventMeasures(measurements);
2016-04-27 16:04:26 -07:00
_client.TrackEvent(eventName, eventProperties, eventMeasurements);
_client.Flush();
}
catch (Exception)
{
2016-05-02 14:12:48 -07:00
Debug.Fail("Exception during TrackEventTask");
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;
}
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
}
}
}
}