: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): derive log retention from per-row retention_days (#73826)
Frank Hamand00e1715ef5c6
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hclindex 2cc42b5d62e4..da618c7eb096 100644--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl@@ -1,60 +1,4 @@database "posthog" {- table "kafka_logs_avro" {- column "uuid" {- type = "String"- }- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "body" {- type = "String"- }- column "severity_text" {- type = "String"- }- column "severity_number" {- type = "Int32"- }- column "service_name" {- type = "String"- }- column "resource_attributes" {- type = "Map(LowCardinality(String), String)"- }- column "instrumentation_scope" {- type = "String"- }- column "event_name" {- type = "String"- }- column "attributes" {- type = "Map(LowCardinality(String), String)"- }- engine "kafka" {- broker_list = "warpstream_logs"- topic_list = "kafka_topic_list = 'clickhouse_logs'"- group_name = "kafka_group_name = 'clickhouse-logs-avro-new'"- format = "kafka_format = 'Avro'"- num_consumers = 8- skip_broken_messages = 100- poll_timeout_ms = 3000- poll_max_batch_size = 1000- thread_per_consumer = true- }- }-table "kafka_metrics_avro" {column "uuid" {type = "String"@@ -2369,106 +2313,6 @@ SQL}}- materialized_view "kafka_logs34_avro_mv" {- to_table = "posthog.logs34"- query = <<SQL-SELECT- uuid,- trace_id,- span_id,- trace_flags,- timestamp,- observed_timestamp,- body,- severity_text,- severity_number,- service_name,- instrumentation_scope,- event_name,- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp- + toIntervalDay(- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))- ) AS original_expiry_timestamp,- _partition,- _topic,- _offset,- toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed-FROM posthog.kafka_logs_avro-SQL-- column "uuid" {- type = "String"- }- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "body" {- type = "String"- }- column "severity_text" {- type = "String"- }- column "severity_number" {- type = "Int32"- }- column "service_name" {- type = "String"- }- column "instrumentation_scope" {- type = "String"- }- column "event_name" {- type = "String"- }- column "attributes_map_str" {- type = "Map(String, String)"- }- column "resource_attributes" {- type = "Map(String, String)"- }- column "team_id" {- type = "Int32"- }- column "original_expiry_timestamp" {- type = "DateTime64(6)"- }- column "_partition" {- type = "UInt64"- }- column "_topic" {- type = "LowCardinality(String)"- }- column "_offset" {- type = "UInt64"- }- column "_record_count" {- type = "Int64"- }- column "_bytes_uncompressed" {- type = "Nullable(Float64)"- }- column "_bytes_compressed" {- type = "Nullable(Float64)"- }- }-materialized_view "kafka_logs_avro_billing_metrics_mv" {to_table = "posthog.logs_billing_metrics"query = <<SQLdiff --git a/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl b/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hclindex dbbb9e27d43e..1ed573c81877 100644--- a/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl@@ -124,6 +124,68 @@ database "posthog" {}}+ table "kafka_logs_avro" {+ settings = {+ input_format_avro_allow_missing_fields = "1"+ }+ column "uuid" {+ type = "String"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "body" {+ type = "String"+ }+ column "severity_text" {+ type = "String"+ }+ column "severity_number" {+ type = "Int32"+ }+ column "service_name" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "event_name" {+ type = "String"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "retention_days" {+ type = "Nullable(Int32)"+ }+ engine "kafka" {+ broker_list = "warpstream_logs"+ topic_list = "kafka_topic_list = 'clickhouse_logs'"+ group_name = "kafka_group_name = 'clickhouse-logs-avro-new'"+ format = "kafka_format = 'Avro'"+ num_consumers = 8+ skip_broken_messages = 100+ poll_timeout_ms = 3000+ poll_max_batch_size = 1000+ thread_per_consumer = true+ }+ }+table "query_log_archive" {column "hostname" {type = "LowCardinality(String)"@@ -578,6 +640,120 @@ database "posthog" {}}+ table "writable_logs34" {+ settings = {+ background_insert_batch = "1"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfDay(timestamp)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "body" {+ type = "String"+ }+ column "severity_text" {+ type = "LowCardinality(String)"+ }+ column "severity_number" {+ type = "Int32"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ materialized = "cityHash64(resource_attributes)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "event_name" {+ type = "String"+ }+ column "attributes_map_str" {+ type = "Map(LowCardinality(String), String)"+ }+ column "level" {+ type = "String"+ alias = "severity_text"+ }+ column "mat_body_ipv4_matches" {+ type = "Array(String)"+ alias = "extractAll(body, '(\\\\d\\\\.((25[0-5]|(2[0-4]|1(0, 1)[0-9])(0, 1)[0-9])\\\\.)(2, 2)([0-9]))')"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ column "attributes_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str))"+ }+ column "attributes_map_datetime" {+ type = "Map(LowCardinality(String), DateTime64(6))"+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ column "_bytes_uncompressed" {+ type = "UInt64"+ }+ column "_bytes_compressed" {+ type = "UInt64"+ }+ column "_record_count" {+ type = "UInt64"+ }+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "logs34"+ }+ }+materialized_view "events_json_table_mv" {to_table = "posthog.writable_events_json"query = <<SQL@@ -702,4 +878,108 @@ SQLtype = "Array(String)"}}++ materialized_view "kafka_logs34_avro_mv" {+ to_table = "posthog.writable_logs34"+ query = <<SQL+SELECT+ uuid,+ trace_id,+ span_id,+ trace_flags,+ timestamp,+ observed_timestamp,+ body,+ severity_text,+ severity_number,+ service_name,+ instrumentation_scope,+ event_name,+ mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ observed_timestamp+ + toIntervalDay(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))+ )+ ) AS original_expiry_timestamp,+ _partition,+ _topic,+ _offset,+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,+ toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,+ toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed+FROM posthog.kafka_logs_avro+SQL++ column "uuid" {+ type = "String"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "body" {+ type = "String"+ }+ column "severity_text" {+ type = "String"+ }+ column "severity_number" {+ type = "Int32"+ }+ column "service_name" {+ type = "String"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "event_name" {+ type = "String"+ }+ column "attributes_map_str" {+ type = "Map(String, String)"+ }+ column "resource_attributes" {+ type = "Map(String, String)"+ }+ column "team_id" {+ type = "Int32"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "_partition" {+ type = "UInt64"+ }+ column "_topic" {+ type = "LowCardinality(String)"+ }+ column "_offset" {+ type = "UInt64"+ }+ column "_record_count" {+ type = "Int64"+ }+ column "_bytes_uncompressed" {+ type = "Nullable(Int64)"+ }+ column "_bytes_compressed" {+ type = "Nullable(Int64)"+ }+ }}diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hclindex 97a1bb7ac42f..0ff746f89c84 100644--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl@@ -1,60 +1,4 @@database "posthog" {- table "kafka_logs_avro" {- column "uuid" {- type = "String"- }- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "body" {- type = "String"- }- column "severity_text" {- type = "String"- }- column "severity_number" {- type = "Int32"- }- column "service_name" {- type = "String"- }- column "resource_attributes" {- type = "Map(LowCardinality(String), String)"- }- column "instrumentation_scope" {- type = "String"- }- column "event_name" {- type = "String"- }- column "attributes" {- type = "Map(LowCardinality(String), String)"- }- engine "kafka" {- broker_list = "warpstream_logs"- topic_list = "kafka_topic_list = 'clickhouse_logs'"- group_name = "kafka_group_name = 'clickhouse-logs-avro-new'"- format = "kafka_format = 'Avro'"- num_consumers = 8- skip_broken_messages = 100- poll_timeout_ms = 3000- poll_max_batch_size = 1000- thread_per_consumer = true- }- }-table "kafka_trace_spans_avro" {column "uuid" {type = "String"@@ -2286,106 +2230,6 @@ SQL}}- materialized_view "kafka_logs34_avro_mv" {- to_table = "posthog.logs34"- query = <<SQL-SELECT- uuid,- trace_id,- span_id,- trace_flags,- timestamp,- observed_timestamp,- body,- severity_text,- severity_number,- service_name,- instrumentation_scope,- event_name,- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp- + toIntervalDay(- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))- ) AS original_expiry_timestamp,- _partition,- _topic,- _offset,- toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed-FROM posthog.kafka_logs_avro-SQL-- column "uuid" {- type = "String"- }- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "body" {- type = "String"- }- column "severity_text" {- type = "String"- }- column "severity_number" {- type = "Int32"- }- column "service_name" {- type = "String"- }- column "instrumentation_scope" {- type = "String"- }- column "event_name" {- type = "String"- }- column "attributes_map_str" {- type = "Map(String, String)"- }- column "resource_attributes" {- type = "Map(String, String)"- }- column "team_id" {- type = "Int32"- }- column "original_expiry_timestamp" {- type = "DateTime64(6)"- }- column "_partition" {- type = "UInt64"- }- column "_topic" {- type = "LowCardinality(String)"- }- column "_offset" {- type = "UInt64"- }- column "_record_count" {- type = "Int64"- }- column "_bytes_uncompressed" {- type = "Nullable(Int64)"- }- column "_bytes_compressed" {- type = "Nullable(Int64)"- }- }-materialized_view "kafka_logs_avro_billing_metrics_mv" {to_table = "posthog.logs_billing_metrics"query = <<SQLdiff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hclindex ac7df857d4b1..08364518a3d3 100644--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl@@ -3350,6 +3350,9 @@ database "posthog" {}table "kafka_logs_avro" {+ settings = {+ input_format_avro_allow_missing_fields = "1"+ }column "uuid" {type = "String"}@@ -3392,6 +3395,9 @@ database "posthog" {column "attributes" {type = "Map(LowCardinality(String), String)"}+ column "retention_days" {+ type = "Nullable(Int32)"+ }engine "kafka" {broker_list = "warpstream_logs"topic_list = "kafka_topic_list = 'clickhouse_logs'"@@ -15760,6 +15766,120 @@ SQL}}+ table "writable_logs34" {+ settings = {+ background_insert_batch = "1"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfDay(timestamp)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "body" {+ type = "String"+ }+ column "severity_text" {+ type = "LowCardinality(String)"+ }+ column "severity_number" {+ type = "Int32"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ materialized = "cityHash64(resource_attributes)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "event_name" {+ type = "String"+ }+ column "attributes_map_str" {+ type = "Map(LowCardinality(String), String)"+ }+ column "level" {+ type = "String"+ alias = "severity_text"+ }+ column "mat_body_ipv4_matches" {+ type = "Array(String)"+ alias = "extractAll(body, '(\\\\d\\\\.((25[0-5]|(2[0-4]|1(0, 1)[0-9])(0, 1)[0-9])\\\\.)(2, 2)([0-9]))')"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ column "attributes_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str))"+ }+ column "attributes_map_datetime" {+ type = "Map(LowCardinality(String), DateTime64(6))"+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ column "_bytes_uncompressed" {+ type = "UInt64"+ }+ column "_bytes_compressed" {+ type = "UInt64"+ }+ column "_record_count" {+ type = "UInt64"+ }+ engine "distributed" {+ cluster_name = "posthog_single_shard"+ remote_database = "posthog"+ remote_table = "logs34"+ }+ }+table "writable_person" {column "id" {type = "UUID"@@ -18917,7 +19037,7 @@ SQL}materialized_view "kafka_logs34_avro_mv" {- to_table = "posthog.logs34"+ to_table = "posthog.writable_logs34"query = <<SQLSELECTuuid,@@ -18937,7 +19057,11 @@ SELECTtoInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,observed_timestamp+ toIntervalDay(- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))+ )) AS original_expiry_timestamp,_partition,_topic,diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hclindex 2487225caa5d..bf6d12bfae6b 100644--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl@@ -1,63 +1,4 @@database "posthog" {- table "kafka_logs_avro" {- column "uuid" {- type = "String"- }- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "body" {- type = "String"- }- column "severity_text" {- type = "String"- }- column "severity_number" {- type = "Int32"- }- column "service_name" {- type = "String"- }- column "resource_attributes" {- type = "Map(LowCardinality(String), String)"- }- column "instrumentation_scope" {- type = "String"- }- column "event_name" {- type = "String"- }- column "attributes" {- type = "Map(LowCardinality(String), String)"- }- column "bytes_uncompressed" {- type = "Int64"- }- engine "kafka" {- broker_list = "warpstream_logs"- topic_list = "kafka_topic_list = 'clickhouse_logs'"- group_name = "kafka_group_name = 'clickhouse-logs-avro-new'"- format = "kafka_format = 'Avro'"- num_consumers = 16- skip_broken_messages = 100- poll_timeout_ms = 3000- poll_max_batch_size = 1000- thread_per_consumer = true- }- }-table "kafka_metrics_avro" {column "uuid" {type = "String"@@ -2409,106 +2350,6 @@ SQL}}- materialized_view "kafka_logs34_avro_mv" {- to_table = "posthog.logs34"- query = <<SQL-SELECT- uuid,- trace_id,- span_id,- trace_flags,- timestamp,- observed_timestamp,- body,- severity_text,- severity_number,- service_name,- instrumentation_scope,- event_name,- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp- + toIntervalDay(- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))- ) AS original_expiry_timestamp,- _partition,- _topic,- _offset,- toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed-FROM posthog.kafka_logs_avro-SQL-- column "uuid" {- type = "String"- }- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "body" {- type = "String"- }- column "severity_text" {- type = "String"- }- column "severity_number" {- type = "Int32"- }- column "service_name" {- type = "String"- }- column "instrumentation_scope" {- type = "String"- }- column "event_name" {- type = "String"- }- column "attributes_map_str" {- type = "Map(String, String)"- }- column "resource_attributes" {- type = "Map(String, String)"- }- column "team_id" {- type = "Int32"- }- column "original_expiry_timestamp" {- type = "DateTime64(6)"- }- column "_partition" {- type = "UInt64"- }- column "_topic" {- type = "LowCardinality(String)"- }- column "_offset" {- type = "UInt64"- }- column "_record_count" {- type = "Int64"- }- column "_bytes_uncompressed" {- type = "Nullable(Int64)"- }- column "_bytes_compressed" {- type = "Nullable(Int64)"- }- }-materialized_view "kafka_logs_avro_billing_metrics_mv" {to_table = "posthog.logs_billing_metrics"query = <<SQLdiff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hclindex b4cdfed2b20f..0ec50c0921a4 100644--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl@@ -1,60 +1,4 @@database "posthog" {- table "kafka_logs_avro" {- column "uuid" {- type = "String"- }- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "body" {- type = "String"- }- column "severity_text" {- type = "String"- }- column "severity_number" {- type = "Int32"- }- column "service_name" {- type = "String"- }- column "resource_attributes" {- type = "Map(LowCardinality(String), String)"- }- column "instrumentation_scope" {- type = "String"- }- column "event_name" {- type = "String"- }- column "attributes" {- type = "Map(LowCardinality(String), String)"- }- engine "kafka" {- broker_list = "warpstream_logs"- topic_list = "kafka_topic_list = 'clickhouse_logs'"- group_name = "kafka_group_name = 'clickhouse-logs-avro-new'"- format = "kafka_format = 'Avro'"- num_consumers = 32- skip_broken_messages = 100- poll_timeout_ms = 3000- poll_max_batch_size = 1000- thread_per_consumer = true- }- }-table "kafka_metrics_avro" {column "uuid" {type = "String"@@ -2388,106 +2332,6 @@ SQL}}- materialized_view "kafka_logs34_avro_mv" {- to_table = "posthog.logs34"- query = <<SQL-SELECT- uuid,- trace_id,- span_id,- trace_flags,- timestamp,- observed_timestamp,- body,- severity_text,- severity_number,- service_name,- instrumentation_scope,- event_name,- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp- + toIntervalDay(- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))- ) AS original_expiry_timestamp,- _partition,- _topic,- _offset,- toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed-FROM posthog.kafka_logs_avro-SQL-- column "uuid" {- type = "String"- }- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "body" {- type = "String"- }- column "severity_text" {- type = "String"- }- column "severity_number" {- type = "Int32"- }- column "service_name" {- type = "String"- }- column "instrumentation_scope" {- type = "String"- }- column "event_name" {- type = "String"- }- column "attributes_map_str" {- type = "Map(String, String)"- }- column "resource_attributes" {- type = "Map(String, String)"- }- column "team_id" {- type = "Int32"- }- column "original_expiry_timestamp" {- type = "DateTime64(6)"- }- column "_partition" {- type = "UInt64"- }- column "_topic" {- type = "LowCardinality(String)"- }- column "_offset" {- type = "UInt64"- }- column "_record_count" {- type = "Int64"- }- column "_bytes_uncompressed" {- type = "Nullable(Int64)"- }- column "_bytes_compressed" {- type = "Nullable(Int64)"- }- }-materialized_view "kafka_logs_avro_billing_metrics_mv" {to_table = "posthog.logs_billing_metrics"query = <<SQLdiff --git a/posthog/clickhouse/hcl/manifest.hcl b/posthog/clickhouse/hcl/manifest.hclindex 778805287bbb..5e29a770eaa6 100644--- a/posthog/clickhouse/hcl/manifest.hcl+++ b/posthog/clickhouse/hcl/manifest.hcl@@ -126,7 +126,7 @@ role "medium" {# and not MULTINODE_CLICKHOUSE. Composed as the deduped union of the local-multi stacks it# hosts, so any name two of those roles declare fails this load instead of drifting.role "all" {- env "local-single" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/ops/shared", "roles/ops/local", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/local", "roles/coshared/ai_events_data", "roles/ai_events/shared", "roles/ai_events/local", "roles/coshared/aux_data", "roles/auxiliary/shared", "roles/auxiliary/local", "roles/coshared/sessions_data", "roles/coshared/tophog", "roles/coshared/events_recent", "roles/coshared/events_recent_write", "roles/coshared/batch_exports_data", "roles/coshared/ingestion_warnings_store", "roles/coshared/events_json_write", "roles/coshared/log_entries_write", "roles/coshared/session_replay_write", "roles/data/shared", "roles/data/local", "roles/ingestion_events/local", "roles/ingestion_small/local", "roles/ingestion_medium/local"] }+ env "local-single" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/ops/shared", "roles/ops/local", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/local", "roles/coshared/ai_events_data", "roles/ai_events/shared", "roles/ai_events/local", "roles/coshared/aux_data", "roles/auxiliary/shared", "roles/auxiliary/local", "roles/coshared/sessions_data", "roles/coshared/tophog", "roles/coshared/events_recent", "roles/coshared/events_recent_write", "roles/coshared/batch_exports_data", "roles/coshared/ingestion_warnings_store", "roles/coshared/events_json_write", "roles/coshared/log_entries_write", "roles/coshared/session_replay_write", "roles/data/shared", "roles/data/local", "roles/ingestion_events/local", "roles/ingestion_events/local-single", "roles/ingestion_small/local", "roles/ingestion_medium/local"] }}# role "endpoints" {diff --git a/posthog/clickhouse/hcl/roles/ingestion_events/local-single/patches.hcl b/posthog/clickhouse/hcl/roles/ingestion_events/local-single/patches.hclnew file mode 100644index 000000000000..723f272b599d--- /dev/null+++ b/posthog/clickhouse/hcl/roles/ingestion_events/local-single/patches.hcl@@ -0,0 +1,13 @@+# local-single deltas to the ingestion-events objects.+database "posthog" {+ # The single-node stack has no `logs` remote_servers entry — every role lives on this+ # one server, so the Distributed front reaches logs34 through the self-pointing+ # posthog_single_shard cluster. CLICKHOUSE_LOGS_WRITE_CLUSTER falls back to that there.+ patch_table "writable_logs34" {+ engine "distributed" {+ cluster_name = "posthog_single_shard"+ remote_database = "posthog"+ remote_table = "logs34"+ }+ }+}diff --git a/posthog/clickhouse/hcl/roles/logs/base/sql/kafka_logs34_avro_mv.sql b/posthog/clickhouse/hcl/roles/ingestion_events/local/sql/kafka_logs34_avro_mv.sqlsimilarity index 83%rename from posthog/clickhouse/hcl/roles/logs/base/sql/kafka_logs34_avro_mv.sqlrename to posthog/clickhouse/hcl/roles/ingestion_events/local/sql/kafka_logs34_avro_mv.sqlindex e391e9964205..e8de26217006 100644--- a/posthog/clickhouse/hcl/roles/logs/base/sql/kafka_logs34_avro_mv.sql+++ b/posthog/clickhouse/hcl/roles/ingestion_events/local/sql/kafka_logs34_avro_mv.sql@@ -16,7 +16,11 @@ SELECTtoInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,observed_timestamp+ toIntervalDay(- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))+ )) AS original_expiry_timestamp,_partition,_topic,diff --git a/posthog/clickhouse/hcl/roles/ingestion_events/local/tables.hcl b/posthog/clickhouse/hcl/roles/ingestion_events/local/tables.hclindex 1b8411b3f58d..8badc1b985f0 100644--- a/posthog/clickhouse/hcl/roles/ingestion_events/local/tables.hcl+++ b/posthog/clickhouse/hcl/roles/ingestion_events/local/tables.hcl@@ -249,4 +249,251 @@ SQLtype = "Array(String)"}}++ table "writable_logs34" {+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfDay(timestamp)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "body" {+ type = "String"+ }+ column "severity_text" {+ type = "LowCardinality(String)"+ }+ column "severity_number" {+ type = "Int32"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ materialized = "cityHash64(resource_attributes)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "event_name" {+ type = "String"+ }+ column "attributes_map_str" {+ type = "Map(LowCardinality(String), String)"+ }+ column "level" {+ type = "String"+ alias = "severity_text"+ }+ column "mat_body_ipv4_matches" {+ type = "Array(String)"+ alias = "extractAll(body, '(\\\\d\\\\.((25[0-5]|(2[0-4]|1(0, 1)[0-9])(0, 1)[0-9])\\\\.)(2, 2)([0-9]))')"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ column "attributes_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str))"+ }+ column "attributes_map_datetime" {+ type = "Map(LowCardinality(String), DateTime64(6))"+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ column "_bytes_uncompressed" {+ type = "UInt64"+ }+ column "_bytes_compressed" {+ type = "UInt64"+ }+ column "_record_count" {+ type = "UInt64"+ }+ settings = {+ background_insert_batch = "1"+ }+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "logs34"+ }+ }++ table "kafka_logs_avro" {+ column "uuid" {+ type = "String"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "body" {+ type = "String"+ }+ column "severity_text" {+ type = "String"+ }+ column "severity_number" {+ type = "Int32"+ }+ column "service_name" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "event_name" {+ type = "String"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "retention_days" {+ type = "Nullable(Int32)"+ }+ engine "kafka" {+ broker_list = "warpstream_logs"+ topic_list = "kafka_topic_list = 'clickhouse_logs'"+ group_name = "kafka_group_name = 'clickhouse-logs-avro-new'"+ format = "kafka_format = 'Avro'"+ num_consumers = 8+ skip_broken_messages = 100+ poll_timeout_ms = 3000+ poll_max_batch_size = 1000+ thread_per_consumer = true+ }+ settings = {+ input_format_avro_allow_missing_fields = "1"+ }+ }++ materialized_view "kafka_logs34_avro_mv" {+ to_table = "posthog.writable_logs34"+ query = file("sql/kafka_logs34_avro_mv.sql")+ column "uuid" {+ type = "String"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "body" {+ type = "String"+ }+ column "severity_text" {+ type = "String"+ }+ column "severity_number" {+ type = "Int32"+ }+ column "service_name" {+ type = "String"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "event_name" {+ type = "String"+ }+ column "attributes_map_str" {+ type = "Map(String, String)"+ }+ column "resource_attributes" {+ type = "Map(String, String)"+ }+ column "team_id" {+ type = "Int32"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "_partition" {+ type = "UInt64"+ }+ column "_topic" {+ type = "LowCardinality(String)"+ }+ column "_offset" {+ type = "UInt64"+ }+ column "_record_count" {+ type = "Int64"+ }+ column "_bytes_uncompressed" {+ type = "Nullable(Int64)"+ }+ column "_bytes_compressed" {+ type = "Nullable(Int64)"+ }+ }}diff --git a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl b/posthog/clickhouse/hcl/roles/logs/base/tables.hclindex a575f6841e1c..07d65d1a0b87 100644--- a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl+++ b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl@@ -879,62 +879,6 @@ database "posthog" {}}- table "kafka_logs_avro" {- column "uuid" {- type = "String"- }- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "body" {- type = "String"- }- column "severity_text" {- type = "String"- }- column "severity_number" {- type = "Int32"- }- column "service_name" {- type = "String"- }- column "resource_attributes" {- type = "Map(LowCardinality(String), String)"- }- column "instrumentation_scope" {- type = "String"- }- column "event_name" {- type = "String"- }- column "attributes" {- type = "Map(LowCardinality(String), String)"- }- engine "kafka" {- broker_list = "warpstream_logs"- topic_list = "kafka_topic_list = 'clickhouse_logs'"- group_name = "kafka_group_name = 'clickhouse-logs-avro-new'"- format = "kafka_format = 'Avro'"- num_consumers = 8- skip_broken_messages = 100- poll_timeout_ms = 3000- poll_max_batch_size = 1000- thread_per_consumer = true- }- }-table "logs34" {order_by = ["team_id", "time_bucket", "service_name", "resource_fingerprint", "severity_text", "timestamp"]partition_by = "toDate(original_expiry_timestamp)"@@ -1117,77 +1061,6 @@ SQL}}- materialized_view "kafka_logs34_avro_mv" {- to_table = "posthog.logs34"- query = file("sql/kafka_logs34_avro_mv.sql")- column "uuid" {- type = "String"- }- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "body" {- type = "String"- }- column "severity_text" {- type = "String"- }- column "severity_number" {- type = "Int32"- }- column "service_name" {- type = "String"- }- column "instrumentation_scope" {- type = "String"- }- column "event_name" {- type = "String"- }- column "attributes_map_str" {- type = "Map(String, String)"- }- column "resource_attributes" {- type = "Map(String, String)"- }- column "team_id" {- type = "Int32"- }- column "original_expiry_timestamp" {- type = "DateTime64(6)"- }- column "_partition" {- type = "UInt64"- }- column "_topic" {- type = "LowCardinality(String)"- }- column "_offset" {- type = "UInt64"- }- column "_record_count" {- type = "Int64"- }- column "_bytes_uncompressed" {- type = "Nullable(Int64)"- }- column "_bytes_compressed" {- type = "Nullable(Int64)"- }- }-materialized_view "kafka_logs_avro_billing_metrics_mv" {to_table = "posthog.logs_billing_metrics"query = file("sql/kafka_logs_avro_billing_metrics_mv.sql")diff --git a/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl b/posthog/clickhouse/hcl/roles/logs/dev/patches.hclindex b106d1467036..6a4620fe433d 100644--- a/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl+++ b/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl@@ -68,12 +68,4 @@ SQL}}- patch_materialized_view "kafka_logs34_avro_mv" {- modify_column "_bytes_uncompressed" {- type = "Nullable(Float64)"- }- modify_column "_bytes_compressed" {- type = "Nullable(Float64)"- }- }}diff --git a/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hcl b/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hclindex b8e22dc6f454..8b2a24ca7101 100644--- a/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hcl+++ b/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hcl@@ -12,23 +12,6 @@ database "posthog" {ttl_only_drop_parts = "1"}}- patch_table "kafka_logs_avro" {- column "bytes_uncompressed" {- type = "Int64"- after = "attributes"- }- engine "kafka" {- broker_list = "warpstream_logs"- topic_list = "kafka_topic_list = 'clickhouse_logs'"- group_name = "kafka_group_name = 'clickhouse-logs-avro-new'"- format = "kafka_format = 'Avro'"- num_consumers = 16- skip_broken_messages = 100- poll_timeout_ms = 3000- poll_max_batch_size = 1000- thread_per_consumer = true- }- }patch_table "kafka_trace_spans_avro" {engine "kafka" {broker_list = "warpstream_traces"diff --git a/posthog/clickhouse/hcl/roles/logs/prod-us/patches.hcl b/posthog/clickhouse/hcl/roles/logs/prod-us/patches.hclindex 0994bc767a7b..96153c42b0fd 100644--- a/posthog/clickhouse/hcl/roles/logs/prod-us/patches.hcl+++ b/posthog/clickhouse/hcl/roles/logs/prod-us/patches.hcl@@ -1,18 +1,5 @@# prod-us deltas to base logs objects.database "posthog" {- patch_table "kafka_logs_avro" {- engine "kafka" {- broker_list = "warpstream_logs"- topic_list = "kafka_topic_list = 'clickhouse_logs'"- group_name = "kafka_group_name = 'clickhouse-logs-avro-new'"- format = "kafka_format = 'Avro'"- num_consumers = 32- skip_broken_messages = 100- poll_timeout_ms = 3000- poll_max_batch_size = 1000- thread_per_consumer = true- }- }patch_table "logs34" {settings = {map_buckets_strategy = "constant"diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sqlindex ba263c0f26ae..a70755423fa5 100644--- a/posthog/clickhouse/hcl/sql/dev/logs.sql+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql@@ -1,22 +1,6 @@-- AUTO-GENERATED from the declarative HCL by ops/gen-sql.sh — do not edit.-- Full CREATE schema for the dev/logs node. Apply to a fresh ClickHouse to build it.-CREATE TABLE posthog.kafka_logs_avro (- uuid String,- trace_id String,- span_id String,- trace_flags Int32,- timestamp DateTime64(6),- observed_timestamp DateTime64(6),- body String,- severity_text String,- severity_number Int32,- service_name String,- resource_attributes Map(LowCardinality(String), String),- instrumentation_scope String,- event_name String,- attributes Map(LowCardinality(String), String)-) ENGINE = Kafka() SETTINGS kafka_broker_list = 'warpstream_logs', kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = 'kafka_group_name = \'clickhouse-logs-avro-new\'', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'kafka_topic_list = \'clickhouse_logs\'';CREATE TABLE posthog.kafka_metrics_avro (uuid String,trace_id String,@@ -619,33 +603,6 @@ CREATE TABLE posthog.writable_query_log_archive (log_comment JSON(max_dynamic_paths=256, access_method LowCardinality(String), alert_config_id String, api_key_label String, api_key_mask String, batch_export_id String, chargeable Bool, client_query_id String, cohort_id Int64, `dagster.job_name` String, `dagster.run_id` String, `dagster.tags.owner` String, dashboard_id Int64, experiment_feature_flag_key String, experiment_id Int64, feature LowCardinality(String), id String, insight_id Int64, is_impersonated Bool, kind LowCardinality(String), name String, org_id String, person_on_events_mode LowCardinality(String), product LowCardinality(String), query_type LowCardinality(String), request_name String, route_id String, service_name String, session_id String, table_id String, team_id Int64, `temporal.activity_id` String, `temporal.activity_type` String, `temporal.attempt` Int64, `temporal.workflow_id` String, `temporal.workflow_namespace` String, `temporal.workflow_run_id` String, `temporal.workflow_type` String, user_id Int64, warehouse_query Bool, workflow LowCardinality(String), workload LowCardinality(String), SKIP cache_key, SKIP filter, SKIP hogql_features, SKIP http_referer, SKIP http_request_id, SKIP http_user_agent, SKIP query_settings, SKIP timings, SKIP user_email),ProfileEvents Map(String, UInt64)) ENGINE = Distributed('ops', 'posthog', 'query_log_archive_buffer');-CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.logs34 (uuid String, trace_id String, span_id String, trace_flags Int32, timestamp DateTime64(6), observed_timestamp DateTime64(6), body String, severity_text String, severity_number Int32, service_name String, instrumentation_scope String, event_name String, attributes_map_str Map(String, String), resource_attributes Map(String, String), team_id Int32, original_expiry_timestamp DateTime64(6), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Float64), _bytes_compressed Nullable(Float64)) AS SELECT- uuid,- trace_id,- span_id,- trace_flags,- timestamp,- observed_timestamp,- body,- severity_text,- severity_number,- service_name,- instrumentation_scope,- event_name,- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp- + toIntervalDay(- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))- ) AS original_expiry_timestamp,- _partition,- _topic,- _offset,- toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed-FROM posthog.kafka_logs_avro;CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_billing_metrics_mv TO posthog.logs_billing_metrics (team_id Int32, time_bucket DateTime, service_name LowCardinality(String), bytes_uncompressed SimpleAggregateFunction(sum, Float64), bytes_compressed SimpleAggregateFunction(sum, Float64), record_count SimpleAggregateFunction(sum, UInt64)) AS SELECTteam_id,time_bucket,diff --git a/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql b/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sqlindex c67fca683e3b..1b51587ba8da 100644--- a/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql@@ -36,6 +36,23 @@ CREATE TABLE posthog.kafka_events_json_native_json (dmat_string_8 Nullable(String),dmat_string_9 Nullable(String)) ENGINE = Kafka() SETTINGS kafka_broker_list = 'msk_cluster', kafka_format = 'kafka_format = \'JSONEachRow\'', kafka_group_name = 'kafka_group_name = \'clickhouse_events_json_native_json\'', kafka_skip_broken_messages = 100, kafka_topic_list = 'kafka_topic_list = \'clickhouse_events_json\'';+CREATE TABLE posthog.kafka_logs_avro (+ uuid String,+ trace_id String,+ span_id String,+ trace_flags Int32,+ timestamp DateTime64(6),+ observed_timestamp DateTime64(6),+ body String,+ severity_text String,+ severity_number Int32,+ service_name String,+ resource_attributes Map(LowCardinality(String), String),+ instrumentation_scope String,+ event_name String,+ attributes Map(LowCardinality(String), String),+ retention_days Nullable(Int32)+) ENGINE = Kafka() SETTINGS input_format_avro_allow_missing_fields = 1, kafka_broker_list = 'warpstream_logs', kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = 'kafka_group_name = \'clickhouse-logs-avro-new\'', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'kafka_topic_list = \'clickhouse_logs\'';CREATE TABLE posthog.query_log_archive (hostname LowCardinality(String),user LowCardinality(String),@@ -162,6 +179,39 @@ CREATE TABLE posthog.writable_events_json (consumer_breadcrumbs Array(String),historical_migration Bool DEFAULT false) ENGINE = Distributed('posthog', 'posthog', 'sharded_events_json', sipHash64(distinct_id));+CREATE TABLE posthog.writable_logs34 (+ time_bucket DateTime MATERIALIZED toStartOfDay(timestamp),+ original_expiry_timestamp DateTime64(6),+ uuid String,+ team_id Int32,+ trace_id String,+ span_id String,+ trace_flags Int32,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ body String,+ severity_text LowCardinality(String),+ severity_number Int32,+ service_name LowCardinality(String),+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ instrumentation_scope String,+ event_name String,+ attributes_map_str Map(LowCardinality(String), String),+ level String ALIAS severity_text,+ mat_body_ipv4_matches Array(String) ALIAS extractAll(body, '(\\d\\.((25[0-5]|(2[0-4]|1(0, 1)[0-9])(0, 1)[0-9])\\.)(2, 2)([0-9]))'),+ time_minute DateTime ALIAS toStartOfMinute(timestamp),+ attributes Map(LowCardinality(String), String) ALIAS mapApply((k, v) -> (left(k, -5), v), attributes_map_str),+ attributes_map_float Map(LowCardinality(String), Float64) MATERIALIZED mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str)),+ attributes_map_datetime Map(LowCardinality(String), DateTime64(6)) MATERIALIZED mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str)),+ _partition UInt32,+ _topic String,+ _offset UInt64,+ _bytes_uncompressed UInt64,+ _bytes_compressed UInt64,+ _record_count UInt64+) ENGINE = Distributed('logs', 'posthog', 'logs34') SETTINGS background_insert_batch = 1;CREATE MATERIALIZED VIEW posthog.events_json_table_mv TO posthog.writable_events_json (uuid UUID, event String, properties JSON, timestamp DateTime64(6, 'UTC'), team_id Int64, distinct_id String, elements_chain String, created_at DateTime64(6, 'UTC'), person_id UUID, person_created_at DateTime64(3), person_properties JSON, group0_properties String, group1_properties String, group2_properties String, group3_properties String, group4_properties String, group0_created_at DateTime64(3), group1_created_at DateTime64(3), group2_created_at DateTime64(3), group3_created_at DateTime64(3), group4_created_at DateTime64(3), person_mode Enum8('full'=0, 'propertyless'=1, 'force_upgrade'=2), historical_migration Bool, _timestamp Nullable(DateTime), _offset UInt64, consumer_breadcrumbs Array(String)) AS SELECTuuid,event,@@ -202,3 +252,34 @@ CREATE MATERIALIZED VIEW posthog.events_json_table_mv TO posthog.writable_events)) AS consumer_breadcrumbsFROM posthog.kafka_events_json_native_json;+CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.writable_logs34 (uuid String, trace_id String, span_id String, trace_flags Int32, timestamp DateTime64(6), observed_timestamp DateTime64(6), body String, severity_text String, severity_number Int32, service_name String, instrumentation_scope String, event_name String, attributes_map_str Map(String, String), resource_attributes Map(String, String), team_id Int32, original_expiry_timestamp DateTime64(6), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Int64), _bytes_compressed Nullable(Int64)) AS SELECT+ uuid,+ trace_id,+ span_id,+ trace_flags,+ timestamp,+ observed_timestamp,+ body,+ severity_text,+ severity_number,+ service_name,+ instrumentation_scope,+ event_name,+ mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ observed_timestamp+ + toIntervalDay(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))+ )+ ) AS original_expiry_timestamp,+ _partition,+ _topic,+ _offset,+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,+ toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,+ toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed+FROM posthog.kafka_logs_avro;diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sqlindex a17edd5ef04b..094e7ba0211d 100644--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql@@ -1,22 +1,6 @@-- AUTO-GENERATED from the declarative HCL by ops/gen-sql.sh — do not edit.-- Full CREATE schema for the local-multi/logs node. Apply to a fresh ClickHouse to build it.-CREATE TABLE posthog.kafka_logs_avro (- uuid String,- trace_id String,- span_id String,- trace_flags Int32,- timestamp DateTime64(6),- observed_timestamp DateTime64(6),- body String,- severity_text String,- severity_number Int32,- service_name String,- resource_attributes Map(LowCardinality(String), String),- instrumentation_scope String,- event_name String,- attributes Map(LowCardinality(String), String)-) ENGINE = Kafka() SETTINGS kafka_broker_list = 'warpstream_logs', kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = 'kafka_group_name = \'clickhouse-logs-avro-new\'', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'kafka_topic_list = \'clickhouse_logs\'';CREATE TABLE posthog.kafka_trace_spans_avro (uuid String,trace_id String,@@ -566,33 +550,6 @@ CREATE TABLE posthog.trace_spans_kafka_metrics (max_created_at SimpleAggregateFunction(max, DateTime64(9)),max_lag SimpleAggregateFunction(max, UInt64)) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_spans_kafka_metrics', '{replica}-{shard}') ORDER BY (_topic, _partition) SETTINGS index_granularity = 8192;-CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.logs34 (uuid String, trace_id String, span_id String, trace_flags Int32, timestamp DateTime64(6), observed_timestamp DateTime64(6), body String, severity_text String, severity_number Int32, service_name String, instrumentation_scope String, event_name String, attributes_map_str Map(String, String), resource_attributes Map(String, String), team_id Int32, original_expiry_timestamp DateTime64(6), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Int64), _bytes_compressed Nullable(Int64)) AS SELECT- uuid,- trace_id,- span_id,- trace_flags,- timestamp,- observed_timestamp,- body,- severity_text,- severity_number,- service_name,- instrumentation_scope,- event_name,- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp- + toIntervalDay(- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))- ) AS original_expiry_timestamp,- _partition,- _topic,- _offset,- toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed-FROM posthog.kafka_logs_avro;CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_billing_metrics_mv TO posthog.logs_billing_metrics (team_id Int32, time_bucket DateTime, service_name LowCardinality(String), bytes_uncompressed SimpleAggregateFunction(sum, Float64), bytes_compressed SimpleAggregateFunction(sum, Float64), record_count SimpleAggregateFunction(sum, UInt64)) AS SELECTteam_id,time_bucket,diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sqlindex a0af2519c857..73d03e3c76c1 100644--- a/posthog/clickhouse/hcl/sql/local-single/all.sql+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql@@ -543,8 +543,9 @@ CREATE TABLE posthog.kafka_logs_avro (resource_attributes Map(LowCardinality(String), String),instrumentation_scope String,event_name String,- attributes Map(LowCardinality(String), String)-) ENGINE = Kafka() SETTINGS kafka_broker_list = 'warpstream_logs', kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = 'kafka_group_name = \'clickhouse-logs-avro-new\'', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'kafka_topic_list = \'clickhouse_logs\'';+ attributes Map(LowCardinality(String), String),+ retention_days Nullable(Int32)+) ENGINE = Kafka() SETTINGS input_format_avro_allow_missing_fields = 1, kafka_broker_list = 'warpstream_logs', kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = 'kafka_group_name = \'clickhouse-logs-avro-new\'', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'kafka_topic_list = \'clickhouse_logs\'';CREATE TABLE posthog.kafka_message_assets (team_id Int64,function_kind LowCardinality(String),@@ -3479,6 +3480,39 @@ CREATE TABLE posthog.writable_log_entries (_timestamp DateTime,_offset UInt64) ENGINE = Distributed('posthog', 'posthog', 'sharded_log_entries', rand());+CREATE TABLE posthog.writable_logs34 (+ time_bucket DateTime MATERIALIZED toStartOfDay(timestamp),+ original_expiry_timestamp DateTime64(6),+ uuid String,+ team_id Int32,+ trace_id String,+ span_id String,+ trace_flags Int32,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ body String,+ severity_text LowCardinality(String),+ severity_number Int32,+ service_name LowCardinality(String),+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ instrumentation_scope String,+ event_name String,+ attributes_map_str Map(LowCardinality(String), String),+ level String ALIAS severity_text,+ mat_body_ipv4_matches Array(String) ALIAS extractAll(body, '(\\d\\.((25[0-5]|(2[0-4]|1(0, 1)[0-9])(0, 1)[0-9])\\.)(2, 2)([0-9]))'),+ time_minute DateTime ALIAS toStartOfMinute(timestamp),+ attributes Map(LowCardinality(String), String) ALIAS mapApply((k, v) -> (left(k, -5), v), attributes_map_str),+ attributes_map_float Map(LowCardinality(String), Float64) MATERIALIZED mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str)),+ attributes_map_datetime Map(LowCardinality(String), DateTime64(6)) MATERIALIZED mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str)),+ _partition UInt32,+ _topic String,+ _offset UInt64,+ _bytes_uncompressed UInt64,+ _bytes_compressed UInt64,+ _record_count UInt64+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs34') SETTINGS background_insert_batch = 1;CREATE TABLE posthog.writable_person (id UUID,created_at DateTime64(3),@@ -4287,7 +4321,7 @@ CREATE MATERIALIZED VIEW posthog.ingestion_warnings_v2_mv TO posthog.ingestion_w_offset,_partitionFROM posthog.kafka_ingestion_warnings_v2;-CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.logs34 (uuid String, trace_id String, span_id String, trace_flags Int32, timestamp DateTime64(6), observed_timestamp DateTime64(6), body String, severity_text String, severity_number Int32, service_name String, instrumentation_scope String, event_name String, attributes_map_str Map(String, String), resource_attributes Map(String, String), team_id Int32, original_expiry_timestamp DateTime64(6), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Int64), _bytes_compressed Nullable(Int64)) AS SELECT+CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.writable_logs34 (uuid String, trace_id String, span_id String, trace_flags Int32, timestamp DateTime64(6), observed_timestamp DateTime64(6), body String, severity_text String, severity_number Int32, service_name String, instrumentation_scope String, event_name String, attributes_map_str Map(String, String), resource_attributes Map(String, String), team_id Int32, original_expiry_timestamp DateTime64(6), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Int64), _bytes_compressed Nullable(Int64)) AS SELECTuuid,trace_id,span_id,@@ -4305,7 +4339,11 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.logs34 (uuid SttoInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,observed_timestamp+ toIntervalDay(- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))+ )) AS original_expiry_timestamp,_partition,_topic,diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sqlindex 175c464fb4d6..8cb706d18955 100644--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql@@ -1,23 +1,6 @@-- AUTO-GENERATED from the declarative HCL by ops/gen-sql.sh — do not edit.-- Full CREATE schema for the prod-eu/logs node. Apply to a fresh ClickHouse to build it.-CREATE TABLE posthog.kafka_logs_avro (- uuid String,- trace_id String,- span_id String,- trace_flags Int32,- timestamp DateTime64(6),- observed_timestamp DateTime64(6),- body String,- severity_text String,- severity_number Int32,- service_name String,- resource_attributes Map(LowCardinality(String), String),- instrumentation_scope String,- event_name String,- attributes Map(LowCardinality(String), String),- bytes_uncompressed Int64-) ENGINE = Kafka() SETTINGS kafka_broker_list = 'warpstream_logs', kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = 'kafka_group_name = \'clickhouse-logs-avro-new\'', kafka_num_consumers = 16, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'kafka_topic_list = \'clickhouse_logs\'';CREATE TABLE posthog.kafka_metrics_avro (uuid String,trace_id String,@@ -635,33 +618,6 @@ CREATE TABLE posthog.writable_query_log_archive (log_comment JSON(max_dynamic_paths=256, access_method LowCardinality(String), alert_config_id String, api_key_label String, api_key_mask String, batch_export_id String, chargeable Bool, client_query_id String, cohort_id Int64, `dagster.job_name` String, `dagster.run_id` String, `dagster.tags.owner` String, dashboard_id Int64, experiment_feature_flag_key String, experiment_id Int64, feature LowCardinality(String), id String, insight_id Int64, is_impersonated Bool, kind LowCardinality(String), name String, org_id String, person_on_events_mode LowCardinality(String), product LowCardinality(String), query_type LowCardinality(String), request_name String, route_id String, service_name String, session_id String, table_id String, team_id Int64, `temporal.activity_id` String, `temporal.activity_type` String, `temporal.attempt` Int64, `temporal.workflow_id` String, `temporal.workflow_namespace` String, `temporal.workflow_run_id` String, `temporal.workflow_type` String, user_id Int64, warehouse_query Bool, workflow LowCardinality(String), workload LowCardinality(String), SKIP cache_key, SKIP filter, SKIP hogql_features, SKIP http_referer, SKIP http_request_id, SKIP http_user_agent, SKIP query_settings, SKIP timings, SKIP user_email),ProfileEvents Map(String, UInt64)) ENGINE = Distributed('ops', 'posthog', 'query_log_archive_buffer');-CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.logs34 (uuid String, trace_id String, span_id String, trace_flags Int32, timestamp DateTime64(6), observed_timestamp DateTime64(6), body String, severity_text String, severity_number Int32, service_name String, instrumentation_scope String, event_name String, attributes_map_str Map(String, String), resource_attributes Map(String, String), team_id Int32, original_expiry_timestamp DateTime64(6), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Int64), _bytes_compressed Nullable(Int64)) AS SELECT- uuid,- trace_id,- span_id,- trace_flags,- timestamp,- observed_timestamp,- body,- severity_text,- severity_number,- service_name,- instrumentation_scope,- event_name,- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp- + toIntervalDay(- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))- ) AS original_expiry_timestamp,- _partition,- _topic,- _offset,- toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed-FROM posthog.kafka_logs_avro;CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_billing_metrics_mv TO posthog.logs_billing_metrics (team_id Int32, time_bucket DateTime, service_name LowCardinality(String), bytes_uncompressed SimpleAggregateFunction(sum, Float64), bytes_compressed SimpleAggregateFunction(sum, Float64), record_count SimpleAggregateFunction(sum, UInt64)) AS SELECTteam_id,time_bucket,diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sqlindex 49376c71aa1e..cb079f884f58 100644--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql@@ -1,22 +1,6 @@-- AUTO-GENERATED from the declarative HCL by ops/gen-sql.sh — do not edit.-- Full CREATE schema for the prod-us/logs node. Apply to a fresh ClickHouse to build it.-CREATE TABLE posthog.kafka_logs_avro (- uuid String,- trace_id String,- span_id String,- trace_flags Int32,- timestamp DateTime64(6),- observed_timestamp DateTime64(6),- body String,- severity_text String,- severity_number Int32,- service_name String,- resource_attributes Map(LowCardinality(String), String),- instrumentation_scope String,- event_name String,- attributes Map(LowCardinality(String), String)-) ENGINE = Kafka() SETTINGS kafka_broker_list = 'warpstream_logs', kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = 'kafka_group_name = \'clickhouse-logs-avro-new\'', kafka_num_consumers = 32, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'kafka_topic_list = \'clickhouse_logs\'';CREATE TABLE posthog.kafka_metrics_avro (uuid String,trace_id String,@@ -628,33 +612,6 @@ CREATE TABLE posthog.writable_query_log_archive (log_comment JSON(max_dynamic_paths=256, access_method LowCardinality(String), alert_config_id String, api_key_label String, api_key_mask String, batch_export_id String, chargeable Bool, client_query_id String, cohort_id Int64, `dagster.job_name` String, `dagster.run_id` String, `dagster.tags.owner` String, dashboard_id Int64, experiment_feature_flag_key String, experiment_id Int64, feature LowCardinality(String), id String, insight_id Int64, is_impersonated Bool, kind LowCardinality(String), name String, org_id String, person_on_events_mode LowCardinality(String), product LowCardinality(String), query_type LowCardinality(String), request_name String, route_id String, service_name String, session_id String, table_id String, team_id Int64, `temporal.activity_id` String, `temporal.activity_type` String, `temporal.attempt` Int64, `temporal.workflow_id` String, `temporal.workflow_namespace` String, `temporal.workflow_run_id` String, `temporal.workflow_type` String, user_id Int64, warehouse_query Bool, workflow LowCardinality(String), workload LowCardinality(String), SKIP cache_key, SKIP filter, SKIP hogql_features, SKIP http_referer, SKIP http_request_id, SKIP http_user_agent, SKIP query_settings, SKIP timings, SKIP user_email),ProfileEvents Map(String, UInt64)) ENGINE = Distributed('ops', 'posthog', 'query_log_archive_buffer');-CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.logs34 (uuid String, trace_id String, span_id String, trace_flags Int32, timestamp DateTime64(6), observed_timestamp DateTime64(6), body String, severity_text String, severity_number Int32, service_name String, instrumentation_scope String, event_name String, attributes_map_str Map(String, String), resource_attributes Map(String, String), team_id Int32, original_expiry_timestamp DateTime64(6), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Int64), _bytes_compressed Nullable(Int64)) AS SELECT- uuid,- trace_id,- span_id,- trace_flags,- timestamp,- observed_timestamp,- body,- severity_text,- severity_number,- service_name,- instrumentation_scope,- event_name,- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp- + toIntervalDay(- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))- ) AS original_expiry_timestamp,- _partition,- _topic,- _offset,- toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed-FROM posthog.kafka_logs_avro;CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_billing_metrics_mv TO posthog.logs_billing_metrics (team_id Int32, time_bucket DateTime, service_name LowCardinality(String), bytes_uncompressed SimpleAggregateFunction(sum, Float64), bytes_compressed SimpleAggregateFunction(sum, Float64), record_count SimpleAggregateFunction(sum, UInt64)) AS SELECTteam_id,time_bucket,diff --git a/posthog/clickhouse/logs/__init__.py b/posthog/clickhouse/logs/__init__.pyindex 7675f61c2a6c..19ba3060148b 100644--- a/posthog/clickhouse/logs/__init__.py+++ b/posthog/clickhouse/logs/__init__.py@@ -23,6 +23,7 @@ from .logs34 import (LOGS34_TO_RESOURCE_ATTRIBUTES_MV,LOGS34_TO_VOLUME_BUCKETS_MV,LOGS_DISTRIBUTED_TABLE_SQL as LOGS34_DISTRIBUTED_TABLE_SQL,+ WRITABLE_LOGS34_TABLE_SQL,)from .logs_billing_metrics import LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL, LOGS_BILLING_METRICS_TABLE_SQLfrom .logs_kafka_metrics import LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL, LOGS_KAFKA_METRICS_TABLE_SQL@@ -47,6 +48,7 @@ __all__ = ["LOGS34_TO_LOG_ATTRIBUTES3_MV","LOGS34_TO_RESOURCE_ATTRIBUTES3_MV","KAFKA_LOGS_AVRO_TABLE_SQL",+ "WRITABLE_LOGS34_TABLE_SQL","KAFKA_LOGS34_AVRO_MV","KAFKA_LOGS34_AVRO_MV_SELECT","KAFKA_LOGS_AVRO_KAFKA_METRICS_MV",diff --git a/posthog/clickhouse/logs/logs34.py b/posthog/clickhouse/logs/logs34.pyindex 833759b57fdc..8f74f14efcf6 100644--- a/posthog/clickhouse/logs/logs34.py+++ b/posthog/clickhouse/logs/logs34.py@@ -105,6 +105,54 @@ CREATE OR REPLACE TABLE {database}.logs_distributed AS {database}.{table_name} E)+def WRITABLE_LOGS34_TABLE_SQL():+ # The write path for the Kafka MV, hosted on the ingestion-events nodes: a Distributed table over+ # the logs cluster's `logs34`. The columns are spelled out (rather than `AS logs34`) because+ # `logs34` is not local on the events nodes — only this Distributed front is. Keep the column list+ # byte-identical to `LOGS34_TABLE_SQL` (indexes/projection are storage-only and omitted here).+ # `CLICKHOUSE_LOGS_WRITE_CLUSTER`, not `CLICKHOUSE_LOGS_CLUSTER`: the events node reaches the logs+ # cluster under its own name, and the latter's default resolves to the data node from there.+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE+ return f"""+CREATE TABLE IF NOT EXISTS {db}.writable_logs34+(+ `time_bucket` DateTime MATERIALIZED toStartOfDay(timestamp),+ `original_expiry_timestamp` DateTime64(6),+ `uuid` String,+ `team_id` Int32,+ `trace_id` String,+ `span_id` String,+ `trace_flags` Int32,+ `timestamp` DateTime64(6) CODEC(DoubleDelta),+ `observed_timestamp` DateTime64(6),+ `created_at` DateTime64(6) MATERIALIZED now(),+ `body` String,+ `severity_text` LowCardinality(String),+ `severity_number` Int32,+ `service_name` LowCardinality(String),+ `resource_attributes` Map(LowCardinality(String), String),+ `resource_fingerprint` UInt64 MATERIALIZED cityHash64(resource_attributes),+ `instrumentation_scope` String,+ `event_name` String,+ `attributes_map_str` Map(LowCardinality(String), String),+ `level` String ALIAS severity_text,+ `mat_body_ipv4_matches` Array(String) ALIAS extractAll(body, '(\\d\\.((25[0-5]|(2[0-4]|1{0, 1}[0-9]){0, 1}[0-9])\\.){2, 2}([0-9]))'),+ `time_minute` DateTime ALIAS toStartOfMinute(timestamp),+ `attributes` Map(LowCardinality(String), String) ALIAS mapApply((k, v) -> (left(k, -5), v), attributes_map_str),+ `attributes_map_float` Map(LowCardinality(String), Float64) MATERIALIZED mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str)),+ `attributes_map_datetime` Map(LowCardinality(String), DateTime64(6)) MATERIALIZED mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str)),+ `_partition` UInt32,+ `_topic` String,+ `_offset` UInt64,+ `_bytes_uncompressed` UInt64,+ `_bytes_compressed` UInt64,+ `_record_count` UInt64+)+ENGINE = {Distributed(data_table=TABLE_NAME, cluster=settings.CLICKHOUSE_LOGS_WRITE_CLUSTER)}+SETTINGS background_insert_batch = 1+"""++def LOGS34_TO_LOG_ATTRIBUTES_MV():return f"""CREATE MATERIALIZED VIEW IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}_to_log_attributes TO {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{LOG_ATTRIBUTES_TABLE_NAME}@@ -328,7 +376,8 @@ CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{KAFKA_TA`resource_attributes` Map(LowCardinality(String), String),`instrumentation_scope` String,`event_name` String,- `attributes` Map(LowCardinality(String), String)+ `attributes` Map(LowCardinality(String), String),+ `retention_days` Nullable(Int32))ENGINE = {kafka_engine(topic=KAFKA_TOPIC, group=KAFKA_GROUP, serialization="Avro", named_collection=KAFKA_NAMED_COLLECTION)}SETTINGS@@ -336,7 +385,8 @@ SETTINGSkafka_num_consumers = 8,kafka_poll_timeout_ms = 3000,kafka_poll_max_batch_size = 1000,- kafka_thread_per_consumer = 1+ kafka_thread_per_consumer = 1,+ input_format_avro_allow_missing_fields = 1"""@@ -358,7 +408,7 @@ def KAFKA_LOGS34_AVRO_MV_SELECT():mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,- observed_timestamp + toIntervalDay(toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))) AS original_expiry_timestamp,+ observed_timestamp + toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0), retention_days, toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15)))) AS original_expiry_timestamp,_partition,_topic,_offset,@@ -368,10 +418,14 @@ def KAFKA_LOGS34_AVRO_MV_SELECT():FROM {db}.{KAFKA_TABLE_NAME}"""-def KAFKA_LOGS34_AVRO_MV():+def KAFKA_LOGS34_AVRO_MV(to_table: str = TABLE_NAME):db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE+ # `to_table` defaults to `logs34` so the historical migration 0280 (which creates this MV on the+ # logs nodes, where `logs34` is local) keeps working when replayed. Migration 0305 passes+ # `writable_logs34` — the Distributed(logs, posthog, logs34) table on the ingestion-events nodes —+ # because `logs34` itself is not local there.return f"""-CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.kafka_logs34_avro_mv TO {db}.{TABLE_NAME}+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.kafka_logs34_avro_mv TO {db}.{to_table}(`uuid` String,`trace_id` String,diff --git a/posthog/clickhouse/migrations/0305_logs_retention_days.py b/posthog/clickhouse/migrations/0305_logs_retention_days.pynew file mode 100644index 000000000000..dd0232161cd7--- /dev/null+++ b/posthog/clickhouse/migrations/0305_logs_retention_days.py@@ -0,0 +1,32 @@+from django.conf import settings++from posthog.clickhouse.client.connection import NodeRole+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions+from posthog.clickhouse.logs import KAFKA_LOGS34_AVRO_MV, KAFKA_LOGS_AVRO_TABLE_SQL, WRITABLE_LOGS34_TABLE_SQL+from posthog.run_mode import run_mode++DB = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE++# The Kafka engine table's column set is fixed at creation and the MV's SELECT changed, so both+# must be dropped and recreated. Neither is replicated (Kafka engine + MV), so no SYNC is needed.+# The recreated Kafka table adds `retention_days Nullable(Int32)` and+# `input_format_avro_allow_missing_fields = 1`; the MV now derives `original_expiry_timestamp` from+# the per-row `retention_days` when set, falling back to the batch `retention-days` header otherwise.+#+# The logs Kafka table + MV live on the ingestion-events nodes, so both are recreated there. Prod+# already hosts them there (moved manually) and drops from there; dev and local still have them on+# the logs nodes, so they drop from logs and recreate on ingestion-events, converging on prod.+# `run_mode()` is resolved here (not a raw CLOUD_DEPLOYMENT check) so a test re-import under a patched+# deployment picks up the right branch.+_drop_role = NodeRole.INGESTION_EVENTS if run_mode().is_prod_cloud else NodeRole.LOGS++operations = [+ run_sql_with_exceptions(f"DROP TABLE IF EXISTS {DB}.kafka_logs34_avro_mv", node_roles=[_drop_role]),+ run_sql_with_exceptions(f"DROP TABLE IF EXISTS {DB}.kafka_logs_avro", node_roles=[_drop_role]),+ # The MV writes into `writable_logs34` (Distributed → the logs cluster's `logs34`). It already+ # exists on prod's ingestion-events nodes; `IF NOT EXISTS` makes this a no-op there and creates it+ # on dev/local so the MV below has its destination.+ run_sql_with_exceptions(WRITABLE_LOGS34_TABLE_SQL(), node_roles=[NodeRole.INGESTION_EVENTS]),+ run_sql_with_exceptions(KAFKA_LOGS_AVRO_TABLE_SQL(), node_roles=[NodeRole.INGESTION_EVENTS]),+ run_sql_with_exceptions(KAFKA_LOGS34_AVRO_MV(to_table="writable_logs34"), node_roles=[NodeRole.INGESTION_EVENTS]),+]diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txtindex 11c6f5711171..d0c3218b11f6 100644--- a/posthog/clickhouse/migrations/max_migration.txt+++ b/posthog/clickhouse/migrations/max_migration.txt@@ -1 +1 @@-0304_logs34_pattern_columns+0305_logs_retention_daysdiff --git a/posthog/settings/data_stores.py b/posthog/settings/data_stores.pyindex 8f3f37d5f19c..f1da14060b89 100644--- a/posthog/settings/data_stores.py+++ b/posthog/settings/data_stores.py@@ -367,6 +367,10 @@ CLICKHOUSE_ALLOW_PER_SHARD_EXECUTION: bool = get_from_env()CLICKHOUSE_LOGS_CLUSTER: str = os.getenv("CLICKHOUSE_LOGS_CLUSTER", "posthog_single_shard")+# The name the logs cluster answers to from the ingestion-events nodes, which host the logs Kafka+# table and the Distributed front that writes into it. Cloud names it the same from both sides, so it+# defaults to CLICKHOUSE_LOGS_CLUSTER; the local multinode stack does not, and sets this explicitly.+CLICKHOUSE_LOGS_WRITE_CLUSTER: str = os.getenv("CLICKHOUSE_LOGS_WRITE_CLUSTER", CLICKHOUSE_LOGS_CLUSTER)CLICKHOUSE_LOGS_CLUSTER_HOST: str = os.getenv("CLICKHOUSE_LOGS_CLUSTER_HOST", "localhost")CLICKHOUSE_LOGS_CLUSTER_PORT: str = os.getenv("CLICKHOUSE_LOGS_CLUSTER_PORT", "9000")CLICKHOUSE_LOGS_CLUSTER_USER: str = os.getenv("CLICKHOUSE_LOGS_CLUSTER_USER", "default")diff --git a/tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke b/tools/infra-scripts/clickhouse-multinode/multinode-migration-smokeindex 58685ad93bdf..a0465b1d106b 100755--- a/tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke+++ b/tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke@@ -156,6 +156,10 @@ export CLICKHOUSE_MIGRATIONS_HOST="${CLICKHOUSE_MIGRATIONS_HOST:-localhost}"export CLICKHOUSE_CLUSTER="${CLICKHOUSE_CLUSTER:-posthog}"export CLICKHOUSE_MIGRATIONS_CLUSTER="${CLICKHOUSE_MIGRATIONS_CLUSTER:-posthog_migrations}"export CLICKHOUSE_SATELLITE_CLUSTERS="${CLICKHOUSE_SATELLITE_CLUSTERS:-ai_events,aux,ops,sessions}"+# The ingestion-events node reaches the logs cluster as `logs` (see+# docker/clickhouse/config.d/multinode/ingestion_events_node.xml). Its `posthog_single_shard`,+# which CLICKHOUSE_LOGS_CLUSTER defaults to, points at the data node instead.+export CLICKHOUSE_LOGS_WRITE_CLUSTER="${CLICKHOUSE_LOGS_WRITE_CLUSTER:-logs}"export CLICKHOUSE_DATABASE="${CLICKHOUSE_DATABASE:-posthog}"export CLICKHOUSE_USER="${CLICKHOUSE_USER:-default}"export CLICKHOUSE_PASSWORD="${CLICKHOUSE_PASSWORD:-}"