: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(logs): scope service and severity facets by resource-attribute filters (#124929)
Jon McCallum632c233b460c
diff --git a/products/logs/backend/log_facet_values_query_runner.py b/products/logs/backend/log_facet_values_query_runner.pyindex 69518271460b..61bf3d75af7b 100644--- a/products/logs/backend/log_facet_values_query_runner.py+++ b/products/logs/backend/log_facet_values_query_runner.py@@ -59,8 +59,10 @@ class LogFacetValuesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueThus, when the user selects a value, the other facets change, but this facet does not change.The applied filters depend on the columns that each rollup has:- - A column facet removes its own filter exactly. It ignores body search, log-attribute filters,- and resource-attribute filters, because logs_volume_buckets does not have these columns.+ - A column facet removes its own filter exactly. It ignores body search and log-attribute+ filters, because neither rollup has these columns. When resource-attribute filters are set,+ a column facet groups log_attributes instead, because logs_volume_buckets has no+ resource_fingerprint.- An attribute facet applies service, severity, and resource-attribute filters. It ignores bodysearch and log-attribute filters.- Only a resource-attribute facet can remove its own filter. Its rollup rows share one@@ -176,35 +178,19 @@ class LogFacetValuesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQuereturn self._attribute_query(self.attribute_facet)if self._scoped_to_person_or_session:return self._column_facet_query_from_logs()- return self._column_facet_query_from_rollup()-- def _column_facet_query_from_rollup(self) -> ast.SelectQuery:- # This query sums log_count in the logs_volume_buckets rollup. A query on the logs table- # reads each row in the range, and at scale it goes above the read limit.- # The rollup has service_name and severity_text, so exclude_facet_field removes the filter- # of this facet exactly. The rollup has no body, log attributes, or resource attributes, so- # this query ignores these filters. When personId or sessionId is set, to_query() calls- # _column_facet_query_from_logs, because the rollup has neither column.- facet = ast.Field(chain=[cast(str, self.facet_field)])filter_builder = LogsFilterBuilder(self.query,self.team,- self.query_date_range,+ self._attributes_query_date_range,exclude_facet_field=self.facet_field,)- date_range = self._volume_buckets_query_date_range- exprs: list[ast.Expr] = [- # Floor the lower limit to the bucket that contains date_from. That bucket starts before- # date_from, but it holds logs from after date_from. A bucket that starts before date_to- # is in the range, so the upper limit needs no change.- parse_expr(- "time_bucket >= {date_from} AND time_bucket < {date_to}",- placeholders={- "date_from": date_range.date_from_to_start_of_interval_hogql(),- "date_to": ast.Constant(value=date_range.date_to()),- },- ),- ]+ if filter_builder.resource_attribute_filters or filter_builder.resource_attribute_negative_filters:+ return self._column_facet_query_from_attributes(filter_builder)+ return self._column_facet_query_from_rollup()++ def _column_facet_filter_exprs(self, filter_builder: LogsFilterBuilder) -> list[ast.Expr]:+ facet = ast.Field(chain=[cast(str, self.facet_field)])+ exprs: list[ast.Expr] = []if self.query.serviceNames and self.facet_field != "service_name":exprs.append(parse_expr(@@ -239,6 +225,35 @@ class LogFacetValuesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQue},))+ return exprs++ def _column_facet_query_from_rollup(self) -> ast.SelectQuery:+ # This query sums log_count in the logs_volume_buckets rollup. A query on the logs table+ # reads each row in the range, and at scale it goes above the read limit.+ # The rollup has service_name and severity_text, so exclude_facet_field removes the filter+ # of this facet exactly. The rollup has no body or log attributes, so this query ignores+ # these filters.+ facet = ast.Field(chain=[cast(str, self.facet_field)])+ filter_builder = LogsFilterBuilder(+ self.query,+ self.team,+ self.query_date_range,+ exclude_facet_field=self.facet_field,+ )+ date_range = self._volume_buckets_query_date_range+ exprs: list[ast.Expr] = [+ # Floor the lower limit to the bucket that contains date_from. That bucket starts before+ # date_from, but it holds logs from after date_from. A bucket that starts before date_to+ # is in the range, so the upper limit needs no change.+ parse_expr(+ "time_bucket >= {date_from} AND time_bucket < {date_to}",+ placeholders={+ "date_from": date_range.date_from_to_start_of_interval_hogql(),+ "date_to": ast.Constant(value=date_range.date_to()),+ },+ ),+ ]+ exprs.extend(self._column_facet_filter_exprs(filter_builder))query = parse_select("""SELECT {facet} AS value, sum(log_count) AS count@@ -257,6 +272,50 @@ class LogFacetValuesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueassert isinstance(query, ast.SelectQuery)return query+ def _column_facet_query_from_attributes(self, filter_builder: LogsFilterBuilder) -> ast.SelectQuery:+ # log_attributes has one row per resource attribute key, so the middle query keeps one count+ # per resource instead of one per key. Logs with no resource attributes have no row here,+ # so negative-only resource filters do not count them.+ facet = ast.Field(chain=[cast(str, self.facet_field)])+ date_range = self._attributes_query_date_range+ exprs = self._column_facet_filter_exprs(filter_builder)+ exprs.append(filter_builder.resource_filter(existing_filters=exprs))+ query = parse_select(+ """+ SELECT {facet} AS value, sum(log_count) AS count+ FROM (+ SELECT service_name, severity_text, max(key_count) AS log_count+ FROM (+ SELECT+ time_bucket,+ resource_fingerprint,+ service_name,+ severity_text,+ attribute_key,+ sum(attribute_count) AS key_count+ FROM log_attributes+ WHERE time_bucket >= {date_from_start_of_interval}+ AND time_bucket < {date_to}+ AND attribute_type = 'resource'+ AND {where}+ GROUP BY time_bucket, resource_fingerprint, service_name, severity_text, attribute_key+ )+ GROUP BY time_bucket, resource_fingerprint, service_name, severity_text+ )+ GROUP BY {facet}+ ORDER BY sum(log_count) DESC, {facet} ASC+ LIMIT {limit}+ """,+ placeholders={+ "facet": facet,+ "where": ast.And(exprs=exprs),+ "limit": ast.Constant(value=self.query.limit or DEFAULT_FACET_LIMIT),+ **date_range.to_placeholders(),+ },+ )+ assert isinstance(query, ast.SelectQuery)+ return query+def _column_facet_query_from_logs(self) -> ast.SelectQuery:# logs_volume_buckets has no personId or sessionId. Thus, for the person and session Logs# tabs, this query groups the logs table. The query reads only the lines of one person ordiff --git a/products/logs/backend/test/test_log_facet_values.py b/products/logs/backend/test/test_log_facet_values.pyindex 65ce2ddeab22..e266450dfdac 100644--- a/products/logs/backend/test/test_log_facet_values.py+++ b/products/logs/backend/test/test_log_facet_values.py@@ -13,6 +13,7 @@ from posthog.clickhouse.client import sync_executeclass TestLogFacetValues(ClickhouseTestMixin, APIBaseTest):CLASS_DATA_LEVEL_SETUP = True+ log_items: list[dict]DATE_RANGE = {"date_from": "2025-12-16T09:00:00Z", "date_to": "2025-12-16T11:00:00Z"}@@ -27,6 +28,8 @@ class TestLogFacetValues(ClickhouseTestMixin, APIBaseTest):log_item["team_id"] = cls.team.idlog_items.append(log_item)+ cls.log_items = log_items+sql = "\n".join(json.dumps(log_item) for log_item in log_items)sync_execute(f"""INSERT INTO logs@@ -97,6 +100,31 @@ class TestLogFacetValues(ClickhouseTestMixin, APIBaseTest):f"{other_filter_key} should re-scope {facet_field} counts",)+ @parameterized.expand(+ [+ ("service_name", "exact"),+ ("service_name", "is_not"),+ ("severity_text", "exact"),+ ("severity_text", "is_not"),+ ]+ )+ def test_column_facet_honors_resource_attribute_filter(self, facet_field, operator):+ namespace = next(iter(self._facet_attr("k8s.namespace.name")))+ filter_group = [+ {"key": "k8s.namespace.name", "type": "log_resource_attribute", "operator": operator, "value": namespace}+ ]+ # The fixture has logs at 10:3x, so this date_to catches buckets that start at or after it.+ date_range = {**self.DATE_RANGE, "date_to": "2025-12-16T10:30:00Z"}+ expected: Counter[str] = Counter()+ for log_item in self.log_items:+ if not ("2025-12-16 09" <= log_item["timestamp"] < "2025-12-16 10:30"):+ continue+ in_namespace = log_item["resource_attributes"].get("k8s.namespace.name") == namespace+ if in_namespace == (operator == "exact"):+ expected[log_item[facet_field]] += 1++ self.assertEqual(self._facet(facet_field, filterGroup=filter_group, dateRange=date_range), dict(expected))+def test_column_facet_keeps_the_bucket_that_contains_date_from(self):# The rollup has 5-minute buckets. A date_from inside a bucket must not drop that bucket, or# the counts lose up to 5 minutes of logs. Every fixture log in DATE_RANGE is at or after