Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(logs): codify logs34 clickhouse schema in a migration (#65384)

Frank Hamandb9084d97cc34
diff --git a/bin/clickhouse-logs.sql b/bin/clickhouse-logs.sql
index 5115f5712538..bd26f511cab6 100644
--- a/bin/clickhouse-logs.sql
+++ b/bin/clickhouse-logs.sql
@@ -111,88 +111,6 @@ FROM
attribute
);
-CREATE OR REPLACE TABLE 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('kafka:9092', 'clickhouse_logs', 'clickhouse-logs-avro', 'Avro')
-SETTINGS
- kafka_skip_broken_messages = 100,
- kafka_security_protocol = 'PLAINTEXT',
- kafka_thread_per_consumer = 1,
- kafka_num_consumers = 1,
- kafka_poll_timeout_ms=15000,
- kafka_poll_max_batch_size=10,
- kafka_max_block_size=10;
-
-drop table if exists kafka_logs_avro_mv;
-
-CREATE MATERIALIZED VIEW kafka_logs_avro_mv TO logs32
-(
- `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)
-)
-AS SELECT
-* except (attributes, resource_attributes),
-mapSort(mapApply((k,v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) as attributes_map_str,
--- mapSort(mapFilter((k, v) -> isNotNull(v), mapApply((k,v) -> (concat(k, '__float'), toFloat64OrNull(JSONExtract(v, 'String'))), attributes))) as attributes_map_float,
--- mapSort(mapFilter((k, v) -> isNotNull(v), mapApply((k,v) -> (concat(k, '__datetime'), parseDateTimeBestEffortOrNull(JSONExtract(v, 'String'), 6)), attributes))) as attributes_map_datetime,
-mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
-toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) as team_id
-FROM kafka_logs_avro settings min_insert_block_size_rows=0, min_insert_block_size_bytes=0;
-
-create or replace table logs_kafka_metrics
-(
- `_partition` UInt32,
- `_topic` String,
- `max_offset` SimpleAggregateFunction(max, UInt64),
- `max_observed_timestamp` SimpleAggregateFunction(max, DateTime64(9)),
- `max_timestamp` SimpleAggregateFunction(max, DateTime64(9)),
- `max_created_at` SimpleAggregateFunction(max, DateTime64(9)),
- `max_lag` SimpleAggregateFunction(max, UInt64)
-)
-ENGINE = MergeTree
-ORDER BY (_topic, _partition);
-
-drop view if exists kafka_logs_avro_kafka_metrics_mv;
-CREATE MATERIALIZED VIEW kafka_logs_avro_kafka_metrics_mv TO logs_kafka_metrics
-AS
- SELECT
- _partition,
- _topic,
- maxSimpleState(_offset) as max_offset,
- maxSimpleState(observed_timestamp) as max_observed_timestamp,
- maxSimpleState(timestamp) as max_timestamp,
- maxSimpleState(now()) as max_created_at,
- maxSimpleState(now() - observed_timestamp) as max_lag
- FROM kafka_logs_avro
- group by _partition, _topic;
-
-- spans tables
CREATE OR REPLACE TABLE trace_spans
(
diff --git a/docker/clickhouse/config.d/default.xml b/docker/clickhouse/config.d/default.xml
index d2fec4ba469e..8acbbf28aeed 100644
--- a/docker/clickhouse/config.d/default.xml
+++ b/docker/clickhouse/config.d/default.xml
@@ -96,6 +96,9 @@
<warpstream_cyclotron>
<kafka_broker_list from_env="KAFKA_HOSTS"/>
</warpstream_cyclotron>
+ <warpstream_logs>
+ <kafka_broker_list from_env="KAFKA_HOSTS"/>
+ </warpstream_logs>
</named_collections>
<macros>
diff --git a/posthog/clickhouse/logs/__init__.py b/posthog/clickhouse/logs/__init__.py
index 7233a38e1085..a40ea42761e8 100644
--- a/posthog/clickhouse/logs/__init__.py
+++ b/posthog/clickhouse/logs/__init__.py
@@ -1,4 +1,5 @@
from .log_attributes import LOG_ATTRIBUTES_DISTRIBUTED_TABLE_SQL, LOG_ATTRIBUTES_TABLE_SQL
+from .log_attributes2 import LOG_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL, LOG_ATTRIBUTES2_TABLE_SQL
from .logs32 import (
LOG_ATTRIBUTES_MV,
LOG_RESOURCE_ATTRIBUTES_MV,
@@ -6,6 +7,17 @@ from .logs32 import (
LOGS_DISTRIBUTED2_TABLE_SQL,
LOGS_DISTRIBUTED_TABLE_SQL,
)
+from .logs34 import (
+ KAFKA_LOGS34_AVRO_MV,
+ KAFKA_LOGS_AVRO_BILLING_METRICS_MV,
+ KAFKA_LOGS_AVRO_KAFKA_METRICS_MV,
+ KAFKA_LOGS_AVRO_TABLE_SQL,
+ LOGS34_TABLE_SQL,
+ LOGS34_TO_LOG_ATTRIBUTES_MV,
+ LOGS34_TO_RESOURCE_ATTRIBUTES_MV,
+ LOGS_DISTRIBUTED_TABLE_SQL as LOGS34_DISTRIBUTED_TABLE_SQL,
+)
+from .logs_billing_metrics import LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL, LOGS_BILLING_METRICS_TABLE_SQL
from .logs_kafka_metrics import LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL, LOGS_KAFKA_METRICS_TABLE_SQL
__all__ = [
@@ -18,4 +30,16 @@ __all__ = [
"LOG_ATTRIBUTES_DISTRIBUTED_TABLE_SQL",
"LOGS_KAFKA_METRICS_TABLE_SQL",
"LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL",
+ "LOGS34_TABLE_SQL",
+ "LOGS34_DISTRIBUTED_TABLE_SQL",
+ "LOGS34_TO_LOG_ATTRIBUTES_MV",
+ "LOGS34_TO_RESOURCE_ATTRIBUTES_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",
+ "LOGS_BILLING_METRICS_TABLE_SQL",
+ "LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL",
]
diff --git a/posthog/clickhouse/logs/log_attributes2.py b/posthog/clickhouse/logs/log_attributes2.py
new file mode 100644
index 000000000000..f8994855a5ac
--- /dev/null
+++ b/posthog/clickhouse/logs/log_attributes2.py
@@ -0,0 +1,46 @@
+from django.conf import settings
+
+from posthog.clickhouse.table_engines import AggregatingMergeTree, Distributed, ReplicationScheme
+
+TABLE_NAME = "log_attributes2"
+
+
+def LOG_ATTRIBUTES2_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(),
+ 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)
+TTL original_expiry_time_bucket
+SETTINGS
+ deduplicate_merge_projection_mode = 'drop',
+ index_granularity = 8192
+"""
+
+
+def LOG_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL():
+ return """
+CREATE OR REPLACE TABLE {database}.log_attributes_distributed AS {database}.{table_name} ENGINE = {engine}
+""".format(
+ engine=Distributed(
+ data_table=TABLE_NAME,
+ cluster=settings.CLICKHOUSE_LOGS_CLUSTER,
+ ),
+ database=settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE,
+ table_name=TABLE_NAME,
+ )
diff --git a/posthog/clickhouse/logs/logs34.py b/posthog/clickhouse/logs/logs34.py
new file mode 100644
index 000000000000..9e96591b3a91
--- /dev/null
+++ b/posthog/clickhouse/logs/logs34.py
@@ -0,0 +1,333 @@
+from django.conf import settings
+
+from posthog.clickhouse.kafka_engine import kafka_engine
+from posthog.clickhouse.table_engines import Distributed, MergeTreeEngine, ReplicationScheme
+
+from .log_attributes2 import TABLE_NAME as LOG_ATTRIBUTES_TABLE_NAME
+
+TABLE_NAME = "logs34"
+KAFKA_TABLE_NAME = "kafka_logs_avro"
+KAFKA_NAMED_COLLECTION = "warpstream_logs"
+KAFKA_TOPIC = "clickhouse_logs"
+KAFKA_GROUP = "clickhouse-logs-avro-new"
+
+
+def LOGS34_TABLE_SQL():
+ return f"""
+CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}
+(
+ `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,
+ INDEX idx_severity_text_set severity_text TYPE set(10) GRANULARITY 1,
+ INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_mat_body_ipv4_matches mat_body_ipv4_matches TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_body_ngram3 lower(body) TYPE ngrambf_v1(3, 25000, 2, 0) GRANULARITY 1,
+ INDEX idx_uuid_bloom uuid TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ PROJECTION projection_aggregate_counts
+ (
+ SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint,
+ count() AS event_count
+ GROUP BY
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint
+ )
+)
+ENGINE = {MergeTreeEngine(TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
+PARTITION BY toDate(original_expiry_timestamp)
+PRIMARY KEY (team_id, time_bucket, service_name, resource_fingerprint, severity_text, timestamp)
+ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, severity_text, timestamp)
+TTL original_expiry_timestamp
+SETTINGS
+ allow_experimental_reverse_key = 1,
+ index_granularity_bytes = 104857600,
+ index_granularity = 8192,
+ ttl_only_drop_parts = 1,
+ add_minmax_index_for_numeric_columns = 1,
+ map_serialization_version = 'with_buckets'
+"""
+
+
+def LOGS_DISTRIBUTED_TABLE_SQL():
+ return """
+CREATE OR REPLACE TABLE {database}.logs_distributed AS {database}.{table_name} ENGINE = {engine}
+""".format(
+ engine=Distributed(
+ data_table=TABLE_NAME,
+ cluster=settings.CLICKHOUSE_LOGS_CLUSTER,
+ ),
+ database=settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE,
+ table_name=TABLE_NAME,
+ )
+
+
+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}
+(
+ `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),
+ `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,
+ 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,
+ 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,
+ attributes
+)
+"""
+
+
+def LOGS34_TO_RESOURCE_ATTRIBUTES_MV():
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}_to_resource_attributes TO {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{LOG_ATTRIBUTES_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),
+ `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,
+ 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,
+ 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,
+ resource_attributes
+)
+"""
+
+
+def KAFKA_LOGS_AVRO_TABLE_SQL():
+ return f"""
+CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{KAFKA_TABLE_NAME}
+(
+ `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_engine(topic=KAFKA_TOPIC, group=KAFKA_GROUP, serialization="Avro", named_collection=KAFKA_NAMED_COLLECTION)}
+SETTINGS
+ kafka_skip_broken_messages = 100,
+ kafka_num_consumers = 8,
+ kafka_poll_timeout_ms = 3000,
+ kafka_poll_max_batch_size = 1000,
+ kafka_thread_per_consumer = 1
+"""
+
+
+def KAFKA_LOGS34_AVRO_MV():
+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.kafka_logs34_avro_mv TO {db}.{TABLE_NAME}
+(
+ `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
+ {KAFKA_TABLE_NAME}.* EXCEPT (created_at, attribute_values, attribute_keys, attributes, attributes_map_str, attributes_map_float, attributes_map_datetime, resource_attributes),
+ 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 {db}.{KAFKA_TABLE_NAME}
+"""
+
+
+def KAFKA_LOGS_AVRO_KAFKA_METRICS_MV():
+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.kafka_logs_avro_kafka_metrics_mv TO {db}.logs_kafka_metrics
+(
+ `_partition` UInt32,
+ `_topic` String,
+ `max_offset` SimpleAggregateFunction(max, UInt64),
+ `max_observed_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
+ `max_timestamp` SimpleAggregateFunction(max, DateTime64(6)),
+ `max_created_at` SimpleAggregateFunction(max, DateTime),
+ `max_lag` SimpleAggregateFunction(max, Decimal(18, 6))
+)
+AS SELECT
+ _partition,
+ _topic,
+ maxSimpleState(_offset) AS max_offset,
+ maxSimpleState(observed_timestamp) AS max_observed_timestamp,
+ maxSimpleState(timestamp) AS max_timestamp,
+ maxSimpleState(now()) AS max_created_at,
+ maxSimpleState(now() - observed_timestamp) AS max_lag
+FROM {db}.{TABLE_NAME}
+GROUP BY _partition, _topic
+"""
+
+
+def KAFKA_LOGS_AVRO_BILLING_METRICS_MV():
+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.kafka_logs_avro_billing_metrics_mv TO {db}.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 SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ sumSimpleState(_bytes_uncompressed) AS bytes_uncompressed,
+ sumSimpleState(_bytes_compressed) AS bytes_compressed,
+ sumSimpleState(1) AS record_count
+FROM
+(
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalMinute(1)) AS time_bucket,
+ service_name AS service_name,
+ _bytes_uncompressed,
+ _bytes_compressed
+ FROM {db}.{TABLE_NAME}
+)
+GROUP BY team_id, time_bucket, service_name
+"""
diff --git a/posthog/clickhouse/logs/logs_billing_metrics.py b/posthog/clickhouse/logs/logs_billing_metrics.py
new file mode 100644
index 000000000000..ffeb4be15bf3
--- /dev/null
+++ b/posthog/clickhouse/logs/logs_billing_metrics.py
@@ -0,0 +1,38 @@
+from django.conf import settings
+
+from posthog.clickhouse.table_engines import AggregatingMergeTree, Distributed, ReplicationScheme
+
+TABLE_NAME = "logs_billing_metrics"
+
+
+def LOGS_BILLING_METRICS_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),
+ `bytes_uncompressed` SimpleAggregateFunction(sum, UInt64),
+ `bytes_compressed` SimpleAggregateFunction(sum, UInt64),
+ `record_count` SimpleAggregateFunction(sum, UInt64)
+)
+ENGINE = {AggregatingMergeTree(TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
+PARTITION BY toYYYYMM(time_bucket)
+ORDER BY (team_id, time_bucket, service_name)
+SETTINGS
+ deduplicate_merge_projection_mode = 'rebuild',
+ index_granularity = 8192
+"""
+
+
+def LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL():
+ return """
+CREATE TABLE IF NOT EXISTS {database}.logs_billing_metrics_distributed AS {database}.{table_name} ENGINE = {engine}
+""".format(
+ engine=Distributed(
+ data_table=TABLE_NAME,
+ cluster=settings.CLICKHOUSE_LOGS_CLUSTER,
+ ),
+ database=settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE,
+ table_name=TABLE_NAME,
+ )
diff --git a/posthog/clickhouse/migrations/0280_logs34.py b/posthog/clickhouse/migrations/0280_logs34.py
new file mode 100644
index 000000000000..76fb66231d98
--- /dev/null
+++ b/posthog/clickhouse/migrations/0280_logs34.py
@@ -0,0 +1,35 @@
+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_BILLING_METRICS_MV,
+ KAFKA_LOGS_AVRO_KAFKA_METRICS_MV,
+ KAFKA_LOGS_AVRO_TABLE_SQL,
+ LOG_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL,
+ LOG_ATTRIBUTES2_TABLE_SQL,
+ LOGS34_DISTRIBUTED_TABLE_SQL,
+ LOGS34_TABLE_SQL,
+ LOGS34_TO_LOG_ATTRIBUTES_MV,
+ LOGS34_TO_RESOURCE_ATTRIBUTES_MV,
+ LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL,
+ LOGS_BILLING_METRICS_TABLE_SQL,
+ LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL,
+ LOGS_KAFKA_METRICS_TABLE_SQL,
+)
+
+operations = [
+ run_sql_with_exceptions(LOGS34_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(LOG_ATTRIBUTES2_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(LOGS_BILLING_METRICS_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(LOGS_KAFKA_METRICS_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(LOGS34_DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(LOG_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(LOGS34_TO_LOG_ATTRIBUTES_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(LOGS34_TO_RESOURCE_ATTRIBUTES_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(KAFKA_LOGS_AVRO_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(KAFKA_LOGS34_AVRO_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(KAFKA_LOGS_AVRO_KAFKA_METRICS_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(KAFKA_LOGS_AVRO_BILLING_METRICS_MV(), node_roles=[NodeRole.LOGS]),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 932241badde4..51dc46f10b9e 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0279_drop_daily_aggregated_query_log_archive_view
+0280_logs34
diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.py
index 5d6c21e5fffe..786b830ce4d0 100644
--- a/posthog/clickhouse/schema.py
+++ b/posthog/clickhouse/schema.py
@@ -10,11 +10,17 @@ from posthog.clickhouse.dead_letter_queue import (
from posthog.clickhouse.distributed_system_processes import DISTRIBUTED_SYSTEM_PROCESSES_TABLE_SQL
from posthog.clickhouse.log_entries import KAFKA_LOG_ENTRIES_TABLE_SQL, LOG_ENTRIES_TABLE_MV_SQL, LOG_ENTRIES_TABLE_SQL
from posthog.clickhouse.logs import (
+ LOG_ATTRIBUTES2_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_ATTRIBUTES_MV,
+ LOGS34_TO_RESOURCE_ATTRIBUTES_MV,
+ LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL,
+ LOGS_BILLING_METRICS_TABLE_SQL,
LOGS_DISTRIBUTED2_TABLE_SQL,
LOGS_DISTRIBUTED_TABLE_SQL,
LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL,
@@ -350,6 +356,9 @@ CREATE_MERGETREE_TABLE_QUERIES = (
LOGS32_TABLE_SQL,
LOG_ATTRIBUTES_TABLE_SQL,
LOGS_KAFKA_METRICS_TABLE_SQL,
+ LOGS34_TABLE_SQL,
+ LOG_ATTRIBUTES2_TABLE_SQL,
+ LOGS_BILLING_METRICS_TABLE_SQL,
METRICS1_TABLE_SQL,
METRIC_ATTRIBUTES_TABLE_SQL,
TOPHOG_DATA_TABLE_SQL,
@@ -420,6 +429,7 @@ CREATE_DISTRIBUTED_TABLE_QUERIES = (
LOGS_DISTRIBUTED2_TABLE_SQL,
LOG_ATTRIBUTES_DISTRIBUTED_TABLE_SQL,
LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL,
+ LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL,
METRICS_DISTRIBUTED_TABLE_SQL,
WRITABLE_TOPHOG_TABLE_SQL,
DISTRIBUTED_TOPHOG_TABLE_SQL,
@@ -490,6 +500,8 @@ CREATE_MV_TABLE_QUERIES = (
DISTINCT_ID_USAGE_MV_SQL,
LOG_ATTRIBUTES_MV,
LOG_RESOURCE_ATTRIBUTES_MV,
+ LOGS34_TO_LOG_ATTRIBUTES_MV,
+ LOGS34_TO_RESOURCE_ATTRIBUTES_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.ambr
index c2a30abee5ee..bfb13f94a8d7 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -2573,6 +2573,35 @@
'''
# ---
+# name: test_create_table_query[log_attributes2]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.log_attributes2
+ (
+ `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(),
+ 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_attributes2', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_time_bucket)
+ ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value)
+ TTL original_expiry_time_bucket
+ SETTINGS
+ deduplicate_merge_projection_mode = 'drop',
+ index_granularity = 8192
+
+ '''
+# ---
# name: test_create_table_query[log_attributes]
'''
@@ -2847,6 +2876,185 @@
'''
# ---
+# name: test_create_table_query[logs34]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.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,
+ INDEX idx_severity_text_set severity_text TYPE set(10) GRANULARITY 1,
+ INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_mat_body_ipv4_matches mat_body_ipv4_matches TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_body_ngram3 lower(body) TYPE ngrambf_v1(3, 25000, 2, 0) GRANULARITY 1,
+ INDEX idx_uuid_bloom uuid TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ PROJECTION projection_aggregate_counts
+ (
+ SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint,
+ count() AS event_count
+ GROUP BY
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint
+ )
+ )
+ ENGINE = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.logs34', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_timestamp)
+ PRIMARY KEY (team_id, time_bucket, service_name, resource_fingerprint, severity_text, timestamp)
+ ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, severity_text, timestamp)
+ TTL original_expiry_timestamp
+ SETTINGS
+ allow_experimental_reverse_key = 1,
+ index_granularity_bytes = 104857600,
+ index_granularity = 8192,
+ ttl_only_drop_parts = 1,
+ add_minmax_index_for_numeric_columns = 1,
+ map_serialization_version = 'with_buckets'
+
+ '''
+# ---
+# name: test_create_table_query[logs34_to_log_attributes]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.logs34_to_log_attributes TO posthog_test.log_attributes2
+ (
+ `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),
+ `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,
+ 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,
+ 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,
+ attributes
+ )
+
+ '''
+# ---
+# name: test_create_table_query[logs34_to_resource_attributes]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.logs34_to_resource_attributes TO posthog_test.log_attributes2
+ (
+ `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),
+ `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,
+ 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,
+ 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,
+ resource_attributes
+ )
+
+ '''
+# ---
# name: test_create_table_query[logs]
'''
@@ -2854,6 +3062,34 @@
'''
# ---
+# name: test_create_table_query[logs_billing_metrics]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.logs_billing_metrics
+ (
+ `team_id` Int32,
+ `time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `bytes_uncompressed` SimpleAggregateFunction(sum, UInt64),
+ `bytes_compressed` SimpleAggregateFunction(sum, UInt64),
+ `record_count` SimpleAggregateFunction(sum, UInt64)
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.logs_billing_metrics', '{replica}-{shard}')
+ PARTITION BY toYYYYMM(time_bucket)
+ ORDER BY (team_id, time_bucket, service_name)
+ SETTINGS
+ deduplicate_merge_projection_mode = 'rebuild',
+ index_granularity = 8192
+
+ '''
+# ---
+# name: test_create_table_query[logs_billing_metrics_distributed]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.logs_billing_metrics_distributed AS posthog_test.logs_billing_metrics ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'logs_billing_metrics')
+
+ '''
+# ---
# name: test_create_table_query[logs_distributed]
'''
@@ -8582,6 +8818,35 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[log_attributes2]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.log_attributes2
+ (
+ `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(),
+ 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_attributes2', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_time_bucket)
+ ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value)
+ TTL original_expiry_time_bucket
+ SETTINGS
+ deduplicate_merge_projection_mode = 'drop',
+ index_granularity = 8192
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[log_attributes]
'''
@@ -8729,6 +8994,105 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[logs34]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.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,
+ INDEX idx_severity_text_set severity_text TYPE set(10) GRANULARITY 1,
+ INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_mat_body_ipv4_matches mat_body_ipv4_matches TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_body_ngram3 lower(body) TYPE ngrambf_v1(3, 25000, 2, 0) GRANULARITY 1,
+ INDEX idx_uuid_bloom uuid TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ PROJECTION projection_aggregate_counts
+ (
+ SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint,
+ count() AS event_count
+ GROUP BY
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint
+ )
+ )
+ ENGINE = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.logs34', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_timestamp)
+ PRIMARY KEY (team_id, time_bucket, service_name, resource_fingerprint, severity_text, timestamp)
+ ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, severity_text, timestamp)
+ TTL original_expiry_timestamp
+ SETTINGS
+ allow_experimental_reverse_key = 1,
+ index_granularity_bytes = 104857600,
+ index_granularity = 8192,
+ ttl_only_drop_parts = 1,
+ add_minmax_index_for_numeric_columns = 1,
+ map_serialization_version = 'with_buckets'
+
+ '''
+# ---
+# name: test_create_table_query_replicated_and_storage[logs_billing_metrics]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.logs_billing_metrics
+ (
+ `team_id` Int32,
+ `time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `bytes_uncompressed` SimpleAggregateFunction(sum, UInt64),
+ `bytes_compressed` SimpleAggregateFunction(sum, UInt64),
+ `record_count` SimpleAggregateFunction(sum, UInt64)
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.logs_billing_metrics', '{replica}-{shard}')
+ PARTITION BY toYYYYMM(time_bucket)
+ ORDER BY (team_id, time_bucket, service_name)
+ SETTINGS
+ deduplicate_merge_projection_mode = 'rebuild',
+ index_granularity = 8192
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[logs_kafka_metrics]
'''
diff --git a/posthog/settings/data_stores.py b/posthog/settings/data_stores.py
index 2285ab1cc269..359675c4921f 100644
--- a/posthog/settings/data_stores.py
+++ b/posthog/settings/data_stores.py
@@ -399,7 +399,9 @@ CLICKHOUSE_LOGS_CLUSTER_HOST: str = os.getenv("CLICKHOUSE_LOGS_CLUSTER_HOST", "l
CLICKHOUSE_LOGS_CLUSTER_PORT: str = os.getenv("CLICKHOUSE_LOGS_CLUSTER_PORT", "9000")
CLICKHOUSE_LOGS_CLUSTER_USER: str = os.getenv("CLICKHOUSE_LOGS_CLUSTER_USER", "default")
CLICKHOUSE_LOGS_CLUSTER_PASSWORD: str = os.getenv("CLICKHOUSE_LOGS_CLUSTER_PASSWORD", "")
-CLICKHOUSE_LOGS_CLUSTER_DATABASE: str = CLICKHOUSE_TEST_DB if TEST else os.getenv("CLICKHOUSE_LOGS_DATABASE", "default")
+CLICKHOUSE_LOGS_CLUSTER_DATABASE: str = (
+ CLICKHOUSE_TEST_DB if TEST else os.getenv("CLICKHOUSE_LOGS_DATABASE", CLICKHOUSE_DATABASE)
+)
CLICKHOUSE_LOGS_CLUSTER_SECURE: bool = get_from_env(
"CLICKHOUSE_LOGS_CLUSTER_SECURE", not TEST and not DEBUG, type_cast=str_to_bool
)