Skip to content
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