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(queries): preserve polled query ids in failure telemetry (#110970)

Vasco de Krijger17c8cc260330
diff --git a/frontend/src/queries/query.test.ts b/frontend/src/queries/query.test.ts
index 48d0887b3fe5..3a0ffb0b5b4c 100644
--- a/frontend/src/queries/query.test.ts
+++ b/frontend/src/queries/query.test.ts
@@ -138,7 +138,7 @@ describe('query', () => {
})
})
- it('emits an event when a query errors', async () => {
+ it.each([undefined, 'client-query-id'])('emits an event when a query errors with query ID %s', async (queryId) => {
const captureSpy = jest.spyOn(posthog, 'capture')
const q: EventsQuery = setLatestVersionsOnQuery({
kind: NodeKind.EventsQuery,
@@ -147,13 +147,14 @@ describe('query', () => {
})
captureSpy.mockClear()
await expect(async () => {
- await performQuery(q)
+ await performQuery(q, undefined, undefined, queryId)
}).rejects.toThrow(ApiError)
const queryFailedCalls = captureSpy.mock.calls.filter((call) => call[0] === 'query failed')
expect(queryFailedCalls).toHaveLength(1)
expect(queryFailedCalls[0][1]).toMatchObject({
query: q,
+ queryId,
duration: expect.any(Number),
error_status: 500,
error_code: null,
@@ -324,20 +325,25 @@ describe('query', () => {
})
it.each([
- ['an error that is not a 404', new ApiError('boom', 500)],
+ ['an error that is not a 404', new ApiError('boom', 500), undefined],
+ ['an error with a caller-supplied ID', new ApiError('boom', 500), 'client-query-id'],
[
'a warehouse whose connection is down',
new ApiError('unavailable', 404, undefined, { code: 'managed_warehouse_connection_unavailable' }),
+ undefined,
],
- ])('does not resubmit on %s', async (_name, failure) => {
+ ])('reports the failed poll without resubmitting on %s', async (_name, failure, clientQueryId) => {
+ const captureSpy = jest.spyOn(posthog, 'capture').mockClear()
const querySpy = jest.spyOn(api, 'query').mockResolvedValueOnce(submitted('gone'))
jest.spyOn(api.queryStatus, 'get').mockRejectedValueOnce(failure)
- await expect(performQuery(query, undefined, 'async')).rejects.toMatchObject({
- status: failure.status,
- })
+ await expect(performQuery(query, undefined, 'async', clientQueryId)).rejects.toBe(failure)
expect(querySpy).toHaveBeenCalledTimes(1)
+ const queryFailedCalls = captureSpy.mock.calls.filter((call) => call[0] === 'query failed')
+ expect(queryFailedCalls).toHaveLength(1)
+ expect(queryFailedCalls[0][1]).toMatchObject({ queryId: 'gone', error_status: failure.status })
+ expect(captureSpy.mock.calls.filter((call) => call[0] === 'query completed')).toHaveLength(0)
})
it('gives up when the second attempt is forgotten too', async () => {
diff --git a/frontend/src/queries/query.ts b/frontend/src/queries/query.ts
index 412fd0821e7b..da1d2b606fa7 100644
--- a/frontend/src/queries/query.ts
+++ b/frontend/src/queries/query.ts
@@ -336,10 +336,10 @@ export async function performQuery<N extends DataNode>(
// '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
+ const error = e as (Error & { status?: number; code?: string | null; queryId?: string }) | null
posthog.capture('query failed', {
query: queryNode,
- queryId,
+ queryId: error?.queryId ?? queryId,
duration: performance.now() - startTime,
error_status: error?.status ?? null,
error_code: error?.code ?? null,