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): wait for the feature preview before loading metric names (#113960)

Daniel Visca5d4cad465ab2
diff --git a/products/metrics/frontend/MetricsScene.tsx b/products/metrics/frontend/MetricsScene.tsx
index 0688dd06e09c..b57390078941 100644
--- a/products/metrics/frontend/MetricsScene.tsx
+++ b/products/metrics/frontend/MetricsScene.tsx
@@ -3,6 +3,7 @@ import posthog from 'posthog-js'
import { LemonBanner, LemonButton, LemonTabs } from '@posthog/lemon-ui'
+import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
import { IconFeedback } from 'lib/lemon-ui/icons'
import { getAccessControlDisabledReason } from 'lib/utils/accessControlUtils'
import { sceneConfigurations } from 'scenes/scenes'
@@ -76,6 +77,10 @@ const MetricsSceneContent = (): JSX.Element => {
// Prime the metric-name list here rather than inside MetricsViewer, so the fetch
// races the has_metrics check instead of waiting on the setup prompt to resolve.
useMountedLogic(metricNamePickerLogic)
+ const { primeItems } = useActions(metricNamePickerLogic)
+ useOnMountEffect(() => {
+ primeItems()
+ })
// Holds cross-tab state: a catalog card click preloads the viewer. Mounted here, a tab
// flip cannot unmount the logic and reset the handoff before the destination reads it.
useMountedLogic(metricsCatalogLogic)
diff --git a/products/metrics/frontend/components/metricNamePickerLogic.ts b/products/metrics/frontend/components/metricNamePickerLogic.ts
index 3afc26daeaaf..48159674bd84 100644
--- a/products/metrics/frontend/components/metricNamePickerLogic.ts
+++ b/products/metrics/frontend/components/metricNamePickerLogic.ts
@@ -1,6 +1,8 @@
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
+import { FEATURE_FLAGS } from 'lib/constants'
+import { featureFlagLogic, FeatureFlagsSet } from 'lib/logic/featureFlagLogic'
import { teamLogic } from 'scenes/teamLogic'
import { metricsNamesRetrieve } from 'products/metrics/frontend/generated/api'
@@ -11,6 +13,7 @@ export type MetricNameItem = _MetricPickerNameApi
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricNamePickerLogicValues {
+ featureFlags: FeatureFlagsSet // featureFlagLogic
currentTeamId: number | null // teamLogic
items: MetricNameItem[]
itemsLoading: boolean
@@ -41,6 +44,9 @@ export interface metricNamePickerLogicActions {
debounce: boolean
}
}
+ primeItems: () => {
+ value: true
+ }
setSearch: (search: string) => {
search: string
}
@@ -54,9 +60,12 @@ export type metricNamePickerLogicType = MakeLogicType<metricNamePickerLogicValue
export const metricNamePickerLogic = kea<metricNamePickerLogicType>([
path(['products', 'metrics', 'frontend', 'components', 'metricNamePickerLogic']),
connect(() => ({
- values: [teamLogic, ['currentTeamId']],
+ values: [teamLogic, ['currentTeamId'], featureFlagLogic, ['featureFlags']],
})),
actions({
+ // Loads the list once per mount. The scene gate calls it again after the feature preview is on,
+ // because the scene logic mounts this logic before the gate renders.
+ primeItems: true,
setSearch: (search: string) => ({ search }),
// Pushed in by the viewer rather than read from it: the viewer already
// reads this logic's `items`, so a connection the other way would cycle.
@@ -94,7 +103,14 @@ export const metricNamePickerLogic = kea<metricNamePickerLogicType>([
},
],
})),
- listeners(({ actions }) => ({
+ listeners(({ actions, cache }) => ({
+ primeItems: () => {
+ if (cache.primed || !canViewMetrics()) {
+ return
+ }
+ cache.primed = true
+ actions.loadItems({ debounce: false })
+ },
setSearch: () => {
actions.loadItems({ debounce: true })
},
@@ -103,11 +119,11 @@ export const metricNamePickerLogic = kea<metricNamePickerLogicType>([
actions.loadItems({ debounce: false })
},
})),
- afterMount(({ actions }) => {
+ afterMount(({ actions, values }) => {
// Prime the list so the dropdown isn't empty on first open. Mirrors
- // serviceFilterLogic's afterMount in logs.
- if (canViewMetrics()) {
- actions.loadItems({ debounce: false })
+ // serviceFilterLogic's afterMount in logs. Without the flag the API rejects the request.
+ if (values.featureFlags[FEATURE_FLAGS.METRICS]) {
+ actions.primeItems()
}
}),
])
diff --git a/products/metrics/frontend/components/metricsCatalogLogic.test.ts b/products/metrics/frontend/components/metricsCatalogLogic.test.ts
index 27ca9b60e258..75d834b296a1 100644
--- a/products/metrics/frontend/components/metricsCatalogLogic.test.ts
+++ b/products/metrics/frontend/components/metricsCatalogLogic.test.ts
@@ -1,5 +1,8 @@
import { expectLogic } from 'kea-test-utils'
+import { FEATURE_FLAGS } from 'lib/constants'
+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+
import { initKeaTests } from '~/test/init'
import { AccessControlLevel, AccessControlResourceType, AppContext } from '~/types'
@@ -47,6 +50,7 @@ describe('metricsCatalogLogic', () => {
},
} as AppContext
initKeaTests()
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.METRICS]: true })
jest.mocked(metricsValuesCreate).mockReset()
jest.mocked(metricsValuesCreate).mockResolvedValue({ results: [SPARKLINE_ITEM] } as any)
jest.mocked(metricsNamesRetrieve).mockReset()
diff --git a/products/metrics/frontend/metricsSceneLogic.test.ts b/products/metrics/frontend/metricsSceneLogic.test.ts
index 06206e8b7813..3cd7556b7830 100644
--- a/products/metrics/frontend/metricsSceneLogic.test.ts
+++ b/products/metrics/frontend/metricsSceneLogic.test.ts
@@ -1,6 +1,9 @@
import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
+import { FEATURE_FLAGS } from 'lib/constants'
+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+
import { initKeaTests } from '~/test/init'
import {
AccessControlLevel,
@@ -14,6 +17,7 @@ import {
import { metricsNamesRetrieve, metricsQueryCreate } from 'products/metrics/frontend/generated/api'
+import { metricNamePickerLogic } from './components/metricNamePickerLogic'
import { metricsViewerLogic } from './components/metricsViewerLogic'
import { metricsSceneLogic } from './metricsSceneLogic'
@@ -57,6 +61,7 @@ describe('metricsSceneLogic', () => {
},
} as AppContext
initKeaTests()
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.METRICS]: true })
jest.mocked(metricsNamesRetrieve).mockResolvedValue({ results: PICKER_ITEMS })
jest.mocked(metricsQueryCreate).mockReset().mockResolvedValue({ results: [] })
logic = metricsSceneLogic()
@@ -68,6 +73,23 @@ describe('metricsSceneLogic', () => {
logic.unmount()
})
+ it('requests metric names only after the flag is on and the gated scene primes them once', async () => {
+ logic.unmount()
+ initKeaTests()
+ featureFlagLogic.actions.setFeatureFlags([], {})
+ jest.mocked(metricsNamesRetrieve).mockClear()
+ logic = metricsSceneLogic()
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+ expect(metricsNamesRetrieve).not.toHaveBeenCalled()
+
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.METRICS]: true })
+ metricNamePickerLogic.actions.primeItems()
+ metricNamePickerLogic.actions.primeItems()
+ await expectLogic(metricNamePickerLogic).toFinishAllListeners()
+ expect(metricsNamesRetrieve).toHaveBeenCalledTimes(1)
+ })
+
describe('URL parameter parsing', () => {
it('restores the viewer query state from URL params', async () => {
await expectLogic(logic, () => {