Framework/Listeners/Telemetry/UsageTelemetry.ps1
Set-StrictMode -Version Latest class UsageTelemetry: ListenerBase { [datetime] $ControlScanStartedOn; hidden UsageTelemetry() { [UsageTelemetryHelper]::Initialize() } hidden static [UsageTelemetry] $Instance = $null; static [UsageTelemetry] GetInstance() { if ( $null -eq [UsageTelemetry]::Instance ) { [UsageTelemetry]::Instance = [UsageTelemetry]::new(); } return [UsageTelemetry]::Instance } [void] RegisterEvents() { $this.UnregisterEvents(); $this.RegisterEvent([AzSdkRootEvent]::GenerateRunIdentifier, { $currentInstance = [UsageTelemetry]::GetInstance(); try { $currentInstance.SetRunIdentifier([AzSdkRootEventArgument] ($Event.SourceArgs | Select-Object -First 1)); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::ControlStarted, { try { $currentInstance = [UsageTelemetry]::GetInstance(); $currentInstance.ControlScanStartedOn = $Event.TimeGenerated } catch { $this.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::ControlCompleted, { try { $SVTEventContext = [SVTEventContext] ($Event.SourceArgs | Select-Object -First 1 ) $currentInstance = [UsageTelemetry]::GetInstance(); try{ $scannerVersion = $currentInstance.GetCurrentModuleVersion() }catch{ $scannerVersion = "NA" } $telemetryProperties = @{ "Feature" = $SVTEventContext.ResourceContext.FeatureName; "ControlId" = $SVTEventContext.ControlItem.ControlID; "ScannerVersion" = $scannerVersion } $telemetryMetrics = @{ [TelemetryKeys]::TimeTakenInMs = ([datetime]::Now - $currentInstance.ControlScanStartedOn).Milliseconds; } [UsageTelemetryHelper]::PublishEvent([TelemetryEvents]::ControlScanned, $telemetryProperties, $telemetryMetrics) } catch { $this.PublishException($_); } }); } } |