mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Handle cases where CustomEvent was not initially sent (#11807)
This commit is contained in:
@@ -87,6 +87,9 @@ namespace Microsoft.PowerShell.Telemetry
|
||||
// the session identifier
|
||||
private static string s_sessionId { get; set; }
|
||||
|
||||
// private semaphore to determine whether we sent the startup telemetry event
|
||||
private static int s_startupEventSent = 0;
|
||||
|
||||
/// Use a hashset for quick lookups.
|
||||
/// We send telemetry only a known set of modules.
|
||||
/// If it's not in the list (initialized in the static constructor), then we report anonymous.
|
||||
@@ -583,6 +586,8 @@ namespace Microsoft.PowerShell.Telemetry
|
||||
return;
|
||||
}
|
||||
|
||||
SendPSCoreStartupTelemetry("hosted");
|
||||
|
||||
string metricName = metricId.ToString();
|
||||
try
|
||||
{
|
||||
@@ -645,6 +650,12 @@ namespace Microsoft.PowerShell.Telemetry
|
||||
/// <param name="mode">The "mode" of the startup.</param>
|
||||
internal static void SendPSCoreStartupTelemetry(string mode)
|
||||
{
|
||||
// Check if we already sent startup telemetry
|
||||
if (Interlocked.CompareExchange(ref s_startupEventSent, 1, 0) == 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!CanSendTelemetry)
|
||||
{
|
||||
return;
|
||||
|
||||
@@ -128,4 +128,29 @@ Describe "Telemetry for shell startup" -Tag CI {
|
||||
$result = & $PWSH -NoProfile -Command '[Microsoft.PowerShell.Telemetry.ApplicationInsightsTelemetry]::CanSendTelemetry'
|
||||
$result | Should -Be $expectedValue
|
||||
}
|
||||
|
||||
It "Should resend startup event if the semaphore says we haven't sent telemetry" {
|
||||
|
||||
$resultJson = & $PWSH -NoProfile -c {
|
||||
$telemetryType = [Microsoft.PowerShell.Telemetry.ApplicationInsightsTelemetry]
|
||||
$bindingFlags = [System.Reflection.BindingFlags]"NonPublic,Static"
|
||||
$initialValue = ${telemetryType}.GetMember("s_startupEventSent", $bindingFlags)[0].GetValue($null)
|
||||
# force a resend of the startup telemetry
|
||||
$null = ${telemetryType}.GetMember("s_startupEventSent", $bindingFlags)[0].SetValue($null,0)
|
||||
$null = Get-Date | Out-String
|
||||
# now check it again, it should be true now that something has executed
|
||||
$finalValue = ${telemetryType}.GetMember("s_startupEventSent", $bindingFlags)[0].GetValue($null)
|
||||
@{
|
||||
initialValue = $initialValue
|
||||
finalValue = $finalValue
|
||||
} | ConvertTo-Json -Compress
|
||||
}
|
||||
|
||||
$result = $resultJson | ConvertFrom-Json
|
||||
|
||||
$result.InitialValue | Should -Be 1 -Because "Should have sent telemetry on console startup"
|
||||
|
||||
$result.FinalValue | Should -Be 1 -Because "Should have resent telemetry"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user