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(logs): route column filter keys to the column from any picker tab (#108797)

Jimmy Zhuc2c68c17429a
diff --git a/products/logs/frontend/components/LogsViewer/Filters/columnFilters.test.ts b/products/logs/frontend/components/LogsViewer/Filters/columnFilters.test.ts
new file mode 100644
index 000000000000..4a0ca7d20c79
--- /dev/null
+++ b/products/logs/frontend/components/LogsViewer/Filters/columnFilters.test.ts
@@ -0,0 +1,50 @@
+import * as fs from 'fs'
+import * as path from 'path'
+
+import { FilterLogicalOperator, PropertyFilterType, PropertyOperator, UniversalFiltersGroup } from '~/types'
+
+import { COLUMN_FILTER_KEYS, pinColumnFilters } from './columnFilters'
+
+const filter = (key: string, type: PropertyFilterType): UniversalFiltersGroup['values'][number] =>
+ ({ key, type, operator: PropertyOperator.Exact, value: ['x'] }) as UniversalFiltersGroup['values'][number]
+
+const group = (...values: UniversalFiltersGroup['values']): UniversalFiltersGroup => ({
+ type: FilterLogicalOperator.And,
+ values,
+})
+
+describe('pinColumnFilters', () => {
+ // A column key picked from the Log attributes tab arrives typed `log_attribute`, so the query reads
+ // a missing attribute map entry -> zero rows. These keys must resolve to `log`. A resource attribute
+ // of the same name is a distinct field, and any other key keeps the type it was picked under.
+ it.each<[string, PropertyFilterType, PropertyFilterType]>([
+ ['service_name', PropertyFilterType.LogAttribute, PropertyFilterType.Log],
+ ['severity_level', PropertyFilterType.LogAttribute, PropertyFilterType.Log],
+ ['service_name', PropertyFilterType.LogResourceAttribute, PropertyFilterType.LogResourceAttribute],
+ ['service_name', PropertyFilterType.Log, PropertyFilterType.Log],
+ ['env', PropertyFilterType.LogAttribute, PropertyFilterType.LogAttribute],
+ ['env', PropertyFilterType.LogResourceAttribute, PropertyFilterType.LogResourceAttribute],
+ ['trace_id', PropertyFilterType.LogAttribute, PropertyFilterType.LogAttribute],
+ ])('resolves %s typed %s to %s', (key, type, expected) => {
+ const pinned = pinColumnFilters(group(group(filter(key, type))))
+ expect(pinned).toEqual(group(group(filter(key, expected))))
+ })
+
+ it('passes a malformed entry through instead of throwing', () => {
+ const malformed = group(null as unknown as UniversalFiltersGroup['values'][number])
+ expect(pinColumnFilters(malformed)).toBe(malformed)
+ })
+
+ // COLUMN_FILTER_KEYS hand-mirrors the backend `COLUMN_FILTER_FACET_FIELDS` dict. If the backend adds
+ // a column filter key without updating the frontend set, an attribute-tab pick of that key would
+ // silently return zero rows again. Parse the backend keys and lock them in step.
+ it('stays in sync with the backend COLUMN_FILTER_FACET_FIELDS dict', () => {
+ const runnerPath = path.resolve(__dirname, '../../../../backend/logs_query_runner.py')
+ const source = fs.readFileSync(runnerPath, 'utf8')
+ const block = source.match(/^COLUMN_FILTER_FACET_FIELDS[^{]*\{([^}]*)\}/m)?.[1] ?? ''
+ const backendKeys = [...block.matchAll(/["']([^"']+)["']\s*:/g)].map((m) => m[1])
+ // Non-empty guards that the dict was actually found and parsed, not silently empty.
+ expect(backendKeys.length).toBeGreaterThan(0)
+ expect(new Set(backendKeys)).toEqual(COLUMN_FILTER_KEYS)
+ })
+})
diff --git a/products/logs/frontend/components/LogsViewer/Filters/columnFilters.ts b/products/logs/frontend/components/LogsViewer/Filters/columnFilters.ts
new file mode 100644
index 000000000000..83e03338d0ad
--- /dev/null
+++ b/products/logs/frontend/components/LogsViewer/Filters/columnFilters.ts
@@ -0,0 +1,38 @@
+import { isUniversalGroupFilterLike } from 'lib/components/UniversalFilters/utils'
+
+import { PropertyFilterType, UniversalFiltersGroup, UniversalFiltersGroupValue } from '~/types'
+
+import {
+ SERVICE_NAME_FILTER,
+ SEVERITY_LEVEL_FILTER,
+} from 'products/logs/frontend/components/LogsViewer/FacetRail/facetFilters'
+
+// Keys the query reads from a top-level column when the filter is typed `log` (backend
+// `COLUMN_FILTER_FACET_FIELDS` in `products/logs/backend/logs_query_runner.py`). A pick from the Log
+// attributes tab types them `log_attribute`, so the query reads a missing attribute and returns
+// nothing. Pin them to `log`, as resolveGroupBySource does for group-by. Resource attributes are not
+// pinned: they have a picker group of their own, so a resource attribute of the same name is a
+// distinct field (see reconcileTarget in logsFilterAdd.ts).
+// `columnFilters.test.ts` fails if this set drifts from the backend dict.
+export const COLUMN_FILTER_KEYS = new Set<string>([SEVERITY_LEVEL_FILTER.key, SERVICE_NAME_FILTER.key])
+
+function pinColumnFilter(value: UniversalFiltersGroupValue): UniversalFiltersGroupValue {
+ // A URL or saved view can carry a malformed entry. Leave it as it is rather than throw in the reducer.
+ if (value == null || typeof value !== 'object') {
+ return value
+ }
+ if (isUniversalGroupFilterLike(value)) {
+ return pinColumnFilters(value)
+ }
+ if ('key' in value && COLUMN_FILTER_KEYS.has(String(value.key)) && value.type === PropertyFilterType.LogAttribute) {
+ return { ...value, type: PropertyFilterType.Log } as UniversalFiltersGroupValue
+ }
+ return value
+}
+
+/** The group with every log attribute filter on a column key retyped to `log`. */
+export function pinColumnFilters(group: UniversalFiltersGroup): UniversalFiltersGroup {
+ const values = group.values.map(pinColumnFilter)
+ // Hand back the same group when nothing moved, so an unchanged group does not reload the query.
+ return values.every((value, index) => value === group.values[index]) ? group : { ...group, values }
+}
diff --git a/products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic.test.ts b/products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic.test.ts
index 6dae746620f6..e9394cee3594 100644
--- a/products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic.test.ts
+++ b/products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic.test.ts
@@ -160,6 +160,37 @@ describe('logsViewerFiltersLogic', () => {
})
})
+ // The pinning rules are covered in columnFilters.test.ts; this pins that both ways a group enters
+ // the viewer apply them, so a `service_name` chip from the Log attributes tab queries the column.
+ describe('column filter keys', () => {
+ const attributeTabGroup: UniversalFiltersGroup = {
+ type: FilterLogicalOperator.And,
+ values: [
+ {
+ type: FilterLogicalOperator.And,
+ values: [
+ {
+ key: 'service_name',
+ value: ['api'],
+ operator: PropertyOperator.Exact,
+ type: PropertyFilterType.LogAttribute,
+ },
+ ],
+ },
+ ],
+ }
+
+ it.each([
+ ['setFilterGroup', (): void => logic.actions.setFilterGroup(attributeTabGroup)],
+ ['setFilters', (): void => logic.actions.setFilters({ filterGroup: attributeTabGroup })],
+ ])('%s retypes an attribute-tab service_name filter to the column', async (_label, apply) => {
+ apply()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(selectedServices()).toEqual(['api'])
+ })
+ })
+
describe('setFilterGroup fallback', () => {
it('falls back to default when given invalid filterGroup', async () => {
logic.actions.setFilterGroup(null as any)
diff --git a/products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic.ts b/products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic.ts
index 6d550a1f7e75..fff21d8147a9 100644
--- a/products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic.ts
+++ b/products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic.ts
@@ -34,6 +34,7 @@ import {
SEVERITY_LEVEL_FILTER,
setFacetIncluded,
} from 'products/logs/frontend/components/LogsViewer/FacetRail/facetFilters'
+import { pinColumnFilters } from 'products/logs/frontend/components/LogsViewer/Filters/columnFilters'
import {
LogsFilterTarget,
mergeFilterIntoValues,
@@ -316,10 +317,12 @@ export const logsViewerFiltersLogic = kea<logsViewerFiltersLogicType>([
DEFAULT_UNIVERSAL_GROUP_FILTER as UniversalFiltersGroup,
{
setFilterGroup: (_, { filterGroup }) =>
- filterGroup && filterGroup.values ? filterGroup : DEFAULT_UNIVERSAL_GROUP_FILTER,
+ filterGroup && filterGroup.values ? pinColumnFilters(filterGroup) : DEFAULT_UNIVERSAL_GROUP_FILTER,
setFilters: (state, { filters }) =>
foldLegacyColumnFilters(
- filters.filterGroup && filters.filterGroup.values ? filters.filterGroup : state,
+ filters.filterGroup && filters.filterGroup.values
+ ? pinColumnFilters(filters.filterGroup)
+ : state,
filters
),
},