Skip to content

Commit 8192c7a

Browse files
committed
Support telemetry
1 parent 79d1ed0 commit 8192c7a

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

tools/Az.Tools.Installer/exports/Install-AzModule.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ function Install-AzModule{
7171

7272
process {
7373

74+
$cmdStarted = Get-Date
75+
7476
Import-Module "$PSScriptRoot\..\internal\utils.psm1"
7577

7678
$author = 'Microsoft Corporation'
@@ -214,5 +216,10 @@ function Install-AzModule{
214216
$_ | Install-Module -Repository $Repository -AllowClobber -Force -AllowPrerelease:$allow_prerelease -SkipPublisherCheck:$skip_publisher_check
215217
}
216218
}
219+
220+
Send-PageViewTelemetry -SourcePSCmdlet $PSCmdlet `
221+
-IsSuccess $true `
222+
-StartDateTime $cmdStarted `
223+
-Duration ((Get-Date) - $cmdStarted)
217224
}
218225
}

tools/Az.Tools.Installer/internal/Classes.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ class Constants
33
static [System.String] $PublicTelemetryInstrumentationKey = "7df6ff70-8353-4672-80d6-568517fed090"
44
static [System.String] $CurrentSessionId = [System.GUID]::NewGuid().ToString()
55
static [Microsoft.ApplicationInsights.TelemetryClient] $TelemetryClient = $null
6+
static [System.String] $HashMacAddress = $null
67
}

tools/Az.Tools.Installer/internal/Send-PageViewTelemetry.ps1

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ function Send-PageViewTelemetry
66
(
77
[Parameter(
88
Mandatory=$true,
9-
HelpMessage='Specify the page or operation name.')]
10-
[System.Management.Automation.CommandInfo]
9+
HelpMessage='Specify the source cmdlet object.')]
10+
[System.Management.Automation.PSCmdlet]
1111
[ValidateNotNullOrEmpty()]
12-
$CommandInfo,
12+
$SourcePSCmdlet,
13+
14+
[Parameter(
15+
Mandatory=$false,
16+
HelpMessage='Specify the parameter set name.')]
17+
[System.Boolean]
18+
$IsSuccess,
1319

1420
[Parameter(
1521
Mandatory=$false,
@@ -31,26 +37,61 @@ function Send-PageViewTelemetry
3137
)
3238
Process
3339
{
34-
if($null -eq [Constants]::TelemetryClient)
35-
{
40+
if($null -eq [Constants]::TelemetryClient) {
41+
Write-Verbose -Message 'Initialize telemetry client'
3642
$TelemetryClient = New-Object Microsoft.ApplicationInsights.TelemetryClient
3743
$TelemetryClient.InstrumentationKey = [Constants]::PublicTelemetryInstrumentationKey
3844
$TelemetryClient.Context.Session.Id = $CurrentSessionId
3945
$TelemetryClient.Context.Device.OperatingSystem = [System.Environment]::OSVersion.ToString()
4046
[Constants]::TelemetryClient = $TelemetryClient
4147
}
4248

49+
if([string]::IsNullOrWhiteSpace([Constants]::HashMacAddress)) {
50+
Write-Verbose -Message 'hash mac address'
51+
$macAddress = ''
52+
$nics = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
53+
foreach ($nic in $nics) {
54+
if($nic.OperationalStatus -eq 'Up' -and -not [string]::IsNullOrWhiteSpace($nic.GetPhysicalAddress())) {
55+
$macAddress = $nic.GetPhysicalAddress().ToString()
56+
}
57+
}
58+
59+
if($macAddress -ne '') {
60+
$bytes = [System.Text.Encoding]::UTF8.GetBytes($macAddress)
61+
$sha256 = New-Object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider
62+
$macAddress = [System.BitConverter]::ToString($sha256.ComputeHash($bytes))
63+
$macAddress = $macAddress.Replace('-', '').ToLowerInvariant()
64+
}
65+
[Constants]::HashMacAddress = $macAddress
66+
}
67+
4368
$client = [Constants]::TelemetryClient
4469

4570
$page = New-Object Microsoft.ApplicationInsights.DataContracts.PageViewTelemetry
4671
$page.Name = "cmdletInvocation"
4772
$page.Duration = $Duration
4873

49-
$page.Properties["IsSuccess"] = $True.ToString()
50-
$page.Properties["OS"] = [System.Environment]::OSVersion.ToString()
74+
if ($PSBoundParameters.ContainsKey('IsSuccess')) {
75+
$page.Properties['IsSuccess'] = $PSBoundParameters['IsSuccess'].ToString()
76+
} else {
77+
$page.Properties['IsSuccess'] = $true.ToString()
78+
}
79+
$page.Properties['x-ms-client-request-id'] = [Constants]::CurrentSessionId
80+
$page.Properties['OS'] = [System.Environment]::OSVersion.ToString()
81+
$page.Properties['HostVersion'] = $PSCmdlet.Host.Version
82+
$page.Properties['HashMacAddress'] = [Constants]::HashMacAddress
83+
$page.Properties['PowerShellVersion'] = $PSVersionTable.PSVersion.ToString()
84+
$page.Properties['Command'] = $SourcePSCmdlet.MyInvocation.MyCommand.Name
85+
$page.Properties['CommandParameterSetName'] = $SourcePSCmdlet.ParameterSetName
86+
$page.Properties['CommandInvocationName'] = $SourcePSCmdlet.MyInvocation.InvocationName
5187

52-
$page.Properties["x-ms-client-request-id"]= [Constants]::CurrentSessionId
53-
$page.Properties["PowerShellVersion"]= $PSVersionTable.PSVersion.ToString();
88+
if($null -ne $SourcePSCmdlet.MyInvocation.BoundParameters) {
89+
$parameters = ""
90+
foreach ($Key in $SourcePSCmdlet.MyInvocation.BoundParameters.Keys) {
91+
$parameters += "-$Key *** "
92+
}
93+
$page.Properties['CommandParameters'] = $parameters
94+
}
5495

5596
if($null -ne $MyInvocation.MyCommand)
5697
{
@@ -60,7 +101,7 @@ function Send-PageViewTelemetry
60101
$page.Properties["ModuleVersion"] = $MyInvocation.MyCommand.Module.Version.ToString()
61102
}
62103
}
63-
$page.Properties["start-time"]= $StartDateTime
104+
$page.Properties["start-time"]= $StartDateTime.ToUniversalTime().ToString("o")
64105
$page.Properties["end-time"]= (Get-Date).ToUniversalTime().ToString("o")
65106
$page.Properties["duration"]= $Duration.ToString("c");
66107

@@ -75,16 +116,11 @@ function Send-PageViewTelemetry
75116
}
76117
}
77118

78-
foreach ($Key in $page.Properties)
79-
{
80-
Write-Debug "collect telemetry data[$Key]=$($CustomProperties[$Key])"
81-
}
82-
83119
$client.TrackPageView($page)
84120

85121
try
86122
{
87-
//$client.Flush()
123+
$client.Flush()
88124
}
89125
catch
90126
{

0 commit comments

Comments
 (0)