-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelemetryHelper.psm1
279 lines (221 loc) · 9.88 KB
/
TelemetryHelper.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
. (Join-Path -Path $PSScriptRoot -ChildPath ".\AL-Go-Helper.ps1" -Resolve)
Import-Module (Join-Path $PSScriptRoot '.\Github-Helper.psm1' -Resolve)
#region Loading telemetry helper
function DownloadNugetPackage($PackageName, $PackageVersion) {
$nugetPackagePath = Join-Path "$ENV:GITHUB_WORKSPACE" "/.nuget/packages/$PackageName/$PackageVersion/"
if (-not (Test-Path -Path $nugetPackagePath)) {
$url = "https://www.nuget.org/api/v2/package/$PackageName/$PackageVersion"
Write-Host "Downloading Nuget package $PackageName $PackageVersion..."
New-Item -ItemType Directory -Path $nugetPackagePath | Out-Null
Invoke-WebRequest -Uri $Url -OutFile "$nugetPackagePath/$PackageName.$PackageVersion.zip"
# Unzip the package
Expand-Archive -Path "$nugetPackagePath/$PackageName.$PackageVersion.zip" -DestinationPath "$nugetPackagePath"
# Remove the zip file
Remove-Item -Path "$nugetPackagePath/$PackageName.$PackageVersion.zip"
}
return $nugetPackagePath
}
function LoadApplicationInsightsDll() {
$packagePath = DownloadNugetPackage -PackageName "Microsoft.ApplicationInsights" -PackageVersion (GetPackageVersion -PackageName "Microsoft.ApplicationInsights")
$AppInsightsDllPath = "$packagePath/lib/net46/Microsoft.ApplicationInsights.dll"
if (-not (Test-Path -Path $AppInsightsDllPath)) {
throw "Failed to download Application Insights DLL"
}
[Reflection.Assembly]::LoadFile($AppInsightsDllPath) | Out-Null
}
function Get-ApplicationInsightsTelemetryClient($TelemetryConnectionString)
{
# Load the Application Insights DLL
LoadApplicationInsightsDll
$TelemetryClient = [Microsoft.ApplicationInsights.TelemetryClient]::new()
$TelemetryClient.TelemetryConfiguration.ConnectionString = $TelemetryConnectionString
return $TelemetryClient
}
#endregion
function AddTelemetryEvent()
{
param(
[Parameter(Mandatory = $true)]
[String] $Message,
[Parameter(Mandatory = $false)]
[System.Collections.Generic.Dictionary[[System.String], [System.String]]] $Data = @{},
[Parameter(Mandatory = $false)]
[ValidateSet("Information", "Warning", "Error")]
[String] $Severity = 'Information'
)
try {
# Add powershell version
Add-TelemetryProperty -Hashtable $Data -Key 'PowerShellVersion' -Value ($PSVersionTable.PSVersion.ToString())
$module = Get-Module BcContainerHelper
if ($module) {
$versionNoFile = Join-Path -Path (Split-Path $module.Path -Parent) -ChildPath 'Version.txt'
Add-TelemetryProperty -Hashtable $Data -Key 'BcContainerHelperVersion' -Value (Get-Content -Path $versionNoFile -Encoding UTF8)
}
Add-TelemetryProperty -Hashtable $Data -Key 'WorkflowName' -Value $ENV:GITHUB_WORKFLOW
Add-TelemetryProperty -Hashtable $Data -Key 'RunnerOs' -Value $ENV:RUNNER_OS
Add-TelemetryProperty -Hashtable $Data -Key 'RunnerEnvironment' -Value $ENV:RUNNER_ENVIRONMENT
Add-TelemetryProperty -Hashtable $Data -Key 'RunId' -Value $ENV:GITHUB_RUN_ID
Add-TelemetryProperty -Hashtable $Data -Key 'RunNumber' -Value $ENV:GITHUB_RUN_NUMBER
Add-TelemetryProperty -Hashtable $Data -Key 'RunAttempt' -Value $ENV:GITHUB_RUN_ATTEMPT
### Add GitHub Repository information
Add-TelemetryProperty -Hashtable $Data -Key 'Repository' -Value $ENV:GITHUB_REPOSITORY_ID
Add-TelemetryProperty -Hashtable $Data -Key 'RepositoryOwnerID' -Value $ENV:GITHUB_REPOSITORY_OWNER_ID
$repoSettings = ReadSettings
if ($repoSettings.microsoftTelemetryConnectionString -ne '') {
Write-Host "Enabling Microsoft telemetry..."
$MicrosoftTelemetryClient = Get-ApplicationInsightsTelemetryClient -TelemetryConnectionString $repoSettings.microsoftTelemetryConnectionString
$MicrosoftTelemetryClient.TrackTrace($Message, [Microsoft.ApplicationInsights.DataContracts.SeverityLevel]::$Severity, $Data)
$MicrosoftTelemetryClient.Flush()
}
### Add telemetry that is only sent to the partner
Add-TelemetryProperty -Hashtable $Data -Key 'RepositoryOwner' -Value $ENV:GITHUB_REPOSITORY_OWNER
Add-TelemetryProperty -Hashtable $Data -Key 'RepositoryName' -Value $ENV:GITHUB_REPOSITORY
Add-TelemetryProperty -Hashtable $Data -Key 'RefName' -Value $ENV:GITHUB_REF_NAME
if ($repoSettings.partnerTelemetryConnectionString -ne '') {
Write-Host "Enabling partner telemetry..."
$PartnerTelemetryClient = Get-ApplicationInsightsTelemetryClient -TelemetryConnectionString $repoSettings.partnerTelemetryConnectionString
$PartnerTelemetryClient.TrackTrace($Message, [Microsoft.ApplicationInsights.DataContracts.SeverityLevel]::$Severity, $Data)
$PartnerTelemetryClient.Flush()
}
} catch {
Write-Host "Failed to log telemetry event: $_"
}
}
<#
.SYNOPSIS
Logs an information message to telemetry
.DESCRIPTION
Logs an information message to telemetry
.PARAMETER Message
The message to log to telemetry
.PARAMETER ActionName
The name of the action to log to telemetry
.PARAMETER AdditionalData
Additional data to log to telemetry
.EXAMPLE
Trace-Information -Message "AL-Go action ran: $actionName"
#>
function Trace-Information() {
param(
[Parameter(ParameterSetName = 'Message', Mandatory = $true)]
[String] $Message,
[Parameter(ParameterSetName = 'ActionName', Mandatory = $true)]
[String] $ActionName,
[Parameter(Mandatory = $false)]
[System.Collections.Generic.Dictionary[[System.String], [System.String]]] $AdditionalData = @{}
)
if (-not $Message) {
$Message = "AL-Go action ran: $ActionName"
}
AddTelemetryEvent -Message $Message -Severity 'Information' -Data $AdditionalData
}
<#
.SYNOPSIS
Logs a warning message to telemetry
.DESCRIPTION
Logs a warning message to telemetry
.PARAMETER Message
The message to log to telemetry
.PARAMETER AdditionalData
Additional data to log to telemetry
.EXAMPLE
Trace-Warning -Message "AL-Go warning: This is a warning message"
#>
function Trace-Warning() {
param(
[Parameter(Mandatory = $true)]
[String] $Message,
[Parameter(Mandatory = $false)]
[System.Collections.Generic.Dictionary[[System.String], [System.String]]] $AdditionalData = @{}
)
AddTelemetryEvent -Message $Message -Severity 'Warning' -Data $AdditionalData
}
<#
.SYNOPSIS
Logs an exception message to telemetry
.DESCRIPTION
Logs an exception message to telemetry
.PARAMETER Message
The message to log to telemetry
.PARAMETER ActionName
The name of the action to log to telemetry
.PARAMETER ErrorRecord
The error record to log to telemetry
.PARAMETER AdditionalData
Additional data to log to telemetry
.EXAMPLE
Trace-Exception -ErrorRecord $ErrorRecord
#>
function Trace-Exception() {
param(
[Parameter(ParameterSetName = 'Message', Mandatory = $true)]
[String] $Message,
[Parameter(ParameterSetName = 'ActionName', Mandatory = $true)]
[String] $ActionName,
[Parameter(Mandatory = $false)]
[System.Management.Automation.ErrorRecord] $ErrorRecord = $null,
[Parameter(Mandatory = $false)]
[System.Collections.Generic.Dictionary[[System.String], [System.String]]] $AdditionalData = @{}
)
if ($ErrorRecord -ne $null) {
Add-TelemetryProperty -Hashtable $AdditionalData -Key 'ErrorMessage' -Value $ErrorRecord.Exception.Message
}
if (-not $Message) {
$Message = "AL-Go action failed: $ActionName"
}
AddTelemetryEvent -Message $Message -Severity 'Error' -Data $AdditionalData
}
<#
.SYNOPSIS
Adds a key-value pair to a hashtable if the key does not already exist
.DESCRIPTION
Adds a key-value pair to a hashtable if the key does not already exist
.PARAMETER Hashtable
The hashtable to add the key-value pair to
.PARAMETER Key
The key to add to the hashtable
.PARAMETER Value
The value to add to the hashtable
.EXAMPLE
Add-TelemetryProperty -Hashtable $AdditionalData -Key 'RepoType' -Value 'PTE'
#>
function Add-TelemetryProperty() {
param(
[System.Collections.Generic.Dictionary[[System.String], [System.String]]] $Hashtable,
[String] $Key,
[String] $Value
)
if (-not $Hashtable.ContainsKey($Key) -and ($Value -ne '')) {
$Hashtable.Add($Key, $Value)
}
}
<#
.SYNOPSIS
Writes a deprecation warning to telemetry and as a warning in the GitHub action
.DESCRIPTION
Writes a deprecation warning to telemetry and as a warning in the GitHub action
.PARAMETER Message
The message to log to telemetry
.PARAMETER DeprecationTag
The tag to use to link to the deprecation documentation
.PARAMETER AdditionalData
Additional data to log to telemetry
.EXAMPLE
Trace-DeprecationWarning -Message "This setting is deprecated. Use the new setting instead." -DeprecationTag 'SettingName'
#>
function Trace-DeprecationWarning {
param(
[Parameter(Mandatory = $true)]
[String] $Message,
[Parameter(Mandatory = $true)]
[String] $DeprecationTag,
[Parameter(Mandatory = $false)]
[System.Collections.Generic.Dictionary[[System.String], [System.String]]] $AdditionalData = @{}
)
# Show deprecation warning in GitHub
OutputWarning -message "$Message. See https://aka.ms/ALGoDeprecations#$($DeprecationTag) for more information."
# Log deprecation warning to telemetry
Add-TelemetryProperty -Hashtable $AdditionalData -Key 'DeprecationTag' -Value $DeprecationTag
Trace-Warning -Message "Deprecation Warning: $Message" -AdditionalData $AdditionalData
}
Export-ModuleMember -Function Trace-Information, Trace-Warning, Trace-Exception, Add-TelemetryProperty, Trace-DeprecationWarning