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.

feat(experiments): detect metric-events fallback in the precompute canary (#98186)

Juraj Majerik79bdf4e04491
diff --git a/products/experiments/backend/hogql_queries/experiment_query_runner.py b/products/experiments/backend/hogql_queries/experiment_query_runner.py
index 97473a4529ab..bc37c1359274 100644
--- a/products/experiments/backend/hogql_queries/experiment_query_runner.py
+++ b/products/experiments/backend/hogql_queries/experiment_query_runner.py
@@ -521,6 +521,17 @@ class ExperimentQueryRunner(QueryRunner):
return self._retention_metric_events_precomputation_enabled()
return False
+ @property
+ def metric_events_path(self) -> str:
+ """
+ Which source fed the metric-events side of the built query: "precomputed",
+ "direct_scan", or "not_applicable". Meaningful after _get_experiment_query()
+ has run. The exposures side is reported separately (response.is_precomputed).
+ """
+ if not self._metric_events_precompute_applicable():
+ return "not_applicable"
+ return "precomputed" if self._metric_events_precomputed else "direct_scan"
+
def _get_experiment_query(self) -> ast.SelectQuery:
"""
Returns the main experiment query.
@@ -644,10 +655,7 @@ class ExperimentQueryRunner(QueryRunner):
# Tag after _get_experiment_query() which sets the precompute flags
exposures_path = "precomputed" if self._is_precomputed else "direct_scan"
- if not self._metric_events_precompute_applicable():
- metric_events_path = "not_applicable"
- else:
- metric_events_path = "precomputed" if self._metric_events_precomputed else "direct_scan"
+ metric_events_path = self.metric_events_path
tag_queries(
experiment_exposures_path=exposures_path,
experiment_metric_events_path=metric_events_path,
diff --git a/products/experiments/backend/temporal/canary_logic.py b/products/experiments/backend/temporal/canary_logic.py
index cc8ad4099197..ef2fc63ec973 100644
--- a/products/experiments/backend/temporal/canary_logic.py
+++ b/products/experiments/backend/temporal/canary_logic.py
@@ -285,8 +285,15 @@ def evaluate_canary_runs(
return CanaryVerdict(outcome=OUTCOME_SKIPPED, detail="empty results (no exposures yet)")
# Checked before the variant-set comparison: a flipped run compares live vs frozen data, so it can
- # also explain a variant showing up in one run but not another.
- flipped = [run.label for run in (run_a, run_b) if not run.is_precomputed]
+ # also explain a variant showing up in one run but not another. Both cache sides count: a run whose
+ # metric-events build fell back to scanning events would compare that scan against run c's scan,
+ # which passes trivially without testing the cache.
+ flipped = []
+ for run in (run_a, run_b):
+ if not run.is_precomputed:
+ flipped.append(f"{run.label} (exposures)")
+ if run.metric_events_path == "direct_scan":
+ flipped.append(f"{run.label} (metric events)")
if flipped:
# A forced-precomputed run fell back to the direct scan (e.g. the lazy computation executor timed
# out). Deviation is expected — not a divergence.
@@ -351,6 +358,7 @@ def _execute_canary_run(
entry.key: CanaryVariantStats(sum=entry.sum, number_of_samples=entry.number_of_samples)
for entry in stats_entries
},
+ metric_events_path=runner.metric_events_path,
)
@@ -439,6 +447,8 @@ def run_metric_canary_sync(target: CanaryMetricTarget) -> CanaryMetricResult:
def _format_run_line(run: CanaryRunSnapshot) -> str:
path = "precomputed" if run.is_precomputed else "direct"
+ if run.metric_events_path != "not_applicable":
+ path += f", metric events {run.metric_events_path}"
variants = ", ".join(
f"{key} {stats.sum:g}/{stats.number_of_samples}" for key, stats in sorted(run.variants.items())
)
diff --git a/products/experiments/backend/temporal/models.py b/products/experiments/backend/temporal/models.py
index 4cef702dd478..f687ff79008f 100644
--- a/products/experiments/backend/temporal/models.py
+++ b/products/experiments/backend/temporal/models.py
@@ -1,6 +1,8 @@
import dataclasses
from typing import Final, Literal
+from posthog.dataclasses import frozen
+
# Shared by the workflow definition, the schedule, and the management command.
CANARY_WORKFLOW_NAME = "experiment-precompute-canary"
@@ -138,14 +140,17 @@ class CanaryVariantStats:
number_of_samples: int
+@frozen
class CanaryRunSnapshot:
"""Per-variant aggregates from one execution of the metric query."""
label: str # "a" | "b" (forced precomputed) | "c" (forced direct scan)
query_id: str # client_query_id, for system.query_log forensics
- is_precomputed: bool
+ is_precomputed: bool # exposures side
variants: dict[str, CanaryVariantStats]
+ # "precomputed" | "direct_scan" | "not_applicable". Defaulted so snapshots
+ # recorded before this field existed still decode during Temporal replay.
+ metric_events_path: str = "not_applicable"
@dataclasses.dataclass(frozen=False)
diff --git a/products/experiments/backend/temporal/test_canary_logic.py b/products/experiments/backend/temporal/test_canary_logic.py
index 3d9c3caca340..b83738fcf655 100644
--- a/products/experiments/backend/temporal/test_canary_logic.py
+++ b/products/experiments/backend/temporal/test_canary_logic.py
@@ -39,7 +39,12 @@ from products.experiments.backend.temporal.models import (
from products.feature_flags.backend.models.feature_flag import FeatureFlag
-def _snapshot(label: str, variants: dict[str, tuple[float, int]], is_precomputed: bool = True) -> CanaryRunSnapshot:
+def _snapshot(
+ label: str,
+ variants: dict[str, tuple[float, int]],
+ is_precomputed: bool = True,
+ metric_events_path: str = "not_applicable",
+) -> CanaryRunSnapshot:
return CanaryRunSnapshot(
label=label,
query_id=f"experiment-canary-test-{label}",
@@ -47,6 +52,7 @@ def _snapshot(label: str, variants: dict[str, tuple[float, int]], is_precomputed
variants={
key: CanaryVariantStats(sum=sum_, number_of_samples=samples) for key, (sum_, samples) in variants.items()
},
+ metric_events_path=metric_events_path,
)
@@ -180,9 +186,22 @@ class TestEvaluateCanaryRuns:
assert verdict.outcome == OUTCOME_PATH_FLIP
assert "a" in (verdict.detail or "")
+ def test_metric_events_fallback_is_path_flip(self):
+ verdict = evaluate_canary_runs(
+ "mean",
+ _snapshot("a", _BASE, metric_events_path="direct_scan"),
+ _snapshot("b", _BASE, metric_events_path="precomputed"),
+ _snapshot("c", _BASE, metric_events_path="direct_scan"),
+ )
+ assert verdict.outcome == OUTCOME_PATH_FLIP
+ assert "a (metric events)" in (verdict.detail or "")
+
def test_direct_run_not_precomputed_is_expected(self):
verdict = evaluate_canary_runs(
- "funnel", _snapshot("a", _BASE), _snapshot("b", _BASE), _snapshot("c", _BASE, is_precomputed=False)
+ "funnel",
+ _snapshot("a", _BASE, metric_events_path="precomputed"),
+ _snapshot("b", _BASE, metric_events_path="precomputed"),
+ _snapshot("c", _BASE, is_precomputed=False, metric_events_path="direct_scan"),
)
assert verdict.outcome == OUTCOME_PASS