navcontainerhelper/TelemetryHelper.ps1

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

2021-07-02 12:51:53 +03:00
function FormatValue {
2021-07-01 22:44:04 +03:00
Param(
$value
)
if ($value -is [switch]) {
"$($value.IsPresent)"
}
elseif ($value -is [boolean]) {
"$value"
}
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('?'))) {
"""$($value.Split('?')[0])?[parameters]"""
}
else {
"""$value"""
}
}
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 {
"$value"
}
}
function AddTelemetryProperty {
Param(
$telemetryScope,
$key,
$value
)
if ($telemetryScope) {
2021-07-05 07:07:07 +03:00
Write-Host "Telemetry scope $($telemetryScope.Name), add property $key = $((FormatValue -value $value))"
2021-07-01 22:44:04 +03:00
$telemetryScope.properties.Add($key, (FormatValue -value $value))
}
}
function InitTelemetryScope {
Param(
[string] $name,
[string[]] $includeParameters = @(),
$parameterValues = $null
)
2021-08-04 08:31:56 +03:00
if ($telemetryClient) {
if ($bcContainerHelperConfig.TelemetryConnectionString) {
if ($telemetryClient.TelemetryConfiguration.ConnectionString -ne $bcContainerHelperConfig.TelemetryConnectionString) {
$telemetryClient.TelemetryConfiguration.ConnectionString = $bcContainerHelperConfig.TelemetryConnectionString
$telemetryClient.TelemetryConfiguration.DisableTelemetry = $false
Write-Host "Telemetry client initialized"
2021-07-02 12:51:53 +03:00
}
2021-08-04 08:31:56 +03:00
if ($telemetryClient.IsEnabled()) {
Write-Host "Init telemetry scope $name"
$scope = @{
"Name" = $name
"StartTime" = [DateTime]::Now
"SeverityLevel" = 1
"Properties" = [Collections.Generic.Dictionary[string, string]]::new()
"Emitted" = $false
}
if ($includeParameters) {
$parameterValues.GetEnumerator() | % {
$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 {
$telemetryClient.TelemetryConfiguration.DisableTelemetry = $true
}
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-07-05 07:07:07 +03:00
if ($telemetryClient.IsEnabled()) {
2021-07-05 08:46:50 +03:00
Write-Host "Emit telemetry trace, scope $($telemetryScope.Name)"
2021-07-05 07:07:07 +03:00
$telemetryScope.Properties.Add("Duration", [DateTime]::Now.Subtract($telemetryScope.StartTime).TotalSeconds)
$telemetryClient.TrackTrace($telemetryScope.Name, $telemetryScope.SeverityLevel, $telemetryScope.Properties)
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
}
2021-07-02 12:51:53 +03:00
2021-07-01 22:44:04 +03:00
}
function TrackException {
Param(
$telemetryScope,
$errorRecord
)
2021-07-05 07:36:37 +03:00
if ($telemetryScope -and !$telemetryScope.Emitted) {
2021-07-05 07:07:07 +03:00
if ($telemetryClient.IsEnabled()) {
2021-07-05 08:46:50 +03:00
Write-Host "Emit telemetry exception, scope $($telemetryScope.Name)"
2021-07-05 07:07:07 +03:00
$telemetryScope.Properties.Add("Duration", [DateTime]::Now.Subtract($telemetryScope.StartTime).TotalSeconds)
$telemetryScope.Properties.Add("StackTrace", $errorRecord.ScriptStackTrace)
$telemetryClient.TrackException($errorRecord.Exception, $telemetryScope.Properties)
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
}
}