: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): tag team_id on precompute canary queries (#90719)
Juraj Majerikaefba27bc8cb
diff --git a/products/experiments/backend/temporal/canary_logic.py b/products/experiments/backend/temporal/canary_logic.pyindex c144c4622308..cc8ad4099197 100644--- a/products/experiments/backend/temporal/canary_logic.py+++ b/products/experiments/backend/temporal/canary_logic.py@@ -335,7 +335,11 @@ def _execute_canary_run(# resolve warehouse tables here instead of failing closed.bypass_warehouse_access_control=True,)- with tags_context(client_query_id=query_id, trigger="experiment_precompute_canary"):+ # team_id has to accompany client_query_id: internal sub-queries during SQL generation (e.g. the+ # materialized-columns cache refresh) inherit these tags, and sync_execute rejects a client_query_id+ # that arrives without a team_id. Web requests tag team_id at request start; temporal workers don't,+ # so tag it here.+ with tags_context(client_query_id=query_id, team_id=experiment.team_id, trigger="experiment_precompute_canary"):response = runner.calculate()stats_entries = [*([response.baseline] if response.baseline else []), *(response.variant_results or [])]diff --git a/products/experiments/backend/temporal/test_canary_logic.py b/products/experiments/backend/temporal/test_canary_logic.pyindex 5c317b4fc13e..3d9c3caca340 100644--- a/products/experiments/backend/temporal/test_canary_logic.py+++ b/products/experiments/backend/temporal/test_canary_logic.py@@ -11,6 +11,8 @@ from django.utils import timezoneimport requestsfrom parameterized import parameterized+from posthog.clickhouse.query_tagging import get_query_tags+from products.experiments.backend.models.experiment import Experiment, ExperimentSavedMetric, ExperimentToSavedMetricfrom products.experiments.backend.models.team_experiments_config import TeamExperimentsConfigfrom products.experiments.backend.temporal.canary_logic import (@@ -421,6 +423,25 @@ class TestRunMetricCanary(BaseTest):assert modes == ["precomputed", "precomputed", "direct"]assert len(result.runs) == 3+ def test_run_tags_team_id_alongside_client_query_id(self):+ metric = _funnel_metric()+ experiment = self._experiment([metric])+ seen = []++ def fake_calculate():+ tags = get_query_tags()+ seen.append((tags.client_query_id, tags.team_id))+ raise RuntimeError("stop after capturing tags")++ with patch("products.experiments.backend.temporal.canary_logic.ExperimentQueryRunner") as runner_cls:+ runner_cls.return_value.calculate.side_effect = fake_calculate+ with pytest.raises(RuntimeError):+ run_metric_canary_sync(self._target(experiment, metric["uuid"]))++ client_query_id, team_id = seen[0]+ assert client_query_id+ assert team_id == experiment.team_id+def test_query_failure_raises_for_temporal_retry(self):metric = _funnel_metric()experiment = self._experiment([metric])