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(dashboards): pick metrics table columns from a metric search (#122892)

Daniel Visca2900709ecb6e
diff --git a/products/dashboards/frontend/widgets/metrics/EditMetricsTableWidgetModal.tsx b/products/dashboards/frontend/widgets/metrics/EditMetricsTableWidgetModal.tsx
index d6b909ac61ef..942189cb6a96 100644
--- a/products/dashboards/frontend/widgets/metrics/EditMetricsTableWidgetModal.tsx
+++ b/products/dashboards/frontend/widgets/metrics/EditMetricsTableWidgetModal.tsx
@@ -9,6 +9,8 @@ import { LemonInput } from 'lib/lemon-ui/LemonInput/LemonInput'
import { LemonModal } from 'lib/lemon-ui/LemonModal'
import { LemonSelect, type LemonSelectOptions } from 'lib/lemon-ui/LemonSelect'
+import { MetricNameFilter } from 'products/metrics/frontend/components/MetricNameFilter'
+
import { getDashboardWidgetGroupLabel } from '../../widget_types/catalog'
import { EditWidgetModalTileDetailsSection } from '../EditWidgetModalTileDetailsSection'
import type { DashboardWidgetEditModalProps } from '../registry'
@@ -81,9 +83,9 @@ function ColumnEditor({ column, index }: { column: MetricsTableColumn; index: nu
/>
</LemonField.Pure>
<LemonField.Pure label="Metric" className="flex-1 min-w-0">
- <LemonInput
+ <MetricNameFilter
value={column.metricName}
- placeholder="http_server_requests_total"
+ allowCustomName
disabled={saving}
onChange={(metricName) => updateColumn(index, { metricName })}
/>
diff --git a/products/metrics/frontend/components/MetricNameFilter.tsx b/products/metrics/frontend/components/MetricNameFilter.tsx
index 46df1fe93aef..a46a84271806 100644
--- a/products/metrics/frontend/components/MetricNameFilter.tsx
+++ b/products/metrics/frontend/components/MetricNameFilter.tsx
@@ -5,7 +5,7 @@ import { List } from 'react-window'
import { IconChevronDown } from '@posthog/icons'
import { LemonButton, LemonDropdown, LemonInput, Spinner } from '@posthog/lemon-ui'
-import { metricNamePickerLogic } from './metricNamePickerLogic'
+import { customMetricName, metricNamePickerLogic } from './metricNamePickerLogic'
const ROW_HEIGHT = 32
const MAX_DROPDOWN_HEIGHT = 320
@@ -53,6 +53,8 @@ export interface MetricNameFilterProps {
placeholder?: string
disabled?: boolean
disabledReason?: string | null
+ /** Offer the typed name when the list does not have it, so a metric older than the list's 24 hours can be set. */
+ allowCustomName?: boolean
}
export const MetricNameFilter = ({
@@ -61,6 +63,7 @@ export const MetricNameFilter = ({
placeholder = 'Pick a metric',
disabled,
disabledReason,
+ allowCustomName,
}: MetricNameFilterProps): JSX.Element => {
return (
<BindLogic logic={metricNamePickerLogic} props={{}}>
@@ -70,6 +73,7 @@ export const MetricNameFilter = ({
placeholder={placeholder}
disabled={disabled}
disabledReason={disabledReason}
+ allowCustomName={allowCustomName}
/>
</BindLogic>
)
@@ -81,12 +85,14 @@ function MetricNameFilterInner({
placeholder,
disabled,
disabledReason,
+ allowCustomName,
}: {
value: string
onChange: (name: string) => void
placeholder: string
disabled?: boolean
disabledReason?: string | null
+ allowCustomName?: boolean
}): JSX.Element {
const { items, filteredItems, fullItemsLoading, searchedItemsLoading, scopeLoading, search } =
useValues(metricNamePickerLogic)
@@ -112,6 +118,7 @@ function MetricNameFilterInner({
}, [filteredItems.length])
const triggerLabel = value || placeholder
+ const customName = allowCustomName ? customMetricName(items, search) : null
return (
<LemonDropdown
@@ -153,6 +160,16 @@ function MetricNameFilterInner({
rowProps={rowProps}
/>
)}
+ {customName ? (
+ <LemonButton
+ size="small"
+ fullWidth
+ onClick={() => onChange(customName)}
+ data-attr="metrics-name-filter-custom"
+ >
+ Use "{customName}"
+ </LemonButton>
+ ) : null}
</div>
}
>
diff --git a/products/metrics/frontend/components/metricNamePickerLogic.test.ts b/products/metrics/frontend/components/metricNamePickerLogic.test.ts
index 55e137e7eaf2..5560c1de591e 100644
--- a/products/metrics/frontend/components/metricNamePickerLogic.test.ts
+++ b/products/metrics/frontend/components/metricNamePickerLogic.test.ts
@@ -7,7 +7,7 @@ import { initKeaTests } from '~/test/init'
import { AccessControlLevel, AccessControlResourceType, AppContext } from '~/types'
import { metricsNamesRetrieve } from '../generated/api'
-import { METRIC_NAMES_LIMIT, filterMetricNames, metricNamePickerLogic } from './metricNamePickerLogic'
+import { METRIC_NAMES_LIMIT, customMetricName, filterMetricNames, metricNamePickerLogic } from './metricNamePickerLogic'
jest.mock('../generated/api', () => ({
...jest.requireActual('../generated/api'),
@@ -351,4 +351,12 @@ describe('filterMetricNames', () => {
it('returns every item for an empty search', () => {
expect(filterMetricNames(ITEMS, '')).toBe(ITEMS)
})
+
+ it.each([
+ ['offers a name that is not in the list', ' queue_depth_2025 ', 'queue_depth_2025'],
+ ['offers nothing for an exact match', 'http', null],
+ ['offers nothing for an empty search', ' ', null],
+ ])('customMetricName %s', (_name, search, expected) => {
+ expect(customMetricName(ITEMS, search)).toEqual(expected)
+ })
})
diff --git a/products/metrics/frontend/components/metricNamePickerLogic.ts b/products/metrics/frontend/components/metricNamePickerLogic.ts
index 004e0f76169f..ec4db650123f 100644
--- a/products/metrics/frontend/components/metricNamePickerLogic.ts
+++ b/products/metrics/frontend/components/metricNamePickerLogic.ts
@@ -34,6 +34,12 @@ export function filterMetricNames(items: MetricNameItem[], search: string): Metr
.map(({ item }) => item)
}
+/** The typed name to offer when no listed metric has it, for a metric with no data in the picker's 24 hours. */
+export function customMetricName(items: MetricNameItem[], search: string): string | null {
+ const name = search.trim()
+ return name && !items.some((item) => item.name === name) ? name : null
+}
+
function mergeMetricNames(items: MetricNameItem[], incoming: MetricNameItem[]): MetricNameItem[] {
const known = new Set(items.map((item) => item.name))
const added = incoming.filter((item) => !known.has(item.name))