: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
fix: Properly persist recently searched queries in logs search (#83853)
Jimmy Zhuf2b336826ed6
diff --git a/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.test.ts b/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.test.tsindex fded517c5e72..fa3541a411a7 100644--- a/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.test.ts+++ b/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.test.ts@@ -1,5 +1,5 @@import { initKeaTests } from '~/test/init'-import { PersonPropertyFilter, PropertyFilterType, PropertyOperator } from '~/types'+import { LogPropertyFilter, PersonPropertyFilter, PropertyFilterType, PropertyOperator } from '~/types'import {MAX_RECENT_FILTERS,@@ -531,4 +531,59 @@ describe('recentTaxonomicFiltersLogic', () => {expect(filters.find((f) => f.value === 'event-0')).toBeUndefined()})})++ describe('logs message search', () => {+ // The Logs query bar builds the property filter itself and records it, and taxonomicFilterLogic+ // separately records the same selection without a value. Both write under groupType Logs / value+ // 'message' — the key, since the Logs group's getValue returns option.key — so they collide on the+ // same record, and the guards above keep the complete one whichever order they land in.+ const messageContainsFoobar: LogPropertyFilter = {+ key: 'message',+ value: 'foobar',+ operator: PropertyOperator.IContains,+ type: PropertyFilterType.Log,+ }++ it('keeps the searched value when the value-less record follows the complete one', () => {+ logic.actions.recordRecentFilter({+ groupType: TaxonomicFilterGroupType.Logs,+ groupName: 'Logs',+ value: 'message',+ item: { name: 'Search log message for "foobar"' },+ propertyFilter: messageContainsFoobar,+ })+ logic.actions.recordRecentFilter({+ groupType: TaxonomicFilterGroupType.Logs,+ groupName: 'Logs',+ value: 'message',+ item: { name: 'Search log message for "foobar"' },+ })++ expect(logic.values.recentFilters).toHaveLength(1)+ expect(logic.values.recentFilterItems).toHaveLength(1)+ // Re-selecting this from "Recent" restores `message contains foobar`, not a bare `message`.+ expect((logic.values.recentFilterItems[0] as any)._recentContext.propertyFilter).toMatchObject(+ messageContainsFoobar+ )+ })++ it('keeps one entry per searched term rather than collapsing onto the message key', () => {+ logic.actions.recordRecentFilter({+ groupType: TaxonomicFilterGroupType.Logs,+ groupName: 'Logs',+ value: 'message',+ item: { name: 'Search log message for "foobar"' },+ propertyFilter: messageContainsFoobar,+ })+ logic.actions.recordRecentFilter({+ groupType: TaxonomicFilterGroupType.Logs,+ groupName: 'Logs',+ value: 'message',+ item: { name: 'Search log message for "baz"' },+ propertyFilter: { ...messageContainsFoobar, value: 'baz' },+ })++ expect(logic.values.recentFilters.map((f) => f.propertyFilter?.value)).toEqual(['baz', 'foobar'])+ })+ })})diff --git a/products/logs/frontend/components/LogsViewer/Filters/LogsFilterBar/LogsFilterBar.test.ts b/products/logs/frontend/components/LogsViewer/Filters/LogsFilterBar/LogsFilterBar.test.tsnew file mode 100644index 000000000000..854b6d01bb50--- /dev/null+++ b/products/logs/frontend/components/LogsViewer/Filters/LogsFilterBar/LogsFilterBar.test.ts@@ -0,0 +1,99 @@+import { recentTaxonomicFiltersLogic } from 'lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic'+import { TaxonomicFilterGroup, TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'++import { initKeaTests } from '~/test/init'+import { PropertyFilterType, PropertyOperator } from '~/types'++import { addLogsValueFilter } from './LogsFilterBar'++// The Logs group's getValue returns option.key, so `value` is 'message' while the searched-for text+// lives on item.value. See buildTaxonomicGroups' localItemsSearch for the Logs group.+const LOGS_GROUP = { name: 'Logs', type: TaxonomicFilterGroupType.Logs } as TaxonomicFilterGroup+const messageSearchItem = (query: string): Record<string, any> => ({+ key: 'message',+ name: `Search log message for "${query}"`,+ value: query,+ propertyFilterType: 'log',+})++describe('addLogsValueFilter', () => {+ let logic: ReturnType<typeof recentTaxonomicFiltersLogic.build>++ beforeEach(() => {+ localStorage.clear()+ initKeaTests()+ logic = recentTaxonomicFiltersLogic.build()+ logic.mount()+ })++ afterEach(() => {+ logic.unmount()+ })++ it('builds a contains filter for the searched text', () => {+ expect(addLogsValueFilter(LOGS_GROUP, 'message', messageSearchItem('foobar'), [])).toEqual([+ {+ key: 'message',+ value: 'foobar',+ operator: PropertyOperator.IContains,+ type: PropertyFilterType.Log,+ },+ ])+ })++ it('appends to existing filters rather than replacing them', () => {+ const existing = [{ key: 'severity_level', value: 'error', type: PropertyFilterType.Log }] as any+ expect(addLogsValueFilter(LOGS_GROUP, 'message', messageSearchItem('foobar'), existing)).toHaveLength(2)+ })++ // The regression: without recording the complete filter here, the only record of this selection is+ // taxonomicFilterLogic's, which drops the value — so re-selecting from "Recent" yields `message`+ // with nothing to match on.+ it('records the searched value to recents so re-selecting it restores the filter', () => {+ addLogsValueFilter(LOGS_GROUP, 'message', messageSearchItem('foobar'), [])++ expect(logic.values.recentFilters).toHaveLength(1)+ expect(logic.values.recentFilters[0].propertyFilter).toMatchObject({+ key: 'message',+ value: 'foobar',+ operator: PropertyOperator.IContains,+ type: PropertyFilterType.Log,+ })+ })++ // Recents are expanded into a bare-key row plus a full-filter row, and the bare row inherits this name+ // while dropping the value. Naming it after the search phrase produced the reported bug: a row reading+ // `Search log message for "foobar"` that applied an empty `message` filter when clicked.+ it('names the recent after the key so the bare-key row does not promise a value', () => {+ addLogsValueFilter(LOGS_GROUP, 'message', messageSearchItem('foobar'), [])++ expect(logic.values.recentFilters[0].item).toEqual({ name: 'message' })+ })++ // What taxonomicFilterLogic writes for the same selection: same group and value, but a stripped item and+ // no propertyFilter. Whichever order the two writes land in, the reducer keeps the complete one — so the+ // outcome does not depend on the ordering between our synchronous write and its deferred one.+ const recordValueLessWrite = (): void =>+ logic.actions.recordRecentFilter({+ groupType: TaxonomicFilterGroupType.Logs,+ groupName: 'Logs',+ value: 'message',+ item: { name: 'message' },+ })++ it("survives taxonomicFilterLogic's value-less record landing after ours", () => {+ addLogsValueFilter(LOGS_GROUP, 'message', messageSearchItem('foobar'), [])+ recordValueLessWrite()++ expect(logic.values.recentFilters).toHaveLength(1)+ expect(logic.values.recentFilters[0].propertyFilter).toMatchObject({ value: 'foobar' })+ })++ it("survives taxonomicFilterLogic's value-less record landing before ours", () => {+ recordValueLessWrite()+ addLogsValueFilter(LOGS_GROUP, 'message', messageSearchItem('foobar'), [])++ expect(logic.values.recentFilters).toHaveLength(1)+ expect(logic.values.recentFilters[0].propertyFilter).toMatchObject({ value: 'foobar' })+ })+})diff --git a/products/logs/frontend/components/LogsViewer/Filters/LogsFilterBar/LogsFilterBar.tsx b/products/logs/frontend/components/LogsViewer/Filters/LogsFilterBar/LogsFilterBar.tsxindex 3d93e772b160..e462658bd17e 100644--- a/products/logs/frontend/components/LogsViewer/Filters/LogsFilterBar/LogsFilterBar.tsx+++ b/products/logs/frontend/components/LogsViewer/Filters/LogsFilterBar/LogsFilterBar.tsx@@ -5,14 +5,21 @@ import { IconRefresh } from '@posthog/icons'import { LemonButton, LemonDropdown } from '@posthog/lemon-ui'import { InfiniteSelectResults } from 'lib/components/TaxonomicFilter/InfiniteSelectResults'+import { recentTaxonomicFiltersLogic } from 'lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic'import { TaxonomicFilterSearchInput } from 'lib/components/TaxonomicFilter/TaxonomicFilter'import { taxonomicFilterLogic } from 'lib/components/TaxonomicFilter/taxonomicFilterLogic'-import { TaxonomicFilterGroupType, TaxonomicFilterLogicProps } from 'lib/components/TaxonomicFilter/types'+import {+ TaxonomicFilterGroup,+ TaxonomicFilterGroupType,+ TaxonomicFilterLogicProps,+ TaxonomicFilterValue,+} from 'lib/components/TaxonomicFilter/types'import UniversalFilters from 'lib/components/UniversalFilters/UniversalFilters'import { universalFiltersLogic } from 'lib/components/UniversalFilters/universalFiltersLogic'import { isUniversalGroupFilterLike } from 'lib/components/UniversalFilters/utils'import { dayjs } from 'lib/dayjs'import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'+import { teamLogic } from 'scenes/teamLogic'import {AnyPropertyFilter,@@ -93,6 +100,52 @@ export const LogsFilterGroup = ({ children }: { children: React.ReactNode }): JS)}+/**+ * Handles selecting a taxonomic item that carries its own value — notably the Logs group's free-text+ * `Search log message for "…"` item, whose value lives on `item.value` rather than in the `value`+ * argument (the Logs group's `getValue` returns the key, `message`).+ *+ * Besides building the filter, this records the *complete* filter to recents itself. taxonomicFilterLogic+ * records the selection too, but it strips the item down to `{ name }` and only carries a propertyFilter+ * through for items that already came from recents — so its record would drop the searched-for value, and+ * re-selecting the entry from "Recent" would yield a bare `message` with nothing to match on.+ *+ * We mirror its groupType/value exactly so both writes collide on one record rather than leaving a duplicate+ * value-less entry behind. Which of the two lands first doesn't matter: recordRecentFilter ignores a+ * value-less write when a complete record already exists, and replaces an existing value-less record when a+ * complete one arrives.+ */+export function addLogsValueFilter(+ taxonomicGroup: TaxonomicFilterGroup,+ value: TaxonomicFilterValue,+ item: any,+ currentValues: UniversalFiltersGroup['values']+): UniversalFiltersGroup['values'] {+ const newPropertyFilter = {+ key: item.key,+ value: item.value,+ operator: PropertyOperator.IContains,+ type: item.propertyFilterType,+ } as AnyPropertyFilter++ if (recentTaxonomicFiltersLogic.isMounted()) {+ recentTaxonomicFiltersLogic.actions.recordRecentFilter({+ groupType: taxonomicGroup.type,+ groupName: taxonomicGroup.name,+ value,+ // Store the key, not `item.name`. Recents are expanded for display into a bare-key row plus a+ // full-filter row, and the bare row inherits this name while dropping the value — so naming it+ // `Search log message for "foobar"` would render a row promising a value it can't apply.+ // `message` reads correctly for both rows, matching how property recents are named elsewhere.+ item: { name: item.key },+ teamId: teamLogic.findMounted()?.values.currentTeamId ?? undefined,+ propertyFilter: newPropertyFilter,+ })+ }++ return [...currentValues, newPropertyFilter]+}+export const LogsFilterSearch = (): JSX.Element => {const [visible, setVisible] = useState<boolean>(false)const { utcDateRange, filters: logsFilters, queryFilterGroup } = useValues(logsViewerFiltersLogic)@@ -122,15 +175,7 @@ export const LogsFilterSearch = (): JSX.Element => {return}- const newValues = [...filterGroup.values]- const newPropertyFilter = {- key: item.key,- value: item.value,- operator: PropertyOperator.IContains,- type: item.propertyFilterType,- } as AnyPropertyFilter- newValues.push(newPropertyFilter)- setGroupValues(newValues)+ setGroupValues(addLogsValueFilter(taxonomicGroup, value, item, filterGroup.values))setVisible(false)},onEnter: onClose,