|
| 1 | +function Get-ApplicationInsightsTelemetryClient |
| 2 | +{ |
| 3 | + [CmdletBinding()] |
| 4 | + param() |
| 5 | + |
| 6 | + if ($null -eq $Global:AzDnsAsCodeTelemetryEngine) |
| 7 | + { |
| 8 | + #$AI = "$PSScriptRoot\..\Dependencies\Microsoft.ApplicationInsights.dll" |
| 9 | + $AI = "C:\git\AzDnsAsCode\AzDnsAsCode\Dependencies\Microsoft.ApplicationInsights.dll" |
| 10 | + [Reflection.Assembly]::LoadFile($AI) | Out-Null |
| 11 | + |
| 12 | + $InstrumentationKey = "1b8dc953-96b6-4876-b127-b0d81e911831" |
| 13 | + if ($null -ne $env:AzDnsAsCodeTelemetryInstrumentationKey) |
| 14 | + { |
| 15 | + $InstrumentationKey = $env:AzDnsAsCodeTelemetryInstrumentationKey |
| 16 | + } |
| 17 | + $TelClient = [Microsoft.ApplicationInsights.TelemetryClient]::new() |
| 18 | + $TelClient.InstrumentationKey = $InstrumentationKey |
| 19 | + |
| 20 | + $Global:AzDnsAsCodeTelemetryEngine = $TelClient |
| 21 | + } |
| 22 | + return $Global:AzDnsAsCodeTelemetryEngine |
| 23 | +} |
| 24 | + |
| 25 | +function Add-AzDnsAsCodeTelemetryEvent |
| 26 | +{ |
| 27 | + [CmdletBinding()] |
| 28 | + param( |
| 29 | + [Parameter()] |
| 30 | + [System.String] |
| 31 | + $Type = 'Flow', |
| 32 | + |
| 33 | + [Parameter()] |
| 34 | + [System.Collections.Generic.Dictionary[[System.String], [System.String]]] |
| 35 | + $Data, |
| 36 | + |
| 37 | + [Parameter()] |
| 38 | + [System.Collections.Generic.Dictionary[[System.String], [System.Double]]] |
| 39 | + $Metrics |
| 40 | + ) |
| 41 | + |
| 42 | + $TelemetryEnabled = [System.Environment]::GetEnvironmentVariable('AzDnsAsCodeTelemetryEnabled', ` |
| 43 | + [System.EnvironmentVariableTarget]::Machine) |
| 44 | + |
| 45 | + if ($null -eq $TelemetryEnabled -or $TelemetryEnabled -eq $true) |
| 46 | + { |
| 47 | + $TelemetryClient = Get-ApplicationInsightsTelemetryClient |
| 48 | + |
| 49 | + try |
| 50 | + { |
| 51 | + if ($null -ne $Data.TenantId) |
| 52 | + { |
| 53 | + $principalValue = $Data.TenantId |
| 54 | + $Data.Add("Tenant", $principalValue) |
| 55 | + } |
| 56 | + |
| 57 | + $Data.Remove("TenandId") | Out-Null |
| 58 | + |
| 59 | + # Capture PowerShell Version Info |
| 60 | + $Data.Add("PSMainVersion", $PSVersionTable.PSVersion.Major.ToString() + "." + $PSVersionTable.PSVersion.Minor.ToString()) |
| 61 | + $Data.Add("PSVersion", $PSVersionTable.PSVersion.ToString()) |
| 62 | + $Data.Add("PSEdition", $PSVersionTable.PSEdition.ToString()) |
| 63 | + |
| 64 | + # Capture Console/Host Information |
| 65 | + if ($host.Name -eq "ConsoleHost" -and $null -eq $env:WT_SESSION) |
| 66 | + { |
| 67 | + $Data.Add("PowerShellAgent", "Console") |
| 68 | + } |
| 69 | + elseif ($host.Name -eq "Windows PowerShell ISE Host") |
| 70 | + { |
| 71 | + $Data.Add("PowerShellAgent", "ISE") |
| 72 | + } |
| 73 | + elseif ($host.Name -eq "ConsoleHost" -and $null -ne $env:WT_SESSION) |
| 74 | + { |
| 75 | + $Data.Add("PowerShellAgent", "Windows Terminal") |
| 76 | + } |
| 77 | + elseif ($host.Name -eq "ConsoleHost" -and $null -eq $env:WT_SESSION -and ` |
| 78 | + $null -ne $env:BUILD_BUILDID -and $env:SYSTEM -eq "build") |
| 79 | + { |
| 80 | + $Data.Add("PowerShellAgent", "Azure DevOPS") |
| 81 | + $Data.Add("AzureDevOPSPipelineType", "Build") |
| 82 | + $Data.Add("AzureDevOPSAgent", $env:POWERSHELL_DISTRIBUTION_CHANNEL) |
| 83 | + } |
| 84 | + elseif ($host.Name -eq "ConsoleHost" -and $null -eq $env:WT_SESSION -and ` |
| 85 | + $null -ne $env:BUILD_BUILDID -and $env:SYSTEM -eq "release") |
| 86 | + { |
| 87 | + $Data.Add("PowerShellAgent", "Azure DevOPS") |
| 88 | + $Data.Add("AzureDevOPSPipelineType", "Release") |
| 89 | + $Data.Add("AzureDevOPSAgent", $env:POWERSHELL_DISTRIBUTION_CHANNEL) |
| 90 | + } |
| 91 | + elseif ($host.Name -eq "Default Host" -and ` |
| 92 | + $null -ne $env:APPSETTING_FUNCTIONS_EXTENSION_VERSION) |
| 93 | + { |
| 94 | + $Data.Add("PowerShellAgent", "Azure Function") |
| 95 | + $Data.Add("AzureFunctionWorkerVersion", $env:FUNCTIONS_WORKER_RUNTIME_VERSION) |
| 96 | + } |
| 97 | + elseif ($host.Name -eq "CloudShell") |
| 98 | + { |
| 99 | + $Data.Add("PowerShellAgent", "Cloud Shell") |
| 100 | + } |
| 101 | + |
| 102 | + [array]$version = (Get-Module 'AzDnsAsCode').Version | Sort-Object -Descending |
| 103 | + $Data.Add("AzDnsAsCodeVersion", $version[0].ToString()) |
| 104 | + |
| 105 | + # Get Dependencies loaded versions |
| 106 | + try |
| 107 | + { |
| 108 | + $currentPath = Join-Path -Path $PSScriptRoot -ChildPath '..\' -Resolve |
| 109 | + $manifest = Import-PowerShellDataFile "$currentPath/AzDnsAsCode.psd1" |
| 110 | + $dependencies = $manifest.RequiredModules |
| 111 | + |
| 112 | + $dependenciesContent = "" |
| 113 | + foreach ($dependency in $dependencies) |
| 114 | + { |
| 115 | + $dependenciesContent += Get-Module $dependency.ModuleName | Out-String |
| 116 | + } |
| 117 | + $Data.Add("DependenciesVersion", $dependenciesContent) |
| 118 | + } |
| 119 | + catch |
| 120 | + { |
| 121 | + Write-Verbose -Message $_ |
| 122 | + } |
| 123 | + |
| 124 | + $TelemetryClient.TrackEvent($Type, $Data, $Metrics) |
| 125 | + $TelemetryClient.Flush() |
| 126 | + } |
| 127 | + catch |
| 128 | + { |
| 129 | + Write-Error $_ |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +function Set-AzDnsAsCodeTelemetryOption |
| 135 | +{ |
| 136 | + [CmdletBinding()] |
| 137 | + param( |
| 138 | + [Parameter()] |
| 139 | + [System.Boolean] |
| 140 | + $Enabled, |
| 141 | + |
| 142 | + [Parameter()] |
| 143 | + [System.String] |
| 144 | + $InstrumentationKey |
| 145 | + ) |
| 146 | + |
| 147 | + if ($null -ne $Enabled) |
| 148 | + { |
| 149 | + [System.Environment]::SetEnvironmentVariable('AzDnsAsCodeTelemetryEnabled', $Enabled, ` |
| 150 | + [System.EnvironmentVariableTarget]::Machine) |
| 151 | + } |
| 152 | + |
| 153 | + if ($null -ne $InstrumentationKey) |
| 154 | + { |
| 155 | + [System.Environment]::SetEnvironmentVariable('AzDnsAsCodeTelemetryInstrumentationKey', $InstrumentationKey, ` |
| 156 | + [System.EnvironmentVariableTarget]::Machine) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +function Get-AzDnsAsCodeTelemetryOption |
| 161 | +{ |
| 162 | + [CmdletBinding()] |
| 163 | + param() |
| 164 | + |
| 165 | + try |
| 166 | + { |
| 167 | + return @{ |
| 168 | + Enabled = [System.Environment]::GetEnvironmentVariable('AzDnsAsCodeTelemetryEnabled', ` |
| 169 | + [System.EnvironmentVariableTarget]::Machine) |
| 170 | + InstrumentationKey = [System.Environment]::GetEnvironmentVariable('AzDnsAsCodeTelemetryInstrumentationKey', ` |
| 171 | + [System.EnvironmentVariableTarget]::Machine) |
| 172 | + } |
| 173 | + } |
| 174 | + catch |
| 175 | + { |
| 176 | + throw $_ |
| 177 | + } |
| 178 | +} |
0 commit comments