: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): add severity dimension to log_attributes for facet filtering (#65798)
Jon McCallum04664583e567
diff --git a/posthog/clickhouse/logs/__init__.py b/posthog/clickhouse/logs/__init__.pyindex a40ea42761e8..4a333899a829 100644--- a/posthog/clickhouse/logs/__init__.py+++ b/posthog/clickhouse/logs/__init__.py@@ -1,5 +1,6 @@from .log_attributes import LOG_ATTRIBUTES_DISTRIBUTED_TABLE_SQL, LOG_ATTRIBUTES_TABLE_SQLfrom .log_attributes2 import LOG_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL, LOG_ATTRIBUTES2_TABLE_SQL+from .log_attributes3 import LOG_ATTRIBUTES3_TABLE_SQLfrom .logs32 import (LOG_ATTRIBUTES_MV,LOG_RESOURCE_ATTRIBUTES_MV,@@ -13,7 +14,9 @@ from .logs34 import (KAFKA_LOGS_AVRO_KAFKA_METRICS_MV,KAFKA_LOGS_AVRO_TABLE_SQL,LOGS34_TABLE_SQL,+ LOGS34_TO_LOG_ATTRIBUTES3_MV,LOGS34_TO_LOG_ATTRIBUTES_MV,+ LOGS34_TO_RESOURCE_ATTRIBUTES3_MV,LOGS34_TO_RESOURCE_ATTRIBUTES_MV,LOGS_DISTRIBUTED_TABLE_SQL as LOGS34_DISTRIBUTED_TABLE_SQL,)@@ -34,12 +37,15 @@ __all__ = ["LOGS34_DISTRIBUTED_TABLE_SQL","LOGS34_TO_LOG_ATTRIBUTES_MV","LOGS34_TO_RESOURCE_ATTRIBUTES_MV",+ "LOGS34_TO_LOG_ATTRIBUTES3_MV",+ "LOGS34_TO_RESOURCE_ATTRIBUTES3_MV","KAFKA_LOGS_AVRO_TABLE_SQL","KAFKA_LOGS34_AVRO_MV","KAFKA_LOGS_AVRO_KAFKA_METRICS_MV","KAFKA_LOGS_AVRO_BILLING_METRICS_MV","LOG_ATTRIBUTES2_TABLE_SQL","LOG_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL",+ "LOG_ATTRIBUTES3_TABLE_SQL","LOGS_BILLING_METRICS_TABLE_SQL","LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL",]diff --git a/posthog/clickhouse/logs/log_attributes3.py b/posthog/clickhouse/logs/log_attributes3.pynew file mode 100644index 000000000000..747a45f53160--- /dev/null+++ b/posthog/clickhouse/logs/log_attributes3.py@@ -0,0 +1,34 @@+from django.conf import settings++from posthog.clickhouse.table_engines import AggregatingMergeTree, ReplicationScheme++TABLE_NAME = "log_attributes3"+++def LOG_ATTRIBUTES3_TABLE_SQL():+ return f"""+CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}+(+ `team_id` Int32,+ `time_bucket` DateTime64(0),+ `service_name` LowCardinality(String),+ `resource_fingerprint` UInt64 DEFAULT 0,+ `attribute_key` LowCardinality(String),+ `attribute_value` String CODEC(ZSTD(5)),+ `attribute_count` SimpleAggregateFunction(sum, UInt64),+ `attribute_type` LowCardinality(String) DEFAULT 'log',+ `original_expiry_time_bucket` DateTime DEFAULT now(),+ `severity_text` LowCardinality(String),+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1+)+ENGINE = {AggregatingMergeTree(TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}+PARTITION BY toDate(original_expiry_time_bucket)+ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value, severity_text)+TTL original_expiry_time_bucket+SETTINGS+ deduplicate_merge_projection_mode = 'drop',+ index_granularity = 8192+"""diff --git a/posthog/clickhouse/logs/logs34.py b/posthog/clickhouse/logs/logs34.pyindex 2e48680558f7..1e4e6ca2adef 100644--- a/posthog/clickhouse/logs/logs34.py+++ b/posthog/clickhouse/logs/logs34.py@@ -4,6 +4,7 @@ from posthog.clickhouse.kafka_engine import kafka_enginefrom posthog.clickhouse.table_engines import Distributed, MergeTreeEngine, ReplicationSchemefrom .log_attributes2 import TABLE_NAME as LOG_ATTRIBUTES_TABLE_NAME+from .log_attributes3 import TABLE_NAME as LOG_ATTRIBUTES3_TABLE_NAMETABLE_NAME = "logs34"KAFKA_TABLE_NAME = "kafka_logs_avro"@@ -201,6 +202,113 @@ FROM"""+def LOGS34_TO_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 LOGS34_TO_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+)+"""++def KAFKA_LOGS_AVRO_TABLE_SQL():return f"""CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{KAFKA_TABLE_NAME}diff --git a/posthog/clickhouse/migrations/0282_log_attributes_severity.py b/posthog/clickhouse/migrations/0282_log_attributes_severity.pynew file mode 100644index 000000000000..8207811e8d56--- /dev/null+++ b/posthog/clickhouse/migrations/0282_log_attributes_severity.py@@ -0,0 +1,18 @@+from posthog.clickhouse.client.connection import NodeRole+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions+from posthog.clickhouse.logs import (+ LOG_ATTRIBUTES3_TABLE_SQL,+ LOGS34_TO_LOG_ATTRIBUTES3_MV,+ LOGS34_TO_RESOURCE_ATTRIBUTES3_MV,+)++# log_attributes2 cannot gain severity_text in place: it is an AggregatingMergeTree whose sort key+# is its aggregation key, and MODIFY ORDER BY would re-key existing parts mid-flight. Instead we add+# a parallel log_attributes3 table that carries severity_text in its ORDER BY and back it with new+# MVs reading from logs34. Existing log_attributes2 and its MVs are left untouched, so ingestion keeps+# running and the read path can be cut over to log_attributes3 once it has backfilled.+operations = [+ run_sql_with_exceptions(LOG_ATTRIBUTES3_TABLE_SQL(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(LOGS34_TO_LOG_ATTRIBUTES3_MV(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(LOGS34_TO_RESOURCE_ATTRIBUTES3_MV(), node_roles=[NodeRole.LOGS]),+]diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txtindex 15b9dd6f8737..cba2044f87a1 100644--- a/posthog/clickhouse/migrations/max_migration.txt+++ b/posthog/clickhouse/migrations/max_migration.txt@@ -1 +1 @@-0281_marketing_costs_preaggregated+0282_log_attributes_severitydiff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.pyindex a7e72c60d5c8..c1625173826e 100644--- a/posthog/clickhouse/schema.py+++ b/posthog/clickhouse/schema.py@@ -11,13 +11,16 @@ 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_TABLE_SQL,LOG_ATTRIBUTES_DISTRIBUTED_TABLE_SQL,LOG_ATTRIBUTES_MV,LOG_ATTRIBUTES_TABLE_SQL,LOG_RESOURCE_ATTRIBUTES_MV,LOGS32_TABLE_SQL,LOGS34_TABLE_SQL,+ LOGS34_TO_LOG_ATTRIBUTES3_MV,LOGS34_TO_LOG_ATTRIBUTES_MV,+ LOGS34_TO_RESOURCE_ATTRIBUTES3_MV,LOGS34_TO_RESOURCE_ATTRIBUTES_MV,LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL,LOGS_BILLING_METRICS_TABLE_SQL,@@ -363,6 +366,7 @@ CREATE_MERGETREE_TABLE_QUERIES = (LOGS_KAFKA_METRICS_TABLE_SQL,LOGS34_TABLE_SQL,LOG_ATTRIBUTES2_TABLE_SQL,+ LOG_ATTRIBUTES3_TABLE_SQL,LOGS_BILLING_METRICS_TABLE_SQL,METRICS1_TABLE_SQL,METRIC_ATTRIBUTES_TABLE_SQL,@@ -508,6 +512,8 @@ CREATE_MV_TABLE_QUERIES = (LOG_RESOURCE_ATTRIBUTES_MV,LOGS34_TO_LOG_ATTRIBUTES_MV,LOGS34_TO_RESOURCE_ATTRIBUTES_MV,+ LOGS34_TO_LOG_ATTRIBUTES3_MV,+ LOGS34_TO_RESOURCE_ATTRIBUTES3_MV,METRIC_ATTRIBUTES_MV,METRIC_RESOURCE_ATTRIBUTES_MV,TOPHOG_MV_SQL,diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambrindex 550ba3eb7682..b43987000487 100644--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr@@ -2602,6 +2602,36 @@'''# ---+# name: test_create_table_query[log_attributes3]+ '''++ CREATE TABLE IF NOT EXISTS posthog_test.log_attributes3+ (+ `team_id` Int32,+ `time_bucket` DateTime64(0),+ `service_name` LowCardinality(String),+ `resource_fingerprint` UInt64 DEFAULT 0,+ `attribute_key` LowCardinality(String),+ `attribute_value` String CODEC(ZSTD(5)),+ `attribute_count` SimpleAggregateFunction(sum, UInt64),+ `attribute_type` LowCardinality(String) DEFAULT 'log',+ `original_expiry_time_bucket` DateTime DEFAULT now(),+ `severity_text` LowCardinality(String),+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1+ )+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.log_attributes3', '{replica}-{shard}')+ PARTITION BY toDate(original_expiry_time_bucket)+ ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value, severity_text)+ TTL original_expiry_time_bucket+ SETTINGS+ deduplicate_merge_projection_mode = 'drop',+ index_granularity = 8192++ '''+# ---# name: test_create_table_query[log_attributes]'''@@ -2954,6 +2984,61 @@'''# ---+# name: test_create_table_query[logs34_to_log_attributes3]+ '''++ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.logs34_to_log_attributes3 TO posthog_test.log_attributes3+ (+ `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 posthog_test.logs34+ GROUP BY+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ resource_fingerprint,+ severity_text,+ attributes+ )++ '''+# ---# name: test_create_table_query[logs34_to_log_attributes]'''@@ -3005,6 +3090,60 @@'''# ---+# name: test_create_table_query[logs34_to_resource_attributes3]+ '''++ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.logs34_to_resource_attributes3 TO posthog_test.log_attributes3+ (+ `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 posthog_test.logs34+ GROUP BY+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ resource_fingerprint,+ severity_text,+ resource_attributes+ )++ '''+# ---# name: test_create_table_query[logs34_to_resource_attributes]'''@@ -8916,6 +9055,36 @@'''# ---+# name: test_create_table_query_replicated_and_storage[log_attributes3]+ '''++ CREATE TABLE IF NOT EXISTS posthog_test.log_attributes3+ (+ `team_id` Int32,+ `time_bucket` DateTime64(0),+ `service_name` LowCardinality(String),+ `resource_fingerprint` UInt64 DEFAULT 0,+ `attribute_key` LowCardinality(String),+ `attribute_value` String CODEC(ZSTD(5)),+ `attribute_count` SimpleAggregateFunction(sum, UInt64),+ `attribute_type` LowCardinality(String) DEFAULT 'log',+ `original_expiry_time_bucket` DateTime DEFAULT now(),+ `severity_text` LowCardinality(String),+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1+ )+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.log_attributes3', '{replica}-{shard}')+ PARTITION BY toDate(original_expiry_time_bucket)+ ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value, severity_text)+ TTL original_expiry_time_bucket+ SETTINGS+ deduplicate_merge_projection_mode = 'drop',+ index_granularity = 8192++ '''+# ---# name: test_create_table_query_replicated_and_storage[log_attributes]'''