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(tracing): add a per-row retention_days column to the trace spans Kafka table (#104592)

Frank Hamandbd7d4953958b
diff --git a/posthog/clickhouse/hcl/golden/dev/apm.hcl b/posthog/clickhouse/hcl/golden/dev/apm.hcl
index bdfbb69df7c9..cf1ce405d7b6 100644
--- a/posthog/clickhouse/hcl/golden/dev/apm.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/apm.hcl
@@ -297,16 +297,18 @@ database "posthog" {
column "status_code" {
type = "Int32"
}
+ column "retention_days" {
+ type = "Nullable(Int32)"
+ }
engine "kafka" {
collection = "warpstream_traces"
topic_list = "clickhouse_traces"
group_name = "clickhouse-traces-avro"
format = "Avro"
- num_consumers = 4
+ num_consumers = 8
skip_broken_messages = 100
- poll_timeout_ms = 10000
+ poll_timeout_ms = 3000
poll_max_batch_size = 1000
- flush_interval_ms = 10000
thread_per_consumer = true
}
}
@@ -1440,7 +1442,19 @@ SQL
to_table = "posthog.writable_trace_spans"
query = <<SQL
SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -1452,7 +1466,11 @@ SELECT
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index e96c9918dc1c..ee96328e0c0f 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -157,6 +157,9 @@ database "posthog" {
}
table "kafka_trace_spans_avro" {
+ settings = {
+ input_format_avro_allow_missing_fields = "1"
+ }
column "uuid" {
type = "String"
}
@@ -220,6 +223,9 @@ database "posthog" {
column "status_code" {
type = "Int32"
}
+ column "retention_days" {
+ type = "Nullable(Int32)"
+ }
engine "kafka" {
collection = "warpstream_traces"
topic_list = "clickhouse_traces"
@@ -4096,7 +4102,19 @@ SQL
to_table = "posthog.trace_spans"
query = <<SQL
SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -4108,7 +4126,11 @@ SELECT
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index 283282480b5d..eb986bfb1c60 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -4683,6 +4683,9 @@ database "posthog" {
}
table "kafka_trace_spans_avro" {
+ settings = {
+ input_format_avro_allow_missing_fields = "1"
+ }
column "uuid" {
type = "String"
}
@@ -4746,6 +4749,9 @@ database "posthog" {
column "status_code" {
type = "Int32"
}
+ column "retention_days" {
+ type = "Nullable(Int32)"
+ }
engine "kafka" {
collection = "warpstream_traces"
topic_list = "clickhouse_traces"
@@ -21366,7 +21372,19 @@ SQL
to_table = "posthog.trace_spans"
query = <<SQL
SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -21378,7 +21396,11 @@ SELECT
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index 4593556aa2ad..50bb0433f10e 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -157,6 +157,9 @@ database "posthog" {
}
table "kafka_trace_spans_avro" {
+ settings = {
+ input_format_avro_allow_missing_fields = "1"
+ }
column "uuid" {
type = "String"
}
@@ -220,6 +223,9 @@ database "posthog" {
column "status_code" {
type = "Int32"
}
+ column "retention_days" {
+ type = "Nullable(Int32)"
+ }
engine "kafka" {
collection = "warpstream_traces"
topic_list = "clickhouse_traces"
@@ -3801,7 +3807,19 @@ SQL
to_table = "posthog.trace_spans"
query = <<SQL
SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -3813,7 +3831,11 @@ SELECT
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index f32a873d1ec3..3fb9334acac9 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -157,6 +157,9 @@ database "posthog" {
}
table "kafka_trace_spans_avro" {
+ settings = {
+ input_format_avro_allow_missing_fields = "1"
+ }
column "uuid" {
type = "String"
}
@@ -220,6 +223,9 @@ database "posthog" {
column "status_code" {
type = "Int32"
}
+ column "retention_days" {
+ type = "Nullable(Int32)"
+ }
engine "kafka" {
collection = "warpstream_traces"
topic_list = "clickhouse_traces"
@@ -3783,7 +3789,19 @@ SQL
to_table = "posthog.trace_spans"
query = <<SQL
SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -3795,7 +3813,11 @@ SELECT
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/roles/apm/dev/tables.hcl b/posthog/clickhouse/hcl/roles/apm/dev/tables.hcl
index fab1314164d6..bc86befc029f 100644
--- a/posthog/clickhouse/hcl/roles/apm/dev/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/apm/dev/tables.hcl
@@ -392,24 +392,6 @@ database "posthog" {
}
}
- patch_table "kafka_trace_spans_avro" {
- engine "kafka" {
- collection = "warpstream_traces"
- topic_list = "clickhouse_traces"
- group_name = "clickhouse-traces-avro"
- format = "Avro"
- num_consumers = 4
- skip_broken_messages = 100
- poll_timeout_ms = 10000
- poll_max_batch_size = 1000
- flush_interval_ms = 10000
- thread_per_consumer = true
- }
- settings = {
- input_format_avro_allow_missing_fields = "1"
- }
- }
-
# Same views the logs role runs, pointed at a different destination: the storage
# tables live on the logs nodes, so these write through the writable proxies above
# rather than straight into local tables.
diff --git a/posthog/clickhouse/hcl/roles/coshared/apm_ingest/sql/kafka_trace_spans_avro_mv.sql b/posthog/clickhouse/hcl/roles/coshared/apm_ingest/sql/kafka_trace_spans_avro_mv.sql
index 88af352654c4..5ba185ab489a 100644
--- a/posthog/clickhouse/hcl/roles/coshared/apm_ingest/sql/kafka_trace_spans_avro_mv.sql
+++ b/posthog/clickhouse/hcl/roles/coshared/apm_ingest/sql/kafka_trace_spans_avro_mv.sql
@@ -1,5 +1,17 @@
SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -11,7 +23,11 @@ SELECT
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/roles/coshared/apm_ingest/tables.hcl b/posthog/clickhouse/hcl/roles/coshared/apm_ingest/tables.hcl
index 4556acc71898..ed91e7d5e624 100644
--- a/posthog/clickhouse/hcl/roles/coshared/apm_ingest/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/coshared/apm_ingest/tables.hcl
@@ -166,6 +166,9 @@ database "posthog" {
column "status_code" {
type = "Int32"
}
+ column "retention_days" {
+ type = "Nullable(Int32)"
+ }
engine "kafka" {
collection = "warpstream_traces"
topic_list = "clickhouse_traces"
@@ -177,6 +180,9 @@ database "posthog" {
poll_max_batch_size = 1000
thread_per_consumer = true
}
+ settings = {
+ input_format_avro_allow_missing_fields = "1"
+ }
}
materialized_view "kafka_trace_spans_avro_mv" {
diff --git a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
index 08936f451cfd..af16885a54f9 100644
--- a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
@@ -597,7 +597,19 @@ SQL
patch_materialized_view "kafka_trace_spans_avro_mv" {
query = <<SQL
SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -609,7 +621,11 @@ SELECT
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/roles/logs/traces/sql/kafka_trace_spans_avro_mv.sql b/posthog/clickhouse/hcl/roles/logs/traces/sql/kafka_trace_spans_avro_mv.sql
index 88af352654c4..5ba185ab489a 100644
--- a/posthog/clickhouse/hcl/roles/logs/traces/sql/kafka_trace_spans_avro_mv.sql
+++ b/posthog/clickhouse/hcl/roles/logs/traces/sql/kafka_trace_spans_avro_mv.sql
@@ -1,5 +1,17 @@
SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -11,7 +23,11 @@ SELECT
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/sql/dev/apm.sql b/posthog/clickhouse/hcl/sql/dev/apm.sql
index 2c728cef8205..913f2cf627bb 100644
--- a/posthog/clickhouse/hcl/sql/dev/apm.sql
+++ b/posthog/clickhouse/hcl/sql/dev/apm.sql
@@ -87,8 +87,9 @@ CREATE TABLE posthog.kafka_trace_spans_avro (
dropped_events_count Int32,
links Array(String),
dropped_links_count Int32,
- status_code Int32
-) ENGINE = Kafka(warpstream_traces) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_flush_interval_ms = 10000, kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 4, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 10000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';
+ status_code Int32,
+ retention_days Nullable(Int32)
+) ENGINE = Kafka(warpstream_traces) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';
CREATE TABLE posthog.metrics4_input (
uuid String,
team_id Int32,
@@ -443,7 +444,19 @@ FROM posthog.kafka_metrics_avro
GROUP BY
_partition, _topic;
CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.writable_trace_spans (uuid String, trace_id String, span_id String, parent_span_id String, trace_state String, name String, kind Int8, flags UInt32, timestamp DateTime64(6), end_time DateTime64(6), observed_timestamp DateTime64(6), service_name String, resource_attributes Map(String, String), instrumentation_scope String, attributes_map_str Map(String, String), dropped_attributes_count UInt32, events Array(String), dropped_events_count UInt32, links Array(String), dropped_links_count UInt32, status_code Int16, 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
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -455,7 +468,11 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.writable_t
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index 3ffcaf305630..cb61c9e45048 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -68,8 +68,9 @@ CREATE TABLE posthog.kafka_trace_spans_avro (
dropped_events_count Int32,
links Array(String),
dropped_links_count Int32,
- status_code Int32
-) ENGINE = Kafka(warpstream_traces) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';
+ status_code Int32,
+ retention_days Nullable(Int32)
+) ENGINE = Kafka(warpstream_traces) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';
CREATE TABLE posthog.log_attributes (
team_id Int32 CODEC(DoubleDelta, ZSTD(1)),
time_bucket DateTime64(0) CODEC(DoubleDelta, ZSTD(1)),
@@ -1064,7 +1065,19 @@ FROM posthog.kafka_metrics_avro
GROUP BY
_partition, _topic;
CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spans (uuid String, trace_id String, span_id String, parent_span_id String, trace_state String, name String, kind Int8, flags UInt32, timestamp DateTime64(6), end_time DateTime64(6), observed_timestamp DateTime64(6), service_name String, resource_attributes Map(LowCardinality(String), String), instrumentation_scope String, attributes_map_str Map(LowCardinality(String), String), dropped_attributes_count UInt32, events Array(String), dropped_events_count UInt32, links Array(String), dropped_links_count UInt32, status_code Int16, team_id Int32, original_expiry_timestamp DateTime64(6)) AS SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -1076,7 +1089,11 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_span
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 210c9ff715fe..a00deee18cf3 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -944,8 +944,9 @@ CREATE TABLE posthog.kafka_trace_spans_avro (
dropped_events_count Int32,
links Array(String),
dropped_links_count Int32,
- status_code Int32
-) ENGINE = Kafka(warpstream_traces) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';
+ status_code Int32,
+ retention_days Nullable(Int32)
+) ENGINE = Kafka(warpstream_traces) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';
CREATE TABLE posthog.kafka_usage_report_events_preagg (
uuid UUID,
event String,
@@ -5023,7 +5024,19 @@ FROM posthog.kafka_metrics_avro
GROUP BY
_partition, _topic;
CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spans (uuid String, trace_id String, span_id String, parent_span_id String, trace_state String, name String, kind Int8, flags UInt32, timestamp DateTime64(6), end_time DateTime64(6), observed_timestamp DateTime64(6), service_name String, resource_attributes Map(LowCardinality(String), String), instrumentation_scope String, attributes_map_str Map(LowCardinality(String), String), dropped_attributes_count UInt32, events Array(String), dropped_events_count UInt32, links Array(String), dropped_links_count UInt32, status_code Int16, team_id Int32, original_expiry_timestamp DateTime64(6)) AS SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -5035,7 +5048,11 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_span
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index be5956ca83f3..a0514c036810 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -68,8 +68,9 @@ CREATE TABLE posthog.kafka_trace_spans_avro (
dropped_events_count Int32,
links Array(String),
dropped_links_count Int32,
- status_code Int32
-) ENGINE = Kafka(warpstream_traces) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 2, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';
+ status_code Int32,
+ retention_days Nullable(Int32)
+) ENGINE = Kafka(warpstream_traces) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 2, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';
CREATE TABLE posthog.log_attributes2 (
team_id Int32,
time_bucket DateTime64(0),
@@ -1039,7 +1040,19 @@ FROM posthog.kafka_metrics_avro
GROUP BY
_partition, _topic;
CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spans (uuid String, trace_id String, span_id String, parent_span_id String, trace_state String, name String, kind Int8, flags UInt32, timestamp DateTime64(6), end_time DateTime64(6), observed_timestamp DateTime64(6), service_name String, resource_attributes Map(LowCardinality(String), String), instrumentation_scope String, attributes_map_str Map(LowCardinality(String), String), dropped_attributes_count UInt32, events Array(String), dropped_events_count UInt32, links Array(String), dropped_links_count UInt32, status_code Int16, team_id Int32, original_expiry_timestamp DateTime64(6)) AS SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -1051,7 +1064,11 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_span
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index 363374b239b5..8d44bd2d5f42 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -68,8 +68,9 @@ CREATE TABLE posthog.kafka_trace_spans_avro (
dropped_events_count Int32,
links Array(String),
dropped_links_count Int32,
- status_code Int32
-) ENGINE = Kafka(warpstream_traces) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';
+ status_code Int32,
+ retention_days Nullable(Int32)
+) ENGINE = Kafka(warpstream_traces) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';
CREATE TABLE posthog.log_attributes2 (
team_id Int32,
time_bucket DateTime64(0),
@@ -1033,7 +1034,19 @@ FROM posthog.kafka_metrics_avro
GROUP BY
_partition, _topic;
CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spans (uuid String, trace_id String, span_id String, parent_span_id String, trace_state String, name String, kind Int8, flags UInt32, timestamp DateTime64(6), end_time DateTime64(6), observed_timestamp DateTime64(6), service_name String, resource_attributes Map(LowCardinality(String), String), instrumentation_scope String, attributes_map_str Map(LowCardinality(String), String), dropped_attributes_count UInt32, events Array(String), dropped_events_count UInt32, links Array(String), dropped_links_count UInt32, status_code Int16, team_id Int32, original_expiry_timestamp DateTime64(6)) AS SELECT
- * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -1045,7 +1058,11 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_span
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
observed_timestamp
+ toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ )
) AS original_expiry_timestamp,
_partition,
_topic,
diff --git a/posthog/clickhouse/migrations/0330_trace_spans_retention_days.py b/posthog/clickhouse/migrations/0330_trace_spans_retention_days.py
new file mode 100644
index 000000000000..2ba31626b34e
--- /dev/null
+++ b/posthog/clickhouse/migrations/0330_trace_spans_retention_days.py
@@ -0,0 +1,30 @@
+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.traces import KAFKA_TRACE_SPANS_AVRO_MV, KAFKA_TRACE_SPANS_AVRO_TABLE_SQL
+from posthog.run_mode import RunMode, run_mode
+
+DB = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+
+# The Kafka engine table's column set is fixed at creation and the MV's SELECT changed, so both
+# must be dropped and recreated. Neither is replicated (Kafka engine + MV), so no SYNC is needed.
+# The recreated Kafka table adds `retention_days Nullable(Int32)` and
+# `input_format_avro_allow_missing_fields = 1`; the MV now derives `original_expiry_timestamp` from
+# the per-row `retention_days` when set, falling back to the batch `retention-days` header otherwise.
+# Mirrors 0305, which did the same for logs.
+#
+# Which role hosts the pair differs by environment: the `coshared/apm_ingest` HCL layer that holds
+# them is composed by the apm role on dev, and by the logs role on prod-us, prod-eu and local-multi.
+# Role matching is exact, so targeting logs everywhere would leave dev's live pair untouched and
+# create a second, unfed one on its logs nodes. `run_mode()` is resolved here (not a raw
+# CLOUD_DEPLOYMENT check) so a test re-import under a patched deployment picks up the right branch.
+# Local single-node is unaffected either way: migration_tools collapses it to NodeRole.ALL.
+_role = NodeRole.APM if run_mode() is RunMode.CLOUD_DEV else NodeRole.LOGS
+
+operations = [
+ run_sql_with_exceptions(f"DROP TABLE IF EXISTS {DB}.kafka_trace_spans_avro_mv", node_roles=[_role]),
+ run_sql_with_exceptions(f"DROP TABLE IF EXISTS {DB}.kafka_trace_spans_avro", node_roles=[_role]),
+ run_sql_with_exceptions(KAFKA_TRACE_SPANS_AVRO_TABLE_SQL(), node_roles=[_role]),
+ run_sql_with_exceptions(KAFKA_TRACE_SPANS_AVRO_MV(), node_roles=[_role]),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 9085bc03afd1..b56505cc2a10 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0329_logs_volume_buckets_retention_ttl
+0330_trace_spans_retention_days
diff --git a/posthog/clickhouse/traces/spans.py b/posthog/clickhouse/traces/spans.py
index dd6a123abfbd..f0c47d04434c 100644
--- a/posthog/clickhouse/traces/spans.py
+++ b/posthog/clickhouse/traces/spans.py
@@ -2,6 +2,7 @@ from django.conf import settings
from posthog.clickhouse.kafka_engine import kafka_engine, kafka_num_consumers
from posthog.clickhouse.table_engines import Distributed, MergeTreeEngine, ReplicationScheme
+from posthog.run_mode import RunMode, run_mode
from .trace_attributes import (
TABLE_NAME as TRACE_ATTRIBUTES_TABLE_NAME,
@@ -285,6 +286,13 @@ def TRACE_SPAN_TO_SPAN_ATTRIBUTES2_MV():
)
+def _kafka_num_consumers() -> int:
+ # EU runs this consumer group at 2 rather than the 8 US runs, tuned against its own
+ # partition count. Recreating the table re-applies whatever this returns, so a plain
+ # kafka_num_consumers(8) here would quietly raise EU to 8 and leave consumers idle.
+ return 2 if run_mode() is RunMode.CLOUD_EU else kafka_num_consumers(8)
+
+
def KAFKA_TRACE_SPANS_AVRO_TABLE_SQL():
return f"""
CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{KAFKA_TABLE_NAME}
@@ -309,15 +317,19 @@ CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{KAFKA_TA
`dropped_events_count` Int32,
`links` Array(String),
`dropped_links_count` Int32,
- `status_code` Int32
+ `status_code` Int32,
+ `retention_days` Nullable(Int32)
)
ENGINE = {kafka_engine(topic=KAFKA_TOPIC, group=KAFKA_GROUP, serialization="Avro", named_collection=KAFKA_NAMED_COLLECTION)}
SETTINGS
kafka_skip_broken_messages = 100,
kafka_thread_per_consumer = 1,
- kafka_num_consumers = {kafka_num_consumers(8)},
+ kafka_num_consumers = {_kafka_num_consumers()},
kafka_poll_timeout_ms = 3000,
- kafka_poll_max_batch_size = 1000
+ kafka_poll_max_batch_size = 1000,
+ -- capture-logs writes `retention_days` only after its Avro schema ships, so payloads
+ -- produced before that must still decode.
+ input_format_avro_allow_missing_fields = 1
"""
@@ -351,7 +363,19 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{KAFKA_TABLE_NAME}_mv TO {db}.{TABLE
`original_expiry_timestamp` DateTime64(6)
)
AS SELECT
- * EXCEPT (attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ uuid,
+ trace_id,
+ span_id,
+ parent_span_id,
+ trace_state,
+ name,
+ timestamp,
+ end_time,
+ observed_timestamp,
+ service_name,
+ instrumentation_scope,
+ events,
+ links,
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
toUInt32(dropped_attributes_count) AS dropped_attributes_count,
@@ -361,7 +385,7 @@ AS SELECT
mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
- observed_timestamp + toIntervalDay(toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))) AS original_expiry_timestamp,
+ observed_timestamp + toIntervalDay(if((retention_days IS NOT NULL) AND (retention_days > 0), retention_days, toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15)))) AS original_expiry_timestamp,
_partition,
_topic,
_offset,