Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/System.Management.Automation/utils/Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public static class ApplicationInsightsTelemetry
// 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.
Expand Down Expand Up @@ -583,6 +586,8 @@ internal static void SendTelemetryMetric(TelemetryType metricId, string data)
return;
}

SendPSCoreStartupTelemetry("hosted");

string metricName = metricId.ToString();
try
{
Expand Down Expand Up @@ -645,6 +650,12 @@ private static string GetModuleName(string moduleNameToValidate)
/// <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;
Expand Down
25 changes: 25 additions & 0 deletions test/powershell/engine/Basic/Telemetry.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

}