Skip to content

: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.

feat(experiments): filter slowest queries by metric type (#65979)

Juraj Majerik5b96367b7096
diff --git a/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsx b/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsx
index 7783b584adb4..4dc7185f3362 100644
--- a/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsx
+++ b/frontend/src/scenes/instance/QueryPerformance/QueryPerformance.tsx
@@ -5,6 +5,7 @@ import { LemonButton, LemonDialog, LemonTabs } from '@posthog/lemon-ui'
import { CodeSnippet, Language } from 'lib/components/CodeSnippet'
import { LemonInput } from 'lib/lemon-ui/LemonInput'
+import { LemonSelect } from 'lib/lemon-ui/LemonSelect'
import { LemonSwitch } from 'lib/lemon-ui/LemonSwitch'
import { LemonTable, LemonTableColumns } from 'lib/lemon-ui/LemonTable'
import { LemonTag } from 'lib/lemon-ui/LemonTag'
@@ -30,6 +31,18 @@ const TIME_RANGE_OPTIONS = [
{ label: '7d', hours: 168 },
]
+// Value is the metric_type, or "funnel:<orderType>" to also filter the funnel order.
+const METRIC_TYPE_OPTIONS = [
+ { value: '', label: 'All metric types' },
+ { value: 'mean', label: 'Mean' },
+ { value: 'funnel', label: 'Funnel (any order)' },
+ { value: 'funnel:ordered', label: 'Funnel: ordered' },
+ { value: 'funnel:unordered', label: 'Funnel: unordered' },
+ { value: 'funnel:strict', label: 'Funnel: strict' },
+ { value: 'ratio', label: 'Ratio' },
+ { value: 'retention', label: 'Retention' },
+]
+
export function QueryPerformance(): JSX.Element {
const { user } = useValues(userLogic)
const {
@@ -41,9 +54,17 @@ export function QueryPerformance(): JSX.Element {
hoursBack,
teamIdFilter,
experimentIdFilter,
+ metricTypeFilter,
} = useValues(queryPerformanceLogic)
- const { setSearch, setPrecomputation, setHoursBack, loadSlowestQueries, setTeamIdFilter, setExperimentIdFilter } =
- useActions(queryPerformanceLogic)
+ const {
+ setSearch,
+ setPrecomputation,
+ setHoursBack,
+ loadSlowestQueries,
+ setTeamIdFilter,
+ setExperimentIdFilter,
+ setMetricTypeFilter,
+ } = useActions(queryPerformanceLogic)
if (!user?.is_staff) {
return (
@@ -345,6 +366,13 @@ export function QueryPerformance(): JSX.Element {
onChange={(value) => setExperimentIdFilter(value != null ? String(value) : '')}
className="w-36"
/>
+ <LemonSelect
+ size="small"
+ value={metricTypeFilter}
+ onChange={(value) => setMetricTypeFilter(value ?? '')}
+ options={METRIC_TYPE_OPTIONS}
+ className="w-44"
+ />
<LemonButton
type="secondary"
size="small"
diff --git a/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.ts b/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.ts
index 3376cb0400c1..61731521c671 100644
--- a/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.ts
+++ b/frontend/src/scenes/instance/QueryPerformance/queryPerformanceLogic.ts
@@ -50,6 +50,7 @@ export const queryPerformanceLogic = kea<queryPerformanceLogicType>([
setHoursBack: (hours: number) => ({ hours }),
setTeamIdFilter: (teamId: string) => ({ teamId }),
setExperimentIdFilter: (experimentId: string) => ({ experimentId }),
+ setMetricTypeFilter: (metricType: string) => ({ metricType }),
}),
reducers({
search: [
@@ -76,6 +77,12 @@ export const queryPerformanceLogic = kea<queryPerformanceLogicType>([
setExperimentIdFilter: (_, { experimentId }) => experimentId,
},
],
+ metricTypeFilter: [
+ '',
+ {
+ setMetricTypeFilter: (_, { metricType }) => metricType,
+ },
+ ],
}),
loaders(({ values }) => ({
precomputationTeams: [
@@ -114,6 +121,14 @@ export const queryPerformanceLogic = kea<queryPerformanceLogicType>([
if (values.experimentIdFilter) {
params.append('experiment_id', values.experimentIdFilter)
}
+ if (values.metricTypeFilter) {
+ // Encoded as "<metricType>" or "funnel:<orderType>" (e.g. "funnel:ordered").
+ const [metricType, funnelOrderType] = values.metricTypeFilter.split(':')
+ params.append('metric_type', metricType)
+ if (funnelOrderType) {
+ params.append('funnel_order_type', funnelOrderType)
+ }
+ }
return await api.get(`api/debug_ch_queries/slowest_queries/?${params.toString()}`)
},
},
@@ -135,6 +150,9 @@ export const queryPerformanceLogic = kea<queryPerformanceLogicType>([
await breakpoint(300)
actions.loadSlowestQueries()
},
+ setMetricTypeFilter: () => {
+ actions.loadSlowestQueries()
+ },
})),
afterMount(({ actions }) => {
if (userLogic.findMounted()?.values.user?.is_staff) {
diff --git a/posthog/api/debug_ch_queries.py b/posthog/api/debug_ch_queries.py
index 6550d6ff88e8..0b2a8fc281ac 100644
--- a/posthog/api/debug_ch_queries.py
+++ b/posthog/api/debug_ch_queries.py
@@ -426,6 +426,17 @@ class DebugCHQueries(viewsets.ViewSet):
if experiment_id_filter <= 0:
raise exceptions.ValidationError("experiment_id must be a positive integer.")
+ metric_type_filter = request.query_params.get("metric_type") or None
+ if metric_type_filter is not None and metric_type_filter not in {"mean", "funnel", "ratio", "retention"}:
+ raise exceptions.ValidationError("metric_type must be one of: mean, funnel, ratio, retention.")
+
+ funnel_order_type_filter = request.query_params.get("funnel_order_type") or None
+ if funnel_order_type_filter is not None:
+ if funnel_order_type_filter not in {"ordered", "unordered", "strict"}:
+ raise exceptions.ValidationError("funnel_order_type must be one of: ordered, unordered, strict.")
+ if metric_type_filter != "funnel":
+ raise exceptions.ValidationError("funnel_order_type can only be used with metric_type=funnel.")
+
params: dict = {
"cluster": CLICKHOUSE_CLUSTER,
"hours": hours,
@@ -438,6 +449,16 @@ class DebugCHQueries(viewsets.ViewSet):
if experiment_id_filter is not None:
extra_filters += " AND JSONExtractInt(log_comment, 'experiment_id') = %(experiment_id)s"
params["experiment_id"] = experiment_id_filter
+ # metric_type and funnel_order_type are tagged before the precompute builds run, so the build
+ # sub-queries carry them too and stay grouped with their parent read under these filters.
+ if metric_type_filter is not None:
+ extra_filters += " AND JSONExtractString(log_comment, 'experiment_metric_type') = %(metric_type)s"
+ params["metric_type"] = metric_type_filter
+ if funnel_order_type_filter is not None:
+ extra_filters += (
+ " AND JSONExtractString(log_comment, 'experiment_funnel_order_type') = %(funnel_order_type)s"
+ )
+ params["funnel_order_type"] = funnel_order_type_filter
# Each row is one ClickHouse query. A top-level read (surface != 'precompute_build') and the
# precompute-build INSERTs it triggered share an experiment_query_group_id (set by the runner).