Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Environment Variable override of telemetry #6063

Merged
merged 4 commits into from
Jan 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 TelemetryOptoutEnvVar = "POWERSHELL_TELEMETRY_OPTOUT";

// The path to the semaphore file which enables telemetry
private static string TelemetrySemaphoreFilePath = Path.Combine(
Expand All @@ -42,6 +43,28 @@ static ApplicationInsightsTelemetry()
TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = _developerMode;
}

private 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;
}
}

/// <summary>
/// Send the telemetry
/// </summary>
Expand All @@ -50,14 +73,18 @@ private static void SendTelemetry(string eventName, Dictionary<string,string>pay
try
{
// if the semaphore file exists, try to send telemetry
if (Utils.NativeFileExists(TelemetrySemaphoreFilePath))
var enabled = Utils.NativeFileExists(TelemetrySemaphoreFilePath) && !GetEnvironmentVariableAsBool(TelemetryOptoutEnvVar, 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)
{
Expand Down