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]) {
|
2021-08-30 10:53:50 +03:00
|
|
|
|
$arr = @($value.GetEnumerator() | ForEach-Object { $_ })
|
2021-08-30 10:18:26 +03:00
|
|
|
|
$str = "{"
|
|
|
|
|
$arr | ForEach-Object {
|
2021-09-03 08:35:47 +03:00
|
|
|
|
if ($_.Key -eq "RefreshToken" -or $_.Key -eq "AccessToken") {
|
|
|
|
|
if ($_.Value) {
|
|
|
|
|
$str += "`n $($_.Key): [token]"
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$str += "`n $($_.Key): [null]"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$str += "`n $($_.Key): $(FormatValue -value $_.Value)"
|
|
|
|
|
}
|
2021-07-01 22:44:04 +03:00
|
|
|
|
}
|
2021-08-30 08:06:53 +03:00
|
|
|
|
"$str`n}"
|
2021-07-01 22:44:04 +03:00
|
|
|
|
}
|
2021-08-30 08:06:53 +03:00
|
|
|
|
elseif ($value -is [System.Collections.IEnumerable]) {
|
2021-08-30 10:53:50 +03:00
|
|
|
|
$arr = @($value.GetEnumerator() | ForEach-Object { $_ })
|
2021-08-30 10:18:26 +03:00
|
|
|
|
if ($arr.Count -gt 1) { $str = "[" } else { $str = "" }
|
|
|
|
|
$arr | ForEach-Object {
|
|
|
|
|
if ($arr.Count -gt 1) { $str += "`n " }
|
2021-07-01 22:44:04 +03:00
|
|
|
|
$str += "$(FormatValue -value $_)"
|
|
|
|
|
}
|
2021-08-30 10:18:26 +03:00
|
|
|
|
if ($arr.Count -gt 1) { "$str`n]" } else { $str }
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-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
|
|
|
|
|
"Properties" = [Collections.Generic.Dictionary[string, string]]::new()
|
2021-09-03 08:35:47 +03:00
|
|
|
|
"Parameters" = [Collections.Generic.Dictionary[string, string]]::new()
|
|
|
|
|
"AllParameters" = [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
|
2021-09-03 08:35:47 +03:00
|
|
|
|
$value = FormatValue -value $_.value
|
|
|
|
|
$scope.allParameters.Add($key, $value)
|
2021-08-04 08:31:56 +03:00
|
|
|
|
$includeParameters | ForEach-Object { if ($key -like $_) { $includeParameter = $true } }
|
|
|
|
|
if ($includeParameter) {
|
2021-09-03 08:35:47 +03:00
|
|
|
|
$scope.parameters.Add($key, $value)
|
2021-08-04 08:31:56 +03:00
|
|
|
|
}
|
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-30 08:06:53 +03:00
|
|
|
|
$telemetryScope.Emitted = $true
|
2021-08-29 09:00:06 +03:00
|
|
|
|
try {
|
|
|
|
|
Stop-Transcript | Out-Null
|
|
|
|
|
$transcript = (@(Get-Content -Path (Join-Path $env:TEMP $telemetryScope.CorrelationId)) | select -skip 18 | select -skiplast 4) -join "`n"
|
2021-08-30 08:06:53 +03:00
|
|
|
|
if ($transcript.Length -gt 32000) {
|
|
|
|
|
$transcript = "$($transcript.SubString(0,16000))`n`n...`n`n$($transcript.SubString($transcript.Length-16000))"
|
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 = ""
|
|
|
|
|
}
|
2021-08-30 10:18:26 +03:00
|
|
|
|
$telemetryScope.Properties.Add("Duration", [DateTime]::Now.Subtract($telemetryScope.StartTime).TotalSeconds)
|
2021-08-29 09:00:06 +03:00
|
|
|
|
|
|
|
|
|
$traceTelemetry = $telemetry.Client.GetType().Assembly.CreateInstance('Microsoft.ApplicationInsights.DataContracts.TraceTelemetry')
|
2021-09-03 08:35:47 +03:00
|
|
|
|
if ($bcContainerHelperConfig.UseExtendedTelemetry) {
|
|
|
|
|
$traceTelemetry.Message = "$($telemetryScope.Name)`n$transcript"
|
|
|
|
|
$traceTelemetry.SeverityLevel = 0
|
|
|
|
|
$telemetryScope.allParameters.GetEnumerator() | ForEach-Object {
|
|
|
|
|
[void]$traceTelemetry.Properties.TryAdd("Parameter[$($_.Key)]", $_.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$traceTelemetry.Message = "$($telemetryScope.Name)"
|
|
|
|
|
$traceTelemetry.SeverityLevel = 1
|
|
|
|
|
$telemetryScope.Parameters.GetEnumerator() | ForEach-Object {
|
|
|
|
|
[void]$traceTelemetry.Properties.TryAdd("Parameter[$($_.Key)]", $_.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-29 21:14:58 +03:00
|
|
|
|
$telemetryScope.Properties.GetEnumerator() | ForEach-Object {
|
|
|
|
|
[void]$traceTelemetry.Properties.TryAdd($_.Key, $_.Value)
|
|
|
|
|
}
|
2021-08-29 09:00:06 +03:00
|
|
|
|
$traceTelemetry.Context.Operation.Name = $telemetryScope.Name
|
|
|
|
|
$traceTelemetry.Context.Operation.Id = $telemetryScope.CorrelationId
|
|
|
|
|
$traceTelemetry.Context.Operation.ParentId = $telemetryScope.ParentId
|
|
|
|
|
$telemetry.Client.TrackTrace($traceTelemetry)
|
2021-08-30 08:06:53 +03:00
|
|
|
|
$telemetry.Client.Flush()
|
2021-08-30 12:54:08 +03:00
|
|
|
|
|
|
|
|
|
Write-Host "Telemetry Correlation Id: $($telemetryScope.CorrelationId)"
|
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-08-30 08:06:53 +03:00
|
|
|
|
$telemetryScope.Emitted = $true
|
2021-08-29 09:00:06 +03:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Stop-Transcript | Out-Null
|
|
|
|
|
$transcript = (@(Get-Content -Path (Join-Path $env:TEMP $telemetryScope.CorrelationId)) | select -skip 18 | select -skiplast 4) -join "`n"
|
2021-08-30 08:06:53 +03:00
|
|
|
|
if ($transcript.Length -gt 32000) {
|
|
|
|
|
$transcript = "$($transcript.SubString(0,16000))`n`n...`n`n$($transcript.SubString($transcript.Length-16000))"
|
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 = ""
|
|
|
|
|
}
|
2021-08-30 10:18:26 +03:00
|
|
|
|
$telemetryScope.Properties.Add("Duration", [DateTime]::Now.Subtract($telemetryScope.StartTime).TotalSeconds)
|
|
|
|
|
if ($scriptStackTrace) {
|
|
|
|
|
$telemetryScope.Properties.Add("Error StackTrace", $scriptStackTrace)
|
|
|
|
|
}
|
|
|
|
|
if ($exception) {
|
|
|
|
|
$telemetryScope.Properties.Add("Error Message", $exception.Message)
|
|
|
|
|
}
|
2021-08-29 09:00:06 +03:00
|
|
|
|
|
|
|
|
|
# emit trace telemetry with Error info
|
|
|
|
|
$traceTelemetry = $telemetry.Client.GetType().Assembly.CreateInstance('Microsoft.ApplicationInsights.DataContracts.TraceTelemetry')
|
2021-09-03 08:35:47 +03:00
|
|
|
|
if ($bcContainerHelperConfig.UseExtendedTelemetry) {
|
|
|
|
|
$traceTelemetry.Message = "$($telemetryScope.Name)`n$transcript"
|
|
|
|
|
$traceTelemetry.SeverityLevel = 0
|
|
|
|
|
$telemetryScope.allParameters.GetEnumerator() | ForEach-Object {
|
|
|
|
|
[void]$traceTelemetry.Properties.TryAdd("Parameter[$($_.Key)]", $_.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$traceTelemetry.Message = "$($telemetryScope.Name)"
|
|
|
|
|
$traceTelemetry.SeverityLevel = 1
|
|
|
|
|
$telemetryScope.Parameters.GetEnumerator() | ForEach-Object {
|
|
|
|
|
[void]$traceTelemetry.Properties.TryAdd("Parameter[$($_.Key)]", $_.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-29 09:00:06 +03:00
|
|
|
|
$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')
|
2021-09-03 08:35:47 +03:00
|
|
|
|
if ($bcContainerHelperConfig.UseExtendedTelemetry) {
|
|
|
|
|
$exceptionTelemetry.Message = "$($telemetryScope.Name)`n$transcript"
|
|
|
|
|
$exceptionTelemetry.SeverityLevel = 3
|
|
|
|
|
$telemetryScope.allParameters.GetEnumerator() | ForEach-Object {
|
|
|
|
|
[void]$exceptionTelemetry.Properties.TryAdd("Parameter[$($_.Key)]", $_.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$exceptionTelemetry.Message = "$($telemetryScope.Name)"
|
|
|
|
|
$exceptionTelemetry.SeverityLevel = 1
|
|
|
|
|
$telemetryScope.Parameters.GetEnumerator() | ForEach-Object {
|
|
|
|
|
[void]$exceptionTelemetry.Properties.TryAdd("Parameter[$($_.Key)]", $_.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-29 09:00:06 +03:00
|
|
|
|
$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-08-30 08:06:53 +03:00
|
|
|
|
$telemetry.Client.Flush()
|
2021-08-30 12:54:08 +03:00
|
|
|
|
|
|
|
|
|
Write-Host "Telemetry Correlation Id: $($telemetryScope.CorrelationId)"
|
2021-07-05 07:07:07 +03:00
|
|
|
|
}
|
2021-07-01 22:44:04 +03:00
|
|
|
|
}
|
|
|
|
|
}
|