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(sql-editor): split total query time from server time (#121172)

Jovan Sakovic0a5b0c424425
diff --git a/frontend/src/lib/apiRequestTiming.test.ts b/frontend/src/lib/apiRequestTiming.test.ts
index 1c5baa356aed..cd83e7c49d44 100644
--- a/frontend/src/lib/apiRequestTiming.test.ts
+++ b/frontend/src/lib/apiRequestTiming.test.ts
@@ -166,6 +166,7 @@ describe('API request timing', () => {
it('rejects the preceding auth response when a successful retry reuses its request ID', () => {
pending = [resource()]
+ recorder.startFetch(url)
now = 500
recorder.startFetch(url)
now = 600
@@ -176,6 +177,7 @@ describe('API request timing', () => {
expect(timing.time_to_headers_ms).toBe(590)
expect(timing.resource_timing_status).toBe('invalid')
expect(timing.resource_ttfb_ms).toBeUndefined()
+ expect(timing.fetch_attempts).toBe(2)
})
})
diff --git a/frontend/src/lib/apiRequestTiming.ts b/frontend/src/lib/apiRequestTiming.ts
index 91e3f60337fd..d3b1cb63a118 100644
--- a/frontend/src/lib/apiRequestTiming.ts
+++ b/frontend/src/lib/apiRequestTiming.ts
@@ -7,6 +7,8 @@ export interface ApiRequestTiming {
body_read_ms?: number
json_parse_ms?: number
http_status?: number
+ // Above 1 when authentication refreshed and retried the request.
+ fetch_attempts?: number
http_request_id?: string
envoy_upstream_ms?: number
resource_timing_status:
@@ -76,6 +78,7 @@ export class ApiRequestTimingRecorder {
private observer?: PerformanceObserver
private resourceEntries: PerformanceResourceTiming[] = []
private resourceCollectionOverflowed = false
+ private fetchAttempts = 0
constructor(private readonly clock: Pick<Performance, 'now'> = performance) {
this.startedAt = clock.now()
@@ -97,6 +100,7 @@ export class ApiRequestTimingRecorder {
this.resourceEntries = []
this.resourceCollectionOverflowed = false
this.fetchStartedAt = this.clock.now()
+ this.fetchAttempts++
try {
this.requestUrl = new URL(url, location.href).href
} catch {
@@ -192,6 +196,7 @@ export class ApiRequestTimingRecorder {
const timing: ApiRequestTiming = {
...this.timing,
request_total_ms: finishedAt - this.startedAt,
+ ...(this.fetchAttempts ? { fetch_attempts: this.fetchAttempts } : {}),
resource_timing_status: 'missing_request_id',
}
try {
diff --git a/frontend/src/queries/nodes/DataNode/ElapsedTime.tsx b/frontend/src/queries/nodes/DataNode/ElapsedTime.tsx
index 882d8f26ba70..7316137289c5 100644
--- a/frontend/src/queries/nodes/DataNode/ElapsedTime.tsx
+++ b/frontend/src/queries/nodes/DataNode/ElapsedTime.tsx
@@ -2,13 +2,19 @@ import clsx from 'clsx'
import { useValues } from 'kea'
import { useMemo, useState } from 'react'
-import { IconChevronRight } from '@posthog/icons'
+import { IconChevronRight, IconWarning } from '@posthog/icons'
import { Popover } from 'lib/lemon-ui/Popover'
+import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { dataNodeLogic } from '~/queries/nodes/DataNode/dataNodeLogic'
+import { getQueryTimeBreakdown } from '~/queries/nodes/DataNode/queryTimeBreakdown'
import { QueryTiming } from '~/queries/schema/schema-general'
+function formatSeconds(ms: number): string {
+ return `${(ms / 1000).toFixed(ms < 1000 ? 2 : 1)}s`
+}
+
export interface TimingsProps {
timings: QueryTiming[]
elapsedTime?: number
@@ -182,7 +188,7 @@ function ElapsedTimeWithTimings({
}
export function ElapsedTime({ showTimings }: { showTimings?: boolean }): JSX.Element | null {
- const { elapsedTime, loadingStart, responseError, isShowingCachedResults, timings, query } =
+ const { elapsedTime, loadingStart, responseError, isShowingCachedResults, timings, serverDurationMs, query } =
useValues(dataNodeLogic)
const [, setTick] = useState(0)
@@ -210,12 +216,39 @@ export function ElapsedTime({ showTimings }: { showTimings?: boolean }): JSX.Ele
return <ElapsedTimeWithTimings elapsedTime={elapsedTime} timings={timings} hasError={!!responseError} />
}
+ const breakdown = elapsedTime && !responseError ? getQueryTimeBreakdown(elapsedTime, serverDurationMs) : null
+
// "{time}s" is two bare text nodes, so React tracks them individually. Once a page-translation
// extension replaces one with a <font> element React's updates go to the detached node and the
// counter freezes mid-query (react#11538). There is nothing to translate in "1.4s" anyway.
+ if (!breakdown) {
+ return (
+ <div className={responseError ? 'text-danger' : ''} translate="no">
+ {formatSeconds(time)}
+ </div>
+ )
+ }
+
return (
- <div className={responseError ? 'text-danger' : ''} translate="no">
- {(time / 1000).toFixed(time < 1000 ? 2 : 1)}s
- </div>
+ <Tooltip
+ title={
+ breakdown.isTransferSlow
+ ? `Our servers returned results in ${formatSeconds(breakdown.serverMs)}. The other ${formatSeconds(
+ breakdown.transferMs
+ )} was spent sending them to your browser. Large results or a slow connection can cause this.`
+ : `Time until results showed in your browser. Our servers took ${formatSeconds(
+ breakdown.serverMs
+ )} of it.`
+ }
+ >
+ <div
+ className={clsx('flex items-center gap-1 cursor-help', breakdown.isTransferSlow && 'text-warning')}
+ translate="no"
+ data-attr="query-elapsed-time"
+ >
+ {breakdown.isTransferSlow ? <IconWarning /> : null}
+ <span>Total {formatSeconds(breakdown.totalMs)}</span>
+ </div>
+ </Tooltip>
)
}
diff --git a/frontend/src/queries/nodes/DataNode/QueryExecutionDetails.tsx b/frontend/src/queries/nodes/DataNode/QueryExecutionDetails.tsx
index f17da8151658..796371cb0309 100644
--- a/frontend/src/queries/nodes/DataNode/QueryExecutionDetails.tsx
+++ b/frontend/src/queries/nodes/DataNode/QueryExecutionDetails.tsx
@@ -12,7 +12,8 @@ import { humanizeBytes } from 'lib/utils/numbers'
import { dataNodeLogic } from './dataNodeLogic'
export function QueryExecutionDetails(): JSX.Element | null {
- const { queryLog, queryId, queryLogLoading, queryLogQueryId } = useValues(dataNodeLogic)
+ const { queryLog, queryId, queryLogLoading, queryLogQueryId, elapsedTime, isShowingCachedResults } =
+ useValues(dataNodeLogic)
const { loadQueryLog } = useActions(dataNodeLogic)
const [popoverVisible, setPopoverVisible] = useState(false)
@@ -92,7 +93,7 @@ export function QueryExecutionDetails(): JSX.Element | null {
)}
{queryDurationMs !== null && (
<div className="flex justify-between items-start deprecated-space-x-2 py-1">
- <span>Duration:</span>
+ <span>Database time:</span>
{queryDurationMs > 999 ? (
<Tooltip title={`${queryDurationMs}ms`}>
<span className="font-mono">
@@ -104,6 +105,14 @@ export function QueryExecutionDetails(): JSX.Element | null {
)}
</div>
)}
+ {elapsedTime && !isShowingCachedResults ? (
+ <div className="flex justify-between items-start deprecated-space-x-2 py-1">
+ <Tooltip title="Time until results showed in your browser, including the transfer over your network">
+ <span>Total time:</span>
+ </Tooltip>
+ <span className="font-mono">{humanFriendlyMilliseconds(elapsedTime)}</span>
+ </div>
+ ) : null}
</>
) : (
<div className="py-1">No execution details available.</div>
diff --git a/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts b/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts
index b0d9c0c1eb3a..e9935026d5ad 100644
--- a/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts
+++ b/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts
@@ -20,6 +20,7 @@ import { subscriptions } from 'kea-subscriptions'
import posthog from 'posthog-js'
import api, { ApiMethodOptions } from 'lib/api'
+import { ApiRequestTiming } from 'lib/apiRequestTiming'
import { dayjs } from 'lib/dayjs'
import { ConcurrencyController } from 'lib/utils/concurrencyController'
import { inStorybook, inStorybookTestRunner, uuid } from 'lib/utils/dom'
@@ -34,6 +35,7 @@ import { userLogic } from 'scenes/userLogic'
import { DataNodeCollectionProps, dataNodeCollectionLogic } from '~/queries/nodes/DataNode/dataNodeCollectionLogic'
import { QueryScanPollResult, QueryScanState, resolveQueryScan } from '~/queries/nodes/DataNode/queryScan'
+import { getSingleRequestServerMs } from '~/queries/nodes/DataNode/queryTimeBreakdown'
import { removeExpressionComment } from '~/queries/nodes/DataTable/utils'
import { performQuery } from '~/queries/query'
import {
@@ -369,6 +371,7 @@ export interface dataNodeLogicValues {
responseError: string | null
responseErrorObject: Record<string, any> | null
responseLoading: boolean
+ serverDurationMs: number | null
shouldCalculateCount: boolean
timings: QueryTiming[] | null
totalCount: number | null
@@ -650,8 +653,12 @@ export interface dataNodeLogicActions {
resetLoadingTimer: () => {
value: true
}
- setElapsedTime: (elapsedTime: number) => {
+ setElapsedTime: (
+ elapsedTime: number,
+ serverDurationMs?: number | null
+ ) => {
elapsedTime: number
+ serverDurationMs: number | null
}
setLoadingTime: (seconds: number) => {
seconds: number
@@ -1082,7 +1089,10 @@ export const dataNodeLogic = kea<dataNodeLogicType>([
stopAutoLoad: true,
toggleAutoLoad: true,
highlightRows: (rows: any[]) => ({ rows }),
- setElapsedTime: (elapsedTime: number) => ({ elapsedTime }),
+ setElapsedTime: (elapsedTime: number, serverDurationMs: number | null = null) => ({
+ elapsedTime,
+ serverDurationMs,
+ }),
setPollResponse: (status: QueryStatus | null) => ({ status }),
setLoadingTime: (seconds: number) => ({ seconds }),
resetLoadingTimer: true,
@@ -1197,8 +1207,10 @@ export const dataNodeLogic = kea<dataNodeLogicType>([
pauseOnPageHidden: false,
})
}
+ const requestTimings: ApiRequestTiming[] = []
const methodOptions: ApiMethodOptions = {
signal: abortController.signal,
+ onRequestTiming: (timing) => requestTimings.push(timing),
}
try {
// For shared contexts, create a minimal team object if needed
@@ -1212,7 +1224,11 @@ export const dataNodeLogic = kea<dataNodeLogicType>([
) {
reclaimReleasedQuery()
}
- const runQuery = async (): Promise<{ duration: number; data: Record<string, any> }> => {
+ const runQuery = async (): Promise<{
+ duration: number
+ serverDurationMs: number | null
+ data: Record<string, any>
+ }> => {
const now = performance.now()
try {
breakpoint()
@@ -1229,7 +1245,8 @@ export const dataNodeLogic = kea<dataNodeLogicType>([
props.limitContext
)) ?? null
const duration = performance.now() - now
- return { data, duration }
+ const serverDurationMs = getSingleRequestServerMs(requestTimings, duration)
+ return { data, duration, serverDurationMs }
} catch (error: any) {
const duration = performance.now() - now
error.duration = duration
@@ -1267,7 +1284,7 @@ export const dataNodeLogic = kea<dataNodeLogicType>([
}
const response = await responsePromise
breakpoint()
- actions.setElapsedTime(response.duration)
+ actions.setElapsedTime(response.duration, response.serverDurationMs)
return response.data
} catch (error: any) {
if (error.duration) {
@@ -1578,6 +1595,16 @@ export const dataNodeLogic = kea<dataNodeLogicType>([
response && 'error' in response ? (response.error ?? null) : null,
},
],
+ serverDurationMs: [
+ null as number | null,
+ {
+ setElapsedTime: (_, { serverDurationMs }) => serverDurationMs,
+ loadData: () => null,
+ loadNewData: () => null,
+ loadNextData: () => null,
+ cancelQuery: () => null,
+ },
+ ],
elapsedTime: [
null as number | null,
{
diff --git a/frontend/src/queries/nodes/DataNode/queryTimeBreakdown.test.ts b/frontend/src/queries/nodes/DataNode/queryTimeBreakdown.test.ts
new file mode 100644
index 000000000000..bb5f4c16c8d0
--- /dev/null
+++ b/frontend/src/queries/nodes/DataNode/queryTimeBreakdown.test.ts
@@ -0,0 +1,46 @@
+import { ApiRequestTiming } from 'lib/apiRequestTiming'
+
+import { getQueryTimeBreakdown, getSingleRequestServerMs } from './queryTimeBreakdown'
+
+describe('query time breakdown', () => {
+ test.each([
+ ['fast server, slow transfer', 40_000, 500, true],
+ ['slow server', 40_000, 38_000, false],
+ ['short total with most of it in transfer', 2_000, 100, false],
+ ['transfer just under the share threshold', 10_000, 2_500, false],
+ ])('%s', (_, totalMs, serverMs, isTransferSlow) => {
+ expect(getQueryTimeBreakdown(totalMs, serverMs)?.isTransferSlow).toBe(isTransferSlow)
+ })
+
+ it('splits the total into server and transfer time', () => {
+ expect(getQueryTimeBreakdown(39_400, 500)).toEqual({
+ totalMs: 39_400,
+ serverMs: 500,
+ transferMs: 38_900,
+ isTransferSlow: true,
+ })
+ })
+
+ it('returns null when the server did not report its time', () => {
+ expect(getQueryTimeBreakdown(5_000, null)).toBeNull()
+ })
+
+ const timing = (overrides: Partial<ApiRequestTiming> = {}): ApiRequestTiming => ({
+ request_total_ms: 39_000,
+ envoy_upstream_ms: 500,
+ resource_timing_status: 'matched',
+ fetch_attempts: 1,
+ ...overrides,
+ })
+
+ test.each([
+ ['one request covering the load', [timing()], 500],
+ ['no upstream time reported', [timing({ envoy_upstream_ms: undefined })], null],
+ ['an async submit followed by polling', [timing({ request_total_ms: 200 })], null],
+ ['an authentication retry', [timing({ fetch_attempts: 2 })], null],
+ ['several submit requests', [timing(), timing()], null],
+ ['no recorded request', [], null],
+ ])('%s', (_, timings, expected) => {
+ expect(getSingleRequestServerMs(timings, 40_000)).toBe(expected)
+ })
+})
diff --git a/frontend/src/queries/nodes/DataNode/queryTimeBreakdown.ts b/frontend/src/queries/nodes/DataNode/queryTimeBreakdown.ts
new file mode 100644
index 000000000000..e4a0015429ac
--- /dev/null
+++ b/frontend/src/queries/nodes/DataNode/queryTimeBreakdown.ts
@@ -0,0 +1,41 @@
+import { ApiRequestTiming } from 'lib/apiRequestTiming'
+
+const SLOW_TRANSFER_MIN_TOTAL_MS = 5000
+const SLOW_TRANSFER_MIN_SHARE = 0.8
+// Below this share, other waits such as status polling or retry backoff filled the rest of the total
+const SINGLE_REQUEST_MIN_SHARE = 0.9
+
+export interface QueryTimeBreakdown {
+ totalMs: number
+ serverMs: number
+ transferMs: number
+ isTransferSlow: boolean
+}
+
+export function getQueryTimeBreakdown(totalMs: number, serverMs: number | null): QueryTimeBreakdown | null {
+ if (serverMs === null) {
+ return null
+ }
+ const transferMs = Math.max(totalMs - serverMs, 0)
+ return {
+ totalMs,
+ serverMs,
+ transferMs,
+ isTransferSlow: totalMs >= SLOW_TRANSFER_MIN_TOTAL_MS && transferMs >= totalMs * SLOW_TRANSFER_MIN_SHARE,
+ }
+}
+
+/**
+ * Server time for a load that was one request with one fetch, so the rest of the total is transfer.
+ * Polled, retried and re-authenticated loads include waits the server time cannot explain.
+ */
+export function getSingleRequestServerMs(timings: ApiRequestTiming[], totalMs: number): number | null {
+ if (timings.length !== 1) {
+ return null
+ }
+ const [timing] = timings
+ if ((timing.fetch_attempts ?? 1) !== 1 || timing.request_total_ms < totalMs * SINGLE_REQUEST_MIN_SHARE) {
+ return null
+ }
+ return timing.envoy_upstream_ms ?? null
+}
diff --git a/frontend/src/queries/nodes/DataTable/dataTableLogic.test.ts b/frontend/src/queries/nodes/DataTable/dataTableLogic.test.ts
index c9af15ff9982..b9e0d2fb6335 100644
--- a/frontend/src/queries/nodes/DataTable/dataTableLogic.test.ts
+++ b/frontend/src/queries/nodes/DataTable/dataTableLogic.test.ts
@@ -67,7 +67,7 @@ describe('dataTableLogic', () => {
expect(performQuery).toHaveBeenCalledWith(
setLatestVersionsOnQuery({ kind: 'EventsQuery', select: ['*', 'event', 'timestamp'] }),
- { signal: expect.any(Object) },
+ { signal: expect.any(Object), onRequestTiming: expect.any(Function) },
'blocking',
expect.any(String),
expect.any(Function),
diff --git a/frontend/src/queries/query.test.ts b/frontend/src/queries/query.test.ts
index 2f37de08e80e..d801e700c070 100644
--- a/frontend/src/queries/query.test.ts
+++ b/frontend/src/queries/query.test.ts
@@ -100,7 +100,8 @@ describe('query', () => {
limit: 100,
})
captureSpy.mockClear()
- await performQuery(q)
+ const onRequestTiming = jest.fn()
+ await performQuery(q, { onRequestTiming })
const queryCompletedCalls = captureSpy.mock.calls.filter((call) => call[0] === 'query completed')
expect(queryCompletedCalls).toHaveLength(1)
expect(queryCompletedCalls[0][1]).toMatchObject({
@@ -125,6 +126,8 @@ describe('query', () => {
json_parse_ms: expect.any(Number),
resource_timing_status: 'missing_request_id',
})
+ expect(onRequestTiming).toHaveBeenCalledTimes(1)
+ expect(onRequestTiming).toHaveBeenCalledWith(queryCompletedCalls[0][1]?.submit_request_timings?.[0])
expect(queryCompletedCalls[0][1]).not.toHaveProperty('result_poll_timing')
})
diff --git a/frontend/src/queries/query.ts b/frontend/src/queries/query.ts
index 65f1bc2b515d..aa34e93490e8 100644
--- a/frontend/src/queries/query.ts
+++ b/frontend/src/queries/query.ts
@@ -328,6 +328,7 @@ async function executeQuery<N extends DataNode>(
if (submitTelemetry.submit_request_timings.length < MAX_RECORDED_SUBMIT_TIMINGS) {
submitTelemetry.submit_request_timings.push(timing)
}
+ methodOptions?.onRequestTiming?.(timing)
},
},
clientQueryId,