: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
fix(experiments): metric recalculation workflow should store the CH query id (#65360)
Rodrigo Iloro61886a0f753d
diff --git a/products/experiments/backend/temporal/recalculation_logic.py b/products/experiments/backend/temporal/recalculation_logic.pyindex be3d488a0366..a6af39f4fd37 100644--- a/products/experiments/backend/temporal/recalculation_logic.py+++ b/products/experiments/backend/temporal/recalculation_logic.py@@ -326,6 +326,7 @@ def _store_result(status: str,result: dict | None,error_message: str | None,+ query_id: str | None = None,) -> None:ExperimentMetricResult.objects.update_or_create(experiment_id=experiment_id,@@ -336,7 +337,7 @@ def _store_result("query_from": query_from,"status": status,"result": result,- "query_id": None,+ "query_id": query_id,"completed_at": timezone.now() if status == ExperimentMetricResult.Status.COMPLETED else None,"error_message": error_message,},@@ -482,6 +483,12 @@ def _calculate_experiment_metric_for_recalculation_sync()recalc_fp = compute_recalc_fingerprint(config_fp, recalculation_id)+ # Deterministic per-metric-per-run id. ClickHouse stamps it into the query_id as+ # `{team_id}_{client_query_id}_{random}`, so the stored value is a greppable prefix for+ # `system.query_log` (covers every attempt, including Temporal retries). Bound before the try so the+ # failure paths can always persist it.+ client_query_id = f"experiment_metric_recalc_{recalculation_id}_{metric_uuid}"+calc_started_at = time.perf_counter()try:# Metric build + query live inside the try so unexpected shapes surface as a calculation-step failure.@@ -497,6 +504,7 @@ def _calculate_experiment_metric_for_recalculation_sync(# warehouse HogQL access control is enforced against an accountable user instead of bypassed.user=experiment.created_by,)+# Attribute CH load back to this team + product so query_log analysis can tell whose recalc is# expensive without reverse-engineering the trigger string.tag_queries(@@ -504,6 +512,7 @@ def _calculate_experiment_metric_for_recalculation_sync(team_id=state.team_id,product=Product.EXPERIMENTS,feature=Feature.CACHE_WARMUP,+ client_query_id=client_query_id,)result = runner.run(execution_mode=ExecutionMode.CALCULATE_BLOCKING_ALWAYS)result_dict = result.model_dump(mode="json")@@ -517,6 +526,7 @@ def _calculate_experiment_metric_for_recalculation_sync(status=ExperimentMetricResult.Status.COMPLETED,result=result_dict,error_message=None,+ query_id=client_query_id,)_capture_experiment_metric_event(experiment,@@ -540,6 +550,7 @@ def _calculate_experiment_metric_for_recalculation_sync(status=ExperimentMetricResult.Status.FAILED,result=None,error_message=message,+ query_id=client_query_id,)logger.warning("Experiment metric recalculation failed due to insufficient data",@@ -584,6 +595,7 @@ def _calculate_experiment_metric_for_recalculation_sync(status=ExperimentMetricResult.Status.FAILED,result=None,error_message=message,+ query_id=client_query_id,)_record_failure(recalculation_id, metric_uuid, "calculation", message)logger.exception(diff --git a/products/experiments/backend/temporal/test_recalculation_activities.py b/products/experiments/backend/temporal/test_recalculation_activities.pyindex 29c6ef001187..55a6c4c806f6 100644--- a/products/experiments/backend/temporal/test_recalculation_activities.py+++ b/products/experiments/backend/temporal/test_recalculation_activities.py@@ -453,6 +453,19 @@ class TestCalculateActivity(BaseTest):row = ExperimentMetricResult.objects.get(experiment=exp, metric_uuid="m1")assert row.query_from == exp.start_date+ def test_query_id_is_persisted_on_result_row(self):+ # The deterministic client_query_id is stamped into ClickHouse's query_id and stored on the row so a+ # metric's executions are greppable in system.query_log by the `{team}_{client_query_id}_` prefix.+ exp = self._experiment(flag_key="calc-query-id", metrics=[_mean_metric("m1")])+ recalc = self._recalc(exp, metric_uuids=["m1"])++ with patch("products.experiments.backend.temporal.recalculation_logic.ExperimentQueryRunner") as mock_runner:+ mock_runner.return_value.run.return_value.model_dump.return_value = {}+ _calculate(exp.id, "m1", str(recalc.id), _QUERY_TO)++ row = ExperimentMetricResult.objects.get(experiment=exp, metric_uuid="m1")+ assert row.query_id == f"experiment_metric_recalc_{recalc.id}_m1"+def test_multiple_failures_accumulate_in_metric_errors(self):# Two metrics fail in sequence (not in parallel — that would need threads + a real Postgres). Pins the# merge-into-dict behavior: each failure writes its own entry keyed by metric_uuid, no overwriting.