Framework/Listeners/Telemetry/UsageTelemetryHelper.ps1
Set-StrictMode -Version Latest class UsageTelemetryHelper { hidden static [string] $TelemetrySessionId; hidden static [psobject] $EventBaseObject; hidden static $BaseEvent; static [void] Initialize() { if ($null -eq [UsageTelemetryHelper]::TelemetrySessionId) { [UsageTelemetryHelper]::TelemetrySessionId = [System.Guid]::NewGuid().ToString() } } hidden static [string] Mask([string] $toMask) { $sha384 = [System.Security.Cryptography.SHA384Managed]::new() $maskBytes = [System.Text.Encoding]::UTF8.GetBytes($toMask) $maskBytes = $sha384.ComputeHash($maskBytes) $sha384.Dispose() $take = 16 $sb = [System.Text.StringBuilder]::new($take) for($i = 0; $i -lt ($take/2); $i++){ $x = $sb.Append($maskBytes[$i].ToString("x2")) } return $sb.ToString(); } hidden static [psobject] GetEventBaseObject([string] $eventName) { $eventObj = "" | Select-Object data, iKey, name, tags, time $eventObj.iKey = [Constants]::UsageTelemetryKey $eventObj.name = "Microsoft.ApplicationInsights." + [Constants]::UsageTelemetryKey.Replace("-", "") + ".Event" $eventObj.time = "{{TIME}}" $eventObj.tags = "" | Select-Object ai.internal.sdkVersion $eventObj.tags.'ai.internal.sdkVersion' = "dotnet: 2.1.0.26048" $eventObj.data = "" | Select-Object baseData, baseType $eventObj.data.baseType = "EventData" $eventObj.data.baseData = "" | Select-Object ver, name, measurements, properties $eventObj.data.baseData.ver = 2 $eventObj.data.baseData.name = $eventName $eventObj.data.baseData.measurements = New-Object 'system.collections.generic.dictionary[string,double]' $eventObj.data.baseData.properties = New-Object 'system.collections.generic.dictionary[string,string]' [UsageTelemetryHelper]::SetCommonProperties($eventObj) return $eventObj; } hidden static [void] SetCommonProperties([psobject] $eventObj) { $eventObj.data.baseData.properties.Add("PSVersion", ` (Get-Variable -Name PSVersionTable).Value.PSVersion.ToString()) } static [void] PublishEvent([string] $eventName, [hashtable] $properties, [hashtable] $metrics) { try { $azsdkSettings = [ConfigurationManager]::GetAzSdkSettings(); if($azsdkSettings.UsageTelemetryLevel -ne "Anonymous") { return; } $eventObj = [UsageTelemetryHelper]::GetEventBaseObject($eventName); $metrics.Keys | ForEach-Object { try { $eventObj.data.baseData.measurements.Add($_ , $metrics[$_]) } catch {} } $properties.Keys | ForEach-Object { try { $eventObj.data.baseData.properties.Add($_ , $properties[$_]) } catch {} } $eventJson = $(ConvertTo-Json $eventObj -Depth 100 -Compress).Replace("{{TIME}}", [datetime]::UtcNow.ToString("o")) Invoke-WebRequest -Uri "https://dc.services.visualstudio.com/v2/track" ` -Method Post ` -ContentType "application/x-json-stream" ` -Body $eventJson ` -UseBasicParsing | Out-Null } catch { } } } |