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(llma): add clickhouse migration for tracing tables (#70737)

Frank Hamande794f24eface
diff --git a/docker/clickhouse/config.d/default.xml b/docker/clickhouse/config.d/default.xml
index 8acbbf28aeed..235bde0d8fee 100644
--- a/docker/clickhouse/config.d/default.xml
+++ b/docker/clickhouse/config.d/default.xml
@@ -99,6 +99,9 @@
<warpstream_logs>
<kafka_broker_list from_env="KAFKA_HOSTS"/>
</warpstream_logs>
+ <warpstream_traces>
+ <kafka_broker_list from_env="KAFKA_HOSTS"/>
+ </warpstream_traces>
</named_collections>
<macros>
diff --git a/docker/clickhouse/config.d/multinode/ai_events_node.xml b/docker/clickhouse/config.d/multinode/ai_events_node.xml
index 677773d50398..3dc79aa1e343 100644
--- a/docker/clickhouse/config.d/multinode/ai_events_node.xml
+++ b/docker/clickhouse/config.d/multinode/ai_events_node.xml
@@ -138,6 +138,9 @@
<warpstream_logs>
<kafka_broker_list from_env="KAFKA_HOSTS"/>
</warpstream_logs>
+ <warpstream_traces>
+ <kafka_broker_list from_env="KAFKA_HOSTS"/>
+ </warpstream_traces>
</named_collections>
<macros>
diff --git a/docker/clickhouse/config.d/multinode/aux_node.xml b/docker/clickhouse/config.d/multinode/aux_node.xml
index 0ebacd0eb02f..c92a3f9230e3 100644
--- a/docker/clickhouse/config.d/multinode/aux_node.xml
+++ b/docker/clickhouse/config.d/multinode/aux_node.xml
@@ -138,6 +138,9 @@
<warpstream_logs>
<kafka_broker_list from_env="KAFKA_HOSTS"/>
</warpstream_logs>
+ <warpstream_traces>
+ <kafka_broker_list from_env="KAFKA_HOSTS"/>
+ </warpstream_traces>
</named_collections>
<macros>
diff --git a/docker/clickhouse/config.d/multinode/data_node.xml b/docker/clickhouse/config.d/multinode/data_node.xml
index 8f0ad99e25b1..51f0fbfaa55d 100644
--- a/docker/clickhouse/config.d/multinode/data_node.xml
+++ b/docker/clickhouse/config.d/multinode/data_node.xml
@@ -138,6 +138,9 @@
<warpstream_logs>
<kafka_broker_list from_env="KAFKA_HOSTS"/>
</warpstream_logs>
+ <warpstream_traces>
+ <kafka_broker_list from_env="KAFKA_HOSTS"/>
+ </warpstream_traces>
</named_collections>
<macros>
diff --git a/docker/clickhouse/config.d/multinode/logs_node.xml b/docker/clickhouse/config.d/multinode/logs_node.xml
index 443d40982ad3..210f1638c4f7 100644
--- a/docker/clickhouse/config.d/multinode/logs_node.xml
+++ b/docker/clickhouse/config.d/multinode/logs_node.xml
@@ -138,6 +138,9 @@
<warpstream_logs>
<kafka_broker_list from_env="KAFKA_HOSTS"/>
</warpstream_logs>
+ <warpstream_traces>
+ <kafka_broker_list from_env="KAFKA_HOSTS"/>
+ </warpstream_traces>
</named_collections>
<macros>
diff --git a/docker/clickhouse/config.d/multinode/ops_node.xml b/docker/clickhouse/config.d/multinode/ops_node.xml
index 8bad6639e632..6c2c1ae6e4c5 100644
--- a/docker/clickhouse/config.d/multinode/ops_node.xml
+++ b/docker/clickhouse/config.d/multinode/ops_node.xml
@@ -138,6 +138,9 @@
<warpstream_logs>
<kafka_broker_list from_env="KAFKA_HOSTS"/>
</warpstream_logs>
+ <warpstream_traces>
+ <kafka_broker_list from_env="KAFKA_HOSTS"/>
+ </warpstream_traces>
</named_collections>
<macros>
diff --git a/docker/clickhouse/config.d/multinode/sessions_node.xml b/docker/clickhouse/config.d/multinode/sessions_node.xml
index 4bd857eedb73..3fe620f04ad2 100644
--- a/docker/clickhouse/config.d/multinode/sessions_node.xml
+++ b/docker/clickhouse/config.d/multinode/sessions_node.xml
@@ -138,6 +138,9 @@
<warpstream_logs>
<kafka_broker_list from_env="KAFKA_HOSTS"/>
</warpstream_logs>
+ <warpstream_traces>
+ <kafka_broker_list from_env="KAFKA_HOSTS"/>
+ </warpstream_traces>
</named_collections>
<macros>
diff --git a/posthog/clickhouse/hcl/duplicates-baseline.txt b/posthog/clickhouse/hcl/duplicates-baseline.txt
index 8d1ca4a51be6..c2fa359309f9 100644
--- a/posthog/clickhouse/hcl/duplicates-baseline.txt
+++ b/posthog/clickhouse/hcl/duplicates-baseline.txt
@@ -66,6 +66,8 @@ posthog.trace_span_to_resource_attributes
posthog.trace_span_to_span_attributes
posthog.trace_spans
posthog.trace_spans_distributed
+posthog.trace_spans_kafka_metrics
+posthog.trace_spans_to_kafka_metrics_mv
posthog.web_bot_definition
posthog.web_bot_definition_dict
posthog.web_bounces_dimensional_preaggregated
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index e29b47c89d11..07ba051e46c1 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -55,6 +55,83 @@ database "posthog" {
}
}
+ table "kafka_trace_spans_avro" {
+ column "uuid" {
+ type = "String"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "String"
+ }
+ column "kind" {
+ type = "Int32"
+ }
+ column "flags" {
+ type = "Int32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "dropped_attributes_count" {
+ type = "Int32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "dropped_events_count" {
+ type = "Int32"
+ }
+ column "links" {
+ type = "Array(String)"
+ }
+ column "dropped_links_count" {
+ type = "Int32"
+ }
+ column "status_code" {
+ type = "Int32"
+ }
+ engine "kafka" {
+ broker_list = "warpstream_traces"
+ topic_list = "kafka_topic_list = 'clickhouse_traces'"
+ group_name = "kafka_group_name = 'clickhouse-traces-avro'"
+ format = "kafka_format = 'Avro'"
+ num_consumers = 8
+ skip_broken_messages = 100
+ poll_timeout_ms = 3000
+ poll_max_batch_size = 1000
+ thread_per_consumer = true
+ }
+ }
+
table "log_attributes" {
order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
partition_by = "toDate(original_expiry_time_bucket)"
@@ -1599,180 +1676,1062 @@ SQL
}
}
- materialized_view "kafka_logs34_avro_mv" {
- to_table = "posthog.logs34"
- query = <<SQL
-SELECT
- kafka_logs_avro.* EXCEPT(created_at, attribute_values, attribute_keys, attributes, attributes_map_str, attributes_map_float, attributes_map_datetime, resource_attributes, bytes_uncompressed),
- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
- observed_timestamp
- + toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
- ) AS original_expiry_timestamp,
- _partition,
- _topic,
- _offset,
- toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,
- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,
- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed
-FROM posthog.kafka_logs_avro
-SQL
-
- column "uuid" {
- type = "String"
+ table "trace_attributes" {
+ order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
}
- column "trace_id" {
- type = "String"
+ column "team_id" {
+ type = "Int32"
}
- column "span_id" {
- type = "String"
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
}
- column "trace_flags" {
- type = "Int32"
+ column "time_bucket" {
+ type = "DateTime64(0)"
}
- column "timestamp" {
- type = "DateTime64(6)"
+ column "service_name" {
+ type = "LowCardinality(String)"
}
- column "observed_timestamp" {
- type = "DateTime64(6)"
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
}
- column "body" {
- type = "String"
+ column "attribute_key" {
+ type = "LowCardinality(String)"
}
- column "severity_text" {
+ column "attribute_value" {
type = "String"
}
- column "severity_number" {
- type = "Int32"
+ column "attribute_type" {
+ type = "LowCardinality(String)"
}
- column "service_name" {
- type = "String"
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
}
- column "instrumentation_scope" {
- type = "String"
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 4
}
- column "event_name" {
- type = "String"
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 4
}
- column "attributes_map_str" {
- type = "Map(String, String)"
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
}
- column "resource_attributes" {
- type = "Map(String, String)"
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_attributes"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "trace_attributes2" {
+ order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
}
column "team_id" {
type = "Int32"
}
- column "original_expiry_timestamp" {
- type = "DateTime64(6)"
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
}
- column "_partition" {
- type = "UInt64"
+ column "time_bucket" {
+ type = "DateTime64(0)"
}
- column "_topic" {
+ column "service_name" {
type = "LowCardinality(String)"
}
- column "_offset" {
- type = "UInt64"
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
}
- column "_record_count" {
- type = "Int64"
+ column "attribute_key" {
+ type = "LowCardinality(String)"
}
- column "_bytes_uncompressed" {
- type = "Nullable(Int64)"
+ column "attribute_value" {
+ type = "String"
}
- column "_bytes_compressed" {
- type = "Nullable(Int64)"
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_attributes2"
+ replica_name = "{replica}-{shard}"
}
}
- materialized_view "kafka_logs_avro_billing_metrics_mv" {
- to_table = "posthog.logs_billing_metrics"
- query = <<SQL
-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 posthog.logs34
- )
-GROUP BY
- team_id, time_bucket, service_name
-SQL
-
+ table "trace_attributes_distributed" {
column "team_id" {
type = "Int32"
}
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
column "time_bucket" {
- type = "DateTime"
+ type = "DateTime64(0)"
}
column "service_name" {
type = "LowCardinality(String)"
}
- column "bytes_uncompressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
}
- column "bytes_compressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
+ column "attribute_key" {
+ type = "LowCardinality(String)"
}
- column "record_count" {
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ remote_database = "posthog"
+ remote_table = "trace_attributes"
+ }
}
- materialized_view "kafka_logs_avro_kafka_metrics_mv" {
- to_table = "posthog.logs_kafka_metrics"
- query = <<SQL
-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 posthog.logs34
+ table "trace_spans" {
+ order_by = ["team_id", "time_bucket", "service_name", "resource_fingerprint", "status_code", "name", "timestamp"]
+ partition_by = "toDate(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ allow_part_offset_column_in_projections = "1"
+ index_granularity = "8192"
+ index_granularity_bytes = "104857600"
+ map_serialization_version = "with_buckets"
+ ttl_only_drop_parts = "1"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfInterval(timestamp, toIntervalHour(4))"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "uuid" {
+ type = "String"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "is_root_span" {
+ type = "Bool"
+ materialized = "replaceAll(trimRight(parent_span_id, '='), 'A', '') = ''"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "LowCardinality(String)"
+ }
+ column "kind" {
+ type = "Int8"
+ }
+ column "flags" {
+ type = "UInt32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "created_at" {
+ type = "DateTime64(6)"
+ materialized = "now()"
+ }
+ column "duration_nano" {
+ type = "UInt64"
+ materialized = "toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000"
+ }
+ column "status_code" {
+ type = "Int16"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"
+ }
+ column "attributes_map_float" {
+ type = "Map(LowCardinality(String), Float64)"
+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str))"
+ }
+ column "attributes_map_datetime" {
+ type = "Map(LowCardinality(String), DateTime64(6))"
+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"
+ }
+ column "dropped_attributes_count" {
+ type = "UInt32"
+ }
+ column "dropped_events_count" {
+ type = "UInt32"
+ }
+ column "dropped_links_count" {
+ type = "UInt32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "links" {
+ type = "Array(String)"
+ }
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_bytes_uncompressed" {
+ type = "UInt64"
+ }
+ column "_bytes_compressed" {
+ type = "UInt64"
+ }
+ column "_record_count" {
+ type = "UInt64"
+ }
+ index "idx_name" {
+ expr = "name"
+ type = "ngrambf_v1(4, 5000, 2, 0)"
+ granularity = 16
+ }
+ index "idx_kind" {
+ expr = "kind"
+ type = "minmax"
+ granularity = 4
+ }
+ index "idx_duration" {
+ expr = "duration_nano"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_status_code" {
+ expr = "status_code"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_timestamp_minmax" {
+ expr = "timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_observed_minmax" {
+ expr = "observed_timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_attributes_str_keys" {
+ expr = "mapKeys(attributes_map_str)"
+ type = "bloom_filter(0.01)"
+ granularity = 16
+ }
+ index "idx_attributes_str_values" {
+ expr = "mapValues(attributes_map_str)"
+ type = "bloom_filter(0.001)"
+ granularity = 16
+ }
+ index "idx_trace_bloom_part" {
+ expr = "trace_id"
+ type = "bloom_filter(0.00001)"
+ granularity = 99999
+ }
+ index "idx_span_id_bloom_part" {
+ expr = "span_id"
+ type = "bloom_filter(0.00001)"
+ granularity = 99999
+ }
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
GROUP BY
- _partition, _topic
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
+SQL
+
+ }
+ projection "projection_index_span_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY span_id
+SQL
+
+ }
+ projection "projection_index_trace_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY trace_id
SQL
+ }
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "trace_spans_distributed" {
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfInterval(timestamp, toIntervalHour(4))"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "uuid" {
+ type = "String"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "is_root_span" {
+ type = "Bool"
+ materialized = "replaceAll(trimRight(parent_span_id, '='), 'A', '') = ''"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "LowCardinality(String)"
+ }
+ column "kind" {
+ type = "Int8"
+ }
+ column "flags" {
+ type = "UInt32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "created_at" {
+ type = "DateTime64(6)"
+ materialized = "now()"
+ }
+ column "duration_nano" {
+ type = "UInt64"
+ materialized = "toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000"
+ }
+ column "status_code" {
+ type = "Int16"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"
+ }
+ column "attributes_map_float" {
+ type = "Map(LowCardinality(String), Float64)"
+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str))"
+ }
+ column "attributes_map_datetime" {
+ type = "Map(LowCardinality(String), DateTime64(6))"
+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"
+ }
+ column "dropped_attributes_count" {
+ type = "UInt32"
+ }
+ column "dropped_events_count" {
+ type = "UInt32"
+ }
+ column "dropped_links_count" {
+ type = "UInt32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "links" {
+ type = "Array(String)"
+ }
column "_partition" {
type = "UInt32"
}
- column "_topic" {
- type = "String"
+ column "_topic" {
+ type = "String"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_bytes_uncompressed" {
+ type = "UInt64"
+ }
+ column "_bytes_compressed" {
+ type = "UInt64"
+ }
+ column "_record_count" {
+ type = "UInt64"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ remote_database = "posthog"
+ remote_table = "trace_spans"
+ }
+ }
+
+ table "trace_spans_kafka_metrics" {
+ order_by = ["_topic", "_partition"]
+ settings = {
+ index_granularity = "8192"
+ }
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans_kafka_metrics"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ materialized_view "kafka_logs34_avro_mv" {
+ to_table = "posthog.logs34"
+ query = <<SQL
+SELECT
+ kafka_logs_avro.* EXCEPT(created_at, attribute_values, attribute_keys, attributes, attributes_map_str, attributes_map_float, attributes_map_datetime, resource_attributes, bytes_uncompressed),
+ mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
+ observed_timestamp
+ + toIntervalDay(
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ ) AS original_expiry_timestamp,
+ _partition,
+ _topic,
+ _offset,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,
+ toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,
+ toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed
+FROM posthog.kafka_logs_avro
+SQL
+
+ column "uuid" {
+ type = "String"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "trace_flags" {
+ type = "Int32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "body" {
+ type = "String"
+ }
+ column "severity_text" {
+ type = "String"
+ }
+ column "severity_number" {
+ type = "Int32"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "event_name" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(String, String)"
+ }
+ column "resource_attributes" {
+ type = "Map(String, String)"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "_partition" {
+ type = "UInt64"
+ }
+ column "_topic" {
+ type = "LowCardinality(String)"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_record_count" {
+ type = "Int64"
+ }
+ column "_bytes_uncompressed" {
+ type = "Nullable(Int64)"
+ }
+ column "_bytes_compressed" {
+ type = "Nullable(Int64)"
+ }
+ }
+
+ materialized_view "kafka_logs_avro_billing_metrics_mv" {
+ to_table = "posthog.logs_billing_metrics"
+ query = <<SQL
+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 posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "bytes_uncompressed" {
+ type = "SimpleAggregateFunction(sum, Float64)"
+ }
+ column "bytes_compressed" {
+ type = "SimpleAggregateFunction(sum, Float64)"
+ }
+ column "record_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "kafka_logs_avro_kafka_metrics_mv" {
+ to_table = "posthog.logs_kafka_metrics"
+ query = <<SQL
+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 posthog.logs34
+GROUP BY
+ _partition, _topic
+SQL
+
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime)"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, Decimal(18, 6))"
+ }
+ }
+
+ materialized_view "kafka_trace_spans_avro_mv" {
+ 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),
+ toInt8(kind) AS kind,
+ toUInt32(flags) AS flags,
+ toUInt32(dropped_attributes_count) AS dropped_attributes_count,
+ toUInt32(dropped_events_count) AS dropped_events_count,
+ toUInt32(dropped_links_count) AS dropped_links_count,
+ toInt16(status_code) AS status_code,
+ 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,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')], toInt64(0)) AS _bytes_uncompressed,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'bytes_compressed')], toInt64(0)) AS _bytes_compressed
+FROM posthog.kafka_trace_spans_avro
+SQL
+
+ column "uuid" {
+ type = "String"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "String"
+ }
+ column "kind" {
+ type = "Int8"
+ }
+ column "flags" {
+ type = "UInt32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "dropped_attributes_count" {
+ type = "UInt32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "dropped_events_count" {
+ type = "UInt32"
+ }
+ column "links" {
+ type = "Array(String)"
+ }
+ column "dropped_links_count" {
+ type = "UInt32"
+ }
+ column "status_code" {
+ type = "Int16"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+
+ materialized_view "logs32_to_log_attributes" {
+ to_table = "posthog.log_attributes"
+ query = <<SQL
+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.logs32
+ GROUP BY
+ team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "logs32_to_resource_attributes" {
+ to_table = "posthog.log_attributes"
+ query = <<SQL
+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.logs32
+ GROUP BY
+ team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, resource_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "logs34_to_log_attributes" {
+ to_table = "posthog.log_attributes2"
+ query = <<SQL
+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.logs34
+ GROUP BY
+ team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "logs34_to_log_attributes3" {
+ to_table = "posthog.log_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ severity_text,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ severity_text AS severity_text,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS attributes,
+ arrayJoin(attributes) AS attribute,
+ 'log' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.logs34
+ GROUP BY
+ team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
}
- column "max_offset" {
- type = "SimpleAggregateFunction(max, UInt64)"
+ column "resource_fingerprint" {
+ type = "UInt64"
}
- column "max_observed_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(6))"
+ column "attribute_key" {
+ type = "LowCardinality(String)"
}
- column "max_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(6))"
+ column "attribute_value" {
+ type = "String"
}
- column "max_created_at" {
- type = "SimpleAggregateFunction(max, DateTime)"
+ column "attribute_type" {
+ type = "LowCardinality(String)"
}
- column "max_lag" {
- type = "SimpleAggregateFunction(max, Decimal(18, 6))"
+ column "severity_text" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
}
}
- materialized_view "logs32_to_log_attributes" {
- to_table = "posthog.log_attributes"
+ materialized_view "logs34_to_resource_attributes" {
+ to_table = "posthog.log_attributes2"
query = <<SQL
SELECT
team_id,
@@ -1792,15 +2751,14 @@ FROM
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,
+ 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.logs32
+ FROM posthog.logs34
GROUP BY
- team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, attributes
+ team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, resource_attributes
)
SQL
@@ -1833,8 +2791,8 @@ SQL
}
}
- materialized_view "logs32_to_resource_attributes" {
- to_table = "posthog.log_attributes"
+ materialized_view "logs34_to_resource_attributes3" {
+ to_table = "posthog.log_attributes3"
query = <<SQL
SELECT
team_id,
@@ -1845,6 +2803,7 @@ SELECT
attribute_key,
attribute_value,
attribute_type,
+ severity_text,
attribute_count
FROM
(
@@ -1854,14 +2813,15 @@ FROM
toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
service_name AS service_name,
resource_fingerprint,
+ severity_text AS severity_text,
arrayJoin(resource_attributes) AS attribute,
'resource' AS attribute_type,
attribute.1 AS attribute_key,
attribute.2 AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.logs32
+ FROM posthog.logs34
GROUP BY
- team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, resource_attributes
+ team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, resource_attributes
)
SQL
@@ -1889,51 +2849,52 @@ SQL
column "attribute_type" {
type = "LowCardinality(String)"
}
+ column "severity_text" {
+ type = "LowCardinality(String)"
+ }
column "attribute_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
}
- materialized_view "logs34_to_log_attributes" {
- to_table = "posthog.log_attributes2"
+ materialized_view "trace_span_to_attributes" {
+ to_table = "posthog.trace_attributes"
query = <<SQL
SELECT
team_id,
- time_bucket,
original_expiry_time_bucket,
+ time_bucket,
service_name,
resource_fingerprint,
attribute_key,
attribute_value,
- attribute_type,
+ 'span_attribute' AS 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,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS 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,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
attribute.1 AS attribute_key,
attribute.2 AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.logs34
+ FROM posthog.trace_spans
GROUP BY
- team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, attributes
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
)
SQL
column "team_id" {
type = "Int32"
}
- column "time_bucket" {
+ column "original_expiry_time_bucket" {
type = "DateTime64(0)"
}
- column "original_expiry_time_bucket" {
+ column "time_bucket" {
type = "DateTime64(0)"
}
column "service_name" {
@@ -1956,48 +2917,44 @@ SQL
}
}
- materialized_view "logs34_to_log_attributes3" {
- to_table = "posthog.log_attributes3"
+ materialized_view "trace_span_to_attributes2" {
+ to_table = "posthog.trace_attributes2"
query = <<SQL
SELECT
team_id,
- time_bucket,
original_expiry_time_bucket,
+ time_bucket,
service_name,
resource_fingerprint,
attribute_key,
attribute_value,
- attribute_type,
- severity_text,
+ 'span_attribute' AS 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,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
service_name AS service_name,
resource_fingerprint,
- severity_text AS severity_text,
- mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS attributes,
- arrayJoin(attributes) AS attribute,
- 'log' AS attribute_type,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
attribute.1 AS attribute_key,
attribute.2 AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.logs34
+ FROM posthog.trace_spans
GROUP BY
- team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, attributes
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
)
SQL
column "team_id" {
type = "Int32"
}
- column "time_bucket" {
+ column "original_expiry_time_bucket" {
type = "DateTime64(0)"
}
- column "original_expiry_time_bucket" {
+ column "time_bucket" {
type = "DateTime64(0)"
}
column "service_name" {
@@ -2015,53 +2972,49 @@ SQL
column "attribute_type" {
type = "LowCardinality(String)"
}
- column "severity_text" {
- type = "LowCardinality(String)"
- }
column "attribute_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
}
- materialized_view "logs34_to_resource_attributes" {
- to_table = "posthog.log_attributes2"
+ materialized_view "trace_span_to_resource_attributes" {
+ to_table = "posthog.trace_attributes"
query = <<SQL
SELECT
team_id,
- time_bucket,
original_expiry_time_bucket,
+ time_bucket,
service_name,
resource_fingerprint,
attribute_key,
attribute_value,
- attribute_type,
+ 'span_resource_attribute' AS 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,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS 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.logs34
+ FROM posthog.trace_spans
GROUP BY
- team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, resource_attributes
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
)
SQL
column "team_id" {
type = "Int32"
}
- column "time_bucket" {
+ column "original_expiry_time_bucket" {
type = "DateTime64(0)"
}
- column "original_expiry_time_bucket" {
+ column "time_bucket" {
type = "DateTime64(0)"
}
column "service_name" {
@@ -2084,49 +3037,105 @@ SQL
}
}
- materialized_view "logs34_to_resource_attributes3" {
- to_table = "posthog.log_attributes3"
+ materialized_view "trace_span_to_resource_attributes2" {
+ to_table = "posthog.trace_attributes2"
query = <<SQL
SELECT
team_id,
- time_bucket,
original_expiry_time_bucket,
+ time_bucket,
service_name,
resource_fingerprint,
attribute_key,
attribute_value,
- attribute_type,
- severity_text,
+ 'span_resource_attribute' AS 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,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
service_name AS service_name,
resource_fingerprint,
- severity_text AS severity_text,
arrayJoin(resource_attributes) AS attribute,
- 'resource' AS attribute_type,
attribute.1 AS attribute_key,
attribute.2 AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.logs34
+ FROM posthog.trace_spans
GROUP BY
- team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, resource_attributes
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
)
SQL
column "team_id" {
type = "Int32"
}
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
column "time_bucket" {
type = "DateTime64(0)"
}
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "trace_span_to_span_attributes" {
+ to_table = "posthog.trace_attributes"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
column "original_expiry_time_bucket" {
type = "DateTime64(0)"
}
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
column "service_name" {
type = "LowCardinality(String)"
}
@@ -2142,11 +3151,106 @@ SQL
column "attribute_type" {
type = "LowCardinality(String)"
}
- column "severity_text" {
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "trace_span_to_span_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
type = "LowCardinality(String)"
}
column "attribute_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
}
+
+ materialized_view "trace_spans_to_kafka_metrics_mv" {
+ to_table = "posthog.trace_spans_kafka_metrics"
+ query = <<SQL
+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 posthog.trace_spans
+GROUP BY
+ _partition, _topic
+SQL
+
+ column "_partition" {
+ type = "UInt64"
+ }
+ column "_topic" {
+ type = "LowCardinality(String)"
+ }
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime)"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, Decimal(18, 6))"
+ }
+ }
}
diff --git a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
index ba409c00a93d..7e3fb1f838ae 100644
--- a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
@@ -2123,4 +2123,1094 @@ SQL
type = "SimpleAggregateFunction(sum, UInt64)"
}
}
+
+ table "trace_spans" {
+ order_by = ["team_id", "time_bucket", "service_name", "resource_fingerprint", "status_code", "name", "timestamp"]
+ partition_by = "toDate(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ allow_part_offset_column_in_projections = "1"
+ index_granularity = "8192"
+ index_granularity_bytes = "104857600"
+ map_serialization_version = "with_buckets"
+ ttl_only_drop_parts = "1"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfInterval(timestamp, toIntervalHour(4))"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "uuid" {
+ type = "String"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "is_root_span" {
+ type = "Bool"
+ materialized = "replaceAll(trimRight(parent_span_id, '='), 'A', '') = ''"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "LowCardinality(String)"
+ }
+ column "kind" {
+ type = "Int8"
+ }
+ column "flags" {
+ type = "UInt32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "created_at" {
+ type = "DateTime64(6)"
+ materialized = "now()"
+ }
+ column "duration_nano" {
+ type = "UInt64"
+ materialized = "toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000"
+ }
+ column "status_code" {
+ type = "Int16"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"
+ }
+ column "attributes_map_float" {
+ type = "Map(LowCardinality(String), Float64)"
+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str))"
+ }
+ column "attributes_map_datetime" {
+ type = "Map(LowCardinality(String), DateTime64(6))"
+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"
+ }
+ column "dropped_attributes_count" {
+ type = "UInt32"
+ }
+ column "dropped_events_count" {
+ type = "UInt32"
+ }
+ column "dropped_links_count" {
+ type = "UInt32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "links" {
+ type = "Array(String)"
+ }
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_bytes_uncompressed" {
+ type = "UInt64"
+ }
+ column "_bytes_compressed" {
+ type = "UInt64"
+ }
+ column "_record_count" {
+ type = "UInt64"
+ }
+ index "idx_name" {
+ expr = "name"
+ type = "ngrambf_v1(4, 5000, 2, 0)"
+ granularity = 16
+ }
+ index "idx_kind" {
+ expr = "kind"
+ type = "minmax"
+ granularity = 4
+ }
+ index "idx_duration" {
+ expr = "duration_nano"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_status_code" {
+ expr = "status_code"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_timestamp_minmax" {
+ expr = "timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_observed_minmax" {
+ expr = "observed_timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_attributes_str_keys" {
+ expr = "mapKeys(attributes_map_str)"
+ type = "bloom_filter(0.01)"
+ granularity = 16
+ }
+ index "idx_attributes_str_values" {
+ expr = "mapValues(attributes_map_str)"
+ type = "bloom_filter(0.001)"
+ granularity = 16
+ }
+ index "idx_trace_bloom_part" {
+ expr = "trace_id"
+ type = "bloom_filter(0.00001)"
+ granularity = 99999
+ }
+ index "idx_span_id_bloom_part" {
+ expr = "span_id"
+ type = "bloom_filter(0.00001)"
+ granularity = 99999
+ }
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
+SQL
+
+ }
+ projection "projection_index_span_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY span_id
+SQL
+
+ }
+ projection "projection_index_trace_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY trace_id
+SQL
+
+ }
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+ table "trace_attributes" {
+ order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_attributes"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+ table "trace_attributes2" {
+ order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_attributes2"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+ table "trace_spans_kafka_metrics" {
+ order_by = ["_topic", "_partition"]
+ settings = {
+ index_granularity = "8192"
+ }
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans_kafka_metrics"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+ table "trace_spans_distributed" {
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfInterval(timestamp, toIntervalHour(4))"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "uuid" {
+ type = "String"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "is_root_span" {
+ type = "Bool"
+ materialized = "replaceAll(trimRight(parent_span_id, '='), 'A', '') = ''"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "LowCardinality(String)"
+ }
+ column "kind" {
+ type = "Int8"
+ }
+ column "flags" {
+ type = "UInt32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "created_at" {
+ type = "DateTime64(6)"
+ materialized = "now()"
+ }
+ column "duration_nano" {
+ type = "UInt64"
+ materialized = "toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000"
+ }
+ column "status_code" {
+ type = "Int16"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"
+ }
+ column "attributes_map_float" {
+ type = "Map(LowCardinality(String), Float64)"
+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str))"
+ }
+ column "attributes_map_datetime" {
+ type = "Map(LowCardinality(String), DateTime64(6))"
+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"
+ }
+ column "dropped_attributes_count" {
+ type = "UInt32"
+ }
+ column "dropped_events_count" {
+ type = "UInt32"
+ }
+ column "dropped_links_count" {
+ type = "UInt32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "links" {
+ type = "Array(String)"
+ }
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_bytes_uncompressed" {
+ type = "UInt64"
+ }
+ column "_bytes_compressed" {
+ type = "UInt64"
+ }
+ column "_record_count" {
+ type = "UInt64"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ remote_database = "posthog"
+ remote_table = "trace_spans"
+ }
+ }
+ table "trace_attributes_distributed" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ remote_database = "posthog"
+ remote_table = "trace_attributes"
+ }
+ }
+ table "kafka_trace_spans_avro" {
+ column "uuid" {
+ type = "String"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "String"
+ }
+ column "kind" {
+ type = "Int32"
+ }
+ column "flags" {
+ type = "Int32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "dropped_attributes_count" {
+ type = "Int32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "dropped_events_count" {
+ type = "Int32"
+ }
+ column "links" {
+ type = "Array(String)"
+ }
+ column "dropped_links_count" {
+ type = "Int32"
+ }
+ column "status_code" {
+ type = "Int32"
+ }
+ engine "kafka" {
+ broker_list = "warpstream_traces"
+ topic_list = "kafka_topic_list = 'clickhouse_traces'"
+ group_name = "kafka_group_name = 'clickhouse-traces-avro'"
+ format = "kafka_format = 'Avro'"
+ num_consumers = 8
+ skip_broken_messages = 100
+ poll_timeout_ms = 3000
+ poll_max_batch_size = 1000
+ thread_per_consumer = true
+ }
+ }
+ materialized_view "trace_span_to_attributes" {
+ to_table = "posthog.trace_attributes"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+ materialized_view "trace_span_to_resource_attributes" {
+ to_table = "posthog.trace_attributes"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_resource_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+ materialized_view "trace_span_to_span_attributes" {
+ to_table = "posthog.trace_attributes"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+ materialized_view "trace_span_to_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+ materialized_view "trace_span_to_resource_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_resource_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+ materialized_view "trace_span_to_span_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+ materialized_view "kafka_trace_spans_avro_mv" {
+ 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),
+ toInt8(kind) AS kind,
+ toUInt32(flags) AS flags,
+ toUInt32(dropped_attributes_count) AS dropped_attributes_count,
+ toUInt32(dropped_events_count) AS dropped_events_count,
+ toUInt32(dropped_links_count) AS dropped_links_count,
+ toInt16(status_code) AS status_code,
+ 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,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')], toInt64(0)) AS _bytes_uncompressed,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'bytes_compressed')], toInt64(0)) AS _bytes_compressed
+FROM posthog.kafka_trace_spans_avro
+SQL
+
+ column "uuid" {
+ type = "String"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "String"
+ }
+ column "kind" {
+ type = "Int8"
+ }
+ column "flags" {
+ type = "UInt32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "dropped_attributes_count" {
+ type = "UInt32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "dropped_events_count" {
+ type = "UInt32"
+ }
+ column "links" {
+ type = "Array(String)"
+ }
+ column "dropped_links_count" {
+ type = "UInt32"
+ }
+ column "status_code" {
+ type = "Int16"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+ materialized_view "trace_spans_to_kafka_metrics_mv" {
+ to_table = "posthog.trace_spans_kafka_metrics"
+ query = <<SQL
+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 posthog.trace_spans
+GROUP BY
+ _partition, _topic
+SQL
+
+ column "_partition" {
+ type = "UInt64"
+ }
+ column "_topic" {
+ type = "LowCardinality(String)"
+ }
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime)"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, Decimal(18, 6))"
+ }
+ }
}
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index b3c8ff0dd950..80ba081cd7e4 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -17,6 +17,29 @@ CREATE TABLE posthog.kafka_logs_avro (
event_name String,
attributes Map(LowCardinality(String), String)
) ENGINE = Kafka() SETTINGS kafka_broker_list = 'warpstream_logs', kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = 'kafka_group_name = \'clickhouse-logs-avro-new\'', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'kafka_topic_list = \'clickhouse_logs\'';
+CREATE TABLE posthog.kafka_trace_spans_avro (
+ uuid String,
+ trace_id String,
+ span_id String,
+ parent_span_id String,
+ trace_state String,
+ name String,
+ kind Int32,
+ flags Int32,
+ 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(LowCardinality(String), String),
+ dropped_attributes_count Int32,
+ events Array(String),
+ dropped_events_count Int32,
+ links Array(String),
+ dropped_links_count Int32,
+ status_code Int32
+) ENGINE = Kafka() SETTINGS kafka_broker_list = 'warpstream_traces', kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = '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 = '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)),
@@ -369,6 +392,158 @@ CREATE TABLE posthog.query_log_archive (
lc_dagster__owner String ALIAS CAST(log_comment.`dagster.tags.owner`, 'String'),
lc_modifiers String ALIAS if(is_initial_query, JSONExtractRaw(toString(log_comment), 'modifiers'), '')
) ENGINE = Distributed('ops', 'posthog', 'sharded_query_log_archive');
+CREATE TABLE posthog.trace_attributes (
+ team_id Int32,
+ original_expiry_time_bucket DateTime64(0),
+ time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ resource_fingerprint UInt64 DEFAULT 0,
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_attributes', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.trace_attributes2 (
+ team_id Int32,
+ original_expiry_time_bucket DateTime64(0),
+ time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ resource_fingerprint UInt64 DEFAULT 0,
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.trace_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.trace_attributes_distributed (
+ team_id Int32,
+ original_expiry_time_bucket DateTime64(0),
+ time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ resource_fingerprint UInt64 DEFAULT 0,
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'trace_attributes');
+CREATE TABLE posthog.trace_spans (
+ time_bucket DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
+ original_expiry_timestamp DateTime64(6),
+ uuid String,
+ team_id Int32,
+ trace_id String,
+ span_id String,
+ parent_span_id String,
+ is_root_span Bool MATERIALIZED replaceAll(trimRight(parent_span_id, '='), 'A', '') = '',
+ trace_state String,
+ name LowCardinality(String),
+ kind Int8,
+ flags UInt32,
+ timestamp DateTime64(6),
+ end_time DateTime64(6),
+ observed_timestamp DateTime64(6),
+ created_at DateTime64(6) MATERIALIZED now(),
+ duration_nano UInt64 MATERIALIZED toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000,
+ status_code Int16,
+ service_name LowCardinality(String),
+ resource_attributes Map(LowCardinality(String), String),
+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),
+ instrumentation_scope String,
+ attributes_map_str Map(LowCardinality(String), String),
+ 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)),
+ dropped_attributes_count UInt32,
+ dropped_events_count UInt32,
+ dropped_links_count UInt32,
+ events Array(String),
+ links Array(String),
+ _partition UInt32,
+ _topic String,
+ _offset UInt64,
+ _bytes_uncompressed UInt64,
+ _bytes_compressed UInt64,
+ _record_count UInt64,
+ INDEX idx_name name TYPE ngrambf_v1(4, 5000, 2, 0) GRANULARITY 16,
+ INDEX idx_kind kind TYPE minmax GRANULARITY 4,
+ INDEX idx_duration duration_nano TYPE minmax GRANULARITY 1,
+ INDEX idx_status_code status_code TYPE minmax GRANULARITY 1,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 16,
+ INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
+ INDEX idx_trace_bloom_part trace_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+ INDEX idx_span_id_bloom_part span_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+ PROJECTION projection_aggregate_counts (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span),
+ PROJECTION projection_index_span_id (SELECT _part_offset
+ORDER BY span_id),
+ PROJECTION projection_index_trace_id (SELECT _part_offset
+ORDER BY trace_id)
+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_spans', '{replica}-{shard}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS allow_part_offset_column_in_projections = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_serialization_version = 'with_buckets', ttl_only_drop_parts = 1;
+CREATE TABLE posthog.trace_spans_distributed (
+ time_bucket DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
+ original_expiry_timestamp DateTime64(6),
+ uuid String,
+ team_id Int32,
+ trace_id String,
+ span_id String,
+ parent_span_id String,
+ is_root_span Bool MATERIALIZED replaceAll(trimRight(parent_span_id, '='), 'A', '') = '',
+ trace_state String,
+ name LowCardinality(String),
+ kind Int8,
+ flags UInt32,
+ timestamp DateTime64(6),
+ end_time DateTime64(6),
+ observed_timestamp DateTime64(6),
+ created_at DateTime64(6) MATERIALIZED now(),
+ duration_nano UInt64 MATERIALIZED toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000,
+ status_code Int16,
+ service_name LowCardinality(String),
+ resource_attributes Map(LowCardinality(String), String),
+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),
+ instrumentation_scope String,
+ attributes_map_str Map(LowCardinality(String), String),
+ 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)),
+ dropped_attributes_count UInt32,
+ dropped_events_count UInt32,
+ dropped_links_count UInt32,
+ events Array(String),
+ links Array(String),
+ _partition UInt32,
+ _topic String,
+ _offset UInt64,
+ _bytes_uncompressed UInt64,
+ _bytes_compressed UInt64,
+ _record_count UInt64
+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'trace_spans');
+CREATE TABLE posthog.trace_spans_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 = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_spans_kafka_metrics', '{replica}-{shard}') ORDER BY (_topic, _partition) SETTINGS index_granularity = 8192;
CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.logs34 (uuid String, trace_id String, span_id String, trace_flags Int32, timestamp DateTime64(6), observed_timestamp DateTime64(6), body String, severity_text String, severity_number Int32, service_name String, instrumentation_scope String, event_name String, attributes_map_str Map(String, String), resource_attributes Map(String, String), team_id Int32, original_expiry_timestamp DateTime64(6), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Int64), _bytes_compressed Nullable(Int64)) AS SELECT
kafka_logs_avro.* EXCEPT(created_at, attribute_values, attribute_keys, attributes, attributes_map_str, attributes_map_float, attributes_map_datetime, resource_attributes, bytes_uncompressed),
mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
@@ -415,6 +590,28 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_kafka_metrics_mv TO posthog.log
FROM posthog.logs34
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),
+ toInt8(kind) AS kind,
+ toUInt32(flags) AS flags,
+ toUInt32(dropped_attributes_count) AS dropped_attributes_count,
+ toUInt32(dropped_events_count) AS dropped_events_count,
+ toUInt32(dropped_links_count) AS dropped_links_count,
+ toInt16(status_code) AS status_code,
+ 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,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')], toInt64(0)) AS _bytes_uncompressed,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'bytes_compressed')], toInt64(0)) AS _bytes_compressed
+FROM posthog.kafka_trace_spans_avro;
CREATE MATERIALIZED VIEW posthog.logs32_to_log_attributes TO posthog.log_attributes (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,
@@ -584,6 +781,171 @@ FROM
GROUP BY
team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, resource_attributes
);
+CREATE MATERIALIZED VIEW posthog.trace_span_to_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), 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,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
+CREATE MATERIALIZED VIEW posthog.trace_span_to_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), 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,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
+CREATE MATERIALIZED VIEW posthog.trace_span_to_resource_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), 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,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_resource_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
+CREATE MATERIALIZED VIEW posthog.trace_span_to_resource_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), 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,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_resource_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
+CREATE MATERIALIZED VIEW posthog.trace_span_to_span_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), 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,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ );
+CREATE MATERIALIZED VIEW posthog.trace_span_to_span_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), 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,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ );
+CREATE MATERIALIZED VIEW posthog.trace_spans_to_kafka_metrics_mv TO posthog.trace_spans_kafka_metrics (_partition UInt64, _topic LowCardinality(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 posthog.trace_spans
+GROUP BY
+ _partition, _topic;
CREATE TABLE posthog.logs (
time_bucket DateTime MATERIALIZED toStartOfDay(timestamp) CODEC(DoubleDelta, ZSTD(1)),
original_expiry_timestamp DateTime64(6) CODEC(DoubleDelta, ZSTD(1)),
diff --git a/posthog/clickhouse/migrations/0290_trace_spans_and_attributes.py b/posthog/clickhouse/migrations/0290_trace_spans_and_attributes.py
new file mode 100644
index 000000000000..fbb04bf04ee4
--- /dev/null
+++ b/posthog/clickhouse/migrations/0290_trace_spans_and_attributes.py
@@ -0,0 +1,37 @@
+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,
+ TRACE_ATTRIBUTES2_TABLE_SQL,
+ TRACE_ATTRIBUTES_DISTRIBUTED_TABLE_SQL,
+ TRACE_ATTRIBUTES_TABLE_SQL,
+ TRACE_SPAN_TO_ATTRIBUTES2_MV,
+ TRACE_SPAN_TO_ATTRIBUTES_MV,
+ TRACE_SPAN_TO_RESOURCE_ATTRIBUTES2_MV,
+ TRACE_SPAN_TO_RESOURCE_ATTRIBUTES_MV,
+ TRACE_SPAN_TO_SPAN_ATTRIBUTES2_MV,
+ TRACE_SPAN_TO_SPAN_ATTRIBUTES_MV,
+ TRACE_SPANS_DISTRIBUTED_TABLE_SQL,
+ TRACE_SPANS_KAFKA_METRICS_TABLE_SQL,
+ TRACE_SPANS_TABLE_SQL,
+ TRACE_SPANS_TO_KAFKA_METRICS_MV,
+)
+
+operations = [
+ run_sql_with_exceptions(TRACE_SPANS_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_ATTRIBUTES_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_ATTRIBUTES2_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_SPANS_KAFKA_METRICS_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_SPANS_DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_ATTRIBUTES_DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_SPAN_TO_ATTRIBUTES_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_SPAN_TO_RESOURCE_ATTRIBUTES_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_SPAN_TO_SPAN_ATTRIBUTES_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_SPAN_TO_ATTRIBUTES2_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_SPAN_TO_RESOURCE_ATTRIBUTES2_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_SPAN_TO_SPAN_ATTRIBUTES2_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(KAFKA_TRACE_SPANS_AVRO_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(KAFKA_TRACE_SPANS_AVRO_MV(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(TRACE_SPANS_TO_KAFKA_METRICS_MV(), node_roles=[NodeRole.LOGS]),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index df0191429f03..260a97959e8a 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0289_events_json_schema
+0290_trace_spans_and_attributes
diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.py
index 2e5568a780b8..93cb35311231 100644
--- a/posthog/clickhouse/schema.py
+++ b/posthog/clickhouse/schema.py
@@ -114,6 +114,14 @@ from posthog.clickhouse.query_log_archive import (
WRITABLE_QUERY_LOG_ARCHIVE_OPS_TABLE_SQL,
WRITABLE_QUERY_LOG_ARCHIVE_TABLE,
)
+from posthog.clickhouse.traces import (
+ TRACE_ATTRIBUTES2_TABLE_SQL,
+ TRACE_ATTRIBUTES_DISTRIBUTED_TABLE_SQL,
+ TRACE_ATTRIBUTES_TABLE_SQL,
+ TRACE_SPANS_DISTRIBUTED_TABLE_SQL,
+ TRACE_SPANS_KAFKA_METRICS_TABLE_SQL,
+ TRACE_SPANS_TABLE_SQL,
+)
from posthog.heatmaps.sql import (
DISTRIBUTED_HEATMAPS_TABLE_SQL,
HEATMAPS_TABLE_MV_SQL,
@@ -394,6 +402,10 @@ CREATE_MERGETREE_TABLE_QUERIES = (
LOGS34_TABLE_SQL,
LOG_ATTRIBUTES2_TABLE_SQL,
LOG_ATTRIBUTES3_TABLE_SQL,
+ TRACE_SPANS_TABLE_SQL,
+ TRACE_ATTRIBUTES_TABLE_SQL,
+ TRACE_ATTRIBUTES2_TABLE_SQL,
+ TRACE_SPANS_KAFKA_METRICS_TABLE_SQL,
LOGS_BILLING_METRICS_TABLE_SQL,
METRICS1_TABLE_SQL,
METRIC_SERIES_TABLE_SQL,
@@ -473,6 +485,8 @@ CREATE_DISTRIBUTED_TABLE_QUERIES = (
LOG_ATTRIBUTES3_DISTRIBUTED_TABLE_SQL,
LOGS_KAFKA_METRICS_DISTRIBUTED_TABLE_SQL,
LOGS_BILLING_METRICS_DISTRIBUTED_TABLE_SQL,
+ TRACE_SPANS_DISTRIBUTED_TABLE_SQL,
+ TRACE_ATTRIBUTES_DISTRIBUTED_TABLE_SQL,
METRICS_DISTRIBUTED_TABLE_SQL,
METRIC_SERIES_DISTRIBUTED_TABLE_SQL,
METRIC_SAMPLES_DISTRIBUTED_TABLE_SQL,
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index f6082a79fc3a..82feb014415c 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -8035,6 +8035,201 @@
'''
# ---
+# name: test_create_table_query[trace_attributes2]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.trace_attributes2
+ (
+ `team_id` Int32,
+ `original_expiry_time_bucket` DateTime64(0),
+ `time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `resource_fingerprint` UInt64 DEFAULT 0,
+ `attribute_key` LowCardinality(String),
+ `attribute_value` String,
+ `attribute_type` LowCardinality(String),
+ `attribute_count` SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.trace_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
+ index_granularity = 8192
+
+ '''
+# ---
+# name: test_create_table_query[trace_attributes]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.trace_attributes
+ (
+ `team_id` Int32,
+ `original_expiry_time_bucket` DateTime64(0),
+ `time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `resource_fingerprint` UInt64 DEFAULT 0,
+ `attribute_key` LowCardinality(String),
+ `attribute_value` String,
+ `attribute_type` LowCardinality(String),
+ `attribute_count` SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
+ )
+ ENGINE = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.trace_attributes', '{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
+ index_granularity = 8192
+
+ '''
+# ---
+# name: test_create_table_query[trace_attributes_distributed]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.trace_attributes_distributed AS posthog_test.trace_attributes ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'trace_attributes')
+
+ '''
+# ---
+# name: test_create_table_query[trace_spans]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.trace_spans
+ (
+ `time_bucket` DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
+ `original_expiry_timestamp` DateTime64(6),
+ `uuid` String,
+ `team_id` Int32,
+ `trace_id` String,
+ `span_id` String,
+ `parent_span_id` String,
+ `is_root_span` Bool MATERIALIZED replaceAll(trimRight(parent_span_id, '='), 'A', '') = '',
+ `trace_state` String,
+ `name` LowCardinality(String),
+ `kind` Int8,
+ `flags` UInt32,
+ `timestamp` DateTime64(6),
+ `end_time` DateTime64(6),
+ `observed_timestamp` DateTime64(6),
+ `created_at` DateTime64(6) MATERIALIZED now(),
+ `duration_nano` UInt64 MATERIALIZED toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000,
+ `status_code` Int16,
+ `service_name` LowCardinality(String),
+ `resource_attributes` Map(LowCardinality(String), String),
+ `resource_fingerprint` UInt64 MATERIALIZED cityHash64(resource_attributes),
+ `instrumentation_scope` String,
+ `attributes_map_str` Map(LowCardinality(String), String),
+ `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)),
+ `dropped_attributes_count` UInt32,
+ `dropped_events_count` UInt32,
+ `dropped_links_count` UInt32,
+ `events` Array(String),
+ `links` Array(String),
+
+ -- kafka metadata
+ `_partition` UInt32,
+ `_topic` String,
+ `_offset` UInt64,
+ `_bytes_uncompressed` UInt64,
+ `_bytes_compressed` UInt64,
+ `_record_count` UInt64,
+
+ INDEX idx_name name TYPE ngrambf_v1(4, 5000, 2, 0) GRANULARITY 16,
+ INDEX idx_kind kind TYPE minmax GRANULARITY 4,
+ INDEX idx_duration duration_nano TYPE minmax GRANULARITY 1,
+ INDEX idx_status_code status_code TYPE minmax GRANULARITY 1,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 16,
+ INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
+ INDEX idx_trace_bloom_part trace_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+ INDEX idx_span_id_bloom_part span_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+
+ -- Powers the Spans-view sparkline (spans per minute via sum(event_count)). is_root_span is a
+ -- projection dimension so the Traces-view sparkline (distinct traces per minute) can serve from
+ -- this projection too via sumIf(event_count, is_root_span = 1) — one root span per trace —
+ -- instead of the uniqExactIf(trace_id, is_root_span = 1) raw scan it currently runs.
+ PROJECTION projection_aggregate_counts
+ (
+ SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
+ GROUP BY
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span
+ ),
+
+ PROJECTION projection_index_span_id
+ (
+ SELECT _part_offset
+ ORDER BY span_id
+ ),
+
+ PROJECTION projection_index_trace_id
+ (
+ SELECT _part_offset
+ ORDER BY trace_id
+ )
+ )
+ ENGINE = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.trace_spans', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_timestamp)
+ PRIMARY KEY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp)
+ ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp)
+ TTL original_expiry_timestamp
+ SETTINGS
+ index_granularity_bytes = 104857600,
+ index_granularity = 8192,
+ ttl_only_drop_parts = 1,
+ allow_part_offset_column_in_projections = 1,
+ map_serialization_version = 'with_buckets'
+
+ '''
+# ---
+# name: test_create_table_query[trace_spans_distributed]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.trace_spans_distributed AS posthog_test.trace_spans ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'trace_spans')
+
+ '''
+# ---
+# name: test_create_table_query[trace_spans_kafka_metrics]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.trace_spans_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 = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.trace_spans_kafka_metrics', '{replica}-{shard}')
+ ORDER BY (_topic, _partition)
+ SETTINGS
+ index_granularity = 8192
+
+ '''
+# ---
# name: test_create_table_query[web_bounces_dimensional_preaggregated]
'''
@@ -12633,6 +12828,187 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[trace_attributes2]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.trace_attributes2
+ (
+ `team_id` Int32,
+ `original_expiry_time_bucket` DateTime64(0),
+ `time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `resource_fingerprint` UInt64 DEFAULT 0,
+ `attribute_key` LowCardinality(String),
+ `attribute_value` String,
+ `attribute_type` LowCardinality(String),
+ `attribute_count` SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.trace_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
+ index_granularity = 8192
+
+ '''
+# ---
+# name: test_create_table_query_replicated_and_storage[trace_attributes]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.trace_attributes
+ (
+ `team_id` Int32,
+ `original_expiry_time_bucket` DateTime64(0),
+ `time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `resource_fingerprint` UInt64 DEFAULT 0,
+ `attribute_key` LowCardinality(String),
+ `attribute_value` String,
+ `attribute_type` LowCardinality(String),
+ `attribute_count` SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
+ )
+ ENGINE = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.trace_attributes', '{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
+ index_granularity = 8192
+
+ '''
+# ---
+# name: test_create_table_query_replicated_and_storage[trace_spans]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.trace_spans
+ (
+ `time_bucket` DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
+ `original_expiry_timestamp` DateTime64(6),
+ `uuid` String,
+ `team_id` Int32,
+ `trace_id` String,
+ `span_id` String,
+ `parent_span_id` String,
+ `is_root_span` Bool MATERIALIZED replaceAll(trimRight(parent_span_id, '='), 'A', '') = '',
+ `trace_state` String,
+ `name` LowCardinality(String),
+ `kind` Int8,
+ `flags` UInt32,
+ `timestamp` DateTime64(6),
+ `end_time` DateTime64(6),
+ `observed_timestamp` DateTime64(6),
+ `created_at` DateTime64(6) MATERIALIZED now(),
+ `duration_nano` UInt64 MATERIALIZED toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000,
+ `status_code` Int16,
+ `service_name` LowCardinality(String),
+ `resource_attributes` Map(LowCardinality(String), String),
+ `resource_fingerprint` UInt64 MATERIALIZED cityHash64(resource_attributes),
+ `instrumentation_scope` String,
+ `attributes_map_str` Map(LowCardinality(String), String),
+ `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)),
+ `dropped_attributes_count` UInt32,
+ `dropped_events_count` UInt32,
+ `dropped_links_count` UInt32,
+ `events` Array(String),
+ `links` Array(String),
+
+ -- kafka metadata
+ `_partition` UInt32,
+ `_topic` String,
+ `_offset` UInt64,
+ `_bytes_uncompressed` UInt64,
+ `_bytes_compressed` UInt64,
+ `_record_count` UInt64,
+
+ INDEX idx_name name TYPE ngrambf_v1(4, 5000, 2, 0) GRANULARITY 16,
+ INDEX idx_kind kind TYPE minmax GRANULARITY 4,
+ INDEX idx_duration duration_nano TYPE minmax GRANULARITY 1,
+ INDEX idx_status_code status_code TYPE minmax GRANULARITY 1,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 16,
+ INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
+ INDEX idx_trace_bloom_part trace_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+ INDEX idx_span_id_bloom_part span_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+
+ -- Powers the Spans-view sparkline (spans per minute via sum(event_count)). is_root_span is a
+ -- projection dimension so the Traces-view sparkline (distinct traces per minute) can serve from
+ -- this projection too via sumIf(event_count, is_root_span = 1) — one root span per trace —
+ -- instead of the uniqExactIf(trace_id, is_root_span = 1) raw scan it currently runs.
+ PROJECTION projection_aggregate_counts
+ (
+ SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
+ GROUP BY
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span
+ ),
+
+ PROJECTION projection_index_span_id
+ (
+ SELECT _part_offset
+ ORDER BY span_id
+ ),
+
+ PROJECTION projection_index_trace_id
+ (
+ SELECT _part_offset
+ ORDER BY trace_id
+ )
+ )
+ ENGINE = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.trace_spans', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_timestamp)
+ PRIMARY KEY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp)
+ ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp)
+ TTL original_expiry_timestamp
+ SETTINGS
+ index_granularity_bytes = 104857600,
+ index_granularity = 8192,
+ ttl_only_drop_parts = 1,
+ allow_part_offset_column_in_projections = 1,
+ map_serialization_version = 'with_buckets'
+
+ '''
+# ---
+# name: test_create_table_query_replicated_and_storage[trace_spans_kafka_metrics]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.trace_spans_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 = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.trace_spans_kafka_metrics', '{replica}-{shard}')
+ ORDER BY (_topic, _partition)
+ SETTINGS
+ index_granularity = 8192
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[web_pre_aggregated_bounces]
'''
diff --git a/posthog/clickhouse/traces/__init__.py b/posthog/clickhouse/traces/__init__.py
index 4a151dcf67e1..f264be18047a 100644
--- a/posthog/clickhouse/traces/__init__.py
+++ b/posthog/clickhouse/traces/__init__.py
@@ -1,17 +1,34 @@
from .spans import (
+ KAFKA_TRACE_SPANS_AVRO_MV,
+ KAFKA_TRACE_SPANS_AVRO_TABLE_SQL,
TRACE_ATTRIBUTES_DISTRIBUTED_TABLE_SQL,
- TRACE_ATTRIBUTES_MV,
- TRACE_RESOURCE_ATTRIBUTES_MV,
+ TRACE_SPAN_TO_ATTRIBUTES2_MV,
+ TRACE_SPAN_TO_ATTRIBUTES_MV,
+ TRACE_SPAN_TO_RESOURCE_ATTRIBUTES2_MV,
+ TRACE_SPAN_TO_RESOURCE_ATTRIBUTES_MV,
+ TRACE_SPAN_TO_SPAN_ATTRIBUTES2_MV,
+ TRACE_SPAN_TO_SPAN_ATTRIBUTES_MV,
TRACE_SPANS_DISTRIBUTED_TABLE_SQL,
+ TRACE_SPANS_KAFKA_METRICS_TABLE_SQL,
TRACE_SPANS_TABLE_SQL,
+ TRACE_SPANS_TO_KAFKA_METRICS_MV,
)
-from .trace_attributes import TRACE_ATTRIBUTES_TABLE_SQL
+from .trace_attributes import TRACE_ATTRIBUTES2_TABLE_SQL, TRACE_ATTRIBUTES_TABLE_SQL
__all__ = [
"TRACE_SPANS_TABLE_SQL",
"TRACE_SPANS_DISTRIBUTED_TABLE_SQL",
"TRACE_ATTRIBUTES_TABLE_SQL",
"TRACE_ATTRIBUTES_DISTRIBUTED_TABLE_SQL",
- "TRACE_ATTRIBUTES_MV",
- "TRACE_RESOURCE_ATTRIBUTES_MV",
+ "TRACE_ATTRIBUTES2_TABLE_SQL",
+ "TRACE_SPAN_TO_ATTRIBUTES_MV",
+ "TRACE_SPAN_TO_RESOURCE_ATTRIBUTES_MV",
+ "TRACE_SPAN_TO_SPAN_ATTRIBUTES_MV",
+ "TRACE_SPAN_TO_ATTRIBUTES2_MV",
+ "TRACE_SPAN_TO_RESOURCE_ATTRIBUTES2_MV",
+ "TRACE_SPAN_TO_SPAN_ATTRIBUTES2_MV",
+ "KAFKA_TRACE_SPANS_AVRO_TABLE_SQL",
+ "KAFKA_TRACE_SPANS_AVRO_MV",
+ "TRACE_SPANS_KAFKA_METRICS_TABLE_SQL",
+ "TRACE_SPANS_TO_KAFKA_METRICS_MV",
]
diff --git a/posthog/clickhouse/traces/spans.py b/posthog/clickhouse/traces/spans.py
index de7f16c71b85..874ddaa5871f 100644
--- a/posthog/clickhouse/traces/spans.py
+++ b/posthog/clickhouse/traces/spans.py
@@ -1,64 +1,80 @@
from django.conf import settings
+from posthog.clickhouse.kafka_engine import kafka_engine
from posthog.clickhouse.table_engines import Distributed, MergeTreeEngine, ReplicationScheme
-from .trace_attributes import TABLE_NAME as TRACE_ATTRIBUTES_TABLE_NAME
+from .trace_attributes import (
+ TABLE_NAME as TRACE_ATTRIBUTES_TABLE_NAME,
+ TABLE_NAME_V2 as TRACE_ATTRIBUTES_TABLE_NAME_V2,
+)
TABLE_NAME = "trace_spans"
-
-TTL = lambda: (
- "TTL timestamp + toIntervalHour(25) TO DISK 's3'" if settings.CLICKHOUSE_LOGS_ENABLE_STORAGE_POLICY else ""
-)
-STORAGE_POLICY = lambda: "tiered" if settings.CLICKHOUSE_LOGS_ENABLE_STORAGE_POLICY else "default"
+KAFKA_METRICS_TABLE_NAME = "trace_spans_kafka_metrics"
+KAFKA_TABLE_NAME = "kafka_trace_spans_avro"
+KAFKA_NAMED_COLLECTION = "warpstream_traces"
+KAFKA_TOPIC = "clickhouse_traces"
+KAFKA_GROUP = "clickhouse-traces-avro"
def TRACE_SPANS_TABLE_SQL():
return f"""
CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}
(
- `time_bucket` DateTime MATERIALIZED toStartOfDay(timestamp) CODEC(DoubleDelta, ZSTD(1)),
- `original_expiry_timestamp` DateTime64(6) CODEC(DoubleDelta, ZSTD(1)),
- `uuid` String CODEC(ZSTD(1)),
- `team_id` Int32 CODEC(ZSTD(1)),
- `trace_id` String CODEC(ZSTD(1)),
- `span_id` String CODEC(ZSTD(1)),
- `parent_span_id` String CODEC(ZSTD(1)),
- `trace_state` String CODEC(ZSTD(1)),
- `name` LowCardinality(String) CODEC(ZSTD(1)),
- `kind` Int8 CODEC(T64, ZSTD(1)),
- `flags` UInt32 CODEC(T64, ZSTD(1)),
- `timestamp` DateTime64(6) CODEC(DoubleDelta, ZSTD(1)),
- `end_time` DateTime64(6) CODEC(DoubleDelta, ZSTD(1)),
- `observed_timestamp` DateTime64(6) CODEC(DoubleDelta, ZSTD(1)),
- `created_at` DateTime64(6) MATERIALIZED now() CODEC(DoubleDelta, ZSTD(1)),
- `duration_nano` UInt64 MATERIALIZED toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000 CODEC(T64, ZSTD(1)),
- `status_code` Int16 CODEC(T64, ZSTD(1)),
- `service_name` LowCardinality(String) CODEC(ZSTD(1)),
- `resource_attributes` Map(LowCardinality(String), String) CODEC(ZSTD(1)),
- `resource_fingerprint` UInt64 MATERIALIZED cityHash64(resource_attributes) CODEC(DoubleDelta, ZSTD(1)),
- `instrumentation_scope` String CODEC(ZSTD(1)),
- `attributes_map_str` Map(LowCardinality(String), String) CODEC(ZSTD(1)),
+ `time_bucket` DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
+ `original_expiry_timestamp` DateTime64(6),
+ `uuid` String,
+ `team_id` Int32,
+ `trace_id` String,
+ `span_id` String,
+ `parent_span_id` String,
+ `is_root_span` Bool MATERIALIZED replaceAll(trimRight(parent_span_id, '='), 'A', '') = '',
+ `trace_state` String,
+ `name` LowCardinality(String),
+ `kind` Int8,
+ `flags` UInt32,
+ `timestamp` DateTime64(6),
+ `end_time` DateTime64(6),
+ `observed_timestamp` DateTime64(6),
+ `created_at` DateTime64(6) MATERIALIZED now(),
+ `duration_nano` UInt64 MATERIALIZED toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000,
+ `status_code` Int16,
+ `service_name` LowCardinality(String),
+ `resource_attributes` Map(LowCardinality(String), String),
+ `resource_fingerprint` UInt64 MATERIALIZED cityHash64(resource_attributes),
+ `instrumentation_scope` String,
+ `attributes_map_str` Map(LowCardinality(String), String),
`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)) CODEC(ZSTD(1)),
- `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)) CODEC(ZSTD(1)),
- `dropped_attributes_count` UInt32 CODEC(ZSTD(1)),
- `dropped_events_count` UInt32 CODEC(ZSTD(1)),
- `dropped_links_count` UInt32 CODEC(ZSTD(1)),
- `events` Array(String) CODEC(ZSTD(2)),
- `links` Array(String) CODEC(ZSTD(1)),
+ `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)),
+ `dropped_attributes_count` UInt32,
+ `dropped_events_count` UInt32,
+ `dropped_links_count` UInt32,
+ `events` Array(String),
+ `links` Array(String),
-- kafka metadata
- `_partition` UInt32 CODEC(DoubleDelta, ZSTD(1)),
+ `_partition` UInt32,
`_topic` String,
- `_offset` UInt64 CODEC(DoubleDelta, ZSTD(1)),
- `_bytes_uncompressed` UInt64 CODEC(DoubleDelta, ZSTD(1)),
- `_bytes_compressed` UInt64 CODEC(DoubleDelta, ZSTD(1)),
- `_record_count` UInt64 CODEC(DoubleDelta, ZSTD(1)),
-
- -- Powers the Spans-view sparkline (count of spans per minute). The Traces-view sparkline instead
- -- counts distinct traces per minute (uniqExactIf(trace_id, is_root_span = 1)) and currently
- -- raw-scans, since this count() projection can't serve a distinct-trace aggregate; a sibling
- -- uniqExactState(trace_id) projection would let it use a projection too. Tracked as a follow-up.
+ `_offset` UInt64,
+ `_bytes_uncompressed` UInt64,
+ `_bytes_compressed` UInt64,
+ `_record_count` UInt64,
+
+ INDEX idx_name name TYPE ngrambf_v1(4, 5000, 2, 0) GRANULARITY 16,
+ INDEX idx_kind kind TYPE minmax GRANULARITY 4,
+ INDEX idx_duration duration_nano TYPE minmax GRANULARITY 1,
+ INDEX idx_status_code status_code TYPE minmax GRANULARITY 1,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 16,
+ INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
+ INDEX idx_trace_bloom_part trace_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+ INDEX idx_span_id_bloom_part span_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+
+ -- Powers the Spans-view sparkline (spans per minute via sum(event_count)). is_root_span is a
+ -- projection dimension so the Traces-view sparkline (distinct traces per minute) can serve from
+ -- this projection too via sumIf(event_count, is_root_span = 1) — one root span per trace —
+ -- instead of the uniqExactIf(trace_id, is_root_span = 1) raw scan it currently runs.
PROJECTION projection_aggregate_counts
(
SELECT
@@ -67,19 +83,15 @@ CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NA
toStartOfMinute(timestamp),
service_name,
resource_fingerprint,
+ is_root_span,
count() AS event_count
GROUP BY
team_id,
time_bucket,
toStartOfMinute(timestamp),
service_name,
- resource_fingerprint
- ),
-
- PROJECTION projection_index_trace_id
- (
- SELECT _part_offset
- ORDER BY trace_id
+ resource_fingerprint,
+ is_root_span
),
PROJECTION projection_index_span_id
@@ -88,29 +100,23 @@ CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NA
ORDER BY span_id
),
- INDEX idx_name name TYPE ngrambf_v1(4, 5000, 2, 0) GRANULARITY 1,
- INDEX idx_kind kind TYPE minmax GRANULARITY 4,
- INDEX idx_duration duration_nano TYPE minmax GRANULARITY 1,
- INDEX idx_status_code status_code TYPE minmax GRANULARITY 1,
- INDEX idx_uuid_bloom uuid TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
- INDEX idx_observed_minmax observed_timestamp TYPE minmax 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
+ PROJECTION projection_index_trace_id
+ (
+ SELECT _part_offset
+ ORDER BY trace_id
+ )
)
ENGINE = {MergeTreeEngine(TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
PARTITION BY toDate(original_expiry_timestamp)
PRIMARY KEY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp)
ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp)
-{TTL()}
+TTL original_expiry_timestamp
SETTINGS
- storage_policy = '{STORAGE_POLICY()}',
- allow_remote_fs_zero_copy_replication = 1,
- allow_experimental_reverse_key = 1,
index_granularity_bytes = 104857600,
index_granularity = 8192,
ttl_only_drop_parts = 1,
- add_minmax_index_for_numeric_columns = 1
+ allow_part_offset_column_in_projections = 1,
+ map_serialization_version = 'with_buckets'
"""
@@ -140,13 +146,14 @@ CREATE TABLE IF NOT EXISTS {database}.trace_attributes_distributed AS {database}
)
-def TRACE_ATTRIBUTES_MV():
+def _trace_attributes_mv(view_name: str, dest_table: str, attribute_type: str, inner_select: str):
+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
return f"""
-CREATE MATERIALIZED VIEW IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}_to_trace_attributes TO {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TRACE_ATTRIBUTES_TABLE_NAME}
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{view_name} TO {db}.{dest_table}
(
`team_id` Int32,
- `time_bucket` DateTime64(0),
`original_expiry_time_bucket` DateTime64(0),
+ `time_bucket` DateTime64(0),
`service_name` LowCardinality(String),
`resource_fingerprint` UInt64,
`attribute_key` LowCardinality(String),
@@ -156,84 +163,255 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABAS
)
AS SELECT
team_id,
- time_bucket,
original_expiry_time_bucket,
+ time_bucket,
service_name,
resource_fingerprint,
attribute_key,
attribute_value,
- attribute_type,
+ '{attribute_type}' AS attribute_type,
attribute_count
FROM
(
- SELECT
+{inner_select}
+)
+"""
+
+
+def _span_attributes_inner():
+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+ return f""" SELECT
team_id AS team_id,
- toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS 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,
- 'span' AS attribute_type,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
attribute.1 AS attribute_key,
attribute.2 AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}
+ FROM {db}.{TABLE_NAME}
GROUP BY
team_id,
- time_bucket,
original_expiry_time_bucket,
+ time_bucket,
service_name,
resource_fingerprint,
- attributes
-)
-"""
+ attribute"""
-def TRACE_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}.{TRACE_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
+def _resource_attributes_inner():
+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+ return f""" SELECT
team_id AS team_id,
- toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS 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}
+ FROM {db}.{TABLE_NAME}
GROUP BY
team_id,
+ original_expiry_time_bucket,
time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute"""
+
+
+def _span_name_inner():
+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+ return f""" SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM {db}.{TABLE_NAME}
+ GROUP BY
+ team_id,
original_expiry_time_bucket,
+ time_bucket,
service_name,
resource_fingerprint,
- resource_attributes
+ name"""
+
+
+def TRACE_SPAN_TO_ATTRIBUTES_MV():
+ return _trace_attributes_mv(
+ "trace_span_to_attributes", TRACE_ATTRIBUTES_TABLE_NAME, "span_attribute", _span_attributes_inner()
+ )
+
+
+def TRACE_SPAN_TO_RESOURCE_ATTRIBUTES_MV():
+ return _trace_attributes_mv(
+ "trace_span_to_resource_attributes",
+ TRACE_ATTRIBUTES_TABLE_NAME,
+ "span_resource_attribute",
+ _resource_attributes_inner(),
+ )
+
+
+def TRACE_SPAN_TO_SPAN_ATTRIBUTES_MV():
+ return _trace_attributes_mv(
+ "trace_span_to_span_attributes", TRACE_ATTRIBUTES_TABLE_NAME, "span", _span_name_inner()
+ )
+
+
+def TRACE_SPAN_TO_ATTRIBUTES2_MV():
+ return _trace_attributes_mv(
+ "trace_span_to_attributes2", TRACE_ATTRIBUTES_TABLE_NAME_V2, "span_attribute", _span_attributes_inner()
+ )
+
+
+def TRACE_SPAN_TO_RESOURCE_ATTRIBUTES2_MV():
+ return _trace_attributes_mv(
+ "trace_span_to_resource_attributes2",
+ TRACE_ATTRIBUTES_TABLE_NAME_V2,
+ "span_resource_attribute",
+ _resource_attributes_inner(),
+ )
+
+
+def TRACE_SPAN_TO_SPAN_ATTRIBUTES2_MV():
+ return _trace_attributes_mv(
+ "trace_span_to_span_attributes2", TRACE_ATTRIBUTES_TABLE_NAME_V2, "span", _span_name_inner()
+ )
+
+
+def KAFKA_TRACE_SPANS_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,
+ `parent_span_id` String,
+ `trace_state` String,
+ `name` String,
+ `kind` Int32,
+ `flags` Int32,
+ `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(LowCardinality(String), String),
+ `dropped_attributes_count` Int32,
+ `events` Array(String),
+ `dropped_events_count` Int32,
+ `links` Array(String),
+ `dropped_links_count` Int32,
+ `status_code` 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 = 8,
+ kafka_poll_timeout_ms = 3000,
+ kafka_poll_max_batch_size = 1000
+"""
+
+
+def KAFKA_TRACE_SPANS_AVRO_MV():
+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{KAFKA_TABLE_NAME}_mv TO {db}.{TABLE_NAME}
+(
+ `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),
+ toInt8(kind) AS kind,
+ toUInt32(flags) AS flags,
+ toUInt32(dropped_attributes_count) AS dropped_attributes_count,
+ toUInt32(dropped_events_count) AS dropped_events_count,
+ toUInt32(dropped_links_count) AS dropped_links_count,
+ toInt16(status_code) AS status_code,
+ 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,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')], toInt64(0)) AS _bytes_uncompressed,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'bytes_compressed')], toInt64(0)) AS _bytes_compressed
+FROM {db}.{KAFKA_TABLE_NAME}
+"""
+
+
+def TRACE_SPANS_KAFKA_METRICS_TABLE_SQL():
+ return f"""
+CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{KAFKA_METRICS_TABLE_NAME}
+(
+ `_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 = {MergeTreeEngine(KAFKA_METRICS_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
+ORDER BY (_topic, _partition)
+SETTINGS
+ index_granularity = 8192
+"""
+
+
+def TRACE_SPANS_TO_KAFKA_METRICS_MV():
+ db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.trace_spans_to_kafka_metrics_mv TO {db}.{KAFKA_METRICS_TABLE_NAME}
+(
+ `_partition` UInt64,
+ `_topic` LowCardinality(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
"""
diff --git a/posthog/clickhouse/traces/trace_attributes.py b/posthog/clickhouse/traces/trace_attributes.py
index 64911746ae01..76f94cef7d15 100644
--- a/posthog/clickhouse/traces/trace_attributes.py
+++ b/posthog/clickhouse/traces/trace_attributes.py
@@ -1,35 +1,47 @@
from django.conf import settings
-from posthog.clickhouse.table_engines import AggregatingMergeTree, ReplicationScheme
+from posthog.clickhouse.table_engines import AggregatingMergeTree, MergeTreeEngine, ReplicationScheme
TABLE_NAME = "trace_attributes"
+TABLE_NAME_V2 = "trace_attributes2"
-STORAGE_POLICY = lambda: "hot" if settings.CLICKHOUSE_LOGS_ENABLE_STORAGE_POLICY else "default"
-
-def TRACE_ATTRIBUTES_TABLE_SQL():
+def _trace_attributes_table_sql(table_name: str, engine: str):
return f"""
-CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NAME}
+CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{table_name}
(
- `team_id` Int32 CODEC(DoubleDelta, ZSTD(1)),
- `time_bucket` DateTime64(0) CODEC(DoubleDelta, ZSTD(1)),
- `original_expiry_time_bucket` DateTime64(0) CODEC(DoubleDelta, ZSTD(1)),
- `service_name` LowCardinality(String) CODEC(ZSTD(1)),
- `resource_fingerprint` UInt64 DEFAULT 0 CODEC(DoubleDelta, ZSTD(1)),
- `attribute_key` LowCardinality(String) CODEC(ZSTD(1)),
- `attribute_value` String CODEC(ZSTD(1)),
- `attribute_count` SimpleAggregateFunction(sum, UInt64),
+ `team_id` Int32,
+ `original_expiry_time_bucket` DateTime64(0),
+ `time_bucket` DateTime64(0),
+ `service_name` LowCardinality(String),
+ `resource_fingerprint` UInt64 DEFAULT 0,
+ `attribute_key` LowCardinality(String),
+ `attribute_value` String,
`attribute_type` LowCardinality(String),
- INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,
- INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1
+ `attribute_count` SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
)
-ENGINE = {AggregatingMergeTree("trace_attributes", replication_scheme=ReplicationScheme.REPLICATED)}
+ENGINE = {engine}
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
- storage_policy = '{STORAGE_POLICY()}',
- deduplicate_merge_projection_mode = 'drop',
index_granularity = 8192
"""
+
+
+def TRACE_ATTRIBUTES_TABLE_SQL():
+ return _trace_attributes_table_sql(
+ TABLE_NAME,
+ str(MergeTreeEngine(TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)),
+ )
+
+
+def TRACE_ATTRIBUTES2_TABLE_SQL():
+ return _trace_attributes_table_sql(
+ TABLE_NAME_V2,
+ str(AggregatingMergeTree(TABLE_NAME_V2, replication_scheme=ReplicationScheme.REPLICATED)),
+ )