Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(error-tracking): stop flagging search typing as kea router url loops (#71721)

Radu Raicea877c272486d8
diff --git a/frontend/src/lib/logic/scenes/urlChangeTracker.test.ts b/frontend/src/lib/logic/scenes/urlChangeTracker.test.ts
index 8d00eb4a7c6a..5dad6fbafdc2 100644
--- a/frontend/src/lib/logic/scenes/urlChangeTracker.test.ts
+++ b/frontend/src/lib/logic/scenes/urlChangeTracker.test.ts
@@ -69,7 +69,7 @@ describe('getUrlChangeTracker', () => {
const insightsTracker = getUrlChangeTracker('insightsLogic')
for (let i = 0; i < 6; i++) {
- webTracker.recordChange(`/web?v=${i}`, 'webAnalyticsLogic', 'setFilters')
+ webTracker.recordChange('/web?foo=bar', 'webAnalyticsLogic', 'setFilters')
}
expect(webTracker.isRapidlyChanging()).toBe(true)
@@ -136,13 +136,34 @@ describe('UrlChangeTracker', () => {
{ count: 4, expected: false },
{ count: 5, expected: true },
{ count: 10, expected: true },
- ])('returns $expected when $count changes recorded', ({ count, expected }) => {
+ ])('flags a loop re-setting the same URL: $expected at $count changes', ({ count, expected }) => {
for (let i = 0; i < count; i++) {
- tracker.recordChange(`/web?v=${i}`, 'webAnalyticsLogic', 'setFilters')
+ tracker.recordChange('/web?foo=bar', 'webAnalyticsLogic', 'setFilters')
}
expect(tracker.isRapidlyChanging()).toBe(expected)
})
+
+ it('does not flag a burst of distinct URLs, e.g. typing into a search box', () => {
+ // Each keystroke syncs a new search term to the query string, so every URL differs.
+ // This is legitimate navigation, not the infinite loop the tracker exists to catch.
+ for (let i = 0; i < 10; i++) {
+ tracker.recordChange(`/dashboard?search=${i}`, 'dashboardsLogic', 'setSearch')
+ }
+
+ expect(tracker.isRapidlyChanging()).toBe(false)
+ })
+
+ it('flags an extreme burst of distinct URLs as a loop', () => {
+ // A runaway loop that mutates the URL each iteration (e.g. an incrementing counter) never
+ // repeats a URL, so the duplicate heuristic alone would miss it. The sheer volume - far
+ // beyond human typing - must still be caught.
+ for (let i = 0; i < 60; i++) {
+ tracker.recordChange(`/web?loop=${i}`, 'webAnalyticsLogic', 'setFilters')
+ }
+
+ expect(tracker.isRapidlyChanging()).toBe(true)
+ })
})
describe('warning throttle', () => {
@@ -239,11 +260,11 @@ describe('UrlChangeTracker', () => {
maxChangesPerSecond: 2,
})
- customTracker.recordChange('/web?v=1', 'webAnalyticsLogic', 'setFilters')
- customTracker.recordChange('/web?v=2', 'webAnalyticsLogic', 'setFilters')
+ customTracker.recordChange('/web?foo=bar', 'webAnalyticsLogic', 'setFilters')
+ customTracker.recordChange('/web?foo=bar', 'webAnalyticsLogic', 'setFilters')
expect(customTracker.isRapidlyChanging()).toBe(false)
- customTracker.recordChange('/web?v=3', 'webAnalyticsLogic', 'setFilters')
+ customTracker.recordChange('/web?foo=bar', 'webAnalyticsLogic', 'setFilters')
expect(customTracker.isRapidlyChanging()).toBe(true)
})
diff --git a/frontend/src/lib/logic/scenes/urlChangeTracker.ts b/frontend/src/lib/logic/scenes/urlChangeTracker.ts
index 7f913cccdd43..46f899ba4ed2 100644
--- a/frontend/src/lib/logic/scenes/urlChangeTracker.ts
+++ b/frontend/src/lib/logic/scenes/urlChangeTracker.ts
@@ -56,12 +56,17 @@ interface UrlChangeTrackerConfig {
maxChangesPerSecond: number
windowMs: number
throttleWarningMs: number
+ loopBurstThreshold: number
}
const DEFAULT_CONFIG: UrlChangeTrackerConfig = {
maxChangesPerSecond: 4,
windowMs: 3000,
throttleWarningMs: 60000,
+ // Above this many changes in the window we always treat the burst as a loop, even when every URL
+ // is distinct. ~16 changes/sec sustained over the window is far beyond human typing but trivially
+ // reached by a runaway router loop, so this catches loops that mutate the URL each iteration.
+ loopBurstThreshold: 50,
}
class UrlChangeTracker {
@@ -84,7 +89,22 @@ class UrlChangeTracker {
}
isRapidlyChanging(): boolean {
- return this.changes.length > this.config.maxChangesPerSecond
+ if (this.changes.length <= this.config.maxChangesPerSecond) {
+ return false
+ }
+ // A runaway loop fires updates far faster and for longer than any human could, so an extreme
+ // burst is always a loop - including one that mutates the URL each iteration (an incrementing
+ // counter, timestamp, or re-serialized param) and so never repeats a URL.
+ if (this.changes.length >= this.config.loopBurstThreshold) {
+ return true
+ }
+ // Below that ceiling, a genuine loop re-sets the same handful of URLs over and over, so the
+ // burst is dominated by duplicates. Legitimate rapid navigation - e.g. a user typing into a
+ // search box that syncs each keystroke to the query string - produces a stream of mostly-distinct
+ // URLs and must not be flagged as a loop. Only treat the burst as rapid when at most half of
+ // the recorded changes are distinct URLs.
+ const uniqueUrls = new Set(this.changes.map((c) => c.url)).size
+ return uniqueUrls * 2 <= this.changes.length
}
canWarn(): boolean {