navcontainerhelper/TelemetryHelper.ps1

267 строки
12 KiB
PowerShell
Исходник Обычный вид История

2021-07-02 12:51:53 +03:00
function FormatValue {
2021-07-01 22:44:04 +03:00
Param(
$value
)
2021-08-29 13:13:08 +03:00
if ($value -eq $null) {
"[null]"
}
elseif ($value -is [switch]) {
2021-08-29 09:00:06 +03:00
$value.IsPresent
2021-07-01 22:44:04 +03:00
}
elseif ($value -is [boolean]) {
2021-08-29 09:00:06 +03:00
$value
2021-07-01 22:44:04 +03:00
}
elseif ($value -is [SecureString]) {
"[SecureString]"
}
elseif ($value -is [PSCredential]) {
"[PSCredential]"
}
elseif ($value -is [string]) {
if (($value -like "https:*" -or $value -like "http:*") -and ($value.Contains('?'))) {
2021-08-29 09:00:06 +03:00
"$($value.Split('?')[0])?[parameters]"
2021-07-01 22:44:04 +03:00
}
else {
2021-08-29 09:00:06 +03:00
"$value"
2021-07-01 22:44:04 +03:00
}
}
elseif ($value -is [System.Collections.IDictionary]) {
$str = ""
$value.GetEnumerator() | ForEach-Object {
if ($str) { $str += ", " } else { $str = "{ " }
$str += "$($_.Key): $(FormatValue -value $_.Value)"
}
"$str }"
}
elseif ($value -is [System.Collections.IEnumerable]
) {
$str = ""
$value.GetEnumerator() | ForEach-Object {
if ($str) { $str += ", " } else { $str = "[ " }
$str += "$(FormatValue -value $_)"
}
"$str ]"
}
else {
2021-08-29 09:00:06 +03:00
$value
2021-07-01 22:44:04 +03:00
}
}
function AddTelemetryProperty {
Param(
$telemetryScope,
$key,
$value
)
if ($telemetryScope) {
2021-08-29 09:00:06 +03:00
$value = FormatValue -value $value
if ($telemetry.Debug) {
Write-Host -ForegroundColor Yellow "Telemetry scope $($telemetryScope.Name), add property $key = '$value', type = $($value.GetType())"
}
2021-08-20 15:09:03 +03:00
if ($telemetryScope.properties.ContainsKey($Key)) {
2021-08-29 09:00:06 +03:00
$telemetryScope.properties."$key" += "`n$value"
2021-08-20 15:09:03 +03:00
}
else {
2021-08-29 09:00:06 +03:00
$telemetryScope.properties.Add($key, $value)
2021-08-20 15:09:03 +03:00
}
2021-07-01 22:44:04 +03:00
}
}
function InitTelemetryScope {
Param(
[string] $name,
2021-08-29 09:00:06 +03:00
[switch] $always,
2021-07-01 22:44:04 +03:00
[string[]] $includeParameters = @(),
$parameterValues = $null
)
2021-08-29 09:00:06 +03:00
$includeParameters = "*"
2021-08-26 16:28:48 +03:00
if ($telemetry.Client) {
2021-08-04 08:31:56 +03:00
if ($bcContainerHelperConfig.TelemetryConnectionString) {
2021-08-26 16:28:48 +03:00
if ($telemetry.Client.TelemetryConfiguration.DisableTelemetry -or $telemetry.Client.TelemetryConfiguration.ConnectionString -ne $bcContainerHelperConfig.TelemetryConnectionString) {
2021-08-20 15:09:03 +03:00
if ($bcContainerHelperConfig.TelemetryConnectionString) {
try {
2021-08-26 16:28:48 +03:00
$telemetry.Client.TelemetryConfiguration.ConnectionString = $bcContainerHelperConfig.TelemetryConnectionString
$telemetry.Client.TelemetryConfiguration.DisableTelemetry = $false
2021-08-29 09:00:06 +03:00
if ($telemetry.Debug) {
Write-Host -ForegroundColor Yellow "Telemetry client initialized"
}
2021-08-20 15:09:03 +03:00
}
catch {
2021-08-26 16:28:48 +03:00
$telemetry.Client.TelemetryConfiguration.DisableTelemetry = $true
2021-08-20 15:09:03 +03:00
}
}
2021-07-02 12:51:53 +03:00
}
2021-08-29 09:00:06 +03:00
if ($telemetry.Client.IsEnabled() -and ($always -or ($telemetry.CorrelationId -eq ""))) {
2021-08-29 20:41:17 +03:00
$CorrelationId = [GUID]::NewGuid().ToString()
Start-Transcript -Path (Join-Path $env:TEMP $CorrelationId) | Out-Null
2021-08-29 09:00:06 +03:00
if ($telemetry.Debug) {
Write-Host -ForegroundColor Yellow "Init telemetry scope $name"
}
if ($telemetry.TopId -eq "") { $telemetry.TopId = $CorrelationId }
2021-08-04 08:31:56 +03:00
$scope = @{
"Name" = $name
"StartTime" = [DateTime]::Now
"SeverityLevel" = 1
"Properties" = [Collections.Generic.Dictionary[string, string]]::new()
2021-08-29 09:00:06 +03:00
"CorrelationId" = $CorrelationId
"ParentId" = $telemetry.CorrelationId
"TopId" = $telemetry.TopId
2021-08-04 08:31:56 +03:00
"Emitted" = $false
}
2021-08-29 09:00:06 +03:00
$telemetry.CorrelationId = $CorrelationId
2021-08-04 08:31:56 +03:00
if ($includeParameters) {
2021-08-29 13:13:08 +03:00
$parameterValues.GetEnumerator() | ForEach-Object {
2021-08-04 08:31:56 +03:00
$includeParameter = $false
$key = $_.key
$includeParameters | ForEach-Object { if ($key -like $_) { $includeParameter = $true } }
if ($includeParameter) {
AddTelemetryProperty -telemetryScope $scope -key "Parameter[$Key]" -value $_.Value
}
2021-07-02 12:51:53 +03:00
}
2021-07-01 22:44:04 +03:00
}
2021-08-04 08:31:56 +03:00
AddTelemetryProperty -telemetryScope $scope -key "BcContainerHelperVersion" -value $BcContainerHelperVersion
AddTelemetryProperty -telemetryScope $scope -key "IsAdministrator" -value $isAdministrator
AddTelemetryProperty -telemetryScope $scope -key "StackTrace" -value (Get-PSCallStack | % { "$($_.Command) at $($_.Location)" }) -join "`n"
$scope
2021-07-01 22:44:04 +03:00
}
}
2021-08-04 08:31:56 +03:00
else {
2021-08-26 16:28:48 +03:00
$telemetry.Client.TelemetryConfiguration.DisableTelemetry = $true
2021-08-04 08:31:56 +03:00
}
2021-07-01 22:44:04 +03:00
}
}
function TrackTrace {
Param(
$telemetryScope
)
2021-07-05 07:36:37 +03:00
if ($telemetryScope -and !$telemetryScope.Emitted) {
2021-08-29 09:00:06 +03:00
if ($telemetry.Client.IsEnabled() -and ($telemetryScope.CorrelationId -eq $telemetry.CorrelationId)) {
if ($telemetry.Debug) {
Write-Host -ForegroundColor Yellow "Emit telemetry trace, scope $($telemetryScope.Name)"
}
$telemetry.CorrelationId = $telemetryScope.ParentId
if ($telemetry.CorrelationId -eq "") {
$telemetry.TopId = ""
}
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "A" }
2021-07-05 07:07:07 +03:00
$telemetryScope.Properties.Add("Duration", [DateTime]::Now.Subtract($telemetryScope.StartTime).TotalSeconds)
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "B" }
2021-08-29 09:00:06 +03:00
try {
Stop-Transcript | Out-Null
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "C" }
2021-08-29 09:00:06 +03:00
$transcript = (@(Get-Content -Path (Join-Path $env:TEMP $telemetryScope.CorrelationId)) | select -skip 18 | select -skiplast 4) -join "`n"
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "D" }
2021-08-29 18:55:08 +03:00
if ($transcript.Length -gt 28000) {
$transcript = "$($transcript.SubString(0,14000))`n`n...`n`n$($transcript.SubString($transcript.Length-14000))"
2021-08-26 16:28:48 +03:00
}
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "E $($transcript.Length)" }
2021-08-29 09:00:06 +03:00
Remove-Item -Path (Join-Path $env:TEMP $telemetryScope.CorrelationId)
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "F" }
2021-08-29 09:00:06 +03:00
}
catch {
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "G" }
2021-08-29 09:00:06 +03:00
$transcript = ""
}
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "H" }
2021-08-29 09:00:06 +03:00
$traceTelemetry = $telemetry.Client.GetType().Assembly.CreateInstance('Microsoft.ApplicationInsights.DataContracts.TraceTelemetry')
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "I" }
2021-08-29 21:14:58 +03:00
$traceTelemetry.Message = "$($telemetryScope.Name)" #`n$transcript"
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "J" }
2021-08-29 09:00:06 +03:00
$traceTelemetry.SeverityLevel = $telemetryScope.SeverityLevel
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "K" }
2021-08-29 21:14:58 +03:00
$telemetryScope.Properties.GetEnumerator() | ForEach-Object {
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "L $($_.Key) = $($_.Value)" }
[void]$traceTelemetry.Properties.TryAdd($_.Key, $_.Value)
}
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "M" }
2021-08-29 09:00:06 +03:00
$traceTelemetry.Context.Operation.Name = $telemetryScope.Name
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "N" }
2021-08-29 09:00:06 +03:00
$traceTelemetry.Context.Operation.Id = $telemetryScope.CorrelationId
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "O" }
2021-08-29 09:00:06 +03:00
$traceTelemetry.Context.Operation.ParentId = $telemetryScope.ParentId
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "P" }
2021-08-29 09:00:06 +03:00
$telemetry.Client.TrackTrace($traceTelemetry)
2021-08-29 17:57:40 +03:00
if ($telemetry.Debug) { Write-Host -ForegroundColor Yellow "Q" }
2021-08-29 09:00:06 +03:00
$telemetryScope.Emitted = $true
2021-07-05 07:07:07 +03:00
}
2021-07-01 22:44:04 +03:00
}
}
function TrackException {
Param(
$telemetryScope,
$errorRecord
)
2021-08-26 16:28:48 +03:00
TrackException -telemetryScope $telemetryScope -exception $errorRecord.Exception -scriptStackTrace $errorRecord.scriptStackTrace
}
function TrackException {
Param(
$telemetryScope,
$exception,
$scriptStackTrace = $null
)
2021-07-05 07:36:37 +03:00
if ($telemetryScope -and !$telemetryScope.Emitted) {
2021-08-29 09:00:06 +03:00
if ($telemetry.Client.IsEnabled() -and ($telemetryScope.CorrelationId -eq $telemetry.CorrelationId)) {
if ($telemetry.Debug) {
Write-Host -ForegroundColor Yellow "Emit telemetry exception, scope $($telemetryScope.Name)"
}
$telemetry.CorrelationId = $telemetryScope.ParentId
if ($telemetry.CorrelationId -eq "") {
$telemetry.TopId = ""
}
2021-07-05 07:07:07 +03:00
$telemetryScope.Properties.Add("Duration", [DateTime]::Now.Subtract($telemetryScope.StartTime).TotalSeconds)
2021-08-26 16:28:48 +03:00
if ($scriptStackTrace) {
$telemetryScope.Properties.Add("Error StackTrace", $scriptStackTrace)
}
2021-08-29 09:00:06 +03:00
if ($exception) {
$telemetryScope.Properties.Add("Error Message", $exception.Message)
}
try {
Stop-Transcript | Out-Null
$transcript = (@(Get-Content -Path (Join-Path $env:TEMP $telemetryScope.CorrelationId)) | select -skip 18 | select -skiplast 4) -join "`n"
if ($transcript.Length -gt 30000) {
$transcript = "$($transcript.SubString(0,15000))`n`n...`n`n$($transcript.SubString($transcript.Length-15000))"
2021-08-26 16:28:48 +03:00
}
2021-08-29 09:00:06 +03:00
Remove-Item -Path (Join-Path $env:TEMP $telemetryScope.CorrelationId)
}
catch {
$transcript = ""
}
# emit trace telemetry with Error info
$traceTelemetry = $telemetry.Client.GetType().Assembly.CreateInstance('Microsoft.ApplicationInsights.DataContracts.TraceTelemetry')
$traceTelemetry.Message = "$($telemetryScope.Name)`n$transcript"
$traceTelemetry.SeverityLevel = $telemetryScope.SeverityLevel
$telemetryScope.Properties.GetEnumerator() | ForEach-Object {
[void]$traceTelemetry.Properties.TryAdd($_.Key, $_.Value)
}
$traceTelemetry.Context.Operation.Name = $telemetryScope.Name
$traceTelemetry.Context.Operation.Id = $telemetryScope.CorrelationId
$traceTelemetry.Context.Operation.ParentId = $telemetryScope.ParentId
$telemetry.Client.TrackTrace($traceTelemetry)
# emit exception telemetry
$exceptionTelemetry = $telemetry.Client.GetType().Assembly.CreateInstance('Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry')
$exceptionTelemetry.Message = "$($telemetryScope.Name)`n$transcript"
$exceptionTelemetry.SeverityLevel = $telemetryScope.SeverityLevel
$telemetryScope.Properties.GetEnumerator() | ForEach-Object {
[void]$exceptionTelemetry.Properties.TryAdd($_.Key, $_.Value)
2021-08-26 16:28:48 +03:00
}
2021-08-29 09:00:06 +03:00
$exceptionTelemetry.Context.Operation.Name = $telemetryScope.Name
$exceptionTelemetry.Context.Operation.Id = $telemetryScope.CorrelationId
$exceptionTelemetry.Context.Operation.ParentId = $telemetryScope.ParentId
$telemetry.Client.TrackException($exceptionTelemetry)
2021-07-05 07:36:37 +03:00
$telemetryScope.Emitted = $true
2021-07-05 07:07:07 +03:00
}
2021-07-01 22:44:04 +03:00
}
}