: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): explain why a query took the direct path (#66997)
Juraj Majerik488c4bdbd000
diff --git a/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsx b/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsxindex 0f5bf76340f9..3b0c0b11fc32 100644--- a/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsx+++ b/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsx@@ -49,6 +49,31 @@ const METRIC_TYPE_OPTIONS = [const groupBytes = (item: SlowestQuery): number =>item.read_bytes + item.sub_queries.reduce((sum, q) => sum + q.read_bytes, 0)+// Off/gated reasons come straight from the runner tag; build-failed/not-ready are derived from the+// precompute-build sub-queries. Empty tag + no sub-queries is the legacy/forward-only case ("not ready").+const SKIP_REASON_LABELS: Record<string, string> = {+ override_direct: 'forced direct (query override)',+ team_disabled: 'precompute off for team',+ min_runtime: 'experiment <12h old',+ data_warehouse: 'data warehouse metric',+}++function reasonForDirect(item: SlowestQuery, table: 'exposures' | 'metric_events'): string {+ const skip = item.experiment_precompute_skip_reason+ if (skip && SKIP_REASON_LABELS[skip]) {+ return SKIP_REASON_LABELS[skip]+ }+ const builds = item.sub_queries.filter((q) => q.experiment_precompute_table === table)+ const failed = builds.find((q) => q.exception)+ if (failed) {+ return `build failed (${failed.exception_code || 'error'})`+ }+ if (builds.length > 0) {+ return 'build incomplete / not ready'+ }+ return 'not ready'+}+function QueryStats({read_bytes,read_rows,@@ -278,19 +303,30 @@ export function QueryPerformance(): JSX.Element {</LemonTag>)}- const pathTag = (label: string, value: string): JSX.Element | null => {+ const pathTag = (+ label: string,+ value: string,+ table: 'exposures' | 'metric_events'+ ): JSX.Element | null => {if (!value || value === 'not_applicable') {return null}+ if (value === 'precomputed') {+ return <LemonTag type="success">{label}: precomputed</LemonTag>+ }return (- <LemonTag type={value === 'precomputed' ? 'success' : 'default'}>- {label}: {value === 'precomputed' ? 'precomputed' : 'direct'}- </LemonTag>+ <Tooltip title={reasonForDirect(item, table)}>+ <LemonTag type="default">{label}: direct</LemonTag>+ </Tooltip>)}// Fall back to the deprecated experiment_execution_path for rows logged before the split.- const exposures = pathTag('exposures', item.experiment_exposures_path || item.experiment_execution_path)- const events = pathTag('events', item.experiment_metric_events_path)+ const exposures = pathTag(+ 'exposures',+ item.experiment_exposures_path || item.experiment_execution_path,+ 'exposures'+ )+ const events = pathTag('events', item.experiment_metric_events_path, 'metric_events')if (!exposures && !events) {return null}diff --git a/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.ts b/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.tsindex b31a57fbe06e..5bb72d91f9ad 100644--- a/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.ts+++ b/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.ts@@ -34,6 +34,7 @@ export interface SlowestQuery {experiment_metric_events_path: stringexperiment_query_surface: stringexperiment_precompute_table: string+ experiment_precompute_skip_reason: stringexperiment_query_group_id: stringexperiment_metric_type: stringexperiment_funnel_order_type: string | nulldiff --git a/posthog/api/debug_ch_queries.py b/posthog/api/debug_ch_queries.pyindex 08d581e8e7ff..b6f0402f0816 100644--- a/posthog/api/debug_ch_queries.py+++ b/posthog/api/debug_ch_queries.py@@ -491,7 +491,8 @@ class DebugCHQueries(viewsets.ViewSet):argMax(JSONExtractString(log_comment, 'experiment_metric_events_path'), type) AS experiment_metric_events_path,argMax(JSONExtractString(log_comment, 'experiment_query_surface'), type) AS experiment_query_surface,argMax(JSONExtractString(log_comment, 'experiment_precompute_table'), type) AS experiment_precompute_table,- argMax(JSONExtractString(log_comment, 'experiment_query_group_id'), type) AS experiment_query_group_id+ argMax(JSONExtractString(log_comment, 'experiment_query_group_id'), type) AS experiment_query_group_id,+ argMax(JSONExtractString(log_comment, 'experiment_precompute_skip_reason'), type) AS experiment_precompute_skip_reasonFROM (SELECTquery_id, query, query_start_time, query_duration_ms, exception,@@ -523,7 +524,8 @@ class DebugCHQueries(viewsets.ViewSet):g.experiment_metric_type, g.experiment_funnel_order_type, g.experiment_id, g.experiment_exposures_path,g.experiment_metric_events_path, g.experiment_query_surface, g.experiment_precompute_table,g.experiment_query_group_id, r.total_duration_ms,- g.read_bytes, g.read_rows, g.exception_code, g.memory_usage+ g.read_bytes, g.read_rows, g.exception_code, g.memory_usage,+ g.experiment_precompute_skip_reasonFROM grouped AS gINNER JOIN ranked AS r ON g.grp = r.grpORDER BY@@ -584,6 +586,7 @@ class DebugCHQueries(viewsets.ViewSet):"read_rows": row[21],"exception_code": row[22],"memory_usage": row[23],+ "experiment_precompute_skip_reason": row[24],"sub_queries": [],}diff --git a/posthog/clickhouse/query_tagging.py b/posthog/clickhouse/query_tagging.pyindex 9693af849287..8460e445eb4f 100644--- a/posthog/clickhouse/query_tagging.py+++ b/posthog/clickhouse/query_tagging.py@@ -434,6 +434,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"+ # Why precompute was not used (set on the metric read). One of "override_direct", "team_disabled",+ # "min_runtime", "data_warehouse"; None/absent when precompute was attempted (so a direct path then+ # means the build failed or wasn't ready — derivable from the precompute_build sub-queries).+ experiment_precompute_skip_reason: Optional[str] = None# 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.diff --git a/products/experiments/backend/hogql_queries/experiment_query_runner.py b/products/experiments/backend/hogql_queries/experiment_query_runner.pyindex 2acb91e39a7d..3964492e7780 100644--- a/products/experiments/backend/hogql_queries/experiment_query_runner.py+++ b/products/experiments/backend/hogql_queries/experiment_query_runner.py@@ -313,6 +313,23 @@ class ExperimentQueryRunner(QueryRunner):self.experiment.end_date,)+ def _precompute_skip_reason(self) -> Optional[str]:+ """Why precompute was not used, for the query-performance UI. None when it was attempted."""+ if self.query.precomputation_mode == PrecomputationMode.PRECOMPUTED:+ return None+ if self.query.precomputation_mode == PrecomputationMode.DIRECT:+ return "override_direct"+ if not self._team_experiments_config.experiment_precomputation_enabled:+ return "team_disabled"+ if not experiment_has_min_runtime_for_precomputation(+ self.experiment.start_date,+ self.experiment.end_date,+ ):+ return "min_runtime"+ if self.is_data_warehouse_query:+ return "data_warehouse"+ return None # precompute was attempted; a direct path means the build failed / wasn't ready+def _metric_events_precompute_applicable(self) -> bool:"""Metric-events precompute only supports ordered funnels without breakdowns, CUPED, or data warehouse."""return (@@ -464,6 +481,7 @@ class ExperimentQueryRunner(QueryRunner):experiment_exposures_path=exposures_path,experiment_metric_events_path=metric_events_path,experiment_execution_path=exposures_path,+ experiment_precompute_skip_reason=self._precompute_skip_reason(),)experiment_query_debug = get_experiment_query_debug(experiment_query_ast, self.team)self.hogql = experiment_query_debug[0]