From c3e3663a897c993dd695512633071d40c1eeb152 Mon Sep 17 00:00:00 2001 From: Daniel Llewellyn Date: Mon, 29 Jan 2018 19:16:11 +0000 Subject: [PATCH 1/4] Add Environment Variable override of telemetry Inspired by dotnet cli, and allows for Telemetry optout on packaing systems such as AppImage and Snapcraft where the filesystem is immutable. --- .../host/msh/Telemetry.cs | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs index 58987b322a3..9b6bbbfbfd7 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs @@ -21,6 +21,7 @@ internal static class ApplicationInsightsTelemetry // The name of the file by when present in $PSHOME will enable telemetry. // If this file is not present, no telemetry will be sent. private const string TelemetrySemaphoreFilename = "DELETE_ME_TO_DISABLE_CONSOLEHOST_TELEMETRY"; + private const string TelemetryOptout = "POWERSHELL_TELEMETRY_OPTOUT"; // The path to the semaphore file which enables telemetry private static string TelemetrySemaphoreFilePath = Path.Combine( @@ -42,6 +43,28 @@ static ApplicationInsightsTelemetry() TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = _developerMode; } + internal static 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; + } + } + /// /// Send the telemetry /// @@ -50,14 +73,18 @@ private static void SendTelemetry(string eventName, Dictionarypay try { // if the semaphore file exists, try to send telemetry - if (Utils.NativeFileExists(TelemetrySemaphoreFilePath)) + var Enabled = Utils.NativeFileExists(TelemetrySemaphoreFilePath) && !GetEnvironmentVariableAsBool(TelemetryOptout, false); + + if ( ! Enabled ) + { + return; + } + + if ( _telemetryClient == null ) { - if ( _telemetryClient == null ) - { - _telemetryClient = new TelemetryClient(); - } - _telemetryClient.TrackEvent(eventName, payload, null); + _telemetryClient = new TelemetryClient(); } + _telemetryClient.TrackEvent(eventName, payload, null); } catch (Exception) { From a54f7dce80e6f7c895c8f718d6571ee717364855 Mon Sep 17 00:00:00 2001 From: Daniel Llewellyn Date: Tue, 30 Jan 2018 16:30:00 -0800 Subject: [PATCH 2/4] Refactor TelemetryOptout constant Rename the constant to to better reflect its purpose. --- src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs index 9b6bbbfbfd7..a149ee60643 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs @@ -21,7 +21,7 @@ internal static class ApplicationInsightsTelemetry // The name of the file by when present in $PSHOME will enable telemetry. // If this file is not present, no telemetry will be sent. private const string TelemetrySemaphoreFilename = "DELETE_ME_TO_DISABLE_CONSOLEHOST_TELEMETRY"; - private const string TelemetryOptout = "POWERSHELL_TELEMETRY_OPTOUT"; + private const string TelemetryOptoutEnvVar = "POWERSHELL_TELEMETRY_OPTOUT"; // The path to the semaphore file which enables telemetry private static string TelemetrySemaphoreFilePath = Path.Combine( @@ -73,7 +73,7 @@ private static void SendTelemetry(string eventName, Dictionarypay try { // if the semaphore file exists, try to send telemetry - var Enabled = Utils.NativeFileExists(TelemetrySemaphoreFilePath) && !GetEnvironmentVariableAsBool(TelemetryOptout, false); + var Enabled = Utils.NativeFileExists(TelemetrySemaphoreFilePath) && !GetEnvironmentVariableAsBool(TelemetryOptoutEnvVar, false); if ( ! Enabled ) { From 611b05bf3f69dfa61acd93a92ff78489bd11c057 Mon Sep 17 00:00:00 2001 From: Daniel Llewellyn Date: Tue, 30 Jan 2018 16:47:22 -0800 Subject: [PATCH 3/4] Fix coding style Rename `Enabled` to `enabled` for conformance to camelCase local variables. Remove excess whitespace in `if` constructs. --- src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs index a149ee60643..382ac3f9896 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs @@ -73,14 +73,14 @@ private static void SendTelemetry(string eventName, Dictionarypay try { // if the semaphore file exists, try to send telemetry - var Enabled = Utils.NativeFileExists(TelemetrySemaphoreFilePath) && !GetEnvironmentVariableAsBool(TelemetryOptoutEnvVar, false); + var enabled = Utils.NativeFileExists(TelemetrySemaphoreFilePath) && !GetEnvironmentVariableAsBool(TelemetryOptoutEnvVar, false); - if ( ! Enabled ) + if (!enabled) { return; } - if ( _telemetryClient == null ) + if (_telemetryClient == null) { _telemetryClient = new TelemetryClient(); } From 90c6d8a5ea2d699e6e9c2a547c846b44c33c0c00 Mon Sep 17 00:00:00 2001 From: Daniel Llewellyn Date: Wed, 31 Jan 2018 09:07:50 -0800 Subject: [PATCH 4/4] Change signature of GetEnvironmentVariableAsBool Change visibility of GetEnvironmentVariableAsBool from `internal static` to `private static` --- src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs index 382ac3f9896..ac035ba7305 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs @@ -43,7 +43,7 @@ static ApplicationInsightsTelemetry() TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = _developerMode; } - internal static bool GetEnvironmentVariableAsBool(string name, bool defaultValue) { + private static bool GetEnvironmentVariableAsBool(string name, bool defaultValue) { var str = Environment.GetEnvironmentVariable(name); if (string.IsNullOrEmpty(str)) {