using System; using System.Collections.Generic; using Microsoft.ApplicationInsights; using Microsoft.DotNet.Cli.Utils; using Microsoft.Extensions.PlatformAbstractions; using System.Diagnostics; namespace Microsoft.DotNet.Cli { public class Telemetry : ITelemetry { private bool _isInitialized = false; private TelemetryClient _client = null; private Dictionary _commonProperties = null; private Dictionary _commonMeasurements = null; private const string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254"; private const string TelemetryOptout = "DOTNET_CLI_TELEMETRY_OPTOUT"; private const string OSVersion = "OS Version"; private const string OSPlatform = "OS Platform"; private const string RuntimeId = "Runtime Id"; private const string ProductVersion = "Product Version"; public Telemetry() { bool optout = Env.GetEnvironmentVariableAsBool(TelemetryOptout); if (optout) { return; } 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(); _commonProperties.Add(OSVersion, runtimeEnvironment.OperatingSystemVersion); _commonProperties.Add(OSPlatform, runtimeEnvironment.OperatingSystemPlatform.ToString()); _commonProperties.Add(RuntimeId, runtimeEnvironment.GetRuntimeIdentifier()); _commonProperties.Add(ProductVersion, Product.Version); _commonMeasurements = new Dictionary(); _isInitialized = true; } 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 properties, IDictionary measurements) { if (!_isInitialized) { return; } Dictionary eventMeasurements = GetEventMeasures(measurements); Dictionary eventProperties = GetEventProperties(properties); try { _client.TrackEvent(eventName, eventProperties, eventMeasurements); _client.Flush(); } catch (Exception) { Debug.Fail("Exception during TrackEvent"); } } private Dictionary GetEventMeasures(IDictionary measurements) { Dictionary eventMeasurements = new Dictionary(_commonMeasurements); if (measurements != null) { foreach (var measurement in measurements) { if (eventMeasurements.ContainsKey(measurement.Key)) { eventMeasurements[measurement.Key] = measurement.Value; } else { eventMeasurements.Add(measurement.Key, measurement.Value); } } } return eventMeasurements; } private Dictionary GetEventProperties(IDictionary properties) { if (properties != null) { var eventProperties = new Dictionary(_commonProperties); foreach (var property in properties) { if (eventProperties.ContainsKey(property.Key)) { eventProperties[property.Key] = property.Value; } else { eventProperties.Add(property.Key, property.Value); } } return eventProperties; } else { return _commonProperties; } } } }