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(web-analytics): stop the reverse proxy health check from flapping (#92113)

posthog[bot]ca18df1597f7
diff --git a/products/web_analytics/backend/temporal/health_checks/reverse_proxy.py b/products/web_analytics/backend/temporal/health_checks/reverse_proxy.py
index 996e5ba77e41..6372350ee6fa 100644
--- a/products/web_analytics/backend/temporal/health_checks/reverse_proxy.py
+++ b/products/web_analytics/backend/temporal/health_checks/reverse_proxy.py
@@ -12,7 +12,13 @@ from posthog.temporal.health_checks.framework import (
from posthog.temporal.health_checks.models import HealthCheckResult
from posthog.temporal.health_checks.query import execute_clickhouse_health_team_query
-REVERSE_PROXY_LOOKBACK_DAYS = 1
+# A week-wide window, wider than the daily schedule. A project with intermittent browser
+# traffic keeps at least one browser event in the window across single zero-traffic days,
+# so its verdict stays stable instead of flapping resolved-then-firing day to day.
+REVERSE_PROXY_LOOKBACK_DAYS = 7
+# Judge a team only when the window holds this many browser events. Below it, the query
+# cannot tell "no reverse proxy" apart from "not enough browser traffic to know".
+REVERSE_PROXY_MIN_BROWSER_EVENTS = 50
REVERSE_PROXY_SQL = """
SELECT team_id
FROM events
@@ -20,7 +26,8 @@ WHERE team_id IN %(team_ids)s
AND event IN ('$pageview', '$screen')
AND timestamp >= now() - INTERVAL %(lookback_days)s DAY
GROUP BY team_id
-HAVING countIf(JSONHas(properties, '$lib_custom_api_host')) = 0
+HAVING count() >= %(min_browser_events)s
+ AND countIf(JSONHas(properties, '$lib_custom_api_host')) = 0
"""
@@ -79,6 +86,7 @@ class ReverseProxyCheck(HealthCheck):
REVERSE_PROXY_SQL,
team_ids=team_ids,
lookback_days=REVERSE_PROXY_LOOKBACK_DAYS,
+ params={"min_browser_events": REVERSE_PROXY_MIN_BROWSER_EVENTS},
)
issues: dict[int, list[HealthCheckResult]] = {}
diff --git a/products/web_analytics/backend/test/test_reverse_proxy.py b/products/web_analytics/backend/test/test_reverse_proxy.py
new file mode 100644
index 000000000000..af4d76e295e5
--- /dev/null
+++ b/products/web_analytics/backend/test/test_reverse_proxy.py
@@ -0,0 +1,47 @@
+import pytest
+from unittest.mock import MagicMock, patch
+
+from posthog.models.health_issue import HealthIssue
+
+from products.web_analytics.backend.temporal.health_checks.reverse_proxy import (
+ REVERSE_PROXY_LOOKBACK_DAYS,
+ ReverseProxyCheck,
+)
+
+_QUERY_PATH = "products.web_analytics.backend.temporal.health_checks.reverse_proxy.execute_clickhouse_health_team_query"
+
+
+ "mock_rows, expected_teams",
+ [
+ ([], set()),
+ ([(42,)], {42}),
+ ([(1,), (3,)], {1, 3}),
+ ],
+ ids=["all_healthy", "single_team_no_proxy", "multiple_teams_mixed"],
+)
+@patch(_QUERY_PATH)
+def test_detect_maps_rows_to_issues(mock_query: MagicMock, mock_rows: list, expected_teams: set) -> None:
+ mock_query.return_value = mock_rows
+
+ result = ReverseProxyCheck().detect([1, 2, 3, 42])
+
+ assert set(result.keys()) == expected_teams
+ for team_id in expected_teams:
+ issues = result[team_id]
+ assert len(issues) == 1
+ assert issues[0].severity == HealthIssue.Severity.WARNING
+
+
+@patch(_QUERY_PATH)
+def test_detect_judges_over_a_multi_day_window_with_a_minimum_event_floor(mock_query: MagicMock) -> None:
+ # Guards the flapping fix: a one-day window drops a project on any zero-traffic day, and
+ # without a floor the check flags a project it has too little browser traffic to judge.
+ mock_query.return_value = []
+
+ ReverseProxyCheck().detect([1])
+
+ _, kwargs = mock_query.call_args
+ assert REVERSE_PROXY_LOOKBACK_DAYS > 1
+ assert kwargs["lookback_days"] == REVERSE_PROXY_LOOKBACK_DAYS
+ assert kwargs["params"]["min_browser_events"] >= 1