: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): tag query group id to link reads and sub-queries (#65730)
Juraj Majerik14c4303406c7
diff --git a/posthog/clickhouse/query_tagging.py b/posthog/clickhouse/query_tagging.pyindex 0e19cb36c1e0..63615ff7cf34 100644--- a/posthog/clickhouse/query_tagging.py+++ b/posthog/clickhouse/query_tagging.py@@ -425,6 +425,10 @@ class QueryTags(BaseModel):experiment_metric_events_path: Optional[str] = None # "direct_scan", "precomputed", or "not_applicable"experiment_query_surface: Optional[str] = None # "metric", "exposures_timeseries", "actors", "precompute_build"experiment_precompute_table: Optional[str] = None # on precompute_build rows: "exposures" or "metric_events"+ # Shared id linking a top-level query to its precompute-build sub-queries. Generated once per+ # top-level evaluation; sub-queries inherit it through the tag context. Lets the query-performance+ # UI group the (synchronous) build INSERTs under the read that triggered them.+ experiment_query_group_id: Optional[uuid.UUID] = Noneexperiment_actors_query_step: Optional[int] = None # funnel step for actors queryexperiment_actors_query_variant: Optional[str] = None # variant filter for actors queryexperiment_actors_query_includes_recordings: Optional[bool] = None # whether recordings are includeddiff --git a/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py b/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.pyindex b5e38de6f18d..8832393f4a7b 100644--- a/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py+++ b/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py@@ -1,3 +1,4 @@+import uuidfrom datetime import UTC, datetime, timedeltafrom typing import Optionalfrom zoneinfo import ZoneInfo@@ -292,6 +293,9 @@ class ExperimentExposuresQueryRunner(QueryRunner):product=Product.EXPERIMENTS,experiment_query_surface="exposures_timeseries",experiment_metric_events_path="not_applicable",+ # Set before _get_exposure_query() runs the exposures precompute build, so that build+ # sub-query inherits this id via the tag context and can be grouped under this read.+ experiment_query_group_id=uuid.uuid4(),)# Set limit to avoid being cut-off by the default 100 rows limitdiff --git a/products/experiments/backend/hogql_queries/experiment_query_runner.py b/products/experiments/backend/hogql_queries/experiment_query_runner.pyindex 1aec10016815..d3f9fb12635c 100644--- a/products/experiments/backend/hogql_queries/experiment_query_runner.py+++ b/products/experiments/backend/hogql_queries/experiment_query_runner.py@@ -1,3 +1,4 @@+import uuidfrom datetime import UTC, datetime, timedeltafrom functools import cached_propertyfrom typing import Optional@@ -426,6 +427,9 @@ class ExperimentQueryRunner(QueryRunner):tag_queries(product=Product.EXPERIMENTS,experiment_query_surface="metric",+ # Generated before _get_experiment_query() runs the precompute builds, so those build+ # sub-queries inherit this id via the tag context and can be grouped under this read.+ experiment_query_group_id=uuid.uuid4(),experiment_id=self.experiment.id,experiment_name=self.experiment.name,experiment_feature_flag_key=self.feature_flag_key,@@ -774,6 +778,8 @@ class ExperimentQueryRunner(QueryRunner):tag_queries(product=Product.EXPERIMENTS,experiment_query_surface="actors",+ # No sub-queries here, but tag for a consistent group id per top-level query.+ experiment_query_group_id=uuid.uuid4(),experiment_exposures_path="direct_scan",experiment_metric_events_path="not_applicable",experiment_id=self.experiment.id,