: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(experiments): report precomputation failures to error tracking (#60512)
Anders4539f6538d27
diff --git a/posthog/hogql_queries/experiments/experiment_query_runner.py b/posthog/hogql_queries/experiments/experiment_query_runner.pyindex 1322e98b0670..4ec88975ca46 100644--- a/posthog/hogql_queries/experiments/experiment_query_runner.py+++ b/posthog/hogql_queries/experiments/experiment_query_runner.py@@ -29,6 +29,7 @@ from posthog.hogql.modifiers import create_default_modifiers_for_teamfrom posthog.hogql.query import execute_hogql_queryfrom posthog.clickhouse.query_tagging import Product, tag_queries+from posthog.exceptions_capture import capture_exceptionfrom posthog.hogql_queries.experiments import CONTROL_VARIANT_KEY, MULTIPLE_VARIANT_KEYfrom posthog.hogql_queries.experiments.base_query_utils import get_experiment_date_rangefrom posthog.hogql_queries.experiments.cuped_config import get_cuped_config@@ -342,8 +343,18 @@ class ExperimentQueryRunner(QueryRunner):self._is_precomputed = Trueelse:logger.warning("exposure_lazy_computation_not_ready", experiment_id=self.experiment.id)- except Exception:- logger.exception("exposure_lazy_computation_failed", experiment_id=self.experiment.id)+ except Exception as e:+ # Swallowed: the direct-scan fallback below still returns results, which would+ # otherwise hide a broken precomputation path. Report so it isn't silent.+ capture_exception(+ e,+ additional_properties={+ "tag": "exposure_lazy_computation_failed",+ "experiment_id": self.experiment.id,+ "precomputation_path": "exposure",+ "metric_type": self.metric.metric_type,+ },+ )# Precompute metric events for ordered funnel metrics. CUPED extends the# funnel scan back by `lookback_days` to source the pre-exposure covariate;@@ -361,8 +372,16 @@ class ExperimentQueryRunner(QueryRunner):builder.metric_events_preaggregation_job_ids = [str(job_id) for job_id in metric_result.job_ids]else:logger.warning("metric_events_lazy_computation_not_ready", experiment_id=self.experiment.id)- except Exception:- logger.exception("metric_events_lazy_computation_failed", experiment_id=self.experiment.id)+ except Exception as e:+ capture_exception(+ e,+ additional_properties={+ "tag": "metric_events_lazy_computation_failed",+ "experiment_id": self.experiment.id,+ "precomputation_path": "metric_events",+ "metric_type": self.metric.metric_type,+ },+ )return builder.build_query()diff --git a/posthog/hogql_queries/experiments/test/test_experiment_exposure_preaggregation.py b/posthog/hogql_queries/experiments/test/test_experiment_exposure_preaggregation.pyindex 30c8e886d925..db2d5cd6bf85 100644--- a/posthog/hogql_queries/experiments/test/test_experiment_exposure_preaggregation.py+++ b/posthog/hogql_queries/experiments/test/test_experiment_exposure_preaggregation.py@@ -2,12 +2,15 @@ from datetime import UTC, datetimefrom typing import castfrom posthog.test.base import _create_event, _create_person-from unittest.mock import patch+from unittest.mock import MagicMock, patchfrom django.test import override_settings+from parameterized import parameterized+from posthog.schema import (EventsNode,+ ExperimentFunnelMetric,ExperimentMeanMetric,ExperimentMetricMathType,ExperimentQuery,@@ -680,7 +683,13 @@ class TestExperimentExposurePreaggregation(ExperimentQueryRunnerBaseTest):assert direct_result.variant_results is not Noneassert direct_result.variant_results[0].number_of_samples == 1- def test_falls_back_to_events_scan_on_lazy_computation_failure(self):+ @parameterized.expand(+ [+ ("exposure", "_ensure_exposures_precomputed", "_ensure_metric_events_precomputed"),+ ("metric_events", "_ensure_metric_events_precomputed", "_ensure_exposures_precomputed"),+ ]+ )+ def test_falls_back_to_events_scan_on_lazy_computation_failure(self, expected_path, failing_method, other_method):feature_flag = self.create_feature_flag()experiment = self.create_experiment(feature_flag=feature_flag,@@ -689,9 +698,9 @@ class TestExperimentExposurePreaggregation(ExperimentQueryRunnerBaseTest):)self._enable_precomputation()- metric = ExperimentMeanMetric(- source=EventsNode(event="purchase", math=ExperimentMetricMathType.TOTAL),- )+ # An ordered funnel metric reaches both precomputation paths (exposures and metric events),+ # so a single test body can cover either failure by parameterizing which one raises.+ metric = ExperimentFunnelMetric(series=[EventsNode(event="purchase")])experiment.metrics = [metric.model_dump(mode="json")]experiment.save()@@ -723,10 +732,21 @@ class TestExperimentExposurePreaggregation(ExperimentQueryRunnerBaseTest):properties={feature_flag_property: "test"},)- with patch.object(ExperimentQueryRunner, "_ensure_exposures_precomputed", side_effect=Exception("boom")):+ with (+ patch.object(ExperimentQueryRunner, failing_method, side_effect=Exception("boom")),+ # The non-failing path returns "not ready" so it neither errors nor hits ClickHouse,+ # keeping the assertion scoped to the single failure under test.+ patch.object(ExperimentQueryRunner, other_method, return_value=MagicMock(ready=False)),+ patch("posthog.hogql_queries.experiments.experiment_query_runner.capture_exception") as mock_capture,+ ):result = self._run_experiment(experiment, metric)assert result.baseline is not Noneassert result.baseline.number_of_samples == 3assert result.variant_results is not Noneassert result.variant_results[0].number_of_samples == 3++ # The fallback keeps the query working, but the precomputation failure must still+ # be surfaced to error tracking rather than silently masked.+ mock_capture.assert_called_once()+ assert mock_capture.call_args.kwargs["additional_properties"]["precomputation_path"] == expected_path