navcontainerhelper/TelemetryHelper.ps1

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

2021-09-15 19:35:11 +03:00
$eventIds = @{
2021-11-01 17:46:54 +03:00
"New-BcContainer" = "LC0050"
"New-BcImage" = "LC0051"
"Import-TestToolkitToBcContainer" = "LC0052"
"Compile-AppInBcContainer" = "LC0053"
"Publish-BcContainerApp" = "LC0054"
"Run-AlCops" = "LC0055"
"Run-AlValidation" = "LC0056"
"Run-AlPipeline" = "LC0057"
"Run-TestsInBcContainer" = "LC0058"
"Sign-BcContainerApp" = "LC0059"
"Publish-PerTenantExtensionApps" = "LC0060"
"Install-BcAppFromAppSource" = "LC0061"
"New-BcEnvironment" = "LC0062"
"New-BcDatabaseExport" = "LC0063"
"Remove-BcEnvironment" = "LC0064"
"Download-Artifacts" = "LC0065"
"New-CompanyInNavContainer" = "LC0066"
"UploadImportAndApply-ConfigPackageInBcContainer" = "LC0067"
2021-09-15 19:35:11 +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,
[string[]] $includeParameters = @(),
2021-11-01 17:29:11 +03:00
$parameterValues = $null,
[string] $eventId = ""
2021-07-01 22:44:04 +03:00
)
2021-11-01 16:12:28 +03:00
"Microsoft","Partner" | ForEach-Object {
2021-11-01 16:32:43 +03:00
$clientName = "$($_)Client"
2021-11-01 16:12:28 +03:00
$telemetryConnectionString = $bcContainerHelperConfig."$($_)TelemetryConnectionString"
if ($telemetryConnectionString -and $telemetry.Assembly -ne $null) {
if ($telemetry."$clientName" -eq $null -or $telemetry."$ClientName".TelemetryConfiguration.ConnectionString -ne $telemetryConnectionString) {
try {
$telemetryConfiguration = $telemetry.Assembly.CreateInstance('Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration')
$telemetryConfiguration.Connectionstring = $telemetryConnectionString
2021-11-01 17:29:11 +03:00
$telemetry."$clientName" = $telemetry.Assembly.CreateInstance('Microsoft.ApplicationInsights.TelemetryClient', $false, 0, $null, $telemetryConfiguration, $null, $null)
}
catch {
$telemetry."$clientName" = $null
}
}
}
else {
$telemetry."$clientName" = $null
}
}
if ($telemetry.MicrosoftClient -ne $null -or $telemetry.PartnerClient -ne $null) {
if ($eventId -eq "" -and ($eventIds.ContainsKey($name))) {
2021-11-01 16:12:28 +03:00
$eventId = $eventIds[$name]
}
if (($eventId -ne "") -or ($telemetry.CorrelationId -eq "")) {
$CorrelationId = [GUID]::NewGuid().ToString()
Start-Transcript -Path (Join-Path $env:TEMP $CorrelationId) | Out-Null
if ($telemetry.Debug) {
Write-Host -ForegroundColor Yellow "Init telemetry scope $name"
2021-07-02 12:51:53 +03:00
}
2021-11-01 16:12:28 +03:00
if ($telemetry.TopId -eq "") { $telemetry.TopId = $CorrelationId }
$scope = @{
"Name" = $name
"EventId" = $eventId
"StartTime" = [DateTime]::Now
"Properties" = [Collections.Generic.Dictionary[string, string]]::new()
"Parameters" = [Collections.Generic.Dictionary[string, string]]::new()
"AllParameters" = [Collections.Generic.Dictionary[string, string]]::new()
"CorrelationId" = $CorrelationId
"ParentId" = $telemetry.CorrelationId
"TopId" = $telemetry.TopId
"Emitted" = $false
2021-09-15 19:35:11 +03:00
}
2021-11-01 16:12:28 +03:00
$telemetry.CorrelationId = $CorrelationId
if ($includeParameters) {
$parameterValues.GetEnumerator() | ForEach-Object {
$includeParameter = $false
$key = $_.key
$value = FormatValue -value $_.value
$scope.allParameters.Add($key, $value)
$includeParameters | ForEach-Object { if ($key -like $_) { $includeParameter = $true } }
if ($includeParameter) {
$scope.parameters.Add($key, $value)
2021-07-02 12:51:53 +03:00
}
2021-07-01 22:44:04 +03:00
}
}
2021-11-01 16:12:28 +03:00
AddTelemetryProperty -telemetryScope $scope -key "eventId" -value $eventId
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-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-11-01 16:12:28 +03:00
if ($telemetryScope.CorrelationId -eq $telemetry.CorrelationId) {
2021-08-29 09:00:06 +03:00
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-09-15 19:35:11 +03:00
$telemetryScope.Properties.Add("duration", [DateTime]::Now.Subtract($telemetryScope.StartTime).TotalSeconds)
2021-08-29 09:00:06 +03:00
2021-11-03 01:46:36 +03:00
if ($telemetry.Assembly -ne $null) {
"Microsoft","Partner" | ForEach-Object {
$clientName = "$($_)Client"
$extendedTelemetry = $bcContainerHelperConfig.SendExtendedTelemetryToMicrosoft -or $_ -eq "Partner"
if ($telemetry."$clientName") {
$traceTelemetry = $telemetry.Assembly.CreateInstance('Microsoft.ApplicationInsights.DataContracts.TraceTelemetry')
if ($extendedTelemetry) {
$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)
}
}
$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."$clientName".TrackTrace($traceTelemetry)
$telemetry."$clientName".Flush()
2021-11-01 16:12:28 +03:00
}
2021-11-03 01:46:36 +03:00
if ($extendedTelemetry -and $telemetryScope.EventId) {
Write-Host "$($telemetryScope.Name) Telemetry Correlation Id: $($telemetryScope.CorrelationId)"
2021-11-01 16:12:28 +03:00
}
2021-09-03 08:35:47 +03:00
}
2021-09-15 19:35:11 +03:00
}
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-11-01 16:12:28 +03:00
if ($telemetryScope.CorrelationId -eq $telemetry.CorrelationId) {
2021-08-29 09:00:06 +03:00
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-09-15 19:35:11 +03:00
$telemetryScope.Properties.Add("duration", [DateTime]::Now.Subtract($telemetryScope.StartTime).TotalSeconds)
2021-08-30 10:18:26 +03:00
if ($scriptStackTrace) {
2021-09-15 19:35:11 +03:00
$telemetryScope.Properties.Add("errorStackTrace", $scriptStackTrace)
2021-08-30 10:18:26 +03:00
}
if ($exception) {
2021-09-15 19:35:11 +03:00
$telemetryScope.Properties.Add("errorMessage", $exception.Message)
2021-08-30 10:18:26 +03:00
}
2021-08-29 09:00:06 +03:00
2021-11-03 01:46:36 +03:00
if ($telemetry.Assembly -ne $null) {
"Microsoft","Partner" | ForEach-Object {
$clientName = "$($_)Client"
$extendedTelemetry = $bcContainerHelperConfig.SendExtendedTelemetryToMicrosoft -or $_ -eq "Partner"
if ($telemetry."$clientName") {
$traceTelemetry = $telemetry.Assembly.CreateInstance('Microsoft.ApplicationInsights.DataContracts.TraceTelemetry')
if ($extendedTelemetry) {
$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)
}
}
$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."$clientName".TrackTrace($traceTelemetry)
# emit exception telemetry
$exceptionTelemetry = $telemetry.Assembly.CreateInstance('Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry')
if ($extendedTelemetry) {
$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)
}
}
$telemetryScope.Properties.GetEnumerator() | ForEach-Object {
[void]$exceptionTelemetry.Properties.TryAdd($_.Key, $_.Value)
}
$exceptionTelemetry.Context.Operation.Name = $telemetryScope.Name
$exceptionTelemetry.Context.Operation.Id = $telemetryScope.CorrelationId
$exceptionTelemetry.Context.Operation.ParentId = $telemetryScope.ParentId
$telemetry."$clientName".TrackException($exceptionTelemetry)
$telemetry."$clientName".Flush()
2021-11-01 16:12:28 +03:00
}
2021-09-03 08:35:47 +03:00
}
2021-11-03 01:46:36 +03:00
Write-Host "$($telemetryScope.Name) Telemetry Correlation Id: $($telemetryScope.CorrelationId)"
2021-09-03 08:35:47 +03:00
}
2021-07-05 07:07:07 +03:00
}
2021-07-01 22:44:04 +03:00
}
}