: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(web-analytics): fleet-wide demand-driven cache warming + dry-run (#72472)
Lucas Ricoydd4b36f47204
diff --git a/posthog/dags/locations/web_analytics.py b/posthog/dags/locations/web_analytics.pyindex fe023a9abe35..6f5a4c4b5963 100644--- a/posthog/dags/locations/web_analytics.py+++ b/posthog/dags/locations/web_analytics.py@@ -55,6 +55,7 @@ defs = dagster.Definitions(web_preaggregated_team_selection.web_analytics_team_candidates_job,web_analytics_watchdog.web_analytics_watchdog_job,cache_warming.web_analytics_cache_warming_job,+ cache_warming.web_analytics_cache_warming_dry_run_job,eager_web_analytics_precompute.web_analytics_eager_baseline_warming_job,eager_web_analytics_precompute.web_analytics_eager_backfill_job,web_dimensional_precompute.web_dimensional_precompute_job,diff --git a/posthog/settings/dynamic_settings.py b/posthog/settings/dynamic_settings.pyindex 27284fcd43d6..4599ef0b98b8 100644--- a/posthog/settings/dynamic_settings.py+++ b/posthog/settings/dynamic_settings.py@@ -301,6 +301,11 @@ CONSTANCE_CONFIG = {"Teams that will have web analytics cache warming enabled",list[int],),+ "WEB_ANALYTICS_WARMING_MAX_SHAPES": (+ get_from_env("WEB_ANALYTICS_WARMING_MAX_SHAPES", default=20000, type_cast=int),+ "Cap on the number of hot query shapes web analytics warming selects fleet-wide per run",+ int,+ ),}SETTINGS_ALLOWING_API_OVERRIDE = (@@ -356,6 +361,7 @@ SETTINGS_ALLOWING_API_OVERRIDE = ("REDIRECT_APP_TO_US","WEB_ANALYTICS_WARMING_DAYS","WEB_ANALYTICS_WARMING_MIN_QUERY_COUNT",+ "WEB_ANALYTICS_WARMING_MAX_SHAPES",)# SECRET_SETTINGS can only be updated but will never be exposed through the API (we do store them plain text in the DB)diff --git a/products/web_analytics/backend/hogql_queries/test/test_web_lazy_precompute_common.py b/products/web_analytics/backend/hogql_queries/test/test_web_lazy_precompute_common.pyindex 8d5a1fa0c432..d66db9622e88 100644--- a/products/web_analytics/backend/hogql_queries/test/test_web_lazy_precompute_common.py+++ b/products/web_analytics/backend/hogql_queries/test/test_web_lazy_precompute_common.py@@ -75,6 +75,15 @@ class TestIsPrecomputeEnabledForTeam(BaseTest):def test_team_not_in_setting_with_flag_off_is_ineligible(self, _flag) -> None:assert is_precompute_enabled_for_team(self.team) is False+ @override_settings(WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS=[])+ @mock.patch(f"{_COMMON}.is_org_feature_flag_enabled", return_value=False)+ @mock.patch(f"{_COMMON}.is_background_warming_request", return_value=True)+ def test_background_warming_bypasses_org_flag(self, _warming, flag) -> None:+ # Without this bypass, the Dagster warmers (where flag evaluation is+ # unreliable) silently warm the raw path instead of building buckets.+ assert is_precompute_enabled_for_team(self.team) is True+ flag.assert_not_called()+@override_settings(WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS=[])@mock.patch(f"{_COMMON}.is_org_feature_flag_enabled", return_value=False)def test_unrestricted_team_is_enrolled_without_being_in_enrollment_list(self, flag) -> None:diff --git a/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py b/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.pyindex 4f074ca9960f..5e89c4e275a0 100644--- a/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py+++ b/products/web_analytics/backend/hogql_queries/web_lazy_precompute_common.py@@ -452,6 +452,13 @@ def is_precompute_enabled_for_team(team: Team) -> bool:return Trueif is_precompute_unrestricted_for_team(team):return True+ # Background warmers build buckets for every active team regardless of the+ # rollout flag: warming precedes read enablement, and flag evaluation is not+ # reliably available in the Dagster processes the warmers run in. User-facing+ # reads still require flag enrollment; the restricted filter-shape gate keeps+ # the warmed set bounded to canonical shapes.+ if is_background_warming_request():+ return Truereturn is_org_feature_flag_enabled(team)diff --git a/products/web_analytics/dags/cache_warming.py b/products/web_analytics/dags/cache_warming.pyindex 7be5f6c6a773..d7045ee4b2c9 100644--- a/products/web_analytics/dags/cache_warming.py+++ b/products/web_analytics/dags/cache_warming.py@@ -1,4 +1,5 @@import json+import statisticsfrom django.utils.dateparse import parse_datetime@@ -11,7 +12,6 @@ from posthog.hogql.constants import LimitContextfrom posthog.clickhouse.client import sync_executefrom posthog.clickhouse.query_tagging import Feature, tag_queriesfrom posthog.dags.common import JobOwners-from posthog.dags.common.resources import PostHogAnalyticsResourcefrom posthog.event_usage import EventSourcefrom posthog.exceptions_capture import capture_exceptionfrom posthog.hogql_queries.query_cache import DjangoCacheQueryCacheManager@@ -19,18 +19,16 @@ from posthog.hogql_queries.query_runner import get_query_runnerfrom posthog.models import Teamfrom posthog.models.instance_setting import get_instance_setting-from products.web_analytics.backend.hogql_queries.web_lazy_precompute_common import is_precompute_enabled_for_teamfrom products.web_analytics.dags.web_preaggregated_utils import check_for_concurrent_runs-STALE_WEB_QUERIES_GAUGE = Gauge(- "posthog_cache_warming_stale_web_query_gauge",- "Number of stale web queries present",- ["team_id"],+WARMING_SHAPES_SELECTED_GAUGE = Gauge(+ "posthog_web_analytics_warming_shapes_selected",+ "Number of hot query shapes selected for web analytics warming in the last run",)-PRIORITY_WEB_QUERIES_COUNTER = Counter(- "posthog_cache_warming_priority_web_queries",- "Number of priority web queries warmed",- ["team_id", "normalized_query_hash", "is_cached"],+WARMING_QUERIES_COUNTER = Counter(+ "posthog_web_analytics_warming_queries_total",+ "Web analytics warming outcomes per query shape",+ ["outcome"], # warmed | skipped_fresh | failed)cache_warming_retry_policy = RetryPolicy(@@ -41,56 +39,49 @@ cache_warming_retry_policy = RetryPolicy()-def increment_counter(team_id: int, normalized_query_hash: str, is_cached: bool):- PRIORITY_WEB_QUERIES_COUNTER.labels(- team_id=team_id,- normalized_query_hash=normalized_query_hash,- is_cached=is_cached,- ).inc()---def get_teams_enabled_for_web_analytics_cache_warming() -> list[int]:- value = get_instance_setting("WEB_ANALYTICS_WARMING_TEAMS_TO_WARM")- return value if isinstance(value, list) else []---# Query kinds that carry the `useWebAnalyticsPrecompute` per-query opt-in.+# Query kinds that carry the `useWebAnalyticsPrecompute` per-query toggle.LAZY_PRECOMPUTE_QUERY_KINDS = frozenset({"WebStatsTableQuery", "WebOverviewQuery", "WebGoalsQuery", "WebVitalsPathBreakdownQuery"})+# Per-team ceiling on selected shapes. Bounds how much hourly background compute a+# single tenant can claim by running many distinct shapes past the demand threshold+# (the queries replay outside the tenant's own request throttles).+MAX_SHAPES_PER_TEAM = 100-def maybe_opt_into_lazy_precompute(team: Team, query_json: dict) -> dict:- """Opt an enrolled team's replayed query into the lazy precompute path.- Replayed queries are the team's real production shapes, which for a- restricted-enrolled team carry no per-query opt-in (users only send one via- the UI toggle). Without this, warming such a team never computes its lazy- jobs — so enrolling a team on `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS` plus- this warming list is what populates its precompute from real query shapes- while its user-facing reads stay on the raw path (their queries still have- no opt-in). That combination is how a team is evaluated before its reads- are switched over.+def maybe_opt_into_lazy_precompute(query_json: dict) -> dict:+ """Opt a replayed query into the lazy precompute path.- The opt-in changes the runner's cache key, so the warmed Django-cache entry- differs from the one raw user reads hit — acceptable: the durable output of- warming an enrolled team is the precompute jobs (shared by query shape),- not the result-cache row.+ Replayed production shapes carry no per-query toggle (users only send one via+ the UI). Injecting an explicit `True` makes the warmer build precompute+ buckets regardless of the opt-in default in the runner's eligibility gate,+ while an explicit user `False` in the replayed shape is preserved. Whether a+ team may build buckets at all is decided by the runner's own gate — warming+ requests bypass the rollout flag there, so this injection needs no+ enablement check (flag evaluation is unreliable in Dagster anyway)."""if query_json.get("kind") not in LAZY_PRECOMPUTE_QUERY_KINDS:return query_jsonif query_json.get("useWebAnalyticsPrecompute") is not None:return query_json- if not is_precompute_enabled_for_team(team):- return query_jsonreturn {**query_json, "useWebAnalyticsPrecompute": True}def queries_to_keep_fresh(- context: dagster.OpExecutionContext, team_id: int, days: int = 7, minimum_query_count: int = 10+ context: dagster.OpExecutionContext, days: int = 7, minimum_query_count: int = 10, max_shapes: int = 20000) -> list[dict]:+ """Fleet-wide demand selection: every (team, query shape) with at least+ `minimum_query_count` runs in the window, hottest first, capped at+ `max_shapes`.++ The audience is implicit — any team with a hot shape is active on web+ analytics and benefits from warming. One batched query replaces the previous+ per-team loop, which could not scale past a handful of teams.+ """context.log.info(- f"Searching the last {days} days for team {team_id}'s queries with at least {minimum_query_count} runs."+ f"Selecting fleet-wide web analytics queries with >= {minimum_query_count} runs "+ f"in the last {days} days (cap {max_shapes} shapes).")results = sync_execute(@@ -109,7 +100,7 @@ def queries_to_keep_fresh(FROM metrics_query_log_mvWHEREtimestamp >= now() - INTERVAL %(days)s DAY- AND team_id = %(team_id)s+ AND team_id != 0AND (startsWith(query_type, 'stats_table_')-- Overview strategy variants get their own tags (no_join today,@@ -134,8 +125,15 @@ def queries_to_keep_fresh(HAVING query_count >= %(minimum_query_count)sORDER BYquery_count DESC+ LIMIT %(max_shapes_per_team)s BY team_id+ LIMIT %(max_shapes)s""",- {"team_id": team_id, "days": days, "minimum_query_count": minimum_query_count},+ {+ "days": days,+ "minimum_query_count": minimum_query_count,+ "max_shapes": max_shapes,+ "max_shapes_per_team": MAX_SHAPES_PER_TEAM,+ },)return [@@ -149,59 +147,57 @@ def queries_to_keep_fresh(]-def get_teams_for_warming_op(- context: dagster.OpExecutionContext, posthoganalytics: PostHogAnalyticsResource-) -> list[int]:- team_ids = get_teams_enabled_for_web_analytics_cache_warming()-- context.log.info(f"Found {len(team_ids)} teams for cache warming")- context.add_output_metadata({"team_count": len(team_ids), "team_ids": str(team_ids)})- return team_ids--@dagster.op-def get_queries_for_teams_op(- context: dagster.OpExecutionContext,- team_ids: list[int],-) -> dict:+def get_warmable_queries_op(context: dagster.OpExecutionContext) -> list[dict]:days = get_instance_setting("WEB_ANALYTICS_WARMING_DAYS")minimum_query_count = get_instance_setting("WEB_ANALYTICS_WARMING_MIN_QUERY_COUNT")+ max_shapes = get_instance_setting("WEB_ANALYTICS_WARMING_MAX_SHAPES")- all_queries = {}- query_count = 0- for team_id in team_ids:- queries = queries_to_keep_fresh(context, team_id, days=days, minimum_query_count=minimum_query_count)-- context.log.info(f"Loading {len(queries)} frequent web analytics queries for team {team_id}")-- STALE_WEB_QUERIES_GAUGE.labels(team_id=team_id).set(len(queries))- all_queries[team_id] = queries- query_count += len(queries)+ queries = queries_to_keep_fresh(context, days=days, minimum_query_count=minimum_query_count, max_shapes=max_shapes)+ team_count = len({q["team_id"] for q in queries})- context.log.info(f"Found {query_count} total queries to warm")- context.add_output_metadata({"query_count": query_count, "team_count": len(team_ids)})- return all_queries+ WARMING_SHAPES_SELECTED_GAUGE.set(len(queries))+ context.log.info(f"Selected {len(queries)} hot query shapes across {team_count} teams")+ context.add_output_metadata(+ {+ "query_count": len(queries),+ "team_count": team_count,+ "cap_reached": len(queries) >= max_shapes,+ }+ )+ return queries@dagster.op(retry_policy=cache_warming_retry_policy)-def warm_queries_op(context: dagster.OpExecutionContext, queries: dict) -> None:+def warm_queries_op(context: dagster.OpExecutionContext, queries: list[dict]) -> None:queries_warmed = 0queries_skipped = 0+ queries_failed = 0- for team_id, query_infos in queries.items():- try:- team = Team.objects.get(pk=team_id)- except Team.DoesNotExist:- context.log.warning(f"Team {team_id} not found, skipping")+ teams: dict[int, Team | None] = {}++ for query_info in queries:+ team_id = query_info["team_id"]+ query_json = query_info["query_json"]+ normalized_query_hash = query_info["normalized_query_hash"]++ if team_id not in teams:+ try:+ teams[team_id] = Team.objects.get(pk=team_id)+ except Team.DoesNotExist:+ context.log.warning(f"Team {team_id} not found, skipping")+ teams[team_id] = None+ team = teams[team_id]+ if team is None:continue- for query_info in query_infos:- team_id = query_info["team_id"]- query_json = query_info["query_json"]- normalized_query_hash = query_info["normalized_query_hash"]+ try:+ # Tag before any runner work so the whole request — including the lazy+ # precompute gate, which lets warming traffic through regardless of the+ # rollout flag — is classified as background warming.+ tag_queries(team_id=team_id, trigger="webAnalyticsQueryWarming", feature=Feature.CACHE_WARMUP)- query_json = maybe_opt_into_lazy_precompute(team, query_json)+ query_json = maybe_opt_into_lazy_precompute(query_json)runner = get_query_runner(query=query_json,@@ -210,48 +206,94 @@ def warm_queries_op(context: dagster.OpExecutionContext, queries: dict) -> None:)cache_manager = DjangoCacheQueryCacheManager(team_id=team.pk, cache_key=runner.get_cache_key())+ cached_data = cache_manager.get_cache_data()++ if cached_data is not None:+ last_refresh = parse_datetime(cached_data["last_refresh"])+ is_stale = runner._is_stale(last_refresh)++ if not is_stale:+ WARMING_QUERIES_COUNTER.labels(outcome="skipped_fresh").inc()+ queries_skipped += 1+ continue++ # TODO: We shouldn't try to run a query if it failed last run+ runner.run(analytics_props={"source": EventSource.CACHE_WARMING})+ WARMING_QUERIES_COUNTER.labels(outcome="warmed").inc()+ queries_warmed += 1++ except Exception as e:+ context.log.exception(f"Error warming query {normalized_query_hash} for team {team_id}")+ capture_exception(e)+ WARMING_QUERIES_COUNTER.labels(outcome="failed").inc()+ queries_failed += 1++ context.log.info(f"Warmed {queries_warmed} queries ({queries_skipped} already fresh, {queries_failed} failed)")+ context.add_output_metadata(+ {"queries_warmed": queries_warmed, "queries_skipped": queries_skipped, "queries_failed": queries_failed}+ )- try:- cached_data = cache_manager.get_cache_data()-- if cached_data is not None:- last_refresh = parse_datetime(cached_data["last_refresh"])- is_stale = runner._is_stale(last_refresh)-- if not is_stale:- context.log.info(f"Query hash {normalized_query_hash} already cached, skipping warmup.")- increment_counter(team_id, normalized_query_hash, is_cached=True)- queries_skipped += 1- continue-- tag_queries(team_id=team_id, trigger="webAnalyticsQueryWarming", feature=Feature.CACHE_WARMUP)- # TODO: We shouldn't try to run a query if it failed last run- runner.run(analytics_props={"source": EventSource.CACHE_WARMING})- increment_counter(team_id, normalized_query_hash, is_cached=False)- queries_warmed += 1+def report_warming_plan_op(context: dagster.OpExecutionContext, queries: list[dict]) -> None:+ """Dry-run reporter: summarize what the warmer WOULD warm — team count, total+ query shapes, and the per-team distribution — without running (or+ precomputing) anything.- except Exception as e:- context.log.exception(f"Error warming query for team {team_id}")- capture_exception(e)+ Reuses the real selection op, so the counts reflect exactly what a live run+ at the current settings would touch.+ """+ shapes_per_team: dict[int, int] = {}+ for q in queries:+ shapes_per_team[q["team_id"]] = shapes_per_team.get(q["team_id"], 0) + 1+ per_team = sorted(shapes_per_team.items(), key=lambda x: -x[1])+ shape_counts = [c for _, c in per_team]+ total_underlying_requests = sum(q["query_count"] for q in queries)+ median_shapes = statistics.median(shape_counts) if shape_counts else 0- context.log.info(f"Warmed {queries_warmed} queries ({queries_skipped} were already cached)")- context.add_output_metadata({"queries_warmed": queries_warmed, "queries_skipped": queries_skipped})+ context.log.info(+ f"DRY RUN — would warm {len(queries)} query shapes across {len(per_team)} teams "+ f"(~{total_underlying_requests} underlying requests over the warming window). "+ f"Per-team shapes: max={shape_counts[0] if shape_counts else 0}, median={median_shapes}. "+ f"Top teams by shape count: {per_team[:10]}"+ )+ context.add_output_metadata(+ {+ "dry_run": True,+ "team_count": len(per_team),+ "total_query_shapes_to_warm": len(queries),+ "total_underlying_requests": total_underlying_requests,+ "max_shapes_per_team": shape_counts[0] if shape_counts else 0,+ "median_shapes_per_team": median_shapes,+ "top_10_teams_by_shape_count": str(per_team[:10]),+ }+ )@dagster.job(- description="Warms web analytics query cache for frequently-run queries",+ description="Warms web analytics query cache and precompute buckets for frequently-run queries fleet-wide",tags={"owner": JobOwners.TEAM_WEB_ANALYTICS.value,"dagster/web_analytics_cache_warming": "web_analytics_cache_warming",},)def web_analytics_cache_warming_job():- team_ids = get_teams_for_warming_op()- queries = get_queries_for_teams_op(team_ids)+ queries = get_warmable_queries_op()warm_queries_op(queries)+ description="Dry run: report how many web analytics query shapes cache warming would warm, without warming",+ tags={+ "owner": JobOwners.TEAM_WEB_ANALYTICS.value,+ "dagster/web_analytics_cache_warming": "web_analytics_cache_warming_dry_run",+ },+)+def web_analytics_cache_warming_dry_run_job():+ queries = get_warmable_queries_op()+ report_warming_plan_op(queries)++@dagster.schedule(cron_schedule="0 * * * *",job=web_analytics_cache_warming_job,diff --git a/products/web_analytics/dags/tests/test_cache_warming.py b/products/web_analytics/dags/tests/test_cache_warming.pyindex 6708746a4c78..c13bdbf9ee53 100644--- a/products/web_analytics/dags/tests/test_cache_warming.py+++ b/products/web_analytics/dags/tests/test_cache_warming.py@@ -1,17 +1,22 @@from posthog.test.base import BaseTest+from unittest.mock import MagicMock, patch+import dagsterfrom parameterized import parameterized-from products.web_analytics.dags.cache_warming import maybe_opt_into_lazy_precompute+from products.web_analytics.dags.cache_warming import (+ get_warmable_queries_op,+ maybe_opt_into_lazy_precompute,+ queries_to_keep_fresh,+)class TestMaybeOptIntoLazyPrecompute(BaseTest):- def test_enrolled_team_web_query_gets_opt_in(self) -> None:- # If this breaks, warming an enrolled-but-restricted team silently stops- # computing its lazy jobs — the pre-enrollment evaluation pipeline reads empty.+ def test_web_query_gets_opt_in(self) -> None:+ # If this breaks, the warmer silently stops building precompute buckets+ # for replayed shapes wherever the opt-in default still applies.query = {"kind": "WebStatsTableQuery", "properties": []}- with self.settings(WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS=[self.team.id]):- result = maybe_opt_into_lazy_precompute(self.team, query)+ result = maybe_opt_into_lazy_precompute(query)self.assertIs(result["useWebAnalyticsPrecompute"], True)self.assertNotIn("useWebAnalyticsPrecompute", query) # input not mutated@@ -19,16 +24,48 @@ class TestMaybeOptIntoLazyPrecompute(BaseTest):@parameterized.expand([# Non-web kinds don't carry the field; injecting it would break validation.- ("non_web_kind", {"kind": "TrendsQuery"}, True),- # Teams outside the enrollment lists must warm exactly what users run.- ("not_enrolled", {"kind": "WebStatsTableQuery"}, False),+ ("non_web_kind", {"kind": "TrendsQuery"}),# An explicit user opt-out in the replayed shape is preserved.- ("explicit_opt_out", {"kind": "WebStatsTableQuery", "useWebAnalyticsPrecompute": False}, True),+ ("explicit_opt_out", {"kind": "WebStatsTableQuery", "useWebAnalyticsPrecompute": False}),+ ("explicit_opt_in", {"kind": "WebOverviewQuery", "useWebAnalyticsPrecompute": True}),])- def test_leaves_query_untouched(self, _name: str, query: dict, enrolled: bool) -> None:- team_ids = [self.team.id] if enrolled else []- with self.settings(WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS=team_ids):- result = maybe_opt_into_lazy_precompute(self.team, query)+ def test_leaves_query_untouched(self, _name: str, query: dict) -> None:+ self.assertEqual(maybe_opt_into_lazy_precompute(query), query)- self.assertEqual(result, query)++class TestFleetQuerySelection(BaseTest):+ @patch("products.web_analytics.dags.cache_warming.sync_execute")+ def test_parses_fleet_rows_into_query_infos(self, mock_exec: MagicMock) -> None:+ # Guards the row-shape contract with the selection SQL: a column reorder+ # or JSON handling change would make the warmer warm nothing or crash.+ mock_exec.return_value = [+ (101, '{"kind": "WebOverviewQuery"}', 50, "hash-a"),+ (202, '{"kind": "WebStatsTableQuery"}', 12, "hash-b"),+ ]+ result = queries_to_keep_fresh(dagster.build_op_context(), days=7, minimum_query_count=10, max_shapes=100)++ self.assertEqual(+ result,+ [+ {+ "team_id": 101,+ "query_json": {"kind": "WebOverviewQuery"},+ "query_count": 50,+ "normalized_query_hash": "hash-a",+ },+ {+ "team_id": 202,+ "query_json": {"kind": "WebStatsTableQuery"},+ "query_count": 12,+ "normalized_query_hash": "hash-b",+ },+ ],+ )++ @patch("products.web_analytics.dags.cache_warming.sync_execute", return_value=[])+ def test_op_reads_instance_settings(self, _mock_exec: MagicMock) -> None:+ # Runs the op against the real instance-setting machinery so a renamed or+ # unregistered setting key fails here instead of at the hourly run.+ result = get_warmable_queries_op(dagster.build_op_context())+ self.assertEqual(result, [])