: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(logs): tri-state exclusions on the service facet (#73572)
Jon McCallum58c516d6420e
diff --git a/products/logs/backend/logs_query_runner.py b/products/logs/backend/logs_query_runner.pyindex 6f68029c2674..f8973c3e75af 100644--- a/products/logs/backend/logs_query_runner.py+++ b/products/logs/backend/logs_query_runner.py@@ -387,6 +387,12 @@ class LogsFilterBuilder:if self.exclude_facet_field != "severity_text":exprs.append(_severity_level_to_expr(log_filter))continue+ if log_filter.key == "service_name":+ # Same own-facet strip as severity: the rail's service exclusions must not+ # zero out their own value's count when faceting on service_name.+ if self.exclude_facet_field != "service_name":+ exprs.append(property_to_expr(log_filter, team=self.team))+ continueif log_filter.key in ("trace_id", "span_id"):log_filter = log_filter.copy(deep=True)_normalise_trace_id_filter(log_filter)diff --git a/products/logs/backend/test/test_log_facet_values.py b/products/logs/backend/test/test_log_facet_values.pyindex 0e5780326d37..a84c01433e93 100644--- a/products/logs/backend/test/test_log_facet_values.py+++ b/products/logs/backend/test/test_log_facet_values.py@@ -129,22 +129,34 @@ class TestLogFacetValues(ClickhouseTestMixin, APIBaseTest):]self.assertEqual(self._facet_attr("k8s.namespace.name", filterGroup=filter_group), base)- def test_level_facet_ignores_its_own_severity_exclusion(self):- # The rail stores Level exclusions as an is_not severity_level filter in the group; the- # counts query must strip it when faceting on severity_text, or an excluded level's own+ @parameterized.expand(+ [+ ("severity_text", "severity_level"),+ ("service_name", "service_name"),+ ]+ )+ def test_facet_ignores_its_own_log_filter_exclusion(self, facet_field, log_filter_key):+ # The rail stores column-facet exclusions as an is_not log filter in the group; the counts+ # query must strip it when faceting on that facet's own field, or an excluded value's own# count would zero out.- base = self._facet("severity_text")+ base = self._facet(facet_field)own_value = next(iter(base))- filter_group = [{"key": "severity_level", "type": "log", "operator": "is_not", "value": [own_value]}]- self.assertEqual(self._facet("severity_text", filterGroup=filter_group), base)+ filter_group = [{"key": log_filter_key, "type": "log", "operator": "is_not", "value": [own_value]}]+ self.assertEqual(self._facet(facet_field, filterGroup=filter_group), base)- def test_facet_honors_severity_exclusion(self):- # An is_not severity_level filter must remove that severity's rows from other facets'- # counts — proves the NotIn translation end to end on real data.- base = self._facet("service_name")- one_severity = next(iter(self._facet("severity_text")))- filter_group = [{"key": "severity_level", "type": "log", "operator": "is_not", "value": [one_severity]}]- scoped = self._facet("service_name", filterGroup=filter_group)+ @parameterized.expand(+ [+ ("service_name", "severity_text", "severity_level"),+ ("severity_text", "service_name", "service_name"),+ ]+ )+ def test_facet_honors_other_facets_log_filter_exclusion(self, facet_field, other_facet_field, log_filter_key):+ # An is_not log filter must remove matching rows from other facets' counts — proves the+ # NOT IN translation end to end on real data.+ base = self._facet(facet_field)+ one_value = next(iter(self._facet(other_facet_field)))+ filter_group = [{"key": log_filter_key, "type": "log", "operator": "is_not", "value": [one_value]}]+ scoped = self._facet(facet_field, filterGroup=filter_group)self.assertTrue(set(scoped).issubset(set(base)))self.assertLess(sum(scoped.values()), sum(base.values()))diff --git a/products/logs/frontend/components/LogsViewer/FacetRail/facetRailLogic.test.ts b/products/logs/frontend/components/LogsViewer/FacetRail/facetRailLogic.test.tsindex 45bb2bfe23fb..8c692a58f7b3 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/facetRailLogic.test.ts+++ b/products/logs/frontend/components/LogsViewer/FacetRail/facetRailLogic.test.ts@@ -13,7 +13,12 @@ const LEVEL_SOURCE: FacetSource = {filterKey: 'severityLevels',exclusionKey: 'severity_level',}-const SERVICE_SOURCE: FacetSource = { type: 'column', column: 'service_name', filterKey: 'serviceNames' }+const SERVICE_SOURCE: FacetSource = {+ type: 'column',+ column: 'service_name',+ filterKey: 'serviceNames',+ exclusionKey: 'service_name',+}const NAMESPACE_SOURCE: FacetSource = { type: 'resourceAttribute', key: 'k8s.namespace.name' }describe('facetRailLogic', () => {@@ -80,13 +85,42 @@ describe('facetRailLogic', () => {})})- describe('service name toggling', () => {- it('adds then removes a service on the shared filters logic', async () => {+ describe('service name cycling', () => {+ it('cycles a service across the two stores: dedicated field, then is_not log filter, then cleared', async () => {await expectLogic(logic, () => logic.actions.toggleFacetValue(SERVICE_SOURCE, 'api')).toFinishAllListeners()expect(filtersLogic.values.serviceNames).toEqual(['api'])+ expect(logFilterExclusions(filtersLogic.values.filterGroup, 'service_name')).toEqual([])await expectLogic(logic, () => logic.actions.toggleFacetValue(SERVICE_SOURCE, 'api')).toFinishAllListeners()expect(filtersLogic.values.serviceNames).toEqual([])+ expect(logFilterExclusions(filtersLogic.values.filterGroup, 'service_name')).toEqual(['api'])++ await expectLogic(logic, () => logic.actions.toggleFacetValue(SERVICE_SOURCE, 'api')).toFinishAllListeners()+ expect(logFilterExclusions(filtersLogic.values.filterGroup, 'service_name')).toEqual([])+ expect((filtersLogic.values.filterGroup.values[0] as UniversalFiltersGroup).values).toEqual([])+ })++ it('holds one service included while another is excluded', async () => {+ await expectLogic(logic, () => logic.actions.toggleFacetValue(SERVICE_SOURCE, 'api')).toFinishAllListeners()+ await expectLogic(logic, () =>+ logic.actions.toggleFacetValue(SERVICE_SOURCE, 'worker')+ ).toFinishAllListeners()+ await expectLogic(logic, () => logic.actions.toggleFacetValue(SERVICE_SOURCE, 'api')).toFinishAllListeners()++ expect(filtersLogic.values.serviceNames).toEqual(['worker'])+ expect(logFilterExclusions(filtersLogic.values.filterGroup, 'service_name')).toEqual(['api'])+ })++ it('keeps service and severity exclusions under their own keys', async () => {+ // Both column facets store exclusions as is_not log filters — a service exclusion must+ // not clobber a severity exclusion already in the group, or vice versa.+ await expectLogic(logic, () => logic.actions.toggleFacetValue(LEVEL_SOURCE, 'error')).toFinishAllListeners()+ await expectLogic(logic, () => logic.actions.toggleFacetValue(LEVEL_SOURCE, 'error')).toFinishAllListeners()+ await expectLogic(logic, () => logic.actions.toggleFacetValue(SERVICE_SOURCE, 'api')).toFinishAllListeners()+ await expectLogic(logic, () => logic.actions.toggleFacetValue(SERVICE_SOURCE, 'api')).toFinishAllListeners()++ expect(logFilterExclusions(filtersLogic.values.filterGroup, 'severity_level')).toEqual(['error'])+ expect(logFilterExclusions(filtersLogic.values.filterGroup, 'service_name')).toEqual(['api'])})})diff --git a/products/logs/frontend/components/LogsViewer/FacetRail/facetRailLogic.ts b/products/logs/frontend/components/LogsViewer/FacetRail/facetRailLogic.tsindex 274d956969c6..cf5fce331c23 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/facetRailLogic.ts+++ b/products/logs/frontend/components/LogsViewer/FacetRail/facetRailLogic.ts@@ -120,25 +120,35 @@ export const facetRailLogic = kea<facetRailLogicType>([// Selection lives as log_resource_attribute filters inside the group; a click// cycles the value included → excluded → cleared.actions.setFilterGroup(cycleResourceAttributeFilter(filterGroup, source.key, value), false)- } else if (source.filterKey === 'severityLevels') {- // Split representation: includes live in the dedicated severityLevels field,- // exclusions in an is_not log filter inside the group. A click cycles- // included → excluded → cleared across the two stores.+ return+ }++ // Column facets have a split representation: includes live in the facet's dedicated+ // field, exclusions in an is_not log filter inside the group (under exclusionKey).+ // A click cycles included → excluded → cleared across the two stores; without an+ // exclusionKey the facet stays two-state. setIncluded writes the dedicated field on+ // its own (keeping its filter-change analytics); includedFilters produces the same+ // field update as a filters patch for the one transition that must move both stores+ // atomically.+ const cycleColumnValue = (+ included: string[],+ setIncluded: (next: string[]) => void,+ includedFilters: (next: string[]) => Partial<LogsViewerFilters>+ ): void => {const { exclusionKey } = sourceif (!exclusionKey) {- actions.setSeverityLevels(toggleMembership(severityLevels, value as LogSeverityLevel))+ setIncluded(toggleMembership(included, value))return}- const included = severityLevels ?? []const excluded = logFilterExclusions(filterGroup, exclusionKey)- if (included.includes(value as LogSeverityLevel)) {+ if (included.includes(value)) {// included → excluded moves the value across both stores at once (out of the- // dedicated severityLevels field, into an is_not log filter). Write them in a- // single setFilters so the query, URL sync, and filter history all see the- // final state, instead of two setters each firing against a half-applied cycle.+ // dedicated includes field, into an is_not log filter). Write them in a single+ // setFilters so the query, URL sync, and filter history all see the final+ // state, instead of two setters each firing against a half-applied cycle.actions.setFilters({- severityLevels: included.filter((v) => v !== value),+ ...includedFilters(included.filter((v) => v !== value)),filterGroup: setLogFilterExclusions(filterGroup,exclusionKey,@@ -157,10 +167,22 @@ export const facetRailLogic = kea<facetRailLogicType>([false)} else {- actions.setSeverityLevels([...included, value as LogSeverityLevel])+ setIncluded([...included, value])}+ }++ if (source.filterKey === 'severityLevels') {+ cycleColumnValue(+ severityLevels ?? [],+ (next) => actions.setSeverityLevels(next as LogSeverityLevel[]),+ (next) => ({ severityLevels: next as LogSeverityLevel[] })+ )} else if (source.filterKey === 'serviceNames') {- actions.setServiceNames(toggleMembership(serviceNames, value))+ cycleColumnValue(+ serviceNames ?? [],+ (next) => actions.setServiceNames(next),+ (next) => ({ serviceNames: next })+ )} else {// Adding a new column filterKey without wiring its setter here is a compile error.source.filterKey satisfies neverdiff --git a/products/logs/frontend/components/LogsViewer/FacetRail/facets.ts b/products/logs/frontend/components/LogsViewer/FacetRail/facets.tsindex 69ccee75f636..567861b6721f 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/facets.ts+++ b/products/logs/frontend/components/LogsViewer/FacetRail/facets.ts@@ -215,7 +215,7 @@ const SERVICE_FACET: FacetConfig = {title: 'Service',group: 'Standard',kind: 'dynamic',- source: { type: 'column', column: 'service_name', filterKey: 'serviceNames' },+ source: { type: 'column', column: 'service_name', filterKey: 'serviceNames', exclusionKey: 'service_name' },searchable: true,searchPlaceholder: 'Search services…',emptyLabel: 'No services',