incorporate CR feedback for the first PR
This commit is contained in:
parent
6b84a10e57
commit
689a72ffb5
6 changed files with 80 additions and 72 deletions
|
@ -14,8 +14,8 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
public static readonly string AnsiPassThru = Prefix + "ANSI_PASS_THRU";
|
public static readonly string AnsiPassThru = Prefix + "ANSI_PASS_THRU";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Lazy<bool> _verbose = new Lazy<bool>(() => Env.GetBool(Variables.Verbose));
|
private static Lazy<bool> _verbose = new Lazy<bool>(() => Env.GetEnvironmentVariableAsBool(Variables.Verbose));
|
||||||
private static Lazy<bool> _ansiPassThru = new Lazy<bool>(() => Env.GetBool(Variables.AnsiPassThru));
|
private static Lazy<bool> _ansiPassThru = new Lazy<bool>(() => Env.GetEnvironmentVariableAsBool(Variables.AnsiPassThru));
|
||||||
|
|
||||||
public static bool IsVerbose()
|
public static bool IsVerbose()
|
||||||
{
|
{
|
||||||
|
|
|
@ -34,28 +34,9 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions);
|
return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool GetBool(string name, bool defaultValue = false)
|
public static bool GetEnvironmentVariableAsBool(string name, bool defaultValue = false)
|
||||||
{
|
{
|
||||||
var str = Environment.GetEnvironmentVariable(name);
|
return _environment.GetEnvironmentVariableAsBool(name, defaultValue);
|
||||||
if (string.IsNullOrEmpty(str))
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (str.ToLowerInvariant())
|
|
||||||
{
|
|
||||||
case "true":
|
|
||||||
case "1":
|
|
||||||
case "yes":
|
|
||||||
return true;
|
|
||||||
case "false":
|
|
||||||
case "0":
|
|
||||||
case "no":
|
|
||||||
return false;
|
|
||||||
default:
|
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,5 +93,29 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
|
|
||||||
return GetCommandPathFromRootPath(rootPath, commandName, extensionsArr);
|
return GetCommandPathFromRootPath(rootPath, commandName, extensionsArr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool GetEnvironmentVariableAsBool(string name, bool defaultValue)
|
||||||
|
{
|
||||||
|
var str = Environment.GetEnvironmentVariable(name);
|
||||||
|
if (string.IsNullOrEmpty(str))
|
||||||
|
{
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (str.ToLowerInvariant())
|
||||||
|
{
|
||||||
|
case "true":
|
||||||
|
case "1":
|
||||||
|
case "yes":
|
||||||
|
return true;
|
||||||
|
case "false":
|
||||||
|
case "0":
|
||||||
|
case "no":
|
||||||
|
return false;
|
||||||
|
default:
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,5 +16,7 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions);
|
string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions);
|
||||||
|
|
||||||
string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions);
|
string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions);
|
||||||
|
|
||||||
|
bool GetEnvironmentVariableAsBool(string name, bool defaultValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,51 +7,44 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
{
|
{
|
||||||
public class Telemetry
|
public class Telemetry
|
||||||
{
|
{
|
||||||
private class Variables
|
|
||||||
{
|
|
||||||
public static readonly string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254";
|
|
||||||
private static readonly string Prefix = "DOTNET_CLI_TELEMETRY_";
|
|
||||||
public static readonly string Optout = Prefix + "OPTOUT";
|
|
||||||
}
|
|
||||||
|
|
||||||
private class Properties
|
|
||||||
{
|
|
||||||
public static readonly string OSVersion = "OS Version";
|
|
||||||
public static readonly string OSPlatform = "OS Platform";
|
|
||||||
public static readonly string RuntimeId = "Runtime Id";
|
|
||||||
public static readonly string ProductVersion = "Product Version";
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool _isInitialized = false;
|
private static bool _isInitialized = false;
|
||||||
private static TelemetryClient _client = null;
|
private static TelemetryClient _client = null;
|
||||||
|
|
||||||
private static Dictionary<string, string> _commonProperties = null;
|
private static Dictionary<string, string> _commonProperties = null;
|
||||||
private static Dictionary<string, double> _commonMeasurements = null;
|
private static Dictionary<string, double> _commonMeasurements = null;
|
||||||
|
|
||||||
static Telemetry()
|
private static readonly 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)
|
if (_isInitialized)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
bool Optout = Env.GetBool(Variables.Optout);
|
bool optout = Env.GetEnvironmentVariableAsBool(TelemetryOptout);
|
||||||
|
|
||||||
if (Optout)
|
if (optout)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_client = new TelemetryClient();
|
_client = new TelemetryClient();
|
||||||
_client.InstrumentationKey = Variables.InstrumentationKey;
|
_client.InstrumentationKey = InstrumentationKey;
|
||||||
_client.Context.Session.Id = Guid.NewGuid().ToString();
|
_client.Context.Session.Id = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
var runtimeEnvironment = PlatformServices.Default.Runtime;
|
var runtimeEnvironment = PlatformServices.Default.Runtime;
|
||||||
_client.Context.Device.OperatingSystem = runtimeEnvironment.OperatingSystem;
|
_client.Context.Device.OperatingSystem = runtimeEnvironment.OperatingSystem;
|
||||||
|
|
||||||
_commonProperties = new Dictionary<string, string>();
|
_commonProperties = new Dictionary<string, string>();
|
||||||
_commonProperties.Add(Properties.OSVersion, runtimeEnvironment.OperatingSystemVersion);
|
_commonProperties.Add(OSVersion, runtimeEnvironment.OperatingSystemVersion);
|
||||||
_commonProperties.Add(Properties.OSPlatform, runtimeEnvironment.OperatingSystemPlatform.ToString());
|
_commonProperties.Add(OSPlatform, runtimeEnvironment.OperatingSystemPlatform.ToString());
|
||||||
_commonProperties.Add(Properties.RuntimeId, runtimeEnvironment.GetRuntimeIdentifier());
|
_commonProperties.Add(RuntimeId, runtimeEnvironment.GetRuntimeIdentifier());
|
||||||
_commonProperties.Add(Properties.ProductVersion, Product.Version);
|
_commonProperties.Add(ProductVersion, Product.Version);
|
||||||
_commonMeasurements = new Dictionary<string, double>();
|
_commonMeasurements = new Dictionary<string, double>();
|
||||||
|
|
||||||
_isInitialized = true;
|
_isInitialized = true;
|
||||||
|
@ -59,23 +52,24 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
catch (Exception) { }
|
catch (Exception) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void TrackCommand(string command, IDictionary<string, string> properties = null, IDictionary<string, double> measurements = null)
|
public void TrackCommand(string command, IDictionary<string, string> properties = null, IDictionary<string, double> measurements = null)
|
||||||
{
|
{
|
||||||
if (!_isInitialized)
|
if (!_isInitialized)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Dictionary<string, string> eventProperties = new Dictionary<string, string>(_commonProperties);
|
Dictionary<string, double> eventMeasurements = GetEventMeasures(measurements);
|
||||||
if (properties != null)
|
Dictionary<string, string> eventProperties = GetEventProperties(properties);
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
foreach (var p in properties)
|
_client.TrackEvent(command, eventProperties, eventMeasurements);
|
||||||
{
|
_client.Flush();
|
||||||
if (eventProperties.ContainsKey(p.Key))
|
|
||||||
eventProperties[p.Key] = p.Value;
|
|
||||||
else
|
|
||||||
eventProperties.Add(p.Key, p.Value);
|
|
||||||
}
|
}
|
||||||
|
catch (Exception) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Dictionary<string, double> GetEventMeasures(IDictionary<string, double> measurements)
|
||||||
|
{
|
||||||
Dictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements);
|
Dictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements);
|
||||||
if (measurements != null)
|
if (measurements != null)
|
||||||
{
|
{
|
||||||
|
@ -87,13 +81,23 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
eventMeasurements.Add(m.Key, m.Value);
|
eventMeasurements.Add(m.Key, m.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return eventMeasurements;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
private Dictionary<string, string> GetEventProperties(IDictionary<string, string> properties)
|
||||||
{
|
{
|
||||||
_client.TrackEvent(command, eventProperties, eventMeasurements);
|
Dictionary<string, string> eventProperties = new Dictionary<string, string>(_commonProperties);
|
||||||
_client.Flush();
|
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);
|
||||||
}
|
}
|
||||||
catch (Exception) { }
|
}
|
||||||
|
return eventProperties;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,18 +117,13 @@ namespace Microsoft.DotNet.Cli
|
||||||
["test"] = TestCommand.Run
|
["test"] = TestCommand.Run
|
||||||
};
|
};
|
||||||
|
|
||||||
int exitCode = 100;
|
int exitCode;
|
||||||
|
var arguments = string.Empty;
|
||||||
string arguments = string.Empty;
|
|
||||||
|
|
||||||
|
|
||||||
Func<string[], int> builtIn;
|
Func<string[], int> builtIn;
|
||||||
if (builtIns.TryGetValue(command, out builtIn))
|
if (builtIns.TryGetValue(command, out builtIn))
|
||||||
{
|
{
|
||||||
exitCode = builtIn(appArgs.ToArray());
|
exitCode = builtIn(appArgs.ToArray());
|
||||||
|
arguments = string.Join(" ", appArgs);
|
||||||
appArgs.ToList().ForEach(a => { arguments += a + " "; });
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -140,7 +135,9 @@ namespace Microsoft.DotNet.Cli
|
||||||
exitCode = result.ExitCode;
|
exitCode = result.ExitCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
Telemetry.TrackCommand(
|
Telemetry telemetryClient = new Telemetry();
|
||||||
|
|
||||||
|
telemetryClient.TrackCommand(
|
||||||
command,
|
command,
|
||||||
new Dictionary<string, string>
|
new Dictionary<string, string>
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue