: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(observability): time dashboard tile requests (#123957)
Vasco de Krijger5649940c72e5
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.tsindex 470b5db67d9b..13803cc4e103 100644--- a/frontend/src/lib/api.ts+++ b/frontend/src/lib/api.ts@@ -365,9 +365,9 @@ export function isAbortError(error: unknown): boolean {return (error as { name?: string } | null)?.name === 'AbortError'}-export async function getJSONOrNull(response: Response): Promise<any> {+export async function getJSONOrNull(response: Response, timing?: ApiRequestTimingRecorder): Promise<any> {try {- return await response.json()+ return timing ? timing.parseJSON(await timing.readBody(response)) : await response.json()} catch (error) {if (isAbortError(error)) {throw error@@ -7252,7 +7252,7 @@ async function isStaleSessionResponse(response: Response): Promise<boolean> {}}-async function withRequestTiming<T>(+export async function withRequestTiming<T>(options: ApiMethodOptions | undefined,request: (timing?: ApiRequestTimingRecorder) => Promise<T>): Promise<T> {diff --git a/frontend/src/lib/apiRequestTiming.ts b/frontend/src/lib/apiRequestTiming.tsindex a7141f608518..8bb83ebe8be7 100644--- a/frontend/src/lib/apiRequestTiming.ts+++ b/frontend/src/lib/apiRequestTiming.ts@@ -26,6 +26,24 @@ export interface ApiRequestTiming {resource_callback_ms?: number}+const FLATTENED_REQUEST_TIMING_FIELDS = [+ 'http_request_id',+ 'time_to_headers_ms',+ 'body_read_ms',+ 'json_parse_ms',+ 'resource_timing_status',+] as const++export function flattenRequestTiming(+ timing: ApiRequestTiming | undefined,+ prefix: '' | 'result_poll_'+): Record<string, string | number | undefined> {+ if (!timing) {+ return {}+ }+ return Object.fromEntries(FLATTENED_REQUEST_TIMING_FIELDS.map((field) => [`${prefix}${field}`, timing[field]]))+}+const MAX_PENDING_RESOURCES = 32type ResourceTiming = Pick<diff --git a/frontend/src/queries/query.ts b/frontend/src/queries/query.tsindex e1c94639b068..65f1bc2b515d 100644--- a/frontend/src/queries/query.ts+++ b/frontend/src/queries/query.ts@@ -1,6 +1,6 @@import api, { ApiMethodOptions, isAbortError } from 'lib/api'import { ApiError, NetworkError } from 'lib/api-error'-import { ApiRequestTiming } from 'lib/apiRequestTiming'+import { ApiRequestTiming, flattenRequestTiming } from 'lib/apiRequestTiming'import posthog from 'lib/posthog-typed'import { delay, retryWithBackoff } from 'lib/utils/async'import { uuid } from 'lib/utils/dom'@@ -121,24 +121,6 @@ type QuerySubmitTelemetry = {result_poll_timing?: ApiRequestTiming}-const FLATTENED_REQUEST_TIMING_FIELDS = [- 'http_request_id',- 'time_to_headers_ms',- 'body_read_ms',- 'json_parse_ms',- 'resource_timing_status',-] as const--function flattenRequestTiming(- timing: ApiRequestTiming | undefined,- prefix: '' | 'result_poll_'-): Record<string, string | number | undefined> {- if (!timing) {- return {}- }- return Object.fromEntries(FLATTENED_REQUEST_TIMING_FIELDS.map((field) => [`${prefix}${field}`, timing[field]]))-}-function requestTimingProperties(telemetry: QuerySubmitTelemetry): Record<string, string | number | undefined> {return {...flattenRequestTiming(telemetry.submit_request_timings.at(-1), ''),diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.tsindex 6c712d14ef66..4359ae856a6a 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts@@ -2216,6 +2216,39 @@ describe('dashboardLogic', () => {}})+ it.each([+ ['a dashboard refresh', (): void => logic.actions.triggerDashboardRefresh()],+ [+ 'a single tile refresh',+ (): void => logic.actions.refreshDashboardItem({ tile: logic.values.insightTiles[0] }),+ ],+ ])('adds the tile request timing to refresh analytics after %s', async (_name, refreshTiles) => {+ await expectLogic(logic).toFinishAllListeners()+ const requestId = '11111111-1111-4111-8111-111111111111'+ const refresh = jest+ .spyOn(dashboardUtils, 'getInsightWithRetry')+ .mockImplementation(async (_teamId, insight, _dashboardId, _queryId, _refresh, methodOptions) => {+ methodOptions?.onRequestTiming?.({+ request_total_ms: 20,+ http_request_id: requestId,+ resource_timing_status: 'matched',+ })+ return insight+ })+ const capture = jest.spyOn(posthog, 'capture').mockClear()++ try {+ await expectLogic(logic, refreshTiles).toFinishAllListeners()++ expect(capture).toHaveBeenCalledWith(+ 'dashboard insight refreshed',+ expect.objectContaining({ http_request_id: requestId, resource_timing_status: 'matched' })+ )+ } finally {+ refresh.mockRestore()+ }+ })+it('refreshes a large builtin dashboard on a variable change, since it has no Preview button', async () => {const autoPreviewLimit = jest.replaceProperty(dashboardUtils, 'AUTO_PREVIEW_TILE_LIMIT', 0)logic.unmount()diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsxindex cfea4631b5c2..5ffefa92136e 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx@@ -37,6 +37,7 @@ import {import api, { getJSONOrNull } from 'lib/api'import { ApiError, isAccessDeniedError } from 'lib/api-error'+import { ApiRequestTiming, flattenRequestTiming } from 'lib/apiRequestTiming'import { DataColorTheme } from 'lib/colors'import { OrganizationMembershipLevel } from 'lib/constants'import { FEATURE_FLAGS } from 'lib/constants'@@ -220,6 +221,7 @@ function reportDashboardTileRefreshed(variables: Record<string, any>,refreshDurationMs: number,individualRefresh: boolean,+ requestTiming: ApiRequestTiming | undefined,filterChange?: PendingFilterChange): void {const insight = tile.insight@@ -236,6 +238,7 @@ function reportDashboardTileRefreshed(variables,refresh_duration_ms: refreshDurationMs,individual_refresh: individualRefresh,+ ...flattenRequestTiming(requestTiming, ''),...sanitizedQuery,...(filterChange? {@@ -317,6 +320,7 @@ async function refreshInsightTile(const insight = tile.insightconst queryId = uuid()let queryStartTime = performance.now()+ let requestTiming: ApiRequestTiming | undefinedconst { signal, finish } = tileLoads.start([tile.id], refreshSignal, { forceRefresh, settings })try {@@ -329,7 +333,12 @@ async function refreshInsightTile(dashboardId,queryId,forceRefresh ? 'force_blocking' : 'blocking', // 'blocking' returns cached data if available, when manual refresh is triggered we want fresh results- { signal },+ {+ signal,+ onRequestTiming: (timing) => {+ requestTiming = timing+ },+ },filters,variables,tile.filters_overrides@@ -349,7 +358,8 @@ async function refreshInsightTile(urlFilters,variables,Math.floor(performance.now() - queryStartTime),- false+ false,+ requestTiming)return refreshedInsight.is_cached ? 'cached' : 'refreshed'} catch (e: any) {@@ -5308,6 +5318,7 @@ export const dashboardLogic = kea<dashboardLogicType>([breakpoint()const refreshStartTime = performance.now()+ let requestTiming: ApiRequestTiming | undefined// when one insight is refreshed manually, we want to avoid cache and force a refresh of the insight// hence using 'force_blocking', small cost to give latest data for the insight// also it's then consistent with the dashboard refresh button@@ -5317,7 +5328,11 @@ export const dashboardLogic = kea<dashboardLogicType>([dashboardId,uuid(),'force_blocking',- undefined,+ {+ onRequestTiming: (timing) => {+ requestTiming = timing+ },+ },withCompactMetricVariables(effectiveRefreshFilters, savedMetricVariables),urlVariables,tile.filters_overrides@@ -5351,6 +5366,7 @@ export const dashboardLogic = kea<dashboardLogicType>([urlVariables,Math.floor(performance.now() - refreshStartTime),true,+ requestTiming,currentFilterChange)if (currentFilterChange?.remainingInsightIds.size === 0) {diff --git a/frontend/src/scenes/dashboard/dashboardUtils.test.ts b/frontend/src/scenes/dashboard/dashboardUtils.test.tsindex f8390c1169df..3528c661ae25 100644--- a/frontend/src/scenes/dashboard/dashboardUtils.test.ts+++ b/frontend/src/scenes/dashboard/dashboardUtils.test.ts@@ -2,6 +2,7 @@ import posthog from 'posthog-js'import api from 'lib/api'import { ApiError } from 'lib/api-error'+import { ApiRequestTimingRecorder } from 'lib/apiRequestTiming'import { dayjs } from 'lib/dayjs'import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'@@ -399,6 +400,7 @@ describe('getInsightWithRetry', () => {})).mockResolvedValueOnce(insightResponse({ ...insight, result: [{ count: 1 }] }))+ const onRequestTiming = jest.fn()const request = getInsightWithRetry(1,@@ -406,7 +408,7 @@ describe('getInsightWithRetry', () => {60,'query-id','blocking',- undefined,+ { onRequestTiming },undefined,undefined,undefined,@@ -417,7 +419,16 @@ describe('getInsightWithRetry', () => {const result = await requestexpect(getResponseSpy).toHaveBeenCalledTimes(2)+ expect(getResponseSpy).toHaveBeenLastCalledWith(+ expect.any(String),+ { onRequestTiming },+ expect.any(ApiRequestTimingRecorder)+ )expect(result?.result).toEqual([{ count: 1 }])+ expect(onRequestTiming).toHaveBeenCalledTimes(2)+ expect(onRequestTiming).toHaveBeenLastCalledWith(+ expect.objectContaining({ body_read_ms: expect.any(Number), json_parse_ms: expect.any(Number) })+ )expect(capture).toHaveBeenCalledWith('dashboard tile recovered from capacity error',{ insight_short_id: 'abc123', dashboard_id: 60, attempts: 1 },diff --git a/frontend/src/scenes/dashboard/dashboardUtils.ts b/frontend/src/scenes/dashboard/dashboardUtils.tsindex d2a990009e3e..89a9f59688d7 100644--- a/frontend/src/scenes/dashboard/dashboardUtils.ts+++ b/frontend/src/scenes/dashboard/dashboardUtils.ts@@ -4,7 +4,7 @@ import { ResponsiveLayouts } from 'react-grid-layout'import { lemonToast } from '@posthog/lemon-ui'import { getDashboardWidgetCatalogEntry } from '@posthog/products-dashboards/frontend/widget_types/catalog'-import api, { ApiMethodOptions, getJSONOrNull, isAbortError } from 'lib/api'+import api, { ApiMethodOptions, getJSONOrNull, isAbortError, withRequestTiming } from 'lib/api'import { ApiError, CLICKHOUSE_MEMORY_LIMIT_ERROR_CODE } from 'lib/api-error'import type { Dayjs } from 'lib/dayjs'import { currentSessionId } from 'lib/internalMetrics'@@ -477,6 +477,13 @@ export async function getInsightWithRetry(}}+ const getTimedInsight = (url: string): Promise<InsightModel | null> =>+ withRequestTiming(methodOptions, async (timing) => {+ // nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.+ const response: Response = await api.getResponse(url, methodOptions, timing)+ return await getJSONOrNull(response, timing)+ })+while (attempt < maxAttempts) {try {const apiUrl = `api/projects/${currentTeamId}/insights/${insight.id}/?${toParams({@@ -488,9 +495,7 @@ export async function getInsightWithRetry(...(variablesOverride ? { variables_override: variablesOverride } : {}),...(tileFiltersOverride ? { tile_filters_override: tileFiltersOverride } : {}),})}`- // nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.- const insightResponse: Response = await api.getResponse(apiUrl, methodOptions)- const result: InsightModel | null = await getJSONOrNull(insightResponse)+ const result = await getTimedInsight(apiUrl)if (result?.query_status?.error_code === RATE_LIMITED_ERROR_CODE ||@@ -523,13 +528,7 @@ export async function getInsightWithRetry(...(variablesOverride ? { variables_override: variablesOverride } : {}),...(tileFiltersOverride ? { tile_filters_override: tileFiltersOverride } : {}),})}`- // nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.- const refreshedInsightResponse: Response = await api.getResponse(- cacheUrl,- methodOptions- )- const refreshedInsight: InsightModel | null =- await getJSONOrNull(refreshedInsightResponse)+ const refreshedInsight = await getTimedInsight(cacheUrl)if (refreshedInsight) {return {...refreshedInsight,diff --git a/products/product_analytics/backend/presentation/insight.py b/products/product_analytics/backend/presentation/insight.pyindex d6c1a66d043f..3ca898276682 100644--- a/products/product_analytics/backend/presentation/insight.py+++ b/products/product_analytics/backend/presentation/insight.py@@ -47,6 +47,7 @@ from posthog.api.forbid_destroy_model import ForbidDestroyModelfrom posthog.api.mixins import ValidatedRequest, validated_requestfrom posthog.api.monitoring import Feature, monitorfrom posthog.api.openapi_parameters import make_filters_override_param, make_variables_override_param+from posthog.api.query import add_query_response_identity_headers, get_query_request_analytics_propertiesfrom posthog.api.routing import TeamAndOrgViewSetMixinfrom posthog.api.shared import SearchMatchTypeSerializerMixin, UserBasicSerializerfrom posthog.api.sharing_publish_gate import insight_query_access_error@@ -64,7 +65,7 @@ from posthog.clickhouse.client.limit import ConcurrencyLimitExceededfrom posthog.clickhouse.query_tagging import AccessMethod, get_query_tag_value, tag_queries, tags_contextfrom posthog.constants import INSIGHTfrom posthog.errors import CHQueryErrorQueryWasCancelled, ExposedCHQueryError, QueryErrorCategory, classify_query_error-from posthog.event_usage import EventSource, get_event_source, get_request_analytics_properties, report_user_action+from posthog.event_usage import EventSource, get_event_source, report_user_actionfrom posthog.exceptions import ClickHouseAtCapacityfrom posthog.exceptions_capture import capture_exceptionfrom posthog.helpers.impersonation import is_impersonated@@ -1422,7 +1423,7 @@ class InsightSerializer(InsightBasicSerializer):variables_override=variables_override,tile_filters_override=tile_filters_override,cache_age_seconds=shared_cache_age_seconds,- analytics_props=get_request_analytics_properties(self.context["request"]),+ analytics_props=get_query_request_analytics_properties(self.context["request"]),allow_raw_results=allow_raw_results,)access_method: DashboardAccessMethod | None = self.context.get("dashboard_access_method")@@ -2192,6 +2193,9 @@ class InsightViewSet(parser_classes = (QuerySchemaParser,)+ def finalize_response(self, request: Request, response: Response, *args: Any, **kwargs: Any) -> Response:+ return add_query_response_identity_headers(super().finalize_response(request, response, *args, **kwargs))+def get_throttles(self):"""Apply LLM-specific throttles to the endpoints that call a model."""if self.action == "generate_metadata":diff --git a/products/product_analytics/backend/tests/api/test_insight.py b/products/product_analytics/backend/tests/api/test_insight.pyindex ff14d59e2163..0215dc943665 100644--- a/products/product_analytics/backend/tests/api/test_insight.py+++ b/products/product_analytics/backend/tests/api/test_insight.py@@ -587,6 +587,35 @@ class TestInsight(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):else:mock_record_outcome.assert_not_called()+ @patch("posthog.caching.calculate_results.calculate_for_query_based_insight")+ def test_dashboard_insight_response_identity_matches_query_analytics(self, mock_calculate: mock.MagicMock) -> None:+ request_id = "01234567-89ab-4def-8123-456789abcdef"+ dashboard = Dashboard.objects.create(team=self.team, name="Dashboard", created_by=self.user)+ insight = Insight.objects.create(+ team=self.team,+ created_by=self.user,+ query={"kind": "HogQLQuery", "query": "select 1"},+ )+ DashboardTile.objects.create(dashboard=dashboard, insight=insight)+ mock_calculate.return_value = InsightResult(+ result=[],+ last_refresh=timezone.now(),+ cache_key="cache-key",+ is_cached=False,+ timezone=self.team.timezone,+ )++ response = self.client.get(+ f"/api/projects/{self.team.id}/insights/{insight.id}/",+ {"from_dashboard": str(dashboard.id), "refresh": "blocking"},+ HTTP_X_REQUEST_ID=request_id,+ )++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(response["X-Request-ID"], request_id)+ self.assertIn(f'request_id;desc="{request_id}"', response["Server-Timing"])+ self.assertEqual(mock_calculate.call_args.kwargs["analytics_props"]["http_request_id"], request_id)+def test_get_insight_in_shared_context(self) -> None:dashboard_id, _ = self.dashboard_api.create_dashboard({"name": "the dashboard", "filters": {"date_from": "-180d"}}