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(insights): stop counting aborted queries as query failures (#76435)

posthog[bot]0044f4e6b20d
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index 442d4dd22f47..790eb6b81f87 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -350,7 +350,7 @@ export function getCookie(name: string): string | null {
return cookieValue
}
-function isAbortError(error: unknown): boolean {
+export function isAbortError(error: unknown): boolean {
return (error as { name?: string } | null)?.name === 'AbortError'
}
diff --git a/frontend/src/queries/query.test.ts b/frontend/src/queries/query.test.ts
index e309aab77d1e..aaa847254d60 100644
--- a/frontend/src/queries/query.test.ts
+++ b/frontend/src/queries/query.test.ts
@@ -162,6 +162,21 @@ describe('query', () => {
expect(queryFailedCalls[0][1]).not.toHaveProperty('error_message')
})
+ it('does not emit a query failed event when the request is aborted', async () => {
+ const captureSpy = jest.spyOn(posthog, 'capture')
+ const q: HogQLQuery = setLatestVersionsOnQuery({
+ kind: NodeKind.HogQLQuery,
+ query: 'select * from events',
+ })
+ captureSpy.mockClear()
+ const controller = new AbortController()
+ controller.abort()
+ await expect(performQuery(q, { signal: controller.signal })).rejects.toThrow()
+
+ const queryFailedCalls = captureSpy.mock.calls.filter((call) => call[0] === 'query failed')
+ expect(queryFailedCalls).toHaveLength(0)
+ })
+
describe('waitForPageVisible', () => {
const originalVisibilityState = document.visibilityState
diff --git a/frontend/src/queries/query.ts b/frontend/src/queries/query.ts
index ffb54ce851e6..43cc2e573792 100644
--- a/frontend/src/queries/query.ts
+++ b/frontend/src/queries/query.ts
@@ -1,4 +1,4 @@
-import api, { ApiMethodOptions } from 'lib/api'
+import api, { ApiMethodOptions, isAbortError } from 'lib/api'
import posthog from 'lib/posthog-typed'
import { delay } from 'lib/utils/async'
@@ -284,17 +284,21 @@ export async function performQuery<N extends DataNode>(
})
return response
} catch (e) {
- // Raw error detail/message can echo query fragments, so telemetry only gets status and code
- const error = e as (Error & { status?: number; code?: string | null }) | null
- posthog.capture('query failed', {
- query: queryNode,
- queryId,
- duration: performance.now() - startTime,
- error_status: error?.status ?? null,
- error_code: error?.code ?? null,
- uses_data_warehouse_source: queryUsesDataWarehouse(queryNode),
- ...logParams,
- })
+ // A superseded query or navigating away mid-request aborts, not fails — skip so the
+ // 'query failed' metric isn't drowned in cancellation noise.
+ if (!isAbortError(e)) {
+ // Raw error detail/message can echo query fragments, so telemetry only gets status and code
+ const error = e as (Error & { status?: number; code?: string | null }) | null
+ posthog.capture('query failed', {
+ query: queryNode,
+ queryId,
+ duration: performance.now() - startTime,
+ error_status: error?.status ?? null,
+ error_code: error?.code ?? null,
+ uses_data_warehouse_source: queryUsesDataWarehouse(queryNode),
+ ...logParams,
+ })
+ }
throw e
}
}