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): exclude resources matching any negative attribute filter (#59034)

Frank Hamand28cdff0b8624
diff --git a/products/logs/backend/logs_query_runner.py b/products/logs/backend/logs_query_runner.py
index 46e239954085..114e43f67187 100644
--- a/products/logs/backend/logs_query_runner.py
+++ b/products/logs/backend/logs_query_runner.py
@@ -93,11 +93,16 @@ def _generate_resource_attribute_filters(
converted_exprs.append(converted_expr)
IN_ = "NOT IN" if is_negative_filter else "IN"
+ # For positive filters we want resources that match ALL filters (arrayAll), then keep them via IN.
+ # For negative filters we want to exclude resources that match ANY of the inverted filters
+ # (arrayExists), then drop them via NOT IN — otherwise multiple negatives would only exclude
+ # resources that match every one of them, instead of any one of them.
+ array_fn = "arrayExists" if is_negative_filter else "arrayAll"
# this query fetches all resource fingerprints that match at least one resource attribute filter
- # then does a secondary filter for those that match every filter
+ # then does a secondary filter for those that match every filter (positive) or any filter (negative)
# this sounds over complicated but it's because each row in the table is a single attribute - so we need to first group
- # them to collapse the rows into a single row per resource fingerprint, _then_ check every filter is met
+ # them to collapse the rows into a single row per resource fingerprint, _then_ check the per-filter match counts
return parse_expr(
f"""
(resource_fingerprint) {IN_}
@@ -110,7 +115,7 @@ def _generate_resource_attribute_filters(
AND time_bucket <= toStartOfInterval({{date_to}},toIntervalMinute(10))
AND {{resource_attribute_filters}} AND {{existing_filters}}
GROUP BY resource_fingerprint
- HAVING arrayAll(x -> x > 0, sumForEach({{ops}}))
+ HAVING {array_fn}(x -> x > 0, sumForEach({{ops}}))
)
""",
placeholders={
diff --git a/products/logs/backend/test/test_logs_query_runner.py b/products/logs/backend/test/test_logs_query_runner.py
index 8f5170e18c03..b0178dcbca1d 100644
--- a/products/logs/backend/test/test_logs_query_runner.py
+++ b/products/logs/backend/test/test_logs_query_runner.py
@@ -542,6 +542,60 @@ class TestLogsQueryRunner(ClickhouseTestMixin, APIBaseTest):
self.assertEqual(len(response["results"]), 0)
self.assertEqual(len(queries), 2)
+ @freeze_time("2025-12-16T10:33:00Z")
+ def test_multiple_negative_resource_attribute_filters(self):
+ # Two negative resource attribute filters on disjoint values (no log has both an envoy
+ # container AND the kube-system namespace). The fix exists so a resource is excluded when
+ # it matches ANY of the negative filters, not just when it matches every one. Pre-fix this
+ # would return ~all logs because nothing matched both filters at once.
+ query_params = {
+ "dateRange": {"date_from": "2025-12-16 09:00:36.178572Z", "date_to": None},
+ "limit": 2000,
+ "filterGroup": {
+ "type": "AND",
+ "values": [
+ {
+ "type": "AND",
+ "values": [
+ {
+ "key": "k8s.container.name",
+ "value": "envoy",
+ "operator": "is_not",
+ "type": "log_resource_attribute",
+ },
+ {
+ "key": "k8s.namespace.name",
+ "value": "kube-system",
+ "operator": "is_not",
+ "type": "log_resource_attribute",
+ },
+ ],
+ }
+ ],
+ },
+ }
+
+ response = self._make_logs_api_request(query_params)
+ results = response["results"]
+
+ # at least some logs come back (sanity check)
+ self.assertGreater(len(results), 0)
+ # neither excluded value appears anywhere — proves OR semantics
+ for result in results:
+ self.assertNotEqual(result["resource_attributes"].get("k8s.container.name"), "envoy")
+ self.assertNotEqual(result["resource_attributes"].get("k8s.namespace.name"), "kube-system")
+ # both excluded groups exist in the test data, so the result count must be strictly less than
+ # the unfiltered count. Pre-fix this would have returned every log in range (since no resource
+ # matched both filters at once).
+ unfiltered = self._make_logs_api_request(
+ {
+ "dateRange": query_params["dateRange"],
+ "limit": query_params["limit"],
+ "filterGroup": {"type": "AND", "values": [{"type": "AND", "values": []}]},
+ }
+ )
+ self.assertLess(len(results), len(unfiltered["results"]))
+
@freeze_time("2025-12-16T10:33:00Z")
def test_resource_negative_attribute_filters(self):
query_params = {