| |
| |
|
|
| using System; |
| using System.Collections.Generic; |
| using System.Diagnostics; |
| using System.IO; |
| using System.Linq; |
| using System.Management.Automation; |
| using System.Runtime.InteropServices; |
| using System.Threading; |
|
|
| using Microsoft.ApplicationInsights; |
| using Microsoft.ApplicationInsights.Metrics; |
| using Microsoft.ApplicationInsights.Channel; |
| using Microsoft.ApplicationInsights.Extensibility; |
| using Microsoft.ApplicationInsights.Extensibility.Implementation; |
|
|
| namespace Microsoft.PowerShell.Telemetry |
| { |
| |
| |
| |
| internal enum TelemetryType |
| { |
| |
| |
| |
| ApplicationType, |
|
|
| |
| |
| |
| |
| ModuleLoad, |
|
|
| |
| |
| |
| |
| WinCompatModuleLoad, |
|
|
| |
| |
| |
| |
| ExperimentalEngineFeatureDeactivation, |
|
|
| |
| |
| |
| |
| ExperimentalEngineFeatureActivation, |
|
|
| |
| |
| |
| ExperimentalFeatureUse, |
|
|
| |
| |
| |
| |
| |
| |
| ExperimentalModuleFeatureDeactivation, |
|
|
| |
| |
| |
| |
| |
| |
| ExperimentalModuleFeatureActivation, |
|
|
| |
| |
| |
| PowerShellCreate, |
|
|
| |
| |
| |
| RemoteSessionOpen, |
|
|
| |
| |
| |
| |
| |
| FeatureUse, |
| } |
|
|
| |
| |
| |
| internal class NameObscurerTelemetryInitializer : ITelemetryInitializer |
| { |
| |
| private const string _notavailable = "na"; |
|
|
| |
| |
| |
| |
| public void Initialize(ITelemetry telemetry) |
| { |
| telemetry.Context.Cloud.RoleName = _notavailable; |
| telemetry.Context.GetInternalContext().NodeName = _notavailable; |
| telemetry.Context.Cloud.RoleInstance = _notavailable; |
| } |
| } |
|
|
| |
| |
| |
| public static class ApplicationInsightsTelemetry |
| { |
| |
| internal const string s_subsystemRegistration = "Subsystem.Registration"; |
|
|
| |
| private const string _telemetryOptoutEnvVar = "POWERSHELL_TELEMETRY_OPTOUT"; |
|
|
| |
| |
| private const string _psCoreTelemetryKey = "d26a5ef4-d608-452c-a6b8-a4a55935f70d"; |
|
|
| |
| |
| private static readonly Guid _defaultNodeIdentifier = new Guid("2f998828-3f4a-4741-bf50-d11c6be42f50"); |
|
|
| |
| private const string Anonymous = "anonymous"; |
|
|
| |
| private const string AnonymousVersion = "0.0"; |
|
|
| |
| private const string NoTag = "n/a"; |
|
|
| |
| private const string _telemetryFailure = "TELEMETRY_FAILURE"; |
|
|
| |
| private static TelemetryClient s_telemetryClient { get; } |
|
|
| |
| private static string s_uniqueUserIdentifier { get; } |
|
|
| |
| private static string s_sessionId { get; } |
|
|
| |
| private static int s_startupEventSent = 0; |
|
|
| |
| |
| |
| |
| private static readonly HashSet<string> s_knownModules; |
| private static readonly HashSet<string> s_knownModuleTags; |
|
|
| private static readonly HashSet<string> s_knownSubsystemNames; |
|
|
| private static readonly string s_uuidPath; |
|
|
| |
| public static bool CanSendTelemetry { get; private set; } |
|
|
| |
| |
| |
| |
| |
| |
| |
| static ApplicationInsightsTelemetry() |
| { |
| |
| CanSendTelemetry = !Utils.GetEnvironmentVariableAsBool(name: _telemetryOptoutEnvVar, defaultValue: false) |
| && Platform.TryDeriveFromCache("telemetry.uuid", out s_uuidPath); |
|
|
| if (!CanSendTelemetry) |
| { |
| return; |
| } |
|
|
| s_sessionId = Guid.NewGuid().ToString(); |
| TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault(); |
| configuration.ConnectionString = "InstrumentationKey=" + _psCoreTelemetryKey; |
|
|
| |
| configuration.TelemetryChannel.DeveloperMode = false; |
|
|
| |
| configuration.TelemetryInitializers.Add(new NameObscurerTelemetryInitializer()); |
|
|
| s_telemetryClient = new TelemetryClient(configuration); |
|
|
| |
| s_knownModules = new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| { |
| "AADRM", |
| "activedirectory", |
| "adcsadministration", |
| "adcsdeployment", |
| "addsadministration", |
| "addsdeployment", |
| "adfs", |
| "adrms", |
| "adrmsadmin", |
| "agpm", |
| "AIShell", |
| "appbackgroundtask", |
| "applocker", |
| "appv", |
| "appvclient", |
| "appvsequencer", |
| "appvserver", |
| "appx", |
| "assignedaccess", |
| "Az", |
| "Az.Accounts", |
| "Az.Advisor", |
| "Az.Aks", |
| "Az.AlertsManagement", |
| "Az.AnalysisServices", |
| "Az.ApiManagement", |
| "Az.ApplicationInsights", |
| "Az.Attestation", |
| "Az.Automation", |
| "Az.Batch", |
| "Az.Billing", |
| "Az.Blueprint", |
| "Az.Cdn", |
| "Az.CognitiveServices", |
| "Az.Compute", |
| "Az.ContainerInstance", |
| "Az.ContainerRegistry", |
| "Az.DataBox", |
| "Az.DataFactory", |
| "Az.DataLakeAnalytics", |
| "Az.DataLakeStore", |
| "Az.DataMigration", |
| "Az.DataShare", |
| "Az.DeploymentManager", |
| "Az.DeviceProvisioningServices", |
| "Az.DevSpaces", |
| "Az.DevTestLabs", |
| "Az.Dns", |
| "Az.EventGrid", |
| "Az.EventHub", |
| "Az.FrontDoor", |
| "Az.GuestConfiguration", |
| "Az.HDInsight", |
| "Az.HealthcareApis", |
| "Az.IotCentral", |
| "Az.IotHub", |
| "Az.KeyVault", |
| "Az.Kusto", |
| "Az.LogicApp", |
| "Az.MachineLearning", |
| "Az.ManagedServiceIdentity", |
| "Az.ManagedServices", |
| "Az.ManagementPartner", |
| "Az.Maps", |
| "Az.MarketplaceOrdering", |
| "Az.Media", |
| "Az.MixedReality", |
| "Az.Monitor", |
| "Az.NetAppFiles", |
| "Az.Network", |
| "Az.NotificationHubs", |
| "Az.OperationalInsights", |
| "Az.Peering", |
| "Az.PolicyInsights", |
| "Az.PowerBIEmbedded", |
| "Az.PrivateDns", |
| "Az.RecoveryServices", |
| "Az.RedisCache", |
| "Az.Relay", |
| "Az.Reservations", |
| "Az.ResourceGraph", |
| "Az.Resources", |
| "Az.Search", |
| "Az.Security", |
| "Az.ServiceBus", |
| "Az.ServiceFabric", |
| "Az.SignalR", |
| "Az.Sql", |
| "Az.Storage", |
| "Az.StorageSync", |
| "Az.StorageTable", |
| "Az.StreamAnalytics", |
| "Az.Subscription", |
| "Az.Tools.Predictor", |
| "Az.TrafficManager", |
| "Az.Websites", |
| "Azs.Azurebridge.Admin", |
| "Azs.Backup.Admin", |
| "Azs.Commerce.Admin", |
| "Azs.Compute.Admin", |
| "Azs.Fabric.Admin", |
| "Azs.Gallery.Admin", |
| "Azs.Infrastructureinsights.Admin", |
| "Azs.Keyvault.Admin", |
| "Azs.Network.Admin", |
| "Azs.Storage.Admin", |
| "Azs.Subscriptions", |
| "Azs.Subscriptions.Admin", |
| "Azs.Update.Admin", |
| "AzStorageTable", |
| "Azure", |
| "Azure.AnalysisServices", |
| "Azure.Storage", |
| "AzureAD", |
| "AzureInformationProtection", |
| "AzureRM.Aks", |
| "AzureRM.AnalysisServices", |
| "AzureRM.ApiManagement", |
| "AzureRM.ApplicationInsights", |
| "AzureRM.Automation", |
| "AzureRM.Backup", |
| "AzureRM.Batch", |
| "AzureRM.Billing", |
| "AzureRM.Cdn", |
| "AzureRM.CognitiveServices", |
| "AzureRm.Compute", |
| "AzureRM.Compute.ManagedService", |
| "AzureRM.Consumption", |
| "AzureRM.ContainerInstance", |
| "AzureRM.ContainerRegistry", |
| "AzureRM.DataFactories", |
| "AzureRM.DataFactoryV2", |
| "AzureRM.DataLakeAnalytics", |
| "AzureRM.DataLakeStore", |
| "AzureRM.DataMigration", |
| "AzureRM.DeploymentManager", |
| "AzureRM.DeviceProvisioningServices", |
| "AzureRM.DevSpaces", |
| "AzureRM.DevTestLabs", |
| "AzureRm.Dns", |
| "AzureRM.EventGrid", |
| "AzureRM.EventHub", |
| "AzureRM.FrontDoor", |
| "AzureRM.HDInsight", |
| "AzureRm.Insights", |
| "AzureRM.IotCentral", |
| "AzureRM.IotHub", |
| "AzureRm.Keyvault", |
| "AzureRM.LocationBasedServices", |
| "AzureRM.LogicApp", |
| "AzureRM.MachineLearning", |
| "AzureRM.MachineLearningCompute", |
| "AzureRM.ManagedServiceIdentity", |
| "AzureRM.ManagementPartner", |
| "AzureRM.Maps", |
| "AzureRM.MarketplaceOrdering", |
| "AzureRM.Media", |
| "AzureRM.Network", |
| "AzureRM.NotificationHubs", |
| "AzureRM.OperationalInsights", |
| "AzureRM.PolicyInsights", |
| "AzureRM.PowerBIEmbedded", |
| "AzureRM.Profile", |
| "AzureRM.RecoveryServices", |
| "AzureRM.RecoveryServices.Backup", |
| "AzureRM.RecoveryServices.SiteRecovery", |
| "AzureRM.RedisCache", |
| "AzureRM.Relay", |
| "AzureRM.Reservations", |
| "AzureRM.ResourceGraph", |
| "AzureRM.Resources", |
| "AzureRM.Scheduler", |
| "AzureRM.Search", |
| "AzureRM.Security", |
| "AzureRM.ServerManagement", |
| "AzureRM.ServiceBus", |
| "AzureRM.ServiceFabric", |
| "AzureRM.SignalR", |
| "AzureRM.SiteRecovery", |
| "AzureRM.Sql", |
| "AzureRm.Storage", |
| "AzureRM.StorageSync", |
| "AzureRM.StreamAnalytics", |
| "AzureRM.Subscription", |
| "AzureRM.Subscription.Preview", |
| "AzureRM.Tags", |
| "AzureRM.TrafficManager", |
| "AzureRm.UsageAggregates", |
| "AzureRm.Websites", |
| "AzureRmStorageTable", |
| "bestpractices", |
| "bitlocker", |
| "bitstransfer", |
| "booteventcollector", |
| "branchcache", |
| "CimCmdlets", |
| "clusterawareupdating", |
| "CompatPowerShellGet", |
| "configci", |
| "ConfigurationManager", |
| "CompletionPredictor", |
| "DataProtectionManager", |
| "dcbqos", |
| "deduplication", |
| "defender", |
| "devicehealthattestation", |
| "dfsn", |
| "dfsr", |
| "dhcpserver", |
| "directaccessclient", |
| "directaccessclientcomponent", |
| "directaccessclientcomponents", |
| "dism", |
| "dnsclient", |
| "dnsserver", |
| "ElasticDatabaseJobs", |
| "EventTracingManagement", |
| "failoverclusters", |
| "fileserverresourcemanager", |
| "FIMAutomation", |
| "GPRegistryPolicy", |
| "grouppolicy", |
| "hardwarecertification", |
| "hcs", |
| "hgsattestation", |
| "hgsclient", |
| "hgsdiagnostics", |
| "hgskeyprotection", |
| "hgsserver", |
| "hnvdiagnostics", |
| "hostcomputeservice", |
| "hpc", |
| "HPC.ACM", |
| "HPC.ACM.API.PS", |
| "HPCPack2016", |
| "hyper-v", |
| "IISAdministration", |
| "international", |
| "ipamserver", |
| "iscsi", |
| "iscsitarget", |
| "ISE", |
| "kds", |
| "Microsoft.MBAM", |
| "Microsoft.MEDV", |
| "MgmtSvcAdmin", |
| "MgmtSvcConfig", |
| "MgmtSvcMySql", |
| "MgmtSvcSqlServer", |
| "Microsoft.AzureStack.ReadinessChecker", |
| "Microsoft.Crm.PowerShell", |
| "Microsoft.DiagnosticDataViewer", |
| "Microsoft.DirectoryServices.MetadirectoryServices.Config", |
| "Microsoft.Dynamics.Nav.Apps.Management", |
| "Microsoft.Dynamics.Nav.Apps.Tools", |
| "Microsoft.Dynamics.Nav.Ide", |
| "Microsoft.Dynamics.Nav.Management", |
| "Microsoft.Dynamics.Nav.Model.Tools", |
| "Microsoft.Dynamics.Nav.Model.Tools.Crm", |
| "Microsoft.EnterpriseManagement.Warehouse.Cmdlets", |
| "Microsoft.Medv.Administration.Commands.WorkspacePackager", |
| "Microsoft.PowerApps.Checker.PowerShell", |
| "Microsoft.PowerShell.Archive", |
| "Microsoft.PowerShell.ConsoleGuiTools", |
| "Microsoft.PowerShell.Core", |
| "Microsoft.PowerShell.Crescendo", |
| "Microsoft.PowerShell.Diagnostics", |
| "Microsoft.PowerShell.Host", |
| "Microsoft.PowerShell.LocalAccounts", |
| "Microsoft.PowerShell.Management", |
| "Microsoft.PowerShell.ODataUtils", |
| "Microsoft.PowerShell.Operation.Validation", |
| "Microsoft.PowerShell.PSAdapter", |
| "Microsoft.PowerShell.PSResourceGet", |
| "Microsoft.PowerShell.RemotingTools", |
| "Microsoft.PowerShell.SecretManagement", |
| "Microsoft.PowerShell.SecretStore", |
| "Microsoft.PowerShell.Security", |
| "Microsoft.PowerShell.TextUtility", |
| "Microsoft.PowerShell.Utility", |
| "Microsoft.SharePoint.Powershell", |
| "Microsoft.SystemCenter.ServiceManagementAutomation", |
| "Microsoft.Windows.ServerManager.Migration", |
| "Microsoft.WSMan.Management", |
| "Microsoft.Xrm.OnlineManagementAPI", |
| "Microsoft.Xrm.Tooling.CrmConnector.PowerShell", |
| "Microsoft.Xrm.Tooling.PackageDeployment", |
| "Microsoft.Xrm.Tooling.PackageDeployment.Powershell", |
| "Microsoft.Xrm.Tooling.Testing", |
| "MicrosoftPowerBIMgmt", |
| "MicrosoftPowerBIMgmt.Data", |
| "MicrosoftPowerBIMgmt.Profile", |
| "MicrosoftPowerBIMgmt.Reports", |
| "MicrosoftPowerBIMgmt.Workspaces", |
| "MicrosoftStaffHub", |
| "MicrosoftTeams", |
| "MIMPAM", |
| "mlSqlPs", |
| "MMAgent", |
| "MPIO", |
| "MsDtc", |
| "MSMQ", |
| "MSOnline", |
| "MSOnlineBackup", |
| "WmsCmdlets", |
| "WmsCmdlets3", |
| "NanoServerImageGenerator", |
| "NAVWebClientManagement", |
| "NetAdapter", |
| "NetConnection", |
| "NetEventPacketCapture", |
| "Netlbfo", |
| "Netldpagent", |
| "NetNat", |
| "Netqos", |
| "NetSecurity", |
| "NetSwitchtTeam", |
| "Nettcpip", |
| "Netwnv", |
| "NetworkConnectivity", |
| "NetworkConnectivityStatus", |
| "NetworkController", |
| "NetworkControllerDiagnostics", |
| "NetworkloadBalancingClusters", |
| "NetworkSwitchManager", |
| "NetworkTransition", |
| "NFS", |
| "NPS", |
| "OfficeWebapps", |
| "OperationsManager", |
| "PackageManagement", |
| "PartnerCenter", |
| "pcsvdevice", |
| "pef", |
| "Pester", |
| "pkiclient", |
| "platformidentifier", |
| "pnpdevice", |
| "PowerShellEditorServices", |
| "PowerShellGet", |
| "powershellwebaccess", |
| "printmanagement", |
| "ProcessMitigations", |
| "provisioning", |
| "PSDesiredStateConfiguration", |
| "PSDiagnostics", |
| "PSReadLine", |
| "PSScheduledJob", |
| "PSScriptAnalyzer", |
| "PSWorkflow", |
| "PSWorkflowUtility", |
| "RemoteAccess", |
| "RemoteDesktop", |
| "RemoteDesktopServices", |
| "ScheduledTasks", |
| "Secureboot", |
| "ServerCore", |
| "ServerManager", |
| "ServerManagerTasks", |
| "ServerMigrationcmdlets", |
| "ServiceFabric", |
| "Microsoft.Online.SharePoint.PowerShell", |
| "shieldedvmdatafile", |
| "shieldedvmprovisioning", |
| "shieldedvmtemplate", |
| "SkypeOnlineConnector", |
| "SkypeForBusinessHybridHealth", |
| "smbshare", |
| "smbwitness", |
| "smisconfig", |
| "softwareinventorylogging", |
| "SPFAdmin", |
| "Microsoft.SharePoint.MigrationTool.PowerShell", |
| "sqlps", |
| "SqlServer", |
| "StartLayout", |
| "StartScreen", |
| "Storage", |
| "StorageDsc", |
| "storageqos", |
| "Storagereplica", |
| "Storagespaces", |
| "Syncshare", |
| "System.Center.Service.Manager", |
| "TLS", |
| "TroubleshootingPack", |
| "TrustedPlatformModule", |
| "UEV", |
| "UpdateServices", |
| "UserAccessLogging", |
| "vamt", |
| "VirtualMachineManager", |
| "vpnclient", |
| "WasPSExt", |
| "WDAC", |
| "WDS", |
| "WebAdministration", |
| "WebAdministrationDsc", |
| "WebApplicationProxy", |
| "WebSites", |
| "Whea", |
| "WhiteboardAdmin", |
| "WindowsDefender", |
| "WindowsDefenderDsc", |
| "WindowsDeveloperLicense", |
| "WindowsDiagnosticData", |
| "WindowsErrorReporting", |
| "WindowServerRackup", |
| "WindowsSearch", |
| "WindowsServerBackup", |
| "WindowsUpdate", |
| "WinGetCommandNotFound", |
| "wsscmdlets", |
| "wsssetup", |
| "wsus", |
| "xActiveDirectory", |
| "xBitLocker", |
| "xDefender", |
| "xDhcpServer", |
| "xDismFeature", |
| "xDnsServer", |
| "xHyper-V", |
| "xHyper-VBackup", |
| "xPSDesiredStateConfiguration", |
| "xSmbShare", |
| "xSqlPs", |
| "xStorage", |
| "xWebAdministration", |
| "xWindowsUpdate", |
| }; |
|
|
| |
| s_knownModuleTags = new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| { |
| "CrescendoBuilt", |
| }; |
|
|
| s_uniqueUserIdentifier = GetUniqueIdentifier().ToString(); |
| s_knownSubsystemNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| { |
| "Completion", |
| "General Feedback", |
| "Windows Package Manager - WinGet", |
| "Az Predictor" |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| internal static void SendModuleTelemetryMetric(TelemetryType telemetryType, PSModuleInfo moduleInfo) |
| { |
| if (!CanSendTelemetry) |
| { |
| return; |
| } |
|
|
| |
| |
| |
| try |
| { |
| string allowedModuleName = GetModuleName(moduleInfo.Name); |
| string allowedModuleVersion = allowedModuleName == Anonymous ? AnonymousVersion : moduleInfo.Version?.ToString(); |
| var allowedModuleTags = moduleInfo.Tags.Where(t => s_knownModuleTags.Contains(t)).Distinct(); |
| string allowedModuleTagString = allowedModuleTags.Any() ? string.Join(',', allowedModuleTags) : NoTag; |
|
|
| s_telemetryClient. |
| GetMetric(new MetricIdentifier(string.Empty, telemetryType.ToString(), "uuid", "SessionId", "ModuleName", "Version", "Tag")). |
| TrackValue(metricValue: 1.0, s_uniqueUserIdentifier, s_sessionId, allowedModuleName, allowedModuleVersion, allowedModuleTagString); |
| } |
| catch |
| { |
| |
| } |
|
|
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| internal static void SendModuleTelemetryMetric(TelemetryType telemetryType, string moduleName) |
| { |
| if (!CanSendTelemetry) |
| { |
| return; |
| } |
|
|
| try |
| { |
| string allowedModuleName = GetModuleName(moduleName); |
| s_telemetryClient.GetMetric(telemetryType.ToString(), "uuid", "SessionId", "ModuleName", "Version").TrackValue(metricValue: 1.0, s_uniqueUserIdentifier, s_sessionId, allowedModuleName, AnonymousVersion); |
| } |
| catch |
| { |
| |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| internal static void SendTelemetryMetric(TelemetryType metricId, string data, double value = 1.0) |
| { |
| if (!CanSendTelemetry) |
| { |
| return; |
| } |
|
|
| |
| Debug.Assert(metricId != TelemetryType.ModuleLoad, "ModuleLoad should be handled by SendModuleTelemetryMetric."); |
| Debug.Assert(metricId != TelemetryType.WinCompatModuleLoad, "WinCompatModuleLoad should be handled by SendModuleTelemetryMetric."); |
|
|
| string metricName = metricId.ToString(); |
| try |
| { |
| switch (metricId) |
| { |
| case TelemetryType.ApplicationType: |
| case TelemetryType.PowerShellCreate: |
| case TelemetryType.RemoteSessionOpen: |
| case TelemetryType.ExperimentalEngineFeatureActivation: |
| case TelemetryType.ExperimentalEngineFeatureDeactivation: |
| case TelemetryType.ExperimentalFeatureUse: |
| case TelemetryType.FeatureUse: |
| s_telemetryClient.GetMetric(metricName, "uuid", "SessionId", "Detail").TrackValue(metricValue: value, s_uniqueUserIdentifier, s_sessionId, data); |
| break; |
| case TelemetryType.ExperimentalModuleFeatureActivation: |
| case TelemetryType.ExperimentalModuleFeatureDeactivation: |
| string experimentalFeatureName = GetExperimentalFeatureName(data); |
| s_telemetryClient.GetMetric(metricName, "uuid", "SessionId", "Detail").TrackValue(metricValue: value, s_uniqueUserIdentifier, s_sessionId, experimentalFeatureName); |
| break; |
| } |
| } |
| catch |
| { |
| |
| |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| internal static void SendUseTelemetry(string featureName, string detail, double value = 1.0) |
| { |
| if (!CanSendTelemetry) |
| { |
| return; |
| } |
|
|
| |
| if (featureName is null || detail is null || featureName.Length > 33 || detail.Length > 33) |
| { |
| return; |
| } |
|
|
| if (string.Compare(featureName, s_subsystemRegistration, true) == 0) |
| { |
| SendTelemetryMetric(TelemetryType.FeatureUse, string.Join(":", featureName, GetSubsystemName(detail)), value); |
| } |
| else |
| { |
| SendTelemetryMetric(TelemetryType.FeatureUse, string.Join(":", featureName, detail), value); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| internal static void SendExperimentalUseData(string featureName, string detail) |
| { |
| if (!CanSendTelemetry) |
| { |
| return; |
| } |
|
|
| SendTelemetryMetric(TelemetryType.ExperimentalFeatureUse, string.Join(":", featureName, detail)); |
| } |
|
|
| |
| private static string GetExperimentalFeatureName(string featureNameToValidate) |
| { |
| |
| |
| int lastDotIndex = featureNameToValidate.LastIndexOf('.'); |
| string moduleName = featureNameToValidate.Substring(0, lastDotIndex); |
| if (s_knownModules.Contains(moduleName)) |
| { |
| return featureNameToValidate; |
| } |
|
|
| return Anonymous; |
| } |
|
|
| |
| private static string GetSubsystemName(string subsystemNameToValidate) |
| { |
| if (s_knownSubsystemNames.Contains(subsystemNameToValidate)) |
| { |
| return subsystemNameToValidate; |
| } |
|
|
| return Anonymous; |
| } |
|
|
| |
| private static string GetModuleName(string moduleNameToValidate) |
| { |
| if (s_knownModules.Contains(moduleNameToValidate)) |
| { |
| return moduleNameToValidate; |
| } |
|
|
| return Anonymous; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| internal static void SendPSCoreStartupTelemetry(string mode, double parametersUsed) |
| { |
| |
| if (Interlocked.CompareExchange(ref s_startupEventSent, 1, 0) == 1) |
| { |
| return; |
| } |
|
|
| if (!CanSendTelemetry) |
| { |
| return; |
| } |
|
|
| |
| var properties = new Dictionary<string, string>(); |
|
|
| |
| var parameters = new Dictionary<string, double>(); |
|
|
| |
| |
| |
| var channel = Environment.GetEnvironmentVariable("POWERSHELL_DISTRIBUTION_CHANNEL"); |
|
|
| |
| properties.Add("SessionId", s_sessionId); |
| properties.Add("UUID", s_uniqueUserIdentifier); |
| properties.Add("GitCommitID", PSVersionInfo.GitCommitId); |
| properties.Add("OSDescription", RuntimeInformation.OSDescription); |
| properties.Add("RuntimeIdentifier", RuntimeInformation.RuntimeIdentifier); |
| properties.Add("OSChannel", string.IsNullOrEmpty(channel) ? "unknown" : channel); |
| properties.Add("StartMode", string.IsNullOrEmpty(mode) ? "unknown" : mode); |
|
|
| |
| parameters.Add("Param", parametersUsed); |
| try |
| { |
| s_telemetryClient.TrackEvent("ConsoleHostStartup", properties, parameters); |
| } |
| catch |
| { |
| |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| private static bool TryGetIdentifier(string telemetryFilePath, out Guid id) |
| { |
| if (File.Exists(telemetryFilePath)) |
| { |
| |
| const int GuidSize = 16; |
| byte[] buffer = new byte[GuidSize]; |
| try |
| { |
| using (FileStream fs = new FileStream(telemetryFilePath, FileMode.Open, FileAccess.Read)) |
| { |
| |
| int n = fs.Read(buffer, 0, GuidSize); |
| if (n == GuidSize) |
| { |
| |
| id = new Guid(buffer); |
| if (id != Guid.Empty) |
| { |
| return true; |
| } |
| } |
| } |
| } |
| catch |
| { |
| |
| } |
| } |
|
|
| id = Guid.Empty; |
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| private static Guid CreateUniqueIdentifierAndFile(string telemetryFilePath) |
| { |
| |
| Guid id = Guid.Empty; |
| if (TryGetIdentifier(telemetryFilePath, out id)) |
| { |
| return id; |
| } |
|
|
| |
| |
| bool attemptFileCreation = true; |
| try |
| { |
| Directory.CreateDirectory(Path.GetDirectoryName(telemetryFilePath)); |
| } |
| catch |
| { |
| |
| |
| |
| attemptFileCreation = false; |
| } |
|
|
| |
| |
| if (attemptFileCreation) |
| { |
| try |
| { |
| id = Guid.NewGuid(); |
| File.WriteAllBytes(telemetryFilePath, id.ToByteArray()); |
| return id; |
| } |
| catch |
| { |
| |
| s_telemetryClient.GetMetric(_telemetryFailure, "Detail").TrackValue(1, "saveuuid"); |
| } |
| } |
|
|
| |
| id = _defaultNodeIdentifier; |
| return id; |
| } |
|
|
| |
| |
| |
| |
| |
| private static Guid GetUniqueIdentifier() |
| { |
| |
| |
| Guid id = Guid.Empty; |
| if (TryGetIdentifier(s_uuidPath, out id)) |
| { |
| return id; |
| } |
|
|
| |
| |
| |
| try |
| { |
| |
| using var m = new Mutex(true, "CreateUniqueUserId"); |
| m.WaitOne(); |
| try |
| { |
| return CreateUniqueIdentifierAndFile(s_uuidPath); |
| } |
| finally |
| { |
| m.ReleaseMutex(); |
| } |
| } |
| catch (Exception) |
| { |
| |
| |
| s_telemetryClient.GetMetric(_telemetryFailure, "Detail").TrackValue(1, "mutex"); |
| } |
|
|
| |
| CanSendTelemetry = false; |
| return id; |
| } |
| } |
| } |
|
|