Repository navigation
fix: don't let telemetry timers block Angular from becoming stable - #302
Conversation
App Insights keeps a repeating timer alive in the background to batch and retry sending events. If that timer gets scheduled while running inside Angular's zone, Angular never sees the zone go quiet, so it never thinks the app is "stable" - this blocks hydration and can delay it forever while telemetry keeps flowing in. This runs trackPageView (on route change) and processTelemetry outside Angular's zone, using Zone.root.run the same way NgZone.runOutsideAngular does. Since the zone context carries through the rest of the synchronous call chain, this also moves the telemetry SDK's own batch/retry timer out of Angular's zone, without needing any changes to that SDK itself. This does not affect the separate flush-on-unload code path, which already sends synchronously via sendBeacon and never touches this timer at all, so event delivery on page unload is unaffected. Fixes microsoft#117.
|
Hector Hernandez (@hectorhdzg) a friendly ping on this, an easy production tested change. |
|
Thanks for your contribution Artur (@arturovt), could you add a regression test that initializes App Insights with loadAppInsights() inside NgZone, then verifies ApplicationRef.isStable becomes true before the SDK’s 10-second timer expires? Testing only processTelemetry() isn’t enough because the original reporter also had to move SDK initialization outside Angular. Also, why use Zone.root.run() instead of an injected NgZone.runOutsideAngular()? Jumping to root can bypass custom parent-zone behavior. |
Adds a regression test for microsoft#117 that doesn't stub out the channel/Sender like the other spec does - it spins up a real ApplicationInsights instance (the same loadAppInsights() call every app makes) so it exercises the actual timers the SDK schedules, not a mock. This also answers a review question about scope: this plugin only wraps two entry points it owns (trackPageView on route change, and processTelemetry), so it can only keep timers that flow through those out of Angular's zone. It has no way to touch timers AppInsightsCore itself arms directly inside loadAppInsights() - like its diagnosticLogInterval, which defaults to 10s and keeps re-arming itself for as long as the SDK is alive. That call belongs to the app, not this plugin. So the test has two phases: it calls loadAppInsights() itself outside NgZone (the pattern now documented in the README), then separately triggers a route change *inside* NgZone (matching a real click or navigate() call) to prove this plugin's own fix covers what's left. Reverting either the plugin fix or the recommended init pattern makes this test hang past Angular's stability check instead of resolving. Also documents the outside-NgZone init pattern in the README, since it's required for hydration to actually stay unblocked - this plugin's fix alone isn't sufficient on its own.
|
Thanks for the review Hector Hernandez (@hectorhdzg) ! Added a regression test for this — applicationinsights-angularplugin-js.hydration.spec.ts. It spins up a real ApplicationInsights instance and calls loadAppInsights() for real (not stubbing out the channel), then checks ApplicationRef.whenStable() actually resolves quickly. And yeah, you were right to push on this — writing the test made the gap pretty obvious. This plugin only owns two entry points, trackPageView (route change) and processTelemetry, so it can only keep those out of NgZone. That does include the Sender's batch/retry timer since it's reached through processTelemetry, but it can't do anything about timers that AppInsightsCore schedules directly inside loadAppInsights() itself — e.g. diagnosticLogInterval, which defaults to 10s and just keeps re-arming itself the whole time the SDK is alive. That one's armed before any of our plugin code even runs, as part of the app's own loadAppInsights() call — not something this plugin calls or controls. So the test does two things:
On Zone.root.run() vs injecting NgZone.runOutsideAngular() — two reasons we went with root:
Let me know if you want the test shaped differently or the README wording tweaked, happy to adjust. |
Hector Hernandez (hectorhdzg)
left a comment
There was a problem hiding this comment.
LGTM, thank you for taking care of this
b9ebeb9
into
microsoft:main
App Insights keeps a repeating timer alive in the background to batch and retry sending events. If that timer gets scheduled while running inside Angular's zone, Angular never sees the zone go quiet, so it never thinks the app is "stable" - this blocks hydration and can delay it forever while telemetry keeps flowing in.
This runs trackPageView (on route change) and processTelemetry outside Angular's zone, using Zone.root.run the same way NgZone.runOutsideAngular does. Since the zone context carries through the rest of the synchronous call chain, this also moves the telemetry SDK's own batch/retry timer out of Angular's zone, without needing any changes to that SDK itself.
This does not affect the separate flush-on-unload code path, which already sends synchronously via sendBeacon and never touches this timer at all, so event delivery on page unload is unaffected.
Fixes #117.