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

Telemetry changes #17304

Merged
merged 18 commits into from May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
738252e
Stop sending telemetry for the type of entity being executed.
JamesWTruher Apr 23, 2022
45d2a55
Remove the telemetry send from the pipeline.
JamesWTruher Apr 27, 2022
61a4ce9
change parameter bitmap to something more sensible.
JamesWTruher May 5, 2022
fd29ad7
Turn off developer mode for telemetry send.
JamesWTruher May 9, 2022
8e501a1
Do not send the start up event out of the send metric method.
JamesWTruher May 11, 2022
a498b4e
first round of CodeFactor issues.
JamesWTruher May 11, 2022
1bf3cba
Use a default value for the node if we can't create the telemetry.uui…
JamesWTruher May 11, 2022
f6a0781
Fix test to reflect new behavior in startup telemetry send.
JamesWTruher May 11, 2022
d9d3f45
improve comment about why we have a default node identifier.
JamesWTruher May 13, 2022
fc935f0
Add more values to the parameter bitmap.
JamesWTruher May 16, 2022
d1dd59a
Turn off developer mode.
JamesWTruher May 16, 2022
2b8e572
Protect from a null Version property in the PSModuleInfo object.
JamesWTruher May 16, 2022
6e63d35
fix up code factor issue to use string instead of String.
JamesWTruher May 16, 2022
4edd2ef
Change get execution policy map to use a switch.
JamesWTruher May 16, 2022
3982243
another codefactor nit.
JamesWTruher May 16, 2022
6ac116c
change execution policy parsing back to if/else.
JamesWTruher May 16, 2022
964e50e
Add Az.Tools.Predictor to list of reported modules (requested by Stev…
JamesWTruher May 16, 2022
d02b9d3
Fix CodeFactor issues
daxian-dbw May 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,6 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

using System;
Expand Down Expand Up @@ -195,6 +194,57 @@ internal static int MaxNameLength()
"workingdirectory"
};

/// <summary>
/// These represent the parameters that are used when starting pwsh.
/// We can query in our telemetry to determine how pwsh was invoked.
/// </summary>
[Flags]
internal enum ParameterBitmap : long
{
Command = 0x00000001, // -Command | -c
ConfigurationName = 0x00000002, // -ConfigurationName | -config
CustomPipeName = 0x00000004, // -CustomPipeName
EncodedCommand = 0x00000008, // -EncodedCommand | -e | -ec
EncodedArgument = 0x00000010, // -EncodedArgument
ExecutionPolicy = 0x00000020, // -ExecutionPolicy | -ex | -ep
File = 0x00000040, // -File | -f
Help = 0x00000080, // -Help, -?, /?
InputFormat = 0x00000100, // -InputFormat | -inp | -if
Interactive = 0x00000200, // -Interactive | -i
Login = 0x00000400, // -Login | -l
MTA = 0x00000800, // -MTA
NoExit = 0x00001000, // -NoExit | -noe
NoLogo = 0x00002000, // -NoLogo | -nol
NonInteractive = 0x00004000, // -NonInteractive | -noni
NoProfile = 0x00008000, // -NoProfile | -nop
OutputFormat = 0x00010000, // -OutputFormat | -o | -of
SettingsFile = 0x00020000, // -SettingsFile | -settings
SSHServerMode = 0x00040000, // -SSHServerMode | -sshs
SocketServerMode = 0x00080000, // -SocketServerMode | -sockets
ServerMode = 0x00100000, // -ServerMode | -server
NamedPipeServerMode = 0x00200000, // -NamedPipeServerMode | -namedpipes
STA = 0x00400000, // -STA
Version = 0x00800000, // -Version | -v
WindowStyle = 0x01000000, // -WindowStyle | -w
WorkingDirectory = 0x02000000, // -WorkingDirectory | -wd
// Enum values for specified ExecutionPolicy
EPUnrestricted = 0x0000000100000000, // ExecutionPolicy unrestricted
EPRemoteSigned = 0x0000000200000000, // ExecutionPolicy remote signed
EPAllSigned = 0x0000000400000000, // ExecutionPolicy all signed
EPRestricted = 0x0000000800000000, // ExecutionPolicy restricted
EPDefault = 0x0000001000000000, // ExecutionPolicy default
EPBypass = 0x0000002000000000, // ExecutionPolicy bypass
EPUndefined = 0x0000004000000000, // ExecutionPolicy undefined
EPIncorrect = 0x0000008000000000, // ExecutionPolicy incorrect
}

internal ParameterBitmap ParametersUsed = 0;

internal double ParametersUsedAsDouble
{
get { return (double)ParametersUsed; }
}

[Conditional("DEBUG")]
private void AssertArgumentsParsed()
{
Expand Down Expand Up @@ -641,6 +691,53 @@ internal static string NormalizeFilePath(string path)
return Path.GetFullPath(path);
}

/// <summary>
/// Determine the execution policy based on the supplied string.
/// If the string doesn't match to any known execution policy, set it to incorrect.
/// </summary>
/// <param name="_executionPolicy">The value provided on the command line.</param>
/// <returns>The execution policy.</returns>
private static ParameterBitmap GetExecutionPolicy(string? _executionPolicy)
{
if (_executionPolicy is null)
{
return ParameterBitmap.EPUndefined;
}

ParameterBitmap executionPolicySetting = ParameterBitmap.EPIncorrect;

if (string.Equals(_executionPolicy, "default", StringComparison.OrdinalIgnoreCase))
{
executionPolicySetting = ParameterBitmap.EPDefault;
}
else if (string.Equals(_executionPolicy, "remotesigned", StringComparison.OrdinalIgnoreCase))
{
executionPolicySetting = ParameterBitmap.EPRemoteSigned;
}
else if (string.Equals(_executionPolicy, "bypass", StringComparison.OrdinalIgnoreCase))
{
executionPolicySetting = ParameterBitmap.EPBypass;
}
else if (string.Equals(_executionPolicy, "allsigned", StringComparison.OrdinalIgnoreCase))
{
executionPolicySetting = ParameterBitmap.EPAllSigned;
}
else if (string.Equals(_executionPolicy, "restricted", StringComparison.OrdinalIgnoreCase))
{
executionPolicySetting = ParameterBitmap.EPRestricted;
}
else if (string.Equals(_executionPolicy, "unrestricted", StringComparison.OrdinalIgnoreCase))
{
executionPolicySetting = ParameterBitmap.EPUnrestricted;
}
else if (string.Equals(_executionPolicy, "undefined", StringComparison.OrdinalIgnoreCase))
{
executionPolicySetting = ParameterBitmap.EPUndefined;
}

return executionPolicySetting;
}

private static bool MatchSwitch(string switchKey, string match, string smallestUnambiguousMatch)
{
Dbg.Assert(!string.IsNullOrEmpty(match), "need a value");
Expand Down Expand Up @@ -755,6 +852,7 @@ private void ParseHelper(string[] args)
_noInteractive = true;
_skipUserInit = true;
_noExit = false;
ParametersUsed |= ParameterBitmap.Version;
break;
}

Expand All @@ -763,48 +861,59 @@ private void ParseHelper(string[] args)
_showHelp = true;
_showExtendedHelp = true;
_abortStartup = true;
ParametersUsed |= ParameterBitmap.Help;
}
else if (MatchSwitch(switchKey, "login", "l"))
{
// On Windows, '-Login' does nothing.
// On *nix, '-Login' is already handled much earlier to improve startup performance, so we do nothing here.
ParametersUsed |= ParameterBitmap.Login;
}
else if (MatchSwitch(switchKey, "noexit", "noe"))
{
_noExit = true;
noexitSeen = true;
ParametersUsed |= ParameterBitmap.NoExit;
}
else if (MatchSwitch(switchKey, "noprofile", "nop"))
{
_skipUserInit = true;
ParametersUsed |= ParameterBitmap.NoProfile;
}
else if (MatchSwitch(switchKey, "nologo", "nol"))
{
_showBanner = false;
ParametersUsed |= ParameterBitmap.NoLogo;
}
else if (MatchSwitch(switchKey, "noninteractive", "noni"))
{
_noInteractive = true;
ParametersUsed |= ParameterBitmap.NonInteractive;
}
else if (MatchSwitch(switchKey, "socketservermode", "so"))
SteveL-MSFT marked this conversation as resolved.
Show resolved Hide resolved
{
_socketServerMode = true;
ParametersUsed |= ParameterBitmap.SocketServerMode;
}
else if (MatchSwitch(switchKey, "servermode", "s"))
{
_serverMode = true;
ParametersUsed |= ParameterBitmap.ServerMode;
}
else if (MatchSwitch(switchKey, "namedpipeservermode", "nam"))
{
_namedPipeServerMode = true;
ParametersUsed |= ParameterBitmap.NamedPipeServerMode;
}
else if (MatchSwitch(switchKey, "sshservermode", "sshs"))
{
_sshServerMode = true;
ParametersUsed |= ParameterBitmap.SSHServerMode;
}
else if (MatchSwitch(switchKey, "interactive", "i"))
{
_noInteractive = false;
ParametersUsed |= ParameterBitmap.Interactive;
}
else if (MatchSwitch(switchKey, "configurationname", "config"))
{
Expand All @@ -817,6 +926,7 @@ private void ParseHelper(string[] args)
}

_configurationName = args[i];
ParametersUsed |= ParameterBitmap.ConfigurationName;
}
else if (MatchSwitch(switchKey, "custompipename", "cus"))
{
Expand All @@ -841,14 +951,18 @@ private void ParseHelper(string[] args)
break;
}
#endif

_customPipeName = args[i];
ParametersUsed |= ParameterBitmap.CustomPipeName;
}
else if (MatchSwitch(switchKey, "command", "c"))
{
if (!ParseCommand(args, ref i, noexitSeen, false))
{
break;
}

ParametersUsed |= ParameterBitmap.Command;
}
else if (MatchSwitch(switchKey, "windowstyle", "w"))
{
Expand All @@ -875,6 +989,8 @@ private void ParseHelper(string[] args)
string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidWindowStyleArgument, args[i], e.Message));
break;
}

ParametersUsed |= ParameterBitmap.WindowStyle;
#endif
}
else if (MatchSwitch(switchKey, "file", "f"))
Expand All @@ -883,6 +999,8 @@ private void ParseHelper(string[] args)
{
break;
}

ParametersUsed |= ParameterBitmap.File;
}
#if DEBUG
else if (MatchSwitch(switchKey, "isswait", "isswait"))
Expand All @@ -894,14 +1012,18 @@ private void ParseHelper(string[] args)
{
ParseFormat(args, ref i, ref _outFormat, CommandLineParameterParserStrings.MissingOutputFormatParameter);
_outputFormatSpecified = true;
ParametersUsed |= ParameterBitmap.OutputFormat;
}
else if (MatchSwitch(switchKey, "inputformat", "inp") || MatchSwitch(switchKey, "if", "if"))
{
ParseFormat(args, ref i, ref _inFormat, CommandLineParameterParserStrings.MissingInputFormatParameter);
ParametersUsed |= ParameterBitmap.InputFormat;
}
else if (MatchSwitch(switchKey, "executionpolicy", "ex") || MatchSwitch(switchKey, "ep", "ep"))
{
ParseExecutionPolicy(args, ref i, ref _executionPolicy, CommandLineParameterParserStrings.MissingExecutionPolicyParameter);
ParametersUsed |= ParameterBitmap.ExecutionPolicy;
ParametersUsed |= GetExecutionPolicy(_executionPolicy);
}
else if (MatchSwitch(switchKey, "encodedcommand", "e") || MatchSwitch(switchKey, "ec", "e"))
{
Expand All @@ -910,13 +1032,17 @@ private void ParseHelper(string[] args)
{
break;
}

ParametersUsed |= ParameterBitmap.EncodedCommand;
}
else if (MatchSwitch(switchKey, "encodedarguments", "encodeda") || MatchSwitch(switchKey, "ea", "ea"))
{
if (!CollectArgs(args, ref i))
{
break;
}

ParametersUsed |= ParameterBitmap.EncodedArgument;
}
else if (MatchSwitch(switchKey, "settingsfile", "settings"))
{
Expand All @@ -925,6 +1051,8 @@ private void ParseHelper(string[] args)
{
break;
}

ParametersUsed |= ParameterBitmap.SettingsFile;
}
else if (MatchSwitch(switchKey, "sta", "sta"))
{
Expand All @@ -944,6 +1072,7 @@ private void ParseHelper(string[] args)
}

_staMode = true;
ParametersUsed |= ParameterBitmap.STA;
}
else if (MatchSwitch(switchKey, "mta", "mta"))
{
Expand All @@ -963,6 +1092,7 @@ private void ParseHelper(string[] args)
}

_staMode = false;
ParametersUsed |= ParameterBitmap.MTA;
}
else if (MatchSwitch(switchKey, "workingdirectory", "wo") || MatchSwitch(switchKey, "wd", "wd"))
{
Expand All @@ -975,6 +1105,7 @@ private void ParseHelper(string[] args)
}

_workingDirectory = args[i];
ParametersUsed |= ParameterBitmap.WorkingDirectory;
}
#if !UNIX
else if (MatchSwitch(switchKey, "removeworkingdirectorytrailingcharacter", "removeworkingdirectorytrailingcharacter"))
Expand All @@ -990,6 +1121,9 @@ private void ParseHelper(string[] args)
{
break;
}

// default to filename being the next argument.
ParametersUsed |= ParameterBitmap.File;
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Expand Up @@ -192,7 +192,7 @@ internal static int Start(string bannerText, string helpText)
// First check for and handle PowerShell running in a server mode.
if (s_cpp.ServerMode)
{
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("ServerMode");
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("ServerMode", s_cpp.ParametersUsedAsDouble);
ProfileOptimization.StartProfile("StartupProfileData-ServerMode");
StdIOProcessMediator.Run(
initialCommand: s_cpp.InitialCommand,
Expand All @@ -202,7 +202,7 @@ internal static int Start(string bannerText, string helpText)
}
else if (s_cpp.SSHServerMode)
{
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SSHServer");
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SSHServer", s_cpp.ParametersUsedAsDouble);
ProfileOptimization.StartProfile("StartupProfileData-SSHServerMode");
StdIOProcessMediator.Run(
initialCommand: s_cpp.InitialCommand,
Expand All @@ -212,15 +212,15 @@ internal static int Start(string bannerText, string helpText)
}
else if (s_cpp.NamedPipeServerMode)
{
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("NamedPipe");
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("NamedPipe", s_cpp.ParametersUsedAsDouble);
ProfileOptimization.StartProfile("StartupProfileData-NamedPipeServerMode");
RemoteSessionNamedPipeServer.RunServerMode(
configurationName: s_cpp.ConfigurationName);
exitCode = 0;
}
else if (s_cpp.SocketServerMode)
{
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SocketServerMode");
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SocketServerMode", s_cpp.ParametersUsedAsDouble);
ProfileOptimization.StartProfile("StartupProfileData-SocketServerMode");
HyperVSocketMediator.Run(
initialCommand: s_cpp.InitialCommand,
Expand Down Expand Up @@ -255,7 +255,7 @@ internal static int Start(string bannerText, string helpText)
PSHost.IsStdOutputRedirected = Console.IsOutputRedirected;

// Send startup telemetry for ConsoleHost startup
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("Normal");
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("Normal", s_cpp.ParametersUsedAsDouble);

exitCode = s_theConsoleHost.Run(s_cpp, false);
}
Expand Down