Skip to content

Commit

Permalink
Support telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
dingmeng-xue committed Sep 4, 2020
1 parent 79d1ed0 commit 8192c7a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
7 changes: 7 additions & 0 deletions tools/Az.Tools.Installer/exports/Install-AzModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ function Install-AzModule{

process {

$cmdStarted = Get-Date

Import-Module "$PSScriptRoot\..\internal\utils.psm1"

$author = 'Microsoft Corporation'
Expand Down Expand Up @@ -214,5 +216,10 @@ function Install-AzModule{
$_ | Install-Module -Repository $Repository -AllowClobber -Force -AllowPrerelease:$allow_prerelease -SkipPublisherCheck:$skip_publisher_check
}
}

Send-PageViewTelemetry -SourcePSCmdlet $PSCmdlet `
-IsSuccess $true `
-StartDateTime $cmdStarted `
-Duration ((Get-Date) - $cmdStarted)
}
}
1 change: 1 addition & 0 deletions tools/Az.Tools.Installer/internal/Classes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ class Constants
static [System.String] $PublicTelemetryInstrumentationKey = "7df6ff70-8353-4672-80d6-568517fed090"
static [System.String] $CurrentSessionId = [System.GUID]::NewGuid().ToString()
static [Microsoft.ApplicationInsights.TelemetryClient] $TelemetryClient = $null
static [System.String] $HashMacAddress = $null
}
68 changes: 52 additions & 16 deletions tools/Az.Tools.Installer/internal/Send-PageViewTelemetry.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ function Send-PageViewTelemetry
(
[Parameter(
Mandatory=$true,
HelpMessage='Specify the page or operation name.')]
[System.Management.Automation.CommandInfo]
HelpMessage='Specify the source cmdlet object.')]
[System.Management.Automation.PSCmdlet]
[ValidateNotNullOrEmpty()]
$CommandInfo,
$SourcePSCmdlet,

[Parameter(
Mandatory=$false,
HelpMessage='Specify the parameter set name.')]
[System.Boolean]
$IsSuccess,

[Parameter(
Mandatory=$false,
Expand All @@ -31,26 +37,61 @@ function Send-PageViewTelemetry
)
Process
{
if($null -eq [Constants]::TelemetryClient)
{
if($null -eq [Constants]::TelemetryClient) {
Write-Verbose -Message 'Initialize telemetry client'
$TelemetryClient = New-Object Microsoft.ApplicationInsights.TelemetryClient
$TelemetryClient.InstrumentationKey = [Constants]::PublicTelemetryInstrumentationKey
$TelemetryClient.Context.Session.Id = $CurrentSessionId
$TelemetryClient.Context.Device.OperatingSystem = [System.Environment]::OSVersion.ToString()
[Constants]::TelemetryClient = $TelemetryClient
}

if([string]::IsNullOrWhiteSpace([Constants]::HashMacAddress)) {
Write-Verbose -Message 'hash mac address'
$macAddress = ''
$nics = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
foreach ($nic in $nics) {
if($nic.OperationalStatus -eq 'Up' -and -not [string]::IsNullOrWhiteSpace($nic.GetPhysicalAddress())) {
$macAddress = $nic.GetPhysicalAddress().ToString()
}
}

if($macAddress -ne '') {
$bytes = [System.Text.Encoding]::UTF8.GetBytes($macAddress)
$sha256 = New-Object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider
$macAddress = [System.BitConverter]::ToString($sha256.ComputeHash($bytes))
$macAddress = $macAddress.Replace('-', '').ToLowerInvariant()
}
[Constants]::HashMacAddress = $macAddress
}

$client = [Constants]::TelemetryClient

$page = New-Object Microsoft.ApplicationInsights.DataContracts.PageViewTelemetry
$page.Name = "cmdletInvocation"
$page.Duration = $Duration

$page.Properties["IsSuccess"] = $True.ToString()
$page.Properties["OS"] = [System.Environment]::OSVersion.ToString()
if ($PSBoundParameters.ContainsKey('IsSuccess')) {
$page.Properties['IsSuccess'] = $PSBoundParameters['IsSuccess'].ToString()
} else {
$page.Properties['IsSuccess'] = $true.ToString()
}
$page.Properties['x-ms-client-request-id'] = [Constants]::CurrentSessionId
$page.Properties['OS'] = [System.Environment]::OSVersion.ToString()
$page.Properties['HostVersion'] = $PSCmdlet.Host.Version
$page.Properties['HashMacAddress'] = [Constants]::HashMacAddress
$page.Properties['PowerShellVersion'] = $PSVersionTable.PSVersion.ToString()
$page.Properties['Command'] = $SourcePSCmdlet.MyInvocation.MyCommand.Name
$page.Properties['CommandParameterSetName'] = $SourcePSCmdlet.ParameterSetName
$page.Properties['CommandInvocationName'] = $SourcePSCmdlet.MyInvocation.InvocationName

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

if($null -ne $MyInvocation.MyCommand)
{
Expand All @@ -60,7 +101,7 @@ function Send-PageViewTelemetry
$page.Properties["ModuleVersion"] = $MyInvocation.MyCommand.Module.Version.ToString()
}
}
$page.Properties["start-time"]= $StartDateTime
$page.Properties["start-time"]= $StartDateTime.ToUniversalTime().ToString("o")
$page.Properties["end-time"]= (Get-Date).ToUniversalTime().ToString("o")
$page.Properties["duration"]= $Duration.ToString("c");

Expand All @@ -75,16 +116,11 @@ function Send-PageViewTelemetry
}
}

foreach ($Key in $page.Properties)
{
Write-Debug "collect telemetry data[$Key]=$($CustomProperties[$Key])"
}

$client.TrackPageView($page)

try
{
//$client.Flush()
$client.Flush()
}
catch
{
Expand Down

0 comments on commit 8192c7a

Please sign in to comment.