navcontainerhelper/TelemetryHelper.ps1

135 строки
4.5 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-07-02 12:51:53 +03:00
if ($bcContainerHelperConfig.TelemetryConnectionString) {
if ($telemetryClient.TelemetryConfiguration.ConnectionString -ne $bcContainerHelperConfig.TelemetryConnectionString) {
$telemetryClient.TelemetryConfiguration.ConnectionString = $bcContainerHelperConfig.TelemetryConnectionString
$telemetryClient.TelemetryConfiguration.DisableTelemetry = $false
2021-07-03 08:18:29 +03:00
Write-Host "telemetry client initialized"
2021-07-01 22:44:04 +03:00
}
2021-07-02 12:51:53 +03:00
if ($telemetryClient.IsEnabled()) {
2021-07-03 08:18:29 +03:00
Write-Host "init telemetry scope ($name)"
2021-07-02 12:51:53 +03:00
$scope = @{
"Name" = $name
"StartTime" = [DateTime]::Now
"SeverityLevel" = 1
"Properties" = [Collections.Generic.Dictionary[string, string]]::new()
}
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-01 22:44:04 +03:00
}
}
2021-07-02 12:51:53 +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-07-02 12:51:53 +03:00
}
else {
$telemetryClient.TelemetryConfiguration.DisableTelemetry = $true
2021-07-01 22:44:04 +03:00
}
}
function TrackTrace {
Param(
$telemetryScope
)
2021-07-05 07:07:07 +03:00
if ($telemetryScope) {
Write-Host "TrackTrace"
if ($telemetryClient.IsEnabled()) {
Write-Host "Emit telemetry trace"
$telemetryScope.Properties.Add("Duration", [DateTime]::Now.Subtract($telemetryScope.StartTime).TotalSeconds)
$telemetryClient.TrackTrace($telemetryScope.Name, $telemetryScope.SeverityLevel, $telemetryScope.Properties)
}
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:07:07 +03:00
if ($telemetryScope) {
Write-Host "TrackTrace"
if ($telemetryClient.IsEnabled()) {
Write-Host "emit telemetry exception"
$telemetryScope.Properties.Add("Duration", [DateTime]::Now.Subtract($telemetryScope.StartTime).TotalSeconds)
$telemetryScope.Properties.Add("StackTrace", $errorRecord.ScriptStackTrace)
$telemetryClient.TrackException($errorRecord.Exception, $telemetryScope.Properties)
}
2021-07-01 22:44:04 +03:00
}
}