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
Lakshanf/issue2066/telemetry #2145
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6b84a10
Telemetry PR
LakshanF 689a72f
incorporate CR feedback for the first PR
LakshanF e9a6c43
added comment to the instrumentation key field
LakshanF d9e169e
Stopped collecting command arguments due to PII concerns
LakshanF 25745d9
Implemented Telemetry class via an interface for test ease
LakshanF a506a25
using typo
LakshanF e077b60
add test dependency
LakshanF 3e4edf1
CR feedback part 1
danquirk b5e1042
Use InternalsVisibleTo for testing
danquirk 0d067be
Use static member
danquirk 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 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 | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
Debug.Fail("Exception during telemetry initialization"); | ||
} | ||
} | ||
|
||
public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements) | ||
{ | ||
if (!_isInitialized) | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
{ | ||
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 comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
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.
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.
This comment was marked as spam.
Sorry, something went wrong.