Framework/Listeners/Telemetry/ControlTelemetry.ps1
Set-StrictMode -Version Latest class ControlTelemetry: ListenerBase { [datetime] $ControlScanStartedOn; hidden ControlTelemetry() { [ControlTelemetryHelper]::Initialize() } hidden static [ControlTelemetry] $Instance = $null; static [ControlTelemetry] GetInstance() { if ( $null -eq [ControlTelemetry]::Instance ) { [ControlTelemetry]::Instance = [ControlTelemetry]::new(); } return [ControlTelemetry]::Instance } [void] RegisterEvents() { $this.UnregisterEvents(); $this.RegisterEvent([SVTEvent]::ControlStarted, { $currentInstance = [ControlTelemetry]::GetInstance(); try { $currentInstance.ControlScanStartedOn = $Event.TimeGenerated } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::ControlCompleted, { $currentInstance = [ControlTelemetry]::GetInstance(); try { $SVTEventContext = [SVTEventContext] ($Event.SourceArgs | Select-Object -First 1 ) $resourceName = $SVTEventContext.ResourceContext.ResourceName $utilityName = $SVTEventContext.ResourceContext.FeatureName $actionName = $SVTEventContext.ControlItem.MethodName $telemetryProperties = @{ [TelemetryKeys]::SubscriptionId = $SVTEventContext.ResourceContext.SubscriptionId; [TelemetryKeys]::SubscriptionName = $SVTEventContext.ResourceContext.SubscriptionName; [TelemetryKeys]::ResourceGroup = $SVTEventContext.ResourceContext.ResourceGroupName; [TelemetryKeys]::FeatureType = $SVTEventContext.ResourceContext.FeatureName; [TelemetryKeys]::ControlId = $SVTEventContext.ControlItem.ControlID; } if($SVTEventContext.ControlResults.Count -eq 1){ $telemetryMetrics = @{ [TelemetryKeys]::TimeTakenInMs = ([datetime]::Now - $currentInstance.ControlScanStartedOn).Milliseconds; } $telemetryProperties.Add([TelemetryKeys]::ResourceName, $SVTEventContext.ResourceContext.ResourceName); $telemetryProperties.Add([TelemetryKeys]::ControlStatus, $SVTEventContext.ControlResults[0].VerificationResult.ToString()); [ControlTelemetryHelper]::PublishEvent([TelemetryEvents]::ControlScanned, $telemetryProperties, $telemetryMetrics, $actionName, $utilityName) }elseif($SVTEventContext.ControlResults.Count -gt 1){ $telemetryMetrics = @{ [TelemetryKeys]::TimeTakenInMs = ([datetime]::Now - $currentInstance.ControlScanStartedOn).Milliseconds / $SVTEventContext.ControlResults.Count; } $SVTEventContext.ControlResults | Foreach-Object { $fullResourceName = $resourceName + $_.ChildResourceName $verificationResult = $_.VerificationResult.ToString() $telemetryPropertiesClone = $telemetryProperties.Clone() $telemetryPropertiesClone.Add([TelemetryKeys]::ResourceName, $fullResourceName) $telemetryPropertiesClone.Add([TelemetryKeys]::ControlStatus, $verificationResult) [ControlTelemetryHelper]::PublishEvent([TelemetryEvents]::ControlScanned, $telemetryPropertiesClone, $telemetryMetrics, $actionName, $utilityName) } } } catch { $currentInstance.PublishException($_); } }); } } |