This repository was 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
Telemetry PR #2066
Closed
Closed
Telemetry PR #2066
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.ApplicationInsights; | ||
using Microsoft.Extensions.PlatformAbstractions; | ||
using System.Diagnostics; | ||
|
||
namespace Microsoft.DotNet.Cli.Utils | ||
{ | ||
public class Telemetry | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
{ | ||
private static bool _isInitialized = false; | ||
private static TelemetryClient _client = null; | ||
|
||
private static Dictionary<string, string> _commonProperties = null; | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
private static 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() | ||
{ | ||
if (_isInitialized) | ||
return; | ||
|
||
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 | ||
} | ||
} | ||
|
||
public void TrackCommand(string command, IDictionary<string, string> properties = null, IDictionary<string, double> measurements = null) | ||
{ | ||
if (!_isInitialized) | ||
return; | ||
|
||
Dictionary<string, double> eventMeasurements = GetEventMeasures(measurements); | ||
Dictionary<string, string> eventProperties = GetEventProperties(properties); | ||
|
||
try | ||
{ | ||
_client.TrackEvent(command, eventProperties, eventMeasurements); | ||
_client.Flush(); | ||
} | ||
catch (Exception) { } | ||
} | ||
|
||
|
||
private Dictionary<string, double> GetEventMeasures(IDictionary<string, double> measurements) | ||
{ | ||
Dictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements); | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
if (measurements != null) | ||
{ | ||
foreach (var m in measurements) | ||
{ | ||
if (eventMeasurements.ContainsKey(m.Key)) | ||
eventMeasurements[m.Key] = m.Value; | ||
else | ||
eventMeasurements.Add(m.Key, m.Value); | ||
} | ||
} | ||
return eventMeasurements; | ||
} | ||
|
||
private Dictionary<string, string> GetEventProperties(IDictionary<string, string> properties) | ||
{ | ||
Dictionary<string, string> eventProperties = new Dictionary<string, string>(_commonProperties); | ||
if (properties != null) | ||
{ | ||
foreach (var p in properties) | ||
{ | ||
if (eventProperties.ContainsKey(p.Key)) | ||
eventProperties[p.Key] = p.Value; | ||
else | ||
eventProperties.Add(p.Key, p.Value); | ||
} | ||
} | ||
return eventProperties; | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.