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.

fix(metrics): surface query errors instead of a misleading empty state (#69227)

Frank Hamandbb0f91b22a6f
diff --git a/products/metrics/frontend/components/MetricsViewer.tsx b/products/metrics/frontend/components/MetricsViewer.tsx
index 0c3664f9d53a..1ab481c8bf4a 100644
--- a/products/metrics/frontend/components/MetricsViewer.tsx
+++ b/products/metrics/frontend/components/MetricsViewer.tsx
@@ -1,7 +1,14 @@
import { useActions, useMountedLogic, useValues } from 'kea'
import { useCallback, useEffect, useMemo } from 'react'
-import { LemonInputSelect, LemonSegmentedButton, LemonSelect, LemonSwitch, SpinnerOverlay } from '@posthog/lemon-ui'
+import {
+ LemonBanner,
+ LemonInputSelect,
+ LemonSegmentedButton,
+ LemonSelect,
+ LemonSwitch,
+ SpinnerOverlay,
+} from '@posthog/lemon-ui'
import { DateFilter } from 'lib/components/DateFilter/DateFilter'
import { CUSTOM_OPTION_KEY } from 'lib/components/DateFilter/types'
@@ -101,6 +108,7 @@ export const MetricsViewer = (): JSX.Element => {
anomalyBadge,
liveRefresh,
queryResultsLoading,
+ queryError,
hasMetricName,
} = useValues(logic)
const {
@@ -261,6 +269,12 @@ export const MetricsViewer = (): JSX.Element => {
<div className="h-full flex items-center justify-center text-secondary text-sm">
Pick a metric to see its time series.
</div>
+ ) : queryError ? (
+ <div className="h-full flex items-center justify-center">
+ <LemonBanner type="error" className="max-w-md">
+ {queryError}
+ </LemonBanner>
+ </div>
) : hasResults && viewMode === 'stat' ? (
<MetricStatPanel
title={metricName}
diff --git a/products/metrics/frontend/components/metricsViewerLogic.test.ts b/products/metrics/frontend/components/metricsViewerLogic.test.ts
index bad1f60b759e..1d265c3810c1 100644
--- a/products/metrics/frontend/components/metricsViewerLogic.test.ts
+++ b/products/metrics/frontend/components/metricsViewerLogic.test.ts
@@ -3,7 +3,7 @@ import api from 'lib/api'
import { initKeaTests } from '~/test/init'
import { metricNamePickerLogic } from './metricNamePickerLogic'
-import { metricsViewerLogic } from './metricsViewerLogic'
+import { metricsViewerLogic, NEW_QUERY_STARTED_ERROR_MESSAGE } from './metricsViewerLogic'
const PICKER_ITEMS = [
{ name: 'requests_total', metric_type: 'sum' },
@@ -52,4 +52,22 @@ describe('metricsViewerLogic', () => {
logic.actions.setMetricName('mystery_metric')
expect(logic.values.aggregation).toBe('increase')
})
+
+ // A failed query (bad regex, 500) used to render the same "No data" empty state as a genuinely
+ // empty result. The failure records the message so the viewer can show a real error instead.
+ // kea-loaders dispatches `<key>Failure(error.message, error)`, so the reducer reads the message.
+ it('records a real query failure in queryError', () => {
+ logic.actions.fetchQueryResultsFailure('Invalid regex pattern', new Error('Invalid regex pattern'))
+ expect(logic.values.queryError).toBe('Invalid regex pattern')
+ })
+
+ // The debounced viewer aborts the in-flight query on every change; that cancellation rejects with
+ // NEW_QUERY_STARTED_ERROR_MESSAGE (whose text has no "abort"), and must not become an error banner.
+ it('does not record an aborted (superseded) query as an error', () => {
+ logic.actions.fetchQueryResultsFailure(
+ NEW_QUERY_STARTED_ERROR_MESSAGE,
+ new DOMException(NEW_QUERY_STARTED_ERROR_MESSAGE, 'AbortError')
+ )
+ expect(logic.values.queryError).toBeNull()
+ })
})
diff --git a/products/metrics/frontend/components/metricsViewerLogic.tsx b/products/metrics/frontend/components/metricsViewerLogic.tsx
index 6966d568a8e8..679cda192ff7 100644
--- a/products/metrics/frontend/components/metricsViewerLogic.tsx
+++ b/products/metrics/frontend/components/metricsViewerLogic.tsx
@@ -50,7 +50,15 @@ export const RECOMMENDED_AGGREGATION_BY_TYPE: Record<string, MetricAggregation>
exponential_histogram: 'p95',
}
const DEFAULT_DATE_FROM = '-1h'
-const NEW_QUERY_STARTED_ERROR_MESSAGE = 'A new metrics query started, cancelling the previous one'
+export const NEW_QUERY_STARTED_ERROR_MESSAGE = 'A new metrics query started, cancelling the previous one'
+
+// A superseded or unmounted request rejects with an abort, not a real failure — never surface it as an error.
+// The cancel path aborts with NEW_QUERY_STARTED_ERROR_MESSAGE, whose text doesn't contain "abort", so match it
+// explicitly alongside the generic abort check (mirrors logsViewerDataLogic's isUserInitiatedError).
+const isUserInitiatedError = (error: unknown): boolean => {
+ const errorStr = String(error).toLowerCase()
+ return error === NEW_QUERY_STARTED_ERROR_MESSAGE || errorStr.includes('abort')
+}
// The anomaly badge characterizes the most recent slice of the selected window against the rest.
const ANOMALY_WINDOW_FRACTION = 0.2
export const LIVE_REFRESH_MS = 15_000
@@ -113,6 +121,18 @@ export const metricsViewerLogic = kea<metricsViewerLogicType>([
null as AbortController | null,
{ setQueryAbortController: (_, { controller }) => controller },
],
+ // A real query failure (bad regex, 500, timeout) — surfaced as a banner so it isn't mistaken
+ // for the empty-result state. Cleared when a new query starts or one succeeds; an aborted
+ // (superseded) query leaves the previous state untouched so refetches don't flash an error.
+ queryError: [
+ null as string | null,
+ {
+ fetchQueryResults: () => null,
+ fetchQueryResultsSuccess: () => null,
+ fetchQueryResultsFailure: (state, { error }) =>
+ isUserInitiatedError(error) ? state : error || 'Something went wrong running this query.',
+ },
+ ],
}),
listeners(({ actions, values, cache }) => ({
setMetricName: ({ metricName }) => {
@@ -126,7 +146,10 @@ export const metricsViewerLogic = kea<metricsViewerLogicType>([
},
cancelInProgressQuery: ({ controller }) => {
if (values.queryAbortController !== null) {
- values.queryAbortController.abort(NEW_QUERY_STARTED_ERROR_MESSAGE)
+ // An AbortError-named DOMException (not a bare string) is what api.ts and the global
+ // loader onFailure recognize as a cancellation, so a superseded query is swallowed
+ // rather than logged/captured as a real error.
+ values.queryAbortController.abort(new DOMException(NEW_QUERY_STARTED_ERROR_MESSAGE, 'AbortError'))
}
actions.setQueryAbortController(controller)
},