: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): honor severity levels in resource-attribute facets (#70987)
Frank Hamande089de336478
diff --git a/posthog/clickhouse/logs/__init__.py b/posthog/clickhouse/logs/__init__.pyindex 84f9890d90fc..ddda0a1f8ac6 100644--- a/posthog/clickhouse/logs/__init__.py+++ b/posthog/clickhouse/logs/__init__.py@@ -2,7 +2,9 @@ from .log_attributes import LOG_ATTRIBUTES_DISTRIBUTED_TABLE_SQL, LOG_ATTRIBUTESfrom .log_attributes2 import LOG_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL, LOG_ATTRIBUTES2_TABLE_SQLfrom .log_attributes3 import LOG_ATTRIBUTES3_DISTRIBUTED_TABLE_SQL, LOG_ATTRIBUTES3_TABLE_SQLfrom .logs32 import (+ LOG_ATTRIBUTES3_MV,LOG_ATTRIBUTES_MV,+ LOG_RESOURCE_ATTRIBUTES3_MV,LOG_RESOURCE_ATTRIBUTES_MV,LOGS32_TABLE_SQL,LOGS_DISTRIBUTED2_TABLE_SQL,@@ -29,6 +31,8 @@ __all__ = ["LOGS_DISTRIBUTED2_TABLE_SQL","LOG_ATTRIBUTES_MV","LOG_RESOURCE_ATTRIBUTES_MV",+ "LOG_ATTRIBUTES3_MV",+ "LOG_RESOURCE_ATTRIBUTES3_MV","LOG_ATTRIBUTES_TABLE_SQL","LOG_ATTRIBUTES_DISTRIBUTED_TABLE_SQL","LOGS_KAFKA_METRICS_TABLE_SQL",diff --git a/posthog/clickhouse/logs/logs32.py b/posthog/clickhouse/logs/logs32.pyindex 0a4dde2abb42..0bd426288eaf 100644--- a/posthog/clickhouse/logs/logs32.py+++ b/posthog/clickhouse/logs/logs32.py@@ -3,6 +3,7 @@ from django.conf import settingsfrom posthog.clickhouse.table_engines import Distributed, MergeTreeEngine, ReplicationSchemefrom .log_attributes import TABLE_NAME as LOG_ATTRIBUTES_TABLE_NAME+from .log_attributes3 import TABLE_NAME as LOG_ATTRIBUTES3_TABLE_NAMETABLE_NAME = "logs32"@@ -217,3 +218,110 @@ FROMresource_attributes)"""+++def LOG_ATTRIBUTES3_MV():+ return f"""+CREATE MATERIALIZED VIEW IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}_to_log_attributes3 TO {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{LOG_ATTRIBUTES3_TABLE_NAME}+(+ `team_id` Int32,+ `time_bucket` DateTime64(0),+ `original_expiry_time_bucket` DateTime64(0),+ `service_name` LowCardinality(String),+ `resource_fingerprint` UInt64,+ `attribute_key` LowCardinality(String),+ `attribute_value` String,+ `attribute_type` LowCardinality(String),+ `severity_text` LowCardinality(String),+ `attribute_count` SimpleAggregateFunction(sum, UInt64)+)+AS SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ resource_fingerprint,+ attribute_key,+ attribute_value,+ attribute_type,+ severity_text,+ attribute_count+FROM+(+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_fingerprint,+ severity_text AS severity_text,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS attributes,+ arrayJoin(attributes) AS attribute,+ 'log' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}+ GROUP BY+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ resource_fingerprint,+ severity_text,+ attributes+)+"""+++def LOG_RESOURCE_ATTRIBUTES3_MV():+ return f"""+CREATE MATERIALIZED VIEW IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}_to_resource_attributes3 TO {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{LOG_ATTRIBUTES3_TABLE_NAME}+(+ `team_id` Int32,+ `time_bucket` DateTime64(0),+ `original_expiry_time_bucket` DateTime64(0),+ `service_name` LowCardinality(String),+ `resource_fingerprint` UInt64,+ `attribute_key` LowCardinality(String),+ `attribute_value` String,+ `attribute_type` LowCardinality(String),+ `severity_text` LowCardinality(String),+ `attribute_count` SimpleAggregateFunction(sum, UInt64)+)+AS SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ resource_fingerprint,+ attribute_key,+ attribute_value,+ attribute_type,+ severity_text,+ attribute_count+FROM+(+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_fingerprint,+ severity_text AS severity_text,+ arrayJoin(resource_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}+ GROUP BY+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ resource_fingerprint,+ severity_text,+ resource_attributes+)+"""diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.pyindex 68695752ee7e..827a650be195 100644--- a/posthog/clickhouse/schema.py+++ b/posthog/clickhouse/schema.py@@ -12,11 +12,11 @@ from posthog.clickhouse.distributed_system_processes import DISTRIBUTED_SYSTEM_Pfrom posthog.clickhouse.log_entries import KAFKA_LOG_ENTRIES_TABLE_SQL, LOG_ENTRIES_TABLE_MV_SQL, LOG_ENTRIES_TABLE_SQLfrom posthog.clickhouse.logs import (LOG_ATTRIBUTES2_TABLE_SQL,+ LOG_ATTRIBUTES3_DISTRIBUTED_TABLE_SQL,+ LOG_ATTRIBUTES3_MV,LOG_ATTRIBUTES3_TABLE_SQL,- LOG_ATTRIBUTES_DISTRIBUTED_TABLE_SQL,- LOG_ATTRIBUTES_MV,LOG_ATTRIBUTES_TABLE_SQL,- LOG_RESOURCE_ATTRIBUTES_MV,+ LOG_RESOURCE_ATTRIBUTES3_MV,LOGS32_TABLE_SQL,LOGS34_TABLE_SQL,LOGS34_TO_LOG_ATTRIBUTES3_MV,@@ -462,7 +462,7 @@ CREATE_DISTRIBUTED_TABLE_QUERIES = (WRITABLE_DISTINCT_ID_USAGE_TABLE_SQL,LOGS_DISTRIBUTED_TABLE_SQL,LOGS_DISTRIBUTED2_TABLE_SQL,- LOG_ATTRIBUTES_DISTRIBUTED_TABLE_SQL,+ LOG_ATTRIBUTES3_DISTRIBUTED_TABLE_SQL,LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL,LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL,METRICS_DISTRIBUTED_TABLE_SQL,@@ -539,8 +539,8 @@ CREATE_MV_TABLE_QUERIES = (KAFKA_TO_BUFFER_MV_SQL,*[table.materialized_view_sql for table in EMBEDDING_TABLES],DISTINCT_ID_USAGE_MV_SQL,- LOG_ATTRIBUTES_MV,- LOG_RESOURCE_ATTRIBUTES_MV,+ LOG_ATTRIBUTES3_MV,+ LOG_RESOURCE_ATTRIBUTES3_MV,LOGS34_TO_LOG_ATTRIBUTES_MV,LOGS34_TO_RESOURCE_ATTRIBUTES_MV,LOGS34_TO_LOG_ATTRIBUTES3_MV,diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambrindex 2d81e57b97d2..d8e3cdd7e565 100644--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr@@ -2823,7 +2823,7 @@# name: test_create_table_query[log_attributes_distributed]'''- CREATE TABLE IF NOT EXISTS posthog_test.log_attributes_distributed AS posthog_test.log_attributes ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'log_attributes')+ CREATE OR REPLACE TABLE posthog_test.log_attributes_distributed AS posthog_test.log_attributes3 ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'log_attributes3')'''# ---@@ -2964,10 +2964,10 @@'''# ----# name: test_create_table_query[logs32_to_log_attributes]+# name: test_create_table_query[logs32_to_log_attributes3]'''- CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.logs32_to_log_attributes TO posthog_test.log_attributes+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.logs32_to_log_attributes3 TO posthog_test.log_attributes3(`team_id` Int32,`time_bucket` DateTime64(0),@@ -2977,6 +2977,7 @@`attribute_key` LowCardinality(String),`attribute_value` String,`attribute_type` LowCardinality(String),+ `severity_text` LowCardinality(String),`attribute_count` SimpleAggregateFunction(sum, UInt64))AS SELECT@@ -2988,6 +2989,7 @@attribute_key,attribute_value,attribute_type,+ severity_text,attribute_countFROM(@@ -2997,6 +2999,7 @@toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,service_name AS service_name,resource_fingerprint,+ severity_text AS severity_text,mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS attributes,arrayJoin(attributes) AS attribute,'log' AS attribute_type,@@ -3010,15 +3013,16 @@original_expiry_time_bucket,service_name,resource_fingerprint,+ severity_text,attributes)'''# ----# name: test_create_table_query[logs32_to_resource_attributes]+# name: test_create_table_query[logs32_to_resource_attributes3]'''- CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.logs32_to_resource_attributes TO posthog_test.log_attributes+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.logs32_to_resource_attributes3 TO posthog_test.log_attributes3(`team_id` Int32,`time_bucket` DateTime64(0),@@ -3028,6 +3032,7 @@`attribute_key` LowCardinality(String),`attribute_value` String,`attribute_type` LowCardinality(String),+ `severity_text` LowCardinality(String),`attribute_count` SimpleAggregateFunction(sum, UInt64))AS SELECT@@ -3039,6 +3044,7 @@attribute_key,attribute_value,attribute_type,+ severity_text,attribute_countFROM(@@ -3048,6 +3054,7 @@toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,service_name AS service_name,resource_fingerprint,+ severity_text AS severity_text,arrayJoin(resource_attributes) AS attribute,'resource' AS attribute_type,attribute.1 AS attribute_key,@@ -3060,6 +3067,7 @@original_expiry_time_bucket,service_name,resource_fingerprint,+ severity_text,resource_attributes)diff --git a/products/logs/backend/log_facet_values_query_runner.py b/products/logs/backend/log_facet_values_query_runner.pyindex f3ac2c880aaa..f9a4cd87b07b 100644--- a/products/logs/backend/log_facet_values_query_runner.py+++ b/products/logs/backend/log_facet_values_query_runner.py@@ -39,8 +39,9 @@ class LogFacetValuesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQuefacet (e.g. k8s.namespace.name) reads the pre-aggregated log_attributes rollup instead of thelogs Map column — orders of magnitude cheaper, and the only way to keep the query under the readcap at scale. Both exclude the facet's own filter so selecting a value re-scopes the *other*- facets. Resource-attribute facet counts honour service_name and other resource-attribute filters- but not severity / body-search / log-attribute filters (those dimensions aren't in the rollup).+ facets. Resource-attribute facet counts honour service_name, severity levels and other+ resource-attribute filters (severity_text lives on the rollup), but not body-search / log-attribute+ filters (those dimensions aren't in the rollup)."""query: LogsQuery@@ -167,8 +168,9 @@ class LogFacetValuesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQuedef _resource_attribute_query(self) -> ast.SelectQuery:# Served from the pre-aggregated log_attributes rollup (sum(attribute_count)) rather than# grouping the logs Map column, which reads the whole resource_attributes column and blows- # past the read cap at scale. The rollup has no severity/body/log-attribute dimension, so only- # service_name and other resource-attribute filters re-scope the counts.+ # past the read cap at scale. The rollup carries severity_text and service_name, so severity+ # levels, service_name and other resource-attribute filters re-scope the counts; body-search+ # and log-attribute filters still aren't in the rollup.date_range = self._attributes_query_date_rangewhere_exprs: list[ast.Expr] = []if self.query.serviceNames:@@ -180,6 +182,17 @@ class LogFacetValuesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQue},))+ if self.query.severityLevels:+ where_exprs.append(+ parse_expr(+ "severity_text IN {severityLevels}",+ placeholders={+ "severityLevels": ast.Tuple(+ exprs=[ast.Constant(value=str(sl)) for sl in self.query.severityLevels]+ )+ },+ )+ )# Cross-filter by other resource attributes, excluding this facet's own key so selecting a# value doesn't collapse the facet to that single value.filter_builder = LogsFilterBuilder(diff --git a/products/logs/backend/test/test_log_facet_values.py b/products/logs/backend/test/test_log_facet_values.pyindex 058a214a16d7..b6cf10f8a006 100644--- a/products/logs/backend/test/test_log_facet_values.py+++ b/products/logs/backend/test/test_log_facet_values.py@@ -128,13 +128,16 @@ class TestLogFacetValues(ClickhouseTestMixin, APIBaseTest):]self.assertEqual(self._facet_attr("k8s.namespace.name", filterGroup=filter_group), base)- def test_resource_facet_ignores_severity_filter(self):- # Resource-attribute facets are served from the log_attributes rollup, which has no severity- # dimension — a severity filter is accepted but does not re-scope the counts.+ def test_resource_facet_honors_severity_filter(self):+ # severity_text now lives on the log_attributes rollup, so a severity filter re-scopes+ # resource-attribute facet counts (previously it was accepted but silently ignored).base = self._facet_attr("k8s.namespace.name")self.assertGreater(len(base), 0)- scoped = self._facet_attr("k8s.namespace.name", severityLevels=["error"])- self.assertEqual(scoped, base)+ one_severity = next(iter(self._facet("severity_text")))+ scoped = self._facet_attr("k8s.namespace.name", severityLevels=[one_severity])+ self.assertGreater(len(scoped), 0)+ self.assertTrue(set(scoped).issubset(set(base)))+ self.assertLess(sum(scoped.values()), sum(base.values()))def test_resource_facet_honors_other_resource_attribute_filter(self):# A different resource-attribute filter re-scopes the counts via the rollup's