This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2145 from dotnet/lakshanf/issue2066/telemetry
Lakshanf/issue2066/telemetry
- Loading branch information
Showing
13 changed files
with
259 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// 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. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.DotNet.Cli.Utils | ||
{ | ||
public interface ITelemetry | ||
{ | ||
void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Microsoft.DotNet.Cli.Utils | ||
{ | ||
public class Product | ||
{ | ||
public static readonly string LongName = ".NET Command Line Tools"; | ||
public static readonly string Version = GetProductVersion(); | ||
|
||
private static string GetProductVersion() | ||
{ | ||
var attr = typeof(Product).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>(); | ||
return attr?.InformationalVersion; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.ApplicationInsights; | ||
using Microsoft.Extensions.PlatformAbstractions; | ||
using System.Diagnostics; | ||
|
||
namespace Microsoft.DotNet.Cli.Utils | ||
{ | ||
public class Telemetry : ITelemetry | ||
{ | ||
private bool _isInitialized = false; | ||
private TelemetryClient _client = null; | ||
|
||
private Dictionary<string, string> _commonProperties = null; | ||
private Dictionary<string, double> _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<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>(); | ||
|
||
_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<string, string> properties, IDictionary<string, double> measurements) | ||
{ | ||
if (!_isInitialized) | ||
{ | ||
return; | ||
} | ||
|
||
Dictionary<string, double> eventMeasurements = GetEventMeasures(measurements); | ||
Dictionary<string, string> eventProperties = GetEventProperties(properties); | ||
|
||
try | ||
{ | ||
_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); | ||
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<string, string> GetEventProperties(IDictionary<string, string> properties) | ||
{ | ||
if (properties != null) | ||
{ | ||
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); | ||
} | ||
} | ||
return eventProperties; | ||
} | ||
else | ||
{ | ||
return _commonProperties; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("dotnet.Tests")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.