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.

feat(logs): track logs_ingestion partitions in logs_kafka_metrics (#109838)

Frank Hamand2eb62f6eec56
diff --git a/posthog/clickhouse/hcl/golden/dev/apm.hcl b/posthog/clickhouse/hcl/golden/dev/apm.hcl
index 88eb157eb9c3..a871fedb150b 100644
--- a/posthog/clickhouse/hcl/golden/dev/apm.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/apm.hcl
@@ -434,6 +434,12 @@ database "posthog" {
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -1108,7 +1114,9 @@ SELECT
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,
ifNull(pattern, '') AS pattern,
- toUInt8(ifNull(pattern_version, 0)) AS pattern_version
+ toUInt8(ifNull(pattern_version, 0)) AS pattern_version,
+ _headers.value[indexOf(_headers.name, 'source_topic')] AS _source_topic,
+ toUInt32OrZero(_headers.value[indexOf(_headers.name, 'source_partition')]) AS _source_partition
FROM posthog.kafka_logs_avro
SQL
@@ -1184,6 +1192,12 @@ SQL
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
}
materialized_view "kafka_metrics_avro4_mv" {
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index bb1a5a9e75d2..002eb416fece 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -382,6 +382,12 @@ database "posthog" {
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
index "idx_severity_text_set" {
expr = "severity_text"
type = "set(10)"
@@ -610,6 +616,12 @@ SQL
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -3430,16 +3442,27 @@ SQL
to_table = "posthog.logs_kafka_metrics"
query = <<SQL
SELECT
- _partition,
- _topic,
- maxSimpleState(_offset) AS max_offset,
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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 posthog.logs34
+FROM
+ (
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM
+ posthog.logs34 ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+ )
GROUP BY
- _partition, _topic
+ kafka_partition, kafka_topic
SQL
column "_partition" {
diff --git a/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl b/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl
index fead3407d47a..3f7e463dde47 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl
@@ -783,6 +783,12 @@ database "posthog" {
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -984,7 +990,9 @@ SELECT
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,
ifNull(pattern, '') AS pattern,
- toUInt8(ifNull(pattern_version, 0)) AS pattern_version
+ toUInt8(ifNull(pattern_version, 0)) AS pattern_version,
+ _headers.value[indexOf(_headers.name, 'source_topic')] AS _source_topic,
+ toUInt32OrZero(_headers.value[indexOf(_headers.name, 'source_partition')]) AS _source_partition
FROM posthog.kafka_logs_avro
SQL
@@ -1060,6 +1068,12 @@ SQL
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
}
}
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index 8f6a67baf71a..217258a20a35 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -879,6 +879,12 @@ SQL
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
index "idx_severity_text_set" {
expr = "severity_text"
type = "set(10)"
@@ -1107,6 +1113,12 @@ SQL
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
engine "distributed" {
cluster_name = "posthog_single_shard"
remote_database = "posthog"
@@ -3845,16 +3857,27 @@ SQL
to_table = "posthog.logs_kafka_metrics"
query = <<SQL
SELECT
- _partition,
- _topic,
- maxSimpleState(_offset) AS max_offset,
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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 posthog.logs34
+FROM
+ (
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM
+ posthog.logs34 ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+ )
GROUP BY
- _partition, _topic
+ kafka_partition, kafka_topic
SQL
column "_partition" {
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index 3af04d1ac906..6ab4edcc22c3 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -5569,6 +5569,12 @@ SQL
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
index "idx_severity_text_set" {
expr = "severity_text"
type = "set(10)"
@@ -5797,6 +5803,12 @@ SQL
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
engine "distributed" {
cluster_name = "posthog_single_shard"
remote_database = "posthog"
@@ -17685,6 +17697,12 @@ SQL
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
engine "distributed" {
cluster_name = "posthog_single_shard"
remote_database = "posthog"
@@ -21090,7 +21108,9 @@ SELECT
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,
ifNull(pattern, '') AS pattern,
- toUInt8(ifNull(pattern_version, 0)) AS pattern_version
+ toUInt8(ifNull(pattern_version, 0)) AS pattern_version,
+ _headers.value[indexOf(_headers.name, 'source_topic')] AS _source_topic,
+ toUInt32OrZero(_headers.value[indexOf(_headers.name, 'source_partition')]) AS _source_partition
FROM posthog.kafka_logs_avro
SQL
@@ -21166,6 +21186,12 @@ SQL
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
}
materialized_view "kafka_logs_avro_billing_metrics_mv" {
@@ -21216,16 +21242,27 @@ SQL
to_table = "posthog.logs_kafka_metrics"
query = <<SQL
SELECT
- _partition,
- _topic,
- maxSimpleState(_offset) AS max_offset,
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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 posthog.logs34
+FROM
+ (
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM
+ posthog.logs34 ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+ )
GROUP BY
- _partition, _topic
+ kafka_partition, kafka_topic
SQL
column "_partition" {
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index a4ceec626c4c..4bc8751ea2cd 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -464,6 +464,12 @@ database "posthog" {
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
index "idx_severity_text_set" {
expr = "severity_text"
type = "set(10)"
@@ -692,6 +698,12 @@ SQL
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -3548,16 +3560,27 @@ SQL
to_table = "posthog.logs_kafka_metrics"
query = <<SQL
SELECT
- _partition,
- _topic,
- maxSimpleState(_offset) AS max_offset,
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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 posthog.logs34
+FROM
+ (
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM
+ posthog.logs34 ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+ )
GROUP BY
- _partition, _topic
+ kafka_partition, kafka_topic
SQL
column "_partition" {
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index ae8b17962239..857e3413d2da 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -464,6 +464,12 @@ database "posthog" {
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
index "idx_severity_text_set" {
expr = "severity_text"
type = "set(10)"
@@ -692,6 +698,12 @@ SQL
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
engine "distributed" {
cluster_name = "logs"
remote_database = "posthog"
@@ -3530,16 +3542,27 @@ SQL
to_table = "posthog.logs_kafka_metrics"
query = <<SQL
SELECT
- _partition,
- _topic,
- maxSimpleState(_offset) AS max_offset,
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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 posthog.logs34
+FROM
+ (
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM
+ posthog.logs34 ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+ )
GROUP BY
- _partition, _topic
+ kafka_partition, kafka_topic
SQL
column "_partition" {
diff --git a/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/sql/kafka_logs34_avro_mv.sql b/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/sql/kafka_logs34_avro_mv.sql
index 267ac14d472c..94b2ee52baf7 100644
--- a/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/sql/kafka_logs34_avro_mv.sql
+++ b/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/sql/kafka_logs34_avro_mv.sql
@@ -29,5 +29,7 @@ SELECT
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,
ifNull(pattern, '') AS pattern,
- toUInt8(ifNull(pattern_version, 0)) AS pattern_version
+ toUInt8(ifNull(pattern_version, 0)) AS pattern_version,
+ _headers.value[indexOf(_headers.name, 'source_topic')] AS _source_topic,
+ toUInt32OrZero(_headers.value[indexOf(_headers.name, 'source_partition')]) AS _source_partition
FROM posthog.kafka_logs_avro
diff --git a/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/tables.hcl b/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/tables.hcl
index 2eb8d14faf71..29f5f64d5c90 100644
--- a/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/coshared/logs_avro_ingest/tables.hcl
@@ -144,6 +144,12 @@ database "posthog" {
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
}
table "writable_logs34" {
@@ -256,6 +262,12 @@ database "posthog" {
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
settings = {
background_insert_batch = "1"
}
diff --git a/posthog/clickhouse/hcl/roles/logs/base/sql/kafka_logs_avro_kafka_metrics_mv.sql b/posthog/clickhouse/hcl/roles/logs/base/sql/kafka_logs_avro_kafka_metrics_mv.sql
index 6f4c3cdd0ba4..55eeab4cb42c 100644
--- a/posthog/clickhouse/hcl/roles/logs/base/sql/kafka_logs_avro_kafka_metrics_mv.sql
+++ b/posthog/clickhouse/hcl/roles/logs/base/sql/kafka_logs_avro_kafka_metrics_mv.sql
@@ -1,11 +1,22 @@
SELECT
- _partition,
- _topic,
- maxSimpleState(_offset) AS max_offset,
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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 posthog.logs34
+FROM
+ (
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM posthog.logs34
+ ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+ )
GROUP BY
- _partition, _topic
+ kafka_partition, kafka_topic
diff --git a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
index 5ba62b99d817..b3b4512caf63 100644
--- a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
@@ -595,6 +595,12 @@ database "posthog" {
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
engine "distributed" {
cluster_name = "posthog_single_shard"
remote_database = "posthog"
@@ -1024,6 +1030,12 @@ database "posthog" {
column "pattern_version" {
type = "UInt8"
}
+ column "_source_topic" {
+ type = "String"
+ }
+ column "_source_partition" {
+ type = "UInt32"
+ }
index "idx_severity_text_set" {
expr = "severity_text"
type = "set(10)"
diff --git a/posthog/clickhouse/hcl/sql/dev/apm.sql b/posthog/clickhouse/hcl/sql/dev/apm.sql
index da2ff4d573fe..c17bc41d70ff 100644
--- a/posthog/clickhouse/hcl/sql/dev/apm.sql
+++ b/posthog/clickhouse/hcl/sql/dev/apm.sql
@@ -130,7 +130,9 @@ CREATE TABLE posthog.writable_logs34 (
_bytes_compressed UInt64,
_record_count UInt64,
pattern String,
- pattern_version UInt8
+ pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32
) ENGINE = Distributed('logs', 'posthog', 'logs34') SETTINGS background_insert_batch = 1;
CREATE TABLE posthog.writable_metric_samples1 (
team_id Int32,
@@ -328,7 +330,7 @@ CREATE TABLE posthog.writable_trace_spans (
_bytes_compressed UInt64,
_record_count UInt64
) ENGINE = Distributed('logs', 'posthog', 'trace_spans');
-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 Nullable(DateTime64(6)), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Float64), _bytes_compressed Nullable(Float64), pattern String, pattern_version UInt8) 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 Nullable(DateTime64(6)), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Float64), _bytes_compressed Nullable(Float64), pattern String, pattern_version UInt8, _source_topic String, _source_partition UInt32) AS SELECT
uuid,
trace_id,
span_id,
@@ -359,7 +361,9 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.writable_logs34
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,
ifNull(pattern, '') AS pattern,
- toUInt8(ifNull(pattern_version, 0)) AS pattern_version
+ toUInt8(ifNull(pattern_version, 0)) AS pattern_version,
+ _headers.value[indexOf(_headers.name, 'source_topic')] AS _source_topic,
+ toUInt32OrZero(_headers.value[indexOf(_headers.name, 'source_partition')]) AS _source_partition
FROM posthog.kafka_logs_avro;
CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro4_mv TO posthog.metrics4_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT
uuid,
diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index 22e047ee1d57..da74c300f690 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -102,6 +102,8 @@ CREATE TABLE posthog.logs34 (
_record_count UInt64,
pattern String,
pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32,
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,
@@ -170,7 +172,9 @@ CREATE TABLE posthog.logs_distributed (
_bytes_compressed UInt64,
_record_count UInt64,
pattern String,
- pattern_version UInt8
+ pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32
) ENGINE = Distributed('logs', 'posthog', 'logs34');
CREATE TABLE posthog.logs_kafka_metrics (
_partition UInt32,
@@ -915,16 +919,27 @@ FROM
GROUP BY
team_id, time_bucket, service_name;
CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_kafka_metrics_mv TO posthog.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,
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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 posthog.logs34
+FROM
+ (
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM
+ posthog.logs34 ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+ )
GROUP BY
- _partition, _topic;
+ kafka_partition, kafka_topic;
CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT
uuid,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql b/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql
index 2d908b2dfa24..0fe85ba25fe4 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql
@@ -223,7 +223,9 @@ CREATE TABLE posthog.writable_logs34 (
_bytes_compressed UInt64,
_record_count UInt64,
pattern String,
- pattern_version UInt8
+ pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32
) 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 String, temporary_properties String, inserted_at DateTime64(3), timestamp DateTime64(6, 'UTC'), team_id Int64, distinct_id String, elements_chain String, created_at DateTime64(6, 'UTC'), person_id UUID, person_properties String, person_created_at DateTime64(3), 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, captured_at DateTime64(6, 'UTC'), _timestamp Nullable(DateTime), _offset UInt64, _partition UInt64, consumer_breadcrumbs Array(String), total_event_size UInt32) AS SELECT
*,
@@ -285,7 +287,7 @@ FROM
SETTINGS
input_format_try_infer_dates = 0,
input_format_try_infer_datetimes = 0;
-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), pattern String, pattern_version UInt8) 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), pattern String, pattern_version UInt8, _source_topic String, _source_partition UInt32) AS SELECT
uuid,
trace_id,
span_id,
@@ -316,5 +318,7 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.writable_logs34
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,
ifNull(pattern, '') AS pattern,
- toUInt8(ifNull(pattern_version, 0)) AS pattern_version
+ toUInt8(ifNull(pattern_version, 0)) AS pattern_version,
+ _headers.value[indexOf(_headers.name, 'source_topic')] AS _source_topic,
+ toUInt32OrZero(_headers.value[indexOf(_headers.name, 'source_partition')]) AS _source_partition
FROM posthog.kafka_logs_avro;
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index 59009ada1fb0..09943fb96ab3 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -194,6 +194,8 @@ CREATE TABLE posthog.logs34 (
_record_count UInt64,
pattern String,
pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32,
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,
@@ -262,7 +264,9 @@ CREATE TABLE posthog.logs_distributed (
_bytes_compressed UInt64,
_record_count UInt64,
pattern String,
- pattern_version UInt8
+ pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs34');
CREATE TABLE posthog.logs_kafka_metrics (
_partition UInt32,
@@ -979,16 +983,27 @@ FROM
GROUP BY
team_id, time_bucket, service_name;
CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_kafka_metrics_mv TO posthog.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,
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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 posthog.logs34
+FROM
+ (
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM
+ posthog.logs34 ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+ )
GROUP BY
- _partition, _topic;
+ kafka_partition, kafka_topic;
CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT
uuid,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index de2f623d4941..e198568875a4 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -1111,6 +1111,8 @@ CREATE TABLE posthog.logs34 (
_record_count UInt64,
pattern String,
pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32,
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,
@@ -1179,7 +1181,9 @@ CREATE TABLE posthog.logs_distributed (
_bytes_compressed UInt64,
_record_count UInt64,
pattern String,
- pattern_version UInt8
+ pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs34');
CREATE TABLE posthog.logs_kafka_metrics (
_partition UInt32,
@@ -4010,7 +4014,9 @@ CREATE TABLE posthog.writable_logs34 (
_bytes_compressed UInt64,
_record_count UInt64,
pattern String,
- pattern_version UInt8
+ pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs34') SETTINGS background_insert_batch = 1;
CREATE TABLE posthog.writable_metrics4_attributes (
team_id Int32,
@@ -4896,7 +4902,7 @@ CREATE MATERIALIZED VIEW posthog.ingestion_warnings_v2_mv TO posthog.ingestion_w
_offset,
_partition
FROM posthog.kafka_ingestion_warnings_v2;
-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), pattern String, pattern_version UInt8) 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), pattern String, pattern_version UInt8, _source_topic String, _source_partition UInt32) AS SELECT
uuid,
trace_id,
span_id,
@@ -4927,7 +4933,9 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.writable_logs34
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,
ifNull(pattern, '') AS pattern,
- toUInt8(ifNull(pattern_version, 0)) AS pattern_version
+ toUInt8(ifNull(pattern_version, 0)) AS pattern_version,
+ _headers.value[indexOf(_headers.name, 'source_topic')] AS _source_topic,
+ toUInt32OrZero(_headers.value[indexOf(_headers.name, 'source_partition')]) AS _source_partition
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 SELECT
team_id,
@@ -4949,16 +4957,27 @@ FROM
GROUP BY
team_id, time_bucket, service_name;
CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_kafka_metrics_mv TO posthog.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,
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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 posthog.logs34
+FROM
+ (
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM
+ posthog.logs34 ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+ )
GROUP BY
- _partition, _topic;
+ kafka_partition, kafka_topic;
CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT
uuid,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index ad0cd16e90ce..697a2c1247e4 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -126,6 +126,8 @@ CREATE TABLE posthog.logs34 (
_record_count UInt64,
pattern String,
pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32,
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,
@@ -194,7 +196,9 @@ CREATE TABLE posthog.logs_distributed (
_bytes_compressed UInt64,
_record_count UInt64,
pattern String,
- pattern_version UInt8
+ pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32
) ENGINE = Distributed('logs', 'posthog', 'logs34');
CREATE TABLE posthog.logs_kafka_metrics (
_partition UInt32,
@@ -954,16 +958,27 @@ FROM
GROUP BY
team_id, time_bucket, service_name;
CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_kafka_metrics_mv TO posthog.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,
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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 posthog.logs34
+FROM
+ (
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM
+ posthog.logs34 ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+ )
GROUP BY
- _partition, _topic;
+ kafka_partition, kafka_topic;
CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT
uuid,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index 1ae52a0a4a19..59d93d3ddc1c 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -126,6 +126,8 @@ CREATE TABLE posthog.logs34 (
_record_count UInt64,
pattern String,
pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32,
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,
@@ -194,7 +196,9 @@ CREATE TABLE posthog.logs_distributed (
_bytes_compressed UInt64,
_record_count UInt64,
pattern String,
- pattern_version UInt8
+ pattern_version UInt8,
+ _source_topic String,
+ _source_partition UInt32
) ENGINE = Distributed('logs', 'posthog', 'logs34');
CREATE TABLE posthog.logs_kafka_metrics (
_partition UInt32,
@@ -948,16 +952,27 @@ FROM
GROUP BY
team_id, time_bucket, service_name;
CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_kafka_metrics_mv TO posthog.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,
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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 posthog.logs34
+FROM
+ (
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM
+ posthog.logs34 ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+ )
GROUP BY
- _partition, _topic;
+ kafka_partition, kafka_topic;
CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT
uuid,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
diff --git a/posthog/clickhouse/logs/__init__.py b/posthog/clickhouse/logs/__init__.py
index fdcdc9c07d09..4c8edc146b8f 100644
--- a/posthog/clickhouse/logs/__init__.py
+++ b/posthog/clickhouse/logs/__init__.py
@@ -15,6 +15,7 @@ from .logs34 import (
KAFKA_LOGS34_AVRO_MV_SELECT,
KAFKA_LOGS_AVRO_BILLING_METRICS_MV,
KAFKA_LOGS_AVRO_KAFKA_METRICS_MV,
+ KAFKA_LOGS_AVRO_KAFKA_METRICS_MV_SELECT,
KAFKA_LOGS_AVRO_TABLE_SQL,
LOGS34_TABLE_SQL,
LOGS34_TO_LOG_ATTRIBUTES3_MV,
@@ -50,6 +51,7 @@ __all__ = [
"KAFKA_LOGS34_AVRO_MV",
"KAFKA_LOGS34_AVRO_MV_SELECT",
"KAFKA_LOGS_AVRO_KAFKA_METRICS_MV",
+ "KAFKA_LOGS_AVRO_KAFKA_METRICS_MV_SELECT",
"KAFKA_LOGS_AVRO_BILLING_METRICS_MV",
"LOG_ATTRIBUTES2_TABLE_SQL",
"LOG_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL",
diff --git a/posthog/clickhouse/logs/logs34.py b/posthog/clickhouse/logs/logs34.py
index 6c02f1d783f3..b0ec3e1e1562 100644
--- a/posthog/clickhouse/logs/logs34.py
+++ b/posthog/clickhouse/logs/logs34.py
@@ -49,6 +49,8 @@ CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NA
`_record_count` UInt64,
`pattern` String,
`pattern_version` UInt8,
+ `_source_topic` String,
+ `_source_partition` UInt32,
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,
@@ -147,7 +149,9 @@ CREATE TABLE IF NOT EXISTS {db}.writable_logs34
`_bytes_compressed` UInt64,
`_record_count` UInt64,
`pattern` String,
- `pattern_version` UInt8
+ `pattern_version` UInt8,
+ `_source_topic` String,
+ `_source_partition` UInt32
)
ENGINE = {Distributed(data_table=TABLE_NAME, cluster=settings.CLICKHOUSE_LOGS_WRITE_CLUSTER)}
SETTINGS background_insert_batch = 1
@@ -320,7 +324,9 @@ def KAFKA_LOGS34_AVRO_MV_SELECT():
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,
ifNull(pattern, '') AS pattern,
- toUInt8(ifNull(pattern_version, 0)) AS pattern_version
+ toUInt8(ifNull(pattern_version, 0)) AS pattern_version,
+ _headers.value[indexOf(_headers.name, 'source_topic')] AS _source_topic,
+ toUInt32OrZero(_headers.value[indexOf(_headers.name, 'source_partition')]) AS _source_partition
FROM {db}.{KAFKA_TABLE_NAME}"""
@@ -356,7 +362,9 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.kafka_logs34_avro_mv TO {db}.{to_tab
`_bytes_uncompressed` Nullable(Int64),
`_bytes_compressed` Nullable(Int64),
`pattern` String,
- `pattern_version` UInt8
+ `pattern_version` UInt8,
+ `_source_topic` String,
+ `_source_partition` UInt32
)
AS {KAFKA_LOGS34_AVRO_MV_SELECT()}
"""
@@ -431,6 +439,35 @@ AS {LOGS34_TO_VOLUME_BUCKETS_MV_SELECT()}
"""
+def KAFKA_LOGS_AVRO_KAFKA_METRICS_MV_SELECT():
+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+ # Each row counts toward the clickhouse_logs partition and the logs_ingestion partition it came
+ # from, because both topics use random partitioning and the lag checkpoint needs both. A row
+ # without the source header has an empty source topic, and the WHERE skips it. The source
+ # offset is not known, so it counts as 0.
+ return f"""SELECT
+ kafka_partition AS _partition,
+ kafka_topic AS _topic,
+ maxSimpleState(kafka_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
+(
+ SELECT
+ kafka_source.1 AS kafka_topic,
+ kafka_source.2 AS kafka_partition,
+ kafka_source.3 AS kafka_offset,
+ observed_timestamp,
+ timestamp
+ FROM {db}.{TABLE_NAME}
+ ARRAY JOIN [(_topic, _partition, _offset), (_source_topic, _source_partition, 0)] AS kafka_source
+ WHERE kafka_topic != ''
+)
+GROUP BY kafka_partition, kafka_topic"""
+
+
def KAFKA_LOGS_AVRO_KAFKA_METRICS_MV():
db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
return f"""
@@ -444,16 +481,7 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.kafka_logs_avro_kafka_metrics_mv TO
`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
+AS {KAFKA_LOGS_AVRO_KAFKA_METRICS_MV_SELECT()}
"""
diff --git a/posthog/clickhouse/migrations/0343_logs_source_partition.py b/posthog/clickhouse/migrations/0343_logs_source_partition.py
new file mode 100644
index 000000000000..d812c8f45eea
--- /dev/null
+++ b/posthog/clickhouse/migrations/0343_logs_source_partition.py
@@ -0,0 +1,49 @@
+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_KAFKA_METRICS_MV_SELECT
+
+DB = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+
+# Records the logs_ingestion partition of each log row, so the live logs checkpoint can track
+# progress for both topics that a row passes through. The Node consumer sends the partition in the
+# `source_topic` and `source_partition` headers.
+#
+# The columns go on every table that the row passes through, before the MVs that write or read
+# them. Adding a column to a Distributed table changes only its metadata.
+#
+# The Kafka MV gets new output columns, so it is dropped and created again. The Kafka table stays,
+# with the same consumer group, so the gap causes lag and no data loss. The metrics MV keeps the
+# same output columns, so MODIFY QUERY is sufficient.
+
+ADD_COLUMNS = "ADD COLUMN IF NOT EXISTS _source_topic String, ADD COLUMN IF NOT EXISTS _source_partition UInt32"
+
+operations = [
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.logs34 {ADD_COLUMNS}",
+ node_roles=[NodeRole.LOGS],
+ sharded=False,
+ is_alter_on_replicated_table=True,
+ ),
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.logs_distributed {ADD_COLUMNS}",
+ node_roles=[NodeRole.LOGS],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.writable_logs34 {ADD_COLUMNS}",
+ node_roles=[NodeRole.INGESTION_EVENTS],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.kafka_logs_avro_kafka_metrics_mv MODIFY QUERY\n{KAFKA_LOGS_AVRO_KAFKA_METRICS_MV_SELECT()}",
+ node_roles=[NodeRole.LOGS],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+ run_sql_with_exceptions(f"DROP TABLE IF EXISTS {DB}.kafka_logs34_avro_mv", 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.txt
index b08a9384384f..3494eba8fa83 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0342_metrics4_names_service_name
+0343_logs_source_partition
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index c83b485814dc..e7fe54c1f19d 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -3857,6 +3857,8 @@
`_record_count` UInt64,
`pattern` String,
`pattern_version` UInt8,
+ `_source_topic` String,
+ `_source_partition` UInt32,
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,
@@ -12073,6 +12075,8 @@
`_record_count` UInt64,
`pattern` String,
`pattern_version` UInt8,
+ `_source_topic` String,
+ `_source_partition` UInt32,
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,
diff --git a/posthog/clickhouse/test/test_schema.py b/posthog/clickhouse/test/test_schema.py
index 0bd6f7492cf8..2a2715f907ba 100644
--- a/posthog/clickhouse/test/test_schema.py
+++ b/posthog/clickhouse/test/test_schema.py
@@ -13,7 +13,7 @@ from posthog.hogql.database.schema.flag_evaluations import FLAG_EVALUATIONS_CLIC
from posthog.clickhouse.client import sync_execute
from posthog.clickhouse.kafka_engine import CONSUMER_GROUP_EVENTS_JSON_NATIVE_JSON, KAFKA_COLUMNS_WITH_PARTITION
-from posthog.clickhouse.logs import LOGS34_TO_VOLUME_BUCKETS_MV_SELECT
+from posthog.clickhouse.logs import KAFKA_LOGS_AVRO_KAFKA_METRICS_MV_SELECT, LOGS34_TO_VOLUME_BUCKETS_MV_SELECT
from posthog.clickhouse.schema import (
CREATE_KAFKA_TABLE_QUERIES,
CREATE_MERGETREE_TABLE_QUERIES,
@@ -137,6 +137,36 @@ def test_logs_volume_bucket_retention_covers_raw_expiry(
)
[email protected]("clickhouse_database")
+def test_logs_kafka_metrics_counts_rows_toward_source_partition() -> None:
+ select = KAFKA_LOGS_AVRO_KAFKA_METRICS_MV_SELECT().replace(
+ f"FROM {django_settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.logs34", "FROM metrics_input"
+ )
+ rows = sync_execute(
+ """
+ WITH metrics_input AS (
+ SELECT
+ source_row.1 AS _topic,
+ toUInt32(source_row.2) AS _partition,
+ toUInt64(source_row.3) AS _offset,
+ source_row.4 AS _source_topic,
+ toUInt32(source_row.5) AS _source_partition,
+ toDateTime64('2026-01-02 12:00:00', 6, 'UTC') AS observed_timestamp,
+ observed_timestamp AS timestamp
+ FROM (
+ SELECT arrayJoin([
+ ('clickhouse_logs', 3, 10, 'logs_ingestion', 7),
+ ('clickhouse_logs', 5, 11, '', 0)
+ ]) AS source_row
+ )
+ )
+ SELECT _topic, _partition, max_offset FROM ("""
+ + select
+ + ") ORDER BY _topic, _partition",
+ )
+ assert rows == [("clickhouse_logs", 3, 10), ("clickhouse_logs", 5, 11), ("logs_ingestion", 7, 0)]
+
+
def _column_definition_lines(block: str) -> Iterator[str]:
for raw_line in block.splitlines():
line = raw_line.strip().lstrip(",").strip()