Skip to content

Commit 4b48946

Browse files
Fix telemetry failure for long-running sessions (#355)
The telemetry code was creating a singleton base telemetry event the first time that telemetry was being sent. That base event included the date. If a PowerShell session lasted multiple days, the telemetry reporting would eventually start to fail because the date being reported for the event was too old. We now just create a new telemetry event every time one is requested (this also ensures that it honors changes to the ApplicationInsightsKey), and now we only cache a GUID that we use to represent the "session" ID which we consider to be the duration of the current PowerShell session.
1 parent fe881f6 commit 4b48946

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

Telemetry.ps1

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4-
# Singleton. Don't directly access this though....always get it
5-
# by calling Get-BaseTelemetryEvent to ensure that it has been initialized and that you're always
6-
# getting a fresh copy.
7-
$script:GHBaseTelemetryEvent = $null
4+
# Maintain a consistent ID for this PowerShell session that we'll use as our telemetry's session ID.
5+
$script:TelemetrySessionId = [System.GUID]::NewGuid().ToString()
6+
7+
# Tracks if we've seen the telemetry reminder this session.
8+
$script:SeenTelemetryReminder = $false
89

910
function Get-PiiSafeString
1011
{
@@ -76,40 +77,37 @@ function Get-BaseTelemetryEvent
7677
[CmdletBinding()]
7778
param()
7879

79-
if ($null -eq $script:GHBaseTelemetryEvent)
80+
if ((-not $script:SeenTelemetryReminder) -and
81+
(-not (Get-GitHubConfiguration -Name SuppressTelemetryReminder)))
8082
{
81-
if (-not (Get-GitHubConfiguration -Name SuppressTelemetryReminder))
82-
{
83-
Write-Log -Message 'Telemetry is currently enabled. It can be disabled by calling "Set-GitHubConfiguration -DisableTelemetry". Refer to USAGE.md#telemetry for more information. Stop seeing this message in the future by calling "Set-GitHubConfiguration -SuppressTelemetryReminder".'
84-
}
83+
Write-Log -Message 'Telemetry is currently enabled. It can be disabled by calling "Set-GitHubConfiguration -DisableTelemetry". Refer to USAGE.md#telemetry for more information. Stop seeing this message in the future by calling "Set-GitHubConfiguration -SuppressTelemetryReminder".'
84+
$script:SeenTelemetryReminder = $true
85+
}
8586

86-
$username = Get-PiiSafeString -PlainText $env:USERNAME
87-
88-
$script:GHBaseTelemetryEvent = [PSCustomObject] @{
89-
'name' = 'Microsoft.ApplicationInsights.66d83c523070489b886b09860e05e78a.Event'
90-
'time' = (Get-Date).ToUniversalTime().ToString("O")
91-
'iKey' = (Get-GitHubConfiguration -Name ApplicationInsightsKey)
92-
'tags' = [PSCustomObject] @{
93-
'ai.user.id' = $username
94-
'ai.session.id' = [System.GUID]::NewGuid().ToString()
95-
'ai.application.ver' = $MyInvocation.MyCommand.Module.Version.ToString()
96-
'ai.internal.sdkVersion' = '2.0.1.33027' # The version this schema was based off of.
97-
}
87+
$username = Get-PiiSafeString -PlainText $env:USERNAME
88+
89+
return [PSCustomObject] @{
90+
'name' = 'Microsoft.ApplicationInsights.66d83c523070489b886b09860e05e78a.Event'
91+
'time' = (Get-Date).ToUniversalTime().ToString("O")
92+
'iKey' = (Get-GitHubConfiguration -Name ApplicationInsightsKey)
93+
'tags' = [PSCustomObject] @{
94+
'ai.user.id' = $username
95+
'ai.session.id' = $script:TelemetrySessionId
96+
'ai.application.ver' = $MyInvocation.MyCommand.Module.Version.ToString()
97+
'ai.internal.sdkVersion' = '2.0.1.33027' # The version this schema was based off of.
98+
}
9899

99-
'data' = [PSCustomObject] @{
100-
'baseType' = 'EventData'
101-
'baseData' = [PSCustomObject] @{
102-
'ver' = 2
103-
'properties' = [PSCustomObject] @{
104-
'DayOfWeek' = (Get-Date).DayOfWeek.ToString()
105-
'Username' = $username
106-
}
100+
'data' = [PSCustomObject] @{
101+
'baseType' = 'EventData'
102+
'baseData' = [PSCustomObject] @{
103+
'ver' = 2
104+
'properties' = [PSCustomObject] @{
105+
'DayOfWeek' = (Get-Date).DayOfWeek.ToString()
106+
'Username' = $username
107107
}
108108
}
109109
}
110110
}
111-
112-
return $script:GHBaseTelemetryEvent.PSObject.Copy() # Get a new instance, not a reference
113111
}
114112

115113
function Invoke-SendTelemetryEvent

0 commit comments

Comments
 (0)