:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(metrics): add the metrics2 clickhouse chain fed by one kafka mv (#95391)
Frank Hamandc6a23a3d015a
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hclindex 222fd0112d64..1eaa5a4f3f8e 100644--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl@@ -75,6 +75,91 @@ database "posthog" {}}+ table "kafka_metrics_avro2" {+ settings = {+ input_format_avro_allow_missing_fields = "1"+ }+ column "uuid" {+ type = "String"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Nullable(Int32)"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "Nullable(String)"+ }+ column "metric_name" {+ type = "Nullable(String)"+ }+ column "metric_type" {+ type = "Nullable(String)"+ }+ column "value" {+ type = "Nullable(Float64)"+ }+ column "count" {+ type = "Nullable(Int64)"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(Int64)"+ }+ column "unit" {+ type = "Nullable(String)"+ }+ column "aggregation_temporality" {+ type = "Nullable(String)"+ }+ column "is_monotonic" {+ type = "Nullable(UInt8)"+ }+ column "resource_attributes" {+ type = "Map(String, String)"+ }+ column "instrumentation_scope" {+ type = "Nullable(String)"+ }+ column "attributes" {+ type = "Map(String, String)"+ }+ column "series_fingerprint" {+ type = "Nullable(Int64)"+ }+ column "has_labels" {+ type = "Nullable(UInt8)"+ }+ column "retention_days" {+ type = "Nullable(Int32)"+ }+ engine "kafka" {+ collection = "warpstream_metrics"+ topic_list = "clickhouse_metrics"+ group_name = "clickhouse-metrics-avro2"+ format = "Avro"+ num_consumers = 2+ max_block_size = 4096+ skip_broken_messages = 100+ poll_timeout_ms = 10000+ poll_max_batch_size = 4096+ flush_interval_ms = 10000+ thread_per_consumer = true+ }+ }+table "kafka_trace_spans_avro" {column "uuid" {type = "String"@@ -951,6 +1036,96 @@ SQL}}+ table "metric_attributes2" {+ order_by = ["team_id", "attribute_type", "time_bucket", "attribute_key", "attribute_value"]+ partition_by = "toDate(original_expiry_time_bucket)"+ ttl = "original_expiry_time_bucket"+ settings = {+ index_granularity = "8192"+ ttl_only_drop_parts = "1"+ }+ 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 "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 = 1+ }+ index "idx_attribute_value" {+ expr = "attribute_value"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attribute_key_n3" {+ expr = "attribute_key"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ index "idx_attribute_value_n3" {+ expr = "attribute_value"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ engine "replicated_aggregating_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes2"+ replica_name = "{replica}-{shard}"+ }+ }++ table "metric_attributes_distributed" {+ 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 "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 = "logs"+ remote_database = "posthog"+ remote_table = "metric_attributes2"+ }+ }+table "metric_samples" {column "team_id" {type = "Int32"@@ -1160,58 +1335,25 @@ SQL}}- table "metrics" {- column "time_bucket" {- type = "DateTime"- materialized = "toStartOfDay(timestamp)"- }- column "uuid" {- type = "String"+ table "metric_series2" {+ order_by = ["team_id", "metric_name", "series_fingerprint"]+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"}column "team_id" {type = "Int32"}- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "created_at" {- type = "DateTime64(6)"- materialized = "now()"- }- column "service_name" {- type = "LowCardinality(String)"- }column "metric_name" {type = "LowCardinality(String)"}+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }column "metric_type" {type = "LowCardinality(String)"}- column "value" {- type = "Float64"- }- column "count" {- type = "UInt64"- default = "1"- }- column "histogram_bounds" {- type = "Array(Float64)"- }- column "histogram_counts" {- type = "Array(UInt64)"- }column "unit" {type = "LowCardinality(String)"}@@ -1222,6 +1364,12 @@ SQLtype = "Bool"default = "false"}+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }column "resource_attributes" {type = "Map(LowCardinality(String), String)"}@@ -1229,63 +1377,618 @@ SQLtype = "UInt64"materialized = "cityHash64(resource_attributes)"}- column "instrumentation_scope" {- type = "String"- }- column "attributes_map_str" {+ column "attributes" {type = "Map(LowCardinality(String), String)"}- column "attributes_map_float" {- type = "Map(LowCardinality(String), Float64)"- }- column "time_minute" {- type = "DateTime"- alias = "toStartOfMinute(timestamp)"+ column "last_seen" {+ type = "DateTime64(6)"}- column "attributes" {- type = "Map(String, String)"- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"}- engine "distributed" {- cluster_name = "logs"- remote_database = "posthog"- remote_table = "metrics1"+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1}- }-- table "metrics1" {- order_by = ["team_id", "time_bucket", "service_name", "metric_name", "resource_fingerprint", "timestamp"]- partition_by = "toDate(timestamp)"- settings = {- index_granularity = "8192"- index_granularity_bytes = "104857600"- ttl_only_drop_parts = "1"+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attr_keys" {+ expr = "mapKeys(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attr_values" {+ expr = "mapValues(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ engine "replicated_replacing_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"+ replica_name = "{replica}-{shard}"+ version_column = "last_seen"+ }+ }++ table "metric_series_distributed" {+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ materialized = "cityHash64(resource_attributes)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metric_series2"+ }+ }++ table "metrics" {+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfDay(timestamp)"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ 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_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(String, String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metrics1"+ }+ }++ table "metrics1" {+ order_by = ["team_id", "time_bucket", "service_name", "metric_name", "resource_fingerprint", "timestamp"]+ partition_by = "toDate(timestamp)"+ settings = {+ index_granularity = "8192"+ index_granularity_bytes = "104857600"+ ttl_only_drop_parts = "1"}column "time_bucket" {type = "DateTime"materialized = "toStartOfDay(timestamp)"}- column "uuid" {+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ 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_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(String, String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ index "idx_metric_name_set" {+ expr = "metric_name"+ type = "set(100)"+ granularity = 1+ }+ index "idx_metric_type_set" {+ expr = "metric_type"+ type = "set(10)"+ granularity = 1+ }+ index "idx_attributes_str_keys" {+ expr = "mapKeys(attributes_map_str)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attributes_str_values" {+ expr = "mapValues(attributes_map_str)"+ type = "bloom_filter(0.001)"+ granularity = 1+ }+ index "idx_observed_minmax" {+ expr = "observed_timestamp"+ type = "minmax"+ granularity = 1+ }+ projection "projection_aggregate_counts" {+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ toStartOfMinute(timestamp),+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ count() AS event_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value+GROUP BY+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint+SQL++ }+ engine "replicated_merge_tree" {+ zoo_path = "/clickhouse/tables/logs/{shard}/posthog.metrics1"+ replica_name = "{replica}"+ }+ }++ table "metrics2" {+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint", "timestamp"]+ partition_by = "toDate(original_expiry_timestamp)"+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"+ index_granularity_bytes = "104857600"+ ttl_only_drop_parts = "1"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ index "idx_metric_type_set" {+ expr = "metric_type"+ type = "set(10)"+ granularity = 1+ }+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1+ }+ index "idx_trace_id_bf" {+ expr = "trace_id"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_observed_minmax" {+ expr = "observed_timestamp"+ type = "minmax"+ granularity = 1+ }+ projection "projection_series_minute" {+ query = <<SQL+SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+GROUP BY+ team_id, metric_name, service_name, metric_type, resource_fingerprint, series_fingerprint, minute+SQL++ }+ projection "projection_series_activity" {+ query = <<SQL+SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+GROUP BY+ team_id, service_name, metric_name, metric_type, resource_fingerprint, series_fingerprint, hour+SQL++ }+ engine "replicated_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics2"+ replica_name = "{replica}-{shard}"+ }+ }++ table "metrics2_input" {+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "instrumentation_scope" {type = "String"}- column "team_id" {- type = "Int32"+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"}- column "trace_id" {+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {type = "String"}- column "span_id" {+ column "_offset" {+ type = "UInt64"+ }+ engine "null" {+ }+ }++ table "metrics_distributed" {+ column "uuid" {type = "String"}- column "trace_flags" {+ column "team_id" {type = "Int32"}+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }column "timestamp" {- type = "DateTime64(6)"+ type = "DateTime64(6)"+ codec = "DoubleDelta"}column "observed_timestamp" {type = "DateTime64(6)"}+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }column "created_at" {type = "DateTime64(6)"materialized = "now()"@@ -1293,18 +1996,17 @@ SQLcolumn "service_name" {type = "LowCardinality(String)"}- column "metric_name" {- type = "LowCardinality(String)"- }column "metric_type" {type = "LowCardinality(String)"}column "value" {- type = "Float64"+ type = "Float64"+ codec = "Gorilla(8)"}column "count" {type = "UInt64"default = "1"+ codec = "T64"}column "histogram_bounds" {type = "Array(Float64)"@@ -1312,6 +2014,19 @@ SQLcolumn "histogram_counts" {type = "Array(UInt64)"}+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }column "unit" {type = "LowCardinality(String)"}@@ -1322,77 +2037,22 @@ SQLtype = "Bool"default = "false"}- 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_map_float" {- type = "Map(LowCardinality(String), Float64)"- }- column "time_minute" {- type = "DateTime"- alias = "toStartOfMinute(timestamp)"- }- column "attributes" {- type = "Map(String, String)"- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"- }- index "idx_metric_name_set" {- expr = "metric_name"- type = "set(100)"- granularity = 1- }- index "idx_metric_type_set" {- expr = "metric_type"- type = "set(10)"- granularity = 1- }- index "idx_attributes_str_keys" {- expr = "mapKeys(attributes_map_str)"- type = "bloom_filter(0.01)"- granularity = 1- }- index "idx_attributes_str_values" {- expr = "mapValues(attributes_map_str)"- type = "bloom_filter(0.001)"- granularity = 1+ column "_partition" {+ type = "UInt32"}- index "idx_observed_minmax" {- expr = "observed_timestamp"- type = "minmax"- granularity = 1+ column "_topic" {+ type = "String"}- projection "projection_aggregate_counts" {- query = <<SQL-SELECT- team_id,- time_bucket,- toStartOfMinute(timestamp),- service_name,- metric_name,- metric_type,- resource_fingerprint,- count() AS event_count,- sum(value) AS total_value,- min(value) AS min_value,- max(value) AS max_value-GROUP BY- team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint-SQL-+ column "_offset" {+ type = "UInt64"}- engine "replicated_merge_tree" {- zoo_path = "/clickhouse/tables/logs/{shard}/posthog.metrics1"- replica_name = "{replica}"+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metrics2"}}@@ -2462,17 +3122,155 @@ SQLcolumn "max_offset" {type = "SimpleAggregateFunction(max, UInt64)"}- column "max_observed_timestamp" {- type = "SimpleAggregateFunction(max, DateTime64(6))"+ 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_metrics_avro2_mv" {+ to_table = "posthog.metrics2_input"+ query = <<SQL+SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp+ + toIntervalDay(+ assumeNotNull(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(90))+ )+ )+ ) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ toBool(ifNull(has_labels, 1)) AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS resource_attributes,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS attributes,+ _partition,+ _topic,+ _offset+FROM posthog.kafka_metrics_avro2+WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0+SQL++ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "String"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "String"+ }+ column "metric_type" {+ type = "String"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "String"+ }+ column "aggregation_temporality" {+ type = "String"+ }+ column "is_monotonic" {+ type = "UInt8"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(String, String)"}- column "max_timestamp" {- type = "SimpleAggregateFunction(max, DateTime64(6))"+ column "attributes" {+ type = "Map(String, String)"}- column "max_created_at" {- type = "SimpleAggregateFunction(max, DateTime)"+ column "_partition" {+ type = "UInt64"}- column "max_lag" {- type = "SimpleAggregateFunction(max, Decimal(18, 6))"+ column "_topic" {+ type = "LowCardinality(String)"+ }+ column "_offset" {+ type = "UInt64"}}@@ -3138,6 +3936,293 @@ SQL}}+ materialized_view "metrics2_input_to_metric_attributes" {+ to_table = "posthog.metric_attributes2"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_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 "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 "metrics2_input_to_metric_series" {+ to_table = "posthog.metric_series2"+ query = <<SQL+SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM posthog.metrics2_input+WHERE has_labels+SQL++ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ }++ materialized_view "metrics2_input_to_metrics" {+ to_table = "posthog.metrics2"+ query = <<SQL+SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM posthog.metrics2_input+SQL++ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ }++ materialized_view "metrics2_input_to_resource_attributes" {+ to_table = "posthog.metric_attributes2"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_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 "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 "ops_query_log_archive_mv" {to_table = "posthog.writable_query_log_archive"query = <<SQLdiff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hclindex 306c1123565e..a865844433cb 100644--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl@@ -73,6 +73,89 @@ database "posthog" {}}+ table "kafka_metrics_avro2" {+ settings = {+ input_format_avro_allow_missing_fields = "1"+ }+ column "uuid" {+ type = "String"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Nullable(Int32)"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "Nullable(String)"+ }+ column "metric_name" {+ type = "Nullable(String)"+ }+ column "metric_type" {+ type = "Nullable(String)"+ }+ column "value" {+ type = "Nullable(Float64)"+ }+ column "count" {+ type = "Nullable(Int64)"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(Int64)"+ }+ column "unit" {+ type = "Nullable(String)"+ }+ column "aggregation_temporality" {+ type = "Nullable(String)"+ }+ column "is_monotonic" {+ type = "Nullable(UInt8)"+ }+ column "resource_attributes" {+ type = "Map(String, String)"+ }+ column "instrumentation_scope" {+ type = "Nullable(String)"+ }+ column "attributes" {+ type = "Map(String, String)"+ }+ column "series_fingerprint" {+ type = "Nullable(Int64)"+ }+ column "has_labels" {+ type = "Nullable(UInt8)"+ }+ column "retention_days" {+ type = "Nullable(Int32)"+ }+ engine "kafka" {+ collection = "warpstream_metrics"+ topic_list = "clickhouse_metrics"+ group_name = "clickhouse-metrics-avro2"+ format = "Avro"+ num_consumers = 8+ skip_broken_messages = 100+ poll_timeout_ms = 3000+ poll_max_batch_size = 1000+ thread_per_consumer = true+ }+ }+table "kafka_trace_spans_avro" {column "uuid" {type = "String"@@ -1363,6 +1446,96 @@ SQL}}+ table "metric_attributes2" {+ order_by = ["team_id", "attribute_type", "time_bucket", "attribute_key", "attribute_value"]+ partition_by = "toDate(original_expiry_time_bucket)"+ ttl = "original_expiry_time_bucket"+ settings = {+ index_granularity = "8192"+ ttl_only_drop_parts = "1"+ }+ 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 "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 = 1+ }+ index "idx_attribute_value" {+ expr = "attribute_value"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attribute_key_n3" {+ expr = "attribute_key"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ index "idx_attribute_value_n3" {+ expr = "attribute_value"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ engine "replicated_aggregating_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes2"+ replica_name = "{replica}-{shard}"+ }+ }++ table "metric_attributes_distributed" {+ 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 "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 = "metric_attributes2"+ }+ }+table "metric_samples" {column "team_id" {type = "Int32"@@ -1572,60 +1745,25 @@ SQL}}- table "metrics" {- column "time_bucket" {- type = "DateTime"- materialized = "toStartOfDay(timestamp)"- }- column "uuid" {- type = "String"+ table "metric_series2" {+ order_by = ["team_id", "metric_name", "series_fingerprint"]+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"}column "team_id" {type = "Int32"}- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "created_at" {- type = "DateTime64(6)"- materialized = "now()"- }- column "service_name" {- type = "LowCardinality(String)"- }column "metric_name" {type = "LowCardinality(String)"}+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }column "metric_type" {type = "LowCardinality(String)"}- column "value" {- type = "Float64"- codec = "Gorilla(8)"- }- column "count" {- type = "UInt64"- default = "1"- codec = "T64"- }- column "histogram_bounds" {- type = "Array(Float64)"- }- column "histogram_counts" {- type = "Array(UInt64)"- }column "unit" {type = "LowCardinality(String)"}@@ -1636,6 +1774,12 @@ SQLtype = "Bool"default = "false"}+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }column "resource_attributes" {type = "Map(LowCardinality(String), String)"}@@ -1643,38 +1787,96 @@ SQLtype = "UInt64"materialized = "cityHash64(resource_attributes)"}- column "instrumentation_scope" {- type = "String"- }- column "attributes_map_str" {+ column "attributes" {type = "Map(LowCardinality(String), String)"}- column "attributes_map_float" {- type = "Map(LowCardinality(String), Float64)"- }- column "time_minute" {- type = "DateTime"- alias = "toStartOfMinute(timestamp)"+ column "last_seen" {+ type = "DateTime64(6)"}- column "attributes" {- type = "Map(String, String)"- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"}- engine "distributed" {- cluster_name = "posthog_single_shard"- remote_database = "posthog"- remote_table = "metrics1"+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1}- }-- table "metrics1" {- order_by = ["team_id", "time_bucket", "service_name", "metric_name", "resource_fingerprint", "timestamp"]- partition_by = "toDate(timestamp)"- settings = {- index_granularity = "8192"- index_granularity_bytes = "104857600"- ttl_only_drop_parts = "1"+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1}+ index "idx_attr_keys" {+ expr = "mapKeys(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attr_values" {+ expr = "mapValues(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ engine "replicated_replacing_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"+ replica_name = "{replica}-{shard}"+ version_column = "last_seen"+ }+ }++ table "metric_series_distributed" {+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ materialized = "cityHash64(resource_attributes)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ engine "distributed" {+ cluster_name = "posthog_single_shard"+ remote_database = "posthog"+ remote_table = "metric_series2"+ }+ }++ table "metrics" {column "time_bucket" {type = "DateTime"materialized = "toStartOfDay(timestamp)"@@ -1695,11 +1897,512 @@ SQLtype = "Int32"}column "timestamp" {- type = "DateTime64(6)"+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ 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_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(String, String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ engine "distributed" {+ cluster_name = "posthog_single_shard"+ remote_database = "posthog"+ remote_table = "metrics1"+ }+ }++ table "metrics1" {+ order_by = ["team_id", "time_bucket", "service_name", "metric_name", "resource_fingerprint", "timestamp"]+ partition_by = "toDate(timestamp)"+ settings = {+ index_granularity = "8192"+ index_granularity_bytes = "104857600"+ ttl_only_drop_parts = "1"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfDay(timestamp)"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ 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_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(String, String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ index "idx_metric_name_set" {+ expr = "metric_name"+ type = "set(100)"+ granularity = 1+ }+ index "idx_metric_type_set" {+ expr = "metric_type"+ type = "set(10)"+ granularity = 1+ }+ index "idx_attributes_str_keys" {+ expr = "mapKeys(attributes_map_str)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attributes_str_values" {+ expr = "mapValues(attributes_map_str)"+ type = "bloom_filter(0.001)"+ granularity = 1+ }+ index "idx_observed_minmax" {+ expr = "observed_timestamp"+ type = "minmax"+ granularity = 1+ }+ projection "projection_aggregate_counts" {+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ toStartOfMinute(timestamp),+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ count() AS event_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value+GROUP BY+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint+SQL++ }+ engine "replicated_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics1"+ replica_name = "{replica}"+ }+ }++ table "metrics2" {+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint", "timestamp"]+ partition_by = "toDate(original_expiry_timestamp)"+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"+ index_granularity_bytes = "104857600"+ ttl_only_drop_parts = "1"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ index "idx_metric_type_set" {+ expr = "metric_type"+ type = "set(10)"+ granularity = 1+ }+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1+ }+ index "idx_trace_id_bf" {+ expr = "trace_id"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_observed_minmax" {+ expr = "observed_timestamp"+ type = "minmax"+ granularity = 1+ }+ projection "projection_series_minute" {+ query = <<SQL+SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+GROUP BY+ team_id, metric_name, service_name, metric_type, resource_fingerprint, series_fingerprint, minute+SQL++ }+ projection "projection_series_activity" {+ query = <<SQL+SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+GROUP BY+ team_id, service_name, metric_name, metric_type, resource_fingerprint, series_fingerprint, hour+SQL++ }+ engine "replicated_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics2"+ replica_name = "{replica}-{shard}"+ }+ }++ table "metrics2_input" {+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ engine "null" {+ }+ }++ table "metrics_distributed" {+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"}column "observed_timestamp" {type = "DateTime64(6)"}+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }column "created_at" {type = "DateTime64(6)"materialized = "now()"@@ -1707,9 +2410,6 @@ SQLcolumn "service_name" {type = "LowCardinality(String)"}- column "metric_name" {- type = "LowCardinality(String)"- }column "metric_type" {type = "LowCardinality(String)"}@@ -1728,6 +2428,19 @@ SQLcolumn "histogram_counts" {type = "Array(UInt64)"}+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }column "unit" {type = "LowCardinality(String)"}@@ -1738,77 +2451,22 @@ SQLtype = "Bool"default = "false"}- 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_map_float" {- type = "Map(LowCardinality(String), Float64)"- }- column "time_minute" {- type = "DateTime"- alias = "toStartOfMinute(timestamp)"- }- column "attributes" {- type = "Map(String, String)"- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"- }- index "idx_metric_name_set" {- expr = "metric_name"- type = "set(100)"- granularity = 1- }- index "idx_metric_type_set" {- expr = "metric_type"- type = "set(10)"- granularity = 1- }- index "idx_attributes_str_keys" {- expr = "mapKeys(attributes_map_str)"- type = "bloom_filter(0.01)"- granularity = 1- }- index "idx_attributes_str_values" {- expr = "mapValues(attributes_map_str)"- type = "bloom_filter(0.001)"- granularity = 1+ column "_partition" {+ type = "UInt32"}- index "idx_observed_minmax" {- expr = "observed_timestamp"- type = "minmax"- granularity = 1+ column "_topic" {+ type = "String"}- projection "projection_aggregate_counts" {- query = <<SQL-SELECT- team_id,- time_bucket,- toStartOfMinute(timestamp),- service_name,- metric_name,- metric_type,- resource_fingerprint,- count() AS event_count,- sum(value) AS total_value,- min(value) AS min_value,- max(value) AS max_value-GROUP BY- team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint-SQL-+ column "_offset" {+ type = "UInt64"}- engine "replicated_merge_tree" {- zoo_path = "/clickhouse/tables/noshard/posthog.metrics1"- replica_name = "{replica}"+ engine "distributed" {+ cluster_name = "posthog_single_shard"+ remote_database = "posthog"+ remote_table = "metrics2"}}@@ -2794,17 +3452,155 @@ SQLcolumn "max_offset" {type = "SimpleAggregateFunction(max, UInt64)"}- column "max_observed_timestamp" {- type = "SimpleAggregateFunction(max, DateTime64(6))"+ 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_metrics_avro2_mv" {+ to_table = "posthog.metrics2_input"+ query = <<SQL+SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp+ + toIntervalDay(+ assumeNotNull(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(90))+ )+ )+ ) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ toBool(ifNull(has_labels, 1)) AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS resource_attributes,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS attributes,+ _partition,+ _topic,+ _offset+FROM posthog.kafka_metrics_avro2+WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0+SQL++ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "String"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "String"+ }+ column "metric_type" {+ type = "String"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "String"+ }+ column "aggregation_temporality" {+ type = "String"+ }+ column "is_monotonic" {+ type = "UInt8"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(String, String)"}- column "max_timestamp" {- type = "SimpleAggregateFunction(max, DateTime64(6))"+ column "attributes" {+ type = "Map(String, String)"}- column "max_created_at" {- type = "SimpleAggregateFunction(max, DateTime)"+ column "_partition" {+ type = "UInt64"}- column "max_lag" {- type = "SimpleAggregateFunction(max, Decimal(18, 6))"+ column "_topic" {+ type = "LowCardinality(String)"+ }+ column "_offset" {+ type = "UInt64"}}@@ -3593,6 +4389,293 @@ SQL}}+ materialized_view "metrics2_input_to_metric_attributes" {+ to_table = "posthog.metric_attributes2"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_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 "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 "metrics2_input_to_metric_series" {+ to_table = "posthog.metric_series2"+ query = <<SQL+SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM posthog.metrics2_input+WHERE has_labels+SQL++ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ }++ materialized_view "metrics2_input_to_metrics" {+ to_table = "posthog.metrics2"+ query = <<SQL+SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM posthog.metrics2_input+SQL++ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ }++ materialized_view "metrics2_input_to_resource_attributes" {+ to_table = "posthog.metric_attributes2"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_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 "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_attributes" {to_table = "posthog.trace_attributes"query = <<SQLdiff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hclindex 92da583058c8..5347a436db1b 100644--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl@@ -3673,6 +3673,89 @@ database "posthog" {}}+ table "kafka_metrics_avro2" {+ settings = {+ input_format_avro_allow_missing_fields = "1"+ }+ column "uuid" {+ type = "String"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Nullable(Int32)"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "Nullable(String)"+ }+ column "metric_name" {+ type = "Nullable(String)"+ }+ column "metric_type" {+ type = "Nullable(String)"+ }+ column "value" {+ type = "Nullable(Float64)"+ }+ column "count" {+ type = "Nullable(Int64)"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(Int64)"+ }+ column "unit" {+ type = "Nullable(String)"+ }+ column "aggregation_temporality" {+ type = "Nullable(String)"+ }+ column "is_monotonic" {+ type = "Nullable(UInt8)"+ }+ column "resource_attributes" {+ type = "Map(String, String)"+ }+ column "instrumentation_scope" {+ type = "Nullable(String)"+ }+ column "attributes" {+ type = "Map(String, String)"+ }+ column "series_fingerprint" {+ type = "Nullable(Int64)"+ }+ column "has_labels" {+ type = "Nullable(UInt8)"+ }+ column "retention_days" {+ type = "Nullable(Int32)"+ }+ engine "kafka" {+ collection = "warpstream_metrics"+ topic_list = "clickhouse_metrics"+ group_name = "clickhouse-metrics-avro2"+ format = "Avro"+ num_consumers = 8+ skip_broken_messages = 100+ poll_timeout_ms = 3000+ poll_max_batch_size = 1000+ thread_per_consumer = true+ }+ }+table "kafka_performance_events" {column "uuid" {type = "UUID"@@ -6243,6 +6326,96 @@ SQL}}+ table "metric_attributes2" {+ order_by = ["team_id", "attribute_type", "time_bucket", "attribute_key", "attribute_value"]+ partition_by = "toDate(original_expiry_time_bucket)"+ ttl = "original_expiry_time_bucket"+ settings = {+ index_granularity = "8192"+ ttl_only_drop_parts = "1"+ }+ 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 "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 = 1+ }+ index "idx_attribute_value" {+ expr = "attribute_value"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attribute_key_n3" {+ expr = "attribute_key"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ index "idx_attribute_value_n3" {+ expr = "attribute_value"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ engine "replicated_aggregating_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes2"+ replica_name = "{replica}-{shard}"+ }+ }++ table "metric_attributes_distributed" {+ 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 "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 = "metric_attributes2"+ }+ }+table "metric_samples" {column "team_id" {type = "Int32"@@ -6452,60 +6625,25 @@ SQL}}- table "metrics" {- column "time_bucket" {- type = "DateTime"- materialized = "toStartOfDay(timestamp)"- }- column "uuid" {- type = "String"+ table "metric_series2" {+ order_by = ["team_id", "metric_name", "series_fingerprint"]+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"}column "team_id" {type = "Int32"}- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "created_at" {- type = "DateTime64(6)"- materialized = "now()"- }- column "service_name" {- type = "LowCardinality(String)"- }column "metric_name" {type = "LowCardinality(String)"}+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }column "metric_type" {type = "LowCardinality(String)"}- column "value" {- type = "Float64"- codec = "Gorilla(8)"- }- column "count" {- type = "UInt64"- default = "1"- codec = "T64"- }- column "histogram_bounds" {- type = "Array(Float64)"- }- column "histogram_counts" {- type = "Array(UInt64)"- }column "unit" {type = "LowCardinality(String)"}@@ -6516,6 +6654,12 @@ SQLtype = "Bool"default = "false"}+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }column "resource_attributes" {type = "Map(LowCardinality(String), String)"}@@ -6523,63 +6667,622 @@ SQLtype = "UInt64"materialized = "cityHash64(resource_attributes)"}- column "instrumentation_scope" {- type = "String"- }- column "attributes_map_str" {+ column "attributes" {type = "Map(LowCardinality(String), String)"}- column "attributes_map_float" {- type = "Map(LowCardinality(String), Float64)"+ column "last_seen" {+ type = "DateTime64(6)"}- column "time_minute" {- type = "DateTime"- alias = "toStartOfMinute(timestamp)"+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"}- column "attributes" {- type = "Map(String, String)"- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1}- engine "distributed" {- cluster_name = "posthog_single_shard"- remote_database = "posthog"- remote_table = "metrics1"+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1}- }-- table "metrics1" {- order_by = ["team_id", "time_bucket", "service_name", "metric_name", "resource_fingerprint", "timestamp"]- partition_by = "toDate(timestamp)"- settings = {- index_granularity = "8192"- index_granularity_bytes = "104857600"+ index "idx_attr_keys" {+ expr = "mapKeys(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attr_values" {+ expr = "mapValues(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ engine "replicated_replacing_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"+ replica_name = "{replica}-{shard}"+ version_column = "last_seen"+ }+ }++ table "metric_series_distributed" {+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ materialized = "cityHash64(resource_attributes)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ engine "distributed" {+ cluster_name = "posthog_single_shard"+ remote_database = "posthog"+ remote_table = "metric_series2"+ }+ }++ table "metrics" {+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfDay(timestamp)"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ 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_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(String, String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ engine "distributed" {+ cluster_name = "posthog_single_shard"+ remote_database = "posthog"+ remote_table = "metrics1"+ }+ }++ table "metrics1" {+ order_by = ["team_id", "time_bucket", "service_name", "metric_name", "resource_fingerprint", "timestamp"]+ partition_by = "toDate(timestamp)"+ settings = {+ index_granularity = "8192"+ index_granularity_bytes = "104857600"+ ttl_only_drop_parts = "1"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfDay(timestamp)"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ 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_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(String, String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ index "idx_metric_name_set" {+ expr = "metric_name"+ type = "set(100)"+ granularity = 1+ }+ index "idx_metric_type_set" {+ expr = "metric_type"+ type = "set(10)"+ granularity = 1+ }+ index "idx_attributes_str_keys" {+ expr = "mapKeys(attributes_map_str)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attributes_str_values" {+ expr = "mapValues(attributes_map_str)"+ type = "bloom_filter(0.001)"+ granularity = 1+ }+ index "idx_observed_minmax" {+ expr = "observed_timestamp"+ type = "minmax"+ granularity = 1+ }+ projection "projection_aggregate_counts" {+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ toStartOfMinute(timestamp),+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ count() AS event_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value+GROUP BY+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint+SQL++ }+ engine "replicated_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics1"+ replica_name = "{replica}"+ }+ }++ table "metrics2" {+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint", "timestamp"]+ partition_by = "toDate(original_expiry_timestamp)"+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"+ index_granularity_bytes = "104857600"ttl_only_drop_parts = "1"}- column "time_bucket" {- type = "DateTime"- materialized = "toStartOfDay(timestamp)"+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ index "idx_metric_type_set" {+ expr = "metric_type"+ type = "set(10)"+ granularity = 1+ }+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1+ }+ index "idx_trace_id_bf" {+ expr = "trace_id"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_observed_minmax" {+ expr = "observed_timestamp"+ type = "minmax"+ granularity = 1+ }+ projection "projection_series_minute" {+ query = <<SQL+SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+GROUP BY+ team_id, metric_name, service_name, metric_type, resource_fingerprint, series_fingerprint, minute+SQL++ }+ projection "projection_series_activity" {+ query = <<SQL+SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+GROUP BY+ team_id, service_name, metric_name, metric_type, resource_fingerprint, series_fingerprint, hour+SQL++ }+ engine "replicated_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics2"+ replica_name = "{replica}-{shard}"+ }+ }++ table "metrics2_input" {+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"}- column "uuid" {+ column "instrumentation_scope" {type = "String"}- column "team_id" {- type = "Int32"+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"}- column "trace_id" {+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {type = "String"}- column "span_id" {+ column "_offset" {+ type = "UInt64"+ }+ engine "null" {+ }+ }++ table "metrics_distributed" {+ column "uuid" {type = "String"}- column "trace_flags" {+ column "team_id" {type = "Int32"}+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }column "timestamp" {- type = "DateTime64(6)"+ type = "DateTime64(6)"+ codec = "DoubleDelta"}column "observed_timestamp" {type = "DateTime64(6)"}+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }column "created_at" {type = "DateTime64(6)"materialized = "now()"@@ -6587,9 +7290,6 @@ SQLcolumn "service_name" {type = "LowCardinality(String)"}- column "metric_name" {- type = "LowCardinality(String)"- }column "metric_type" {type = "LowCardinality(String)"}@@ -6608,6 +7308,19 @@ SQLcolumn "histogram_counts" {type = "Array(UInt64)"}+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }column "unit" {type = "LowCardinality(String)"}@@ -6618,77 +7331,22 @@ SQLtype = "Bool"default = "false"}- 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_map_float" {- type = "Map(LowCardinality(String), Float64)"- }- column "time_minute" {- type = "DateTime"- alias = "toStartOfMinute(timestamp)"- }- column "attributes" {- type = "Map(String, String)"- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"- }- index "idx_metric_name_set" {- expr = "metric_name"- type = "set(100)"- granularity = 1- }- index "idx_metric_type_set" {- expr = "metric_type"- type = "set(10)"- granularity = 1- }- index "idx_attributes_str_keys" {- expr = "mapKeys(attributes_map_str)"- type = "bloom_filter(0.01)"- granularity = 1- }- index "idx_attributes_str_values" {- expr = "mapValues(attributes_map_str)"- type = "bloom_filter(0.001)"- granularity = 1+ column "_partition" {+ type = "UInt32"}- index "idx_observed_minmax" {- expr = "observed_timestamp"- type = "minmax"- granularity = 1+ column "_topic" {+ type = "String"}- projection "projection_aggregate_counts" {- query = <<SQL-SELECT- team_id,- time_bucket,- toStartOfMinute(timestamp),- service_name,- metric_name,- metric_type,- resource_fingerprint,- count() AS event_count,- sum(value) AS total_value,- min(value) AS min_value,- max(value) AS max_value-GROUP BY- team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint-SQL-+ column "_offset" {+ type = "UInt64"}- engine "replicated_merge_tree" {- zoo_path = "/clickhouse/tables/noshard/posthog.metrics1"- replica_name = "{replica}"+ engine "distributed" {+ cluster_name = "posthog_single_shard"+ remote_database = "posthog"+ remote_table = "metrics2"}}@@ -19821,11 +20479,149 @@ SQLcolumn "max_timestamp" {type = "SimpleAggregateFunction(max, DateTime64(6))"}- column "max_created_at" {- type = "SimpleAggregateFunction(max, DateTime)"+ column "max_created_at" {+ type = "SimpleAggregateFunction(max, DateTime)"+ }+ column "max_lag" {+ type = "SimpleAggregateFunction(max, Decimal(18, 6))"+ }+ }++ materialized_view "kafka_metrics_avro2_mv" {+ to_table = "posthog.metrics2_input"+ query = <<SQL+SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp+ + toIntervalDay(+ assumeNotNull(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(90))+ )+ )+ ) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ toBool(ifNull(has_labels, 1)) AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS resource_attributes,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS attributes,+ _partition,+ _topic,+ _offset+FROM posthog.kafka_metrics_avro2+WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0+SQL++ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "String"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "String"+ }+ column "metric_type" {+ type = "String"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "String"+ }+ column "aggregation_temporality" {+ type = "String"+ }+ column "is_monotonic" {+ type = "UInt8"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(String, String)"+ }+ column "attributes" {+ type = "Map(String, String)"+ }+ column "_partition" {+ type = "UInt64"+ }+ column "_topic" {+ type = "LowCardinality(String)"}- column "max_lag" {- type = "SimpleAggregateFunction(max, Decimal(18, 6))"+ column "_offset" {+ type = "UInt64"}}@@ -20791,6 +21587,293 @@ SQL}}+ materialized_view "metrics2_input_to_metric_attributes" {+ to_table = "posthog.metric_attributes2"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_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 "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 "metrics2_input_to_metric_series" {+ to_table = "posthog.metric_series2"+ query = <<SQL+SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM posthog.metrics2_input+WHERE has_labels+SQL++ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ }++ materialized_view "metrics2_input_to_metrics" {+ to_table = "posthog.metrics2"+ query = <<SQL+SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM posthog.metrics2_input+SQL++ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ }++ materialized_view "metrics2_input_to_resource_attributes" {+ to_table = "posthog.metric_attributes2"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_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 "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 "ops_query_log_archive_mv" {to_table = "posthog.writable_query_log_archive"query = <<SQLdiff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hclindex fc037d577925..e046dd6dfa12 100644--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl@@ -73,6 +73,89 @@ database "posthog" {}}+ table "kafka_metrics_avro2" {+ settings = {+ input_format_avro_allow_missing_fields = "1"+ }+ column "uuid" {+ type = "String"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Nullable(Int32)"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "Nullable(String)"+ }+ column "metric_name" {+ type = "Nullable(String)"+ }+ column "metric_type" {+ type = "Nullable(String)"+ }+ column "value" {+ type = "Nullable(Float64)"+ }+ column "count" {+ type = "Nullable(Int64)"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(Int64)"+ }+ column "unit" {+ type = "Nullable(String)"+ }+ column "aggregation_temporality" {+ type = "Nullable(String)"+ }+ column "is_monotonic" {+ type = "Nullable(UInt8)"+ }+ column "resource_attributes" {+ type = "Map(String, String)"+ }+ column "instrumentation_scope" {+ type = "Nullable(String)"+ }+ column "attributes" {+ type = "Map(String, String)"+ }+ column "series_fingerprint" {+ type = "Nullable(Int64)"+ }+ column "has_labels" {+ type = "Nullable(UInt8)"+ }+ column "retention_days" {+ type = "Nullable(Int32)"+ }+ engine "kafka" {+ collection = "warpstream_metrics"+ topic_list = "clickhouse_metrics"+ group_name = "clickhouse-metrics-avro2"+ format = "Avro"+ num_consumers = 8+ skip_broken_messages = 100+ poll_timeout_ms = 3000+ poll_max_batch_size = 1000+ thread_per_consumer = true+ }+ }+table "kafka_trace_spans_avro" {column "uuid" {type = "String"@@ -949,6 +1032,96 @@ SQL}}+ table "metric_attributes2" {+ order_by = ["team_id", "attribute_type", "time_bucket", "attribute_key", "attribute_value"]+ partition_by = "toDate(original_expiry_time_bucket)"+ ttl = "original_expiry_time_bucket"+ settings = {+ index_granularity = "8192"+ ttl_only_drop_parts = "1"+ }+ 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 "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 = 1+ }+ index "idx_attribute_value" {+ expr = "attribute_value"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attribute_key_n3" {+ expr = "attribute_key"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ index "idx_attribute_value_n3" {+ expr = "attribute_value"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ engine "replicated_aggregating_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes2"+ replica_name = "{replica}-{shard}"+ }+ }++ table "metric_attributes_distributed" {+ 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 "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 = "logs"+ remote_database = "posthog"+ remote_table = "metric_attributes2"+ }+ }+table "metric_samples" {column "team_id" {type = "Int32"@@ -1158,60 +1331,25 @@ SQL}}- table "metrics" {- column "time_bucket" {- type = "DateTime"- materialized = "toStartOfDay(timestamp)"- }- column "uuid" {- type = "String"+ table "metric_series2" {+ order_by = ["team_id", "metric_name", "series_fingerprint"]+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"}column "team_id" {type = "Int32"}- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "created_at" {- type = "DateTime64(6)"- materialized = "now()"- }- column "service_name" {- type = "LowCardinality(String)"- }column "metric_name" {type = "LowCardinality(String)"}+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }column "metric_type" {type = "LowCardinality(String)"}- column "value" {- type = "Float64"- codec = "Gorilla(8)"- }- column "count" {- type = "UInt64"- default = "1"- codec = "T64"- }- column "histogram_bounds" {- type = "Array(Float64)"- }- column "histogram_counts" {- type = "Array(UInt64)"- }column "unit" {type = "LowCardinality(String)"}@@ -1222,6 +1360,12 @@ SQLtype = "Bool"default = "false"}+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }column "resource_attributes" {type = "Map(LowCardinality(String), String)"}@@ -1229,38 +1373,96 @@ SQLtype = "UInt64"materialized = "cityHash64(resource_attributes)"}- column "instrumentation_scope" {- type = "String"- }- column "attributes_map_str" {+ column "attributes" {type = "Map(LowCardinality(String), String)"}- column "attributes_map_float" {- type = "Map(LowCardinality(String), Float64)"- }- column "time_minute" {- type = "DateTime"- alias = "toStartOfMinute(timestamp)"+ column "last_seen" {+ type = "DateTime64(6)"}- column "attributes" {- type = "Map(String, String)"- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"}- engine "distributed" {- cluster_name = "logs"- remote_database = "posthog"- remote_table = "metrics1"+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1}- }-- table "metrics1" {- order_by = ["team_id", "time_bucket", "service_name", "metric_name", "resource_fingerprint", "timestamp"]- partition_by = "toDate(timestamp)"- settings = {- index_granularity = "8192"- index_granularity_bytes = "104857600"- ttl_only_drop_parts = "1"+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1}+ index "idx_attr_keys" {+ expr = "mapKeys(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attr_values" {+ expr = "mapValues(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ engine "replicated_replacing_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"+ replica_name = "{replica}-{shard}"+ version_column = "last_seen"+ }+ }++ table "metric_series_distributed" {+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ materialized = "cityHash64(resource_attributes)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metric_series2"+ }+ }++ table "metrics" {column "time_bucket" {type = "DateTime"materialized = "toStartOfDay(timestamp)"@@ -1281,11 +1483,512 @@ SQLtype = "Int32"}column "timestamp" {- type = "DateTime64(6)"+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ 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_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(String, String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metrics1"+ }+ }++ table "metrics1" {+ order_by = ["team_id", "time_bucket", "service_name", "metric_name", "resource_fingerprint", "timestamp"]+ partition_by = "toDate(timestamp)"+ settings = {+ index_granularity = "8192"+ index_granularity_bytes = "104857600"+ ttl_only_drop_parts = "1"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfDay(timestamp)"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ 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_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(String, String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ index "idx_metric_name_set" {+ expr = "metric_name"+ type = "set(100)"+ granularity = 1+ }+ index "idx_metric_type_set" {+ expr = "metric_type"+ type = "set(10)"+ granularity = 1+ }+ index "idx_attributes_str_keys" {+ expr = "mapKeys(attributes_map_str)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attributes_str_values" {+ expr = "mapValues(attributes_map_str)"+ type = "bloom_filter(0.001)"+ granularity = 1+ }+ index "idx_observed_minmax" {+ expr = "observed_timestamp"+ type = "minmax"+ granularity = 1+ }+ projection "projection_aggregate_counts" {+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ toStartOfMinute(timestamp),+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ count() AS event_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value+GROUP BY+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint+SQL++ }+ engine "replicated_merge_tree" {+ zoo_path = "/clickhouse/tables/logs/{shard}/posthog.metrics1"+ replica_name = "{replica}"+ }+ }++ table "metrics2" {+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint", "timestamp"]+ partition_by = "toDate(original_expiry_timestamp)"+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"+ index_granularity_bytes = "104857600"+ ttl_only_drop_parts = "1"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ index "idx_metric_type_set" {+ expr = "metric_type"+ type = "set(10)"+ granularity = 1+ }+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1+ }+ index "idx_trace_id_bf" {+ expr = "trace_id"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_observed_minmax" {+ expr = "observed_timestamp"+ type = "minmax"+ granularity = 1+ }+ projection "projection_series_minute" {+ query = <<SQL+SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+GROUP BY+ team_id, metric_name, service_name, metric_type, resource_fingerprint, series_fingerprint, minute+SQL++ }+ projection "projection_series_activity" {+ query = <<SQL+SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+GROUP BY+ team_id, service_name, metric_name, metric_type, resource_fingerprint, series_fingerprint, hour+SQL++ }+ engine "replicated_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics2"+ replica_name = "{replica}-{shard}"+ }+ }++ table "metrics2_input" {+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ engine "null" {+ }+ }++ table "metrics_distributed" {+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"}column "observed_timestamp" {type = "DateTime64(6)"}+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }column "created_at" {type = "DateTime64(6)"materialized = "now()"@@ -1293,9 +1996,6 @@ SQLcolumn "service_name" {type = "LowCardinality(String)"}- column "metric_name" {- type = "LowCardinality(String)"- }column "metric_type" {type = "LowCardinality(String)"}@@ -1314,6 +2014,19 @@ SQLcolumn "histogram_counts" {type = "Array(UInt64)"}+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }column "unit" {type = "LowCardinality(String)"}@@ -1324,77 +2037,22 @@ SQLtype = "Bool"default = "false"}- 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_map_float" {- type = "Map(LowCardinality(String), Float64)"- }- column "time_minute" {- type = "DateTime"- alias = "toStartOfMinute(timestamp)"- }- column "attributes" {- type = "Map(String, String)"- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"- }- index "idx_metric_name_set" {- expr = "metric_name"- type = "set(100)"- granularity = 1- }- index "idx_metric_type_set" {- expr = "metric_type"- type = "set(10)"- granularity = 1- }- index "idx_attributes_str_keys" {- expr = "mapKeys(attributes_map_str)"- type = "bloom_filter(0.01)"- granularity = 1- }- index "idx_attributes_str_values" {- expr = "mapValues(attributes_map_str)"- type = "bloom_filter(0.001)"- granularity = 1+ column "_partition" {+ type = "UInt32"}- index "idx_observed_minmax" {- expr = "observed_timestamp"- type = "minmax"- granularity = 1+ column "_topic" {+ type = "String"}- projection "projection_aggregate_counts" {- query = <<SQL-SELECT- team_id,- time_bucket,- toStartOfMinute(timestamp),- service_name,- metric_name,- metric_type,- resource_fingerprint,- count() AS event_count,- sum(value) AS total_value,- min(value) AS min_value,- max(value) AS max_value-GROUP BY- team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint-SQL-+ column "_offset" {+ type = "UInt64"}- engine "replicated_merge_tree" {- zoo_path = "/clickhouse/tables/logs/{shard}/posthog.metrics1"- replica_name = "{replica}"+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metrics2"}}@@ -2499,17 +3157,155 @@ SQLcolumn "max_offset" {type = "SimpleAggregateFunction(max, UInt64)"}- column "max_observed_timestamp" {- type = "SimpleAggregateFunction(max, DateTime64(6))"+ 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_metrics_avro2_mv" {+ to_table = "posthog.metrics2_input"+ query = <<SQL+SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp+ + toIntervalDay(+ assumeNotNull(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(90))+ )+ )+ ) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ toBool(ifNull(has_labels, 1)) AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS resource_attributes,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS attributes,+ _partition,+ _topic,+ _offset+FROM posthog.kafka_metrics_avro2+WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0+SQL++ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "String"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "String"+ }+ column "metric_type" {+ type = "String"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "String"+ }+ column "aggregation_temporality" {+ type = "String"+ }+ column "is_monotonic" {+ type = "UInt8"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(String, String)"}- column "max_timestamp" {- type = "SimpleAggregateFunction(max, DateTime64(6))"+ column "attributes" {+ type = "Map(String, String)"}- column "max_created_at" {- type = "SimpleAggregateFunction(max, DateTime)"+ column "_partition" {+ type = "UInt64"}- column "max_lag" {- type = "SimpleAggregateFunction(max, Decimal(18, 6))"+ column "_topic" {+ type = "LowCardinality(String)"+ }+ column "_offset" {+ type = "UInt64"}}@@ -3175,6 +3971,293 @@ SQL}}+ materialized_view "metrics2_input_to_metric_attributes" {+ to_table = "posthog.metric_attributes2"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_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 "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 "metrics2_input_to_metric_series" {+ to_table = "posthog.metric_series2"+ query = <<SQL+SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM posthog.metrics2_input+WHERE has_labels+SQL++ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ }++ materialized_view "metrics2_input_to_metrics" {+ to_table = "posthog.metrics2"+ query = <<SQL+SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM posthog.metrics2_input+SQL++ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ }++ materialized_view "metrics2_input_to_resource_attributes" {+ to_table = "posthog.metric_attributes2"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_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 "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 "ops_query_log_archive_mv" {to_table = "posthog.writable_query_log_archive"query = <<SQLdiff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hclindex 42e50dd549da..da94d9532082 100644--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl@@ -73,6 +73,89 @@ database "posthog" {}}+ table "kafka_metrics_avro2" {+ settings = {+ input_format_avro_allow_missing_fields = "1"+ }+ column "uuid" {+ type = "String"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Nullable(Int32)"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "Nullable(String)"+ }+ column "metric_name" {+ type = "Nullable(String)"+ }+ column "metric_type" {+ type = "Nullable(String)"+ }+ column "value" {+ type = "Nullable(Float64)"+ }+ column "count" {+ type = "Nullable(Int64)"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(Int64)"+ }+ column "unit" {+ type = "Nullable(String)"+ }+ column "aggregation_temporality" {+ type = "Nullable(String)"+ }+ column "is_monotonic" {+ type = "Nullable(UInt8)"+ }+ column "resource_attributes" {+ type = "Map(String, String)"+ }+ column "instrumentation_scope" {+ type = "Nullable(String)"+ }+ column "attributes" {+ type = "Map(String, String)"+ }+ column "series_fingerprint" {+ type = "Nullable(Int64)"+ }+ column "has_labels" {+ type = "Nullable(UInt8)"+ }+ column "retention_days" {+ type = "Nullable(Int32)"+ }+ engine "kafka" {+ collection = "warpstream_metrics"+ topic_list = "clickhouse_metrics"+ group_name = "clickhouse-metrics-avro2"+ format = "Avro"+ num_consumers = 8+ skip_broken_messages = 100+ poll_timeout_ms = 3000+ poll_max_batch_size = 1000+ thread_per_consumer = true+ }+ }+table "kafka_trace_spans_avro" {column "uuid" {type = "String"@@ -949,6 +1032,96 @@ SQL}}+ table "metric_attributes2" {+ order_by = ["team_id", "attribute_type", "time_bucket", "attribute_key", "attribute_value"]+ partition_by = "toDate(original_expiry_time_bucket)"+ ttl = "original_expiry_time_bucket"+ settings = {+ index_granularity = "8192"+ ttl_only_drop_parts = "1"+ }+ 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 "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 = 1+ }+ index "idx_attribute_value" {+ expr = "attribute_value"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attribute_key_n3" {+ expr = "attribute_key"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ index "idx_attribute_value_n3" {+ expr = "attribute_value"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ engine "replicated_aggregating_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes2"+ replica_name = "{replica}-{shard}"+ }+ }++ table "metric_attributes_distributed" {+ 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 "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 = "logs"+ remote_database = "posthog"+ remote_table = "metric_attributes2"+ }+ }+table "metric_samples" {column "team_id" {type = "Int32"@@ -1158,60 +1331,25 @@ SQL}}- table "metrics" {- column "time_bucket" {- type = "DateTime"- materialized = "toStartOfDay(timestamp)"- }- column "uuid" {- type = "String"+ table "metric_series2" {+ order_by = ["team_id", "metric_name", "series_fingerprint"]+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"}column "team_id" {type = "Int32"}- column "trace_id" {- type = "String"- }- column "span_id" {- type = "String"- }- column "trace_flags" {- type = "Int32"- }- column "timestamp" {- type = "DateTime64(6)"- }- column "observed_timestamp" {- type = "DateTime64(6)"- }- column "created_at" {- type = "DateTime64(6)"- materialized = "now()"- }- column "service_name" {- type = "LowCardinality(String)"- }column "metric_name" {type = "LowCardinality(String)"}+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }column "metric_type" {type = "LowCardinality(String)"}- column "value" {- type = "Float64"- codec = "Gorilla(8)"- }- column "count" {- type = "UInt64"- default = "1"- codec = "T64"- }- column "histogram_bounds" {- type = "Array(Float64)"- }- column "histogram_counts" {- type = "Array(UInt64)"- }column "unit" {type = "LowCardinality(String)"}@@ -1222,6 +1360,12 @@ SQLtype = "Bool"default = "false"}+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }column "resource_attributes" {type = "Map(LowCardinality(String), String)"}@@ -1229,38 +1373,96 @@ SQLtype = "UInt64"materialized = "cityHash64(resource_attributes)"}- column "instrumentation_scope" {- type = "String"- }- column "attributes_map_str" {+ column "attributes" {type = "Map(LowCardinality(String), String)"}- column "attributes_map_float" {- type = "Map(LowCardinality(String), Float64)"- }- column "time_minute" {- type = "DateTime"- alias = "toStartOfMinute(timestamp)"+ column "last_seen" {+ type = "DateTime64(6)"}- column "attributes" {- type = "Map(String, String)"- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"}- engine "distributed" {- cluster_name = "logs"- remote_database = "posthog"- remote_table = "metrics1"+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1}- }-- table "metrics1" {- order_by = ["team_id", "time_bucket", "service_name", "metric_name", "resource_fingerprint", "timestamp"]- partition_by = "toDate(timestamp)"- settings = {- index_granularity = "8192"- index_granularity_bytes = "104857600"- ttl_only_drop_parts = "1"+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1}+ index "idx_attr_keys" {+ expr = "mapKeys(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attr_values" {+ expr = "mapValues(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ engine "replicated_replacing_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"+ replica_name = "{replica}-{shard}"+ version_column = "last_seen"+ }+ }++ table "metric_series_distributed" {+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ materialized = "cityHash64(resource_attributes)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metric_series2"+ }+ }++ table "metrics" {column "time_bucket" {type = "DateTime"materialized = "toStartOfDay(timestamp)"@@ -1281,11 +1483,512 @@ SQLtype = "Int32"}column "timestamp" {- type = "DateTime64(6)"+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ 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_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(String, String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metrics1"+ }+ }++ table "metrics1" {+ order_by = ["team_id", "time_bucket", "service_name", "metric_name", "resource_fingerprint", "timestamp"]+ partition_by = "toDate(timestamp)"+ settings = {+ index_granularity = "8192"+ index_granularity_bytes = "104857600"+ ttl_only_drop_parts = "1"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfDay(timestamp)"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ 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_map_float" {+ type = "Map(LowCardinality(String), Float64)"+ }+ column "time_minute" {+ type = "DateTime"+ alias = "toStartOfMinute(timestamp)"+ }+ column "attributes" {+ type = "Map(String, String)"+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"+ }+ index "idx_metric_name_set" {+ expr = "metric_name"+ type = "set(100)"+ granularity = 1+ }+ index "idx_metric_type_set" {+ expr = "metric_type"+ type = "set(10)"+ granularity = 1+ }+ index "idx_attributes_str_keys" {+ expr = "mapKeys(attributes_map_str)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attributes_str_values" {+ expr = "mapValues(attributes_map_str)"+ type = "bloom_filter(0.001)"+ granularity = 1+ }+ index "idx_observed_minmax" {+ expr = "observed_timestamp"+ type = "minmax"+ granularity = 1+ }+ projection "projection_aggregate_counts" {+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ toStartOfMinute(timestamp),+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ count() AS event_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value+GROUP BY+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint+SQL++ }+ engine "replicated_merge_tree" {+ zoo_path = "/clickhouse/tables/logs/{shard}/posthog.metrics1"+ replica_name = "{replica}"+ }+ }++ table "metrics2" {+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint", "timestamp"]+ partition_by = "toDate(original_expiry_timestamp)"+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"+ index_granularity_bytes = "104857600"+ ttl_only_drop_parts = "1"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ index "idx_metric_type_set" {+ expr = "metric_type"+ type = "set(10)"+ granularity = 1+ }+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1+ }+ index "idx_trace_id_bf" {+ expr = "trace_id"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_observed_minmax" {+ expr = "observed_timestamp"+ type = "minmax"+ granularity = 1+ }+ projection "projection_series_minute" {+ query = <<SQL+SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+GROUP BY+ team_id, metric_name, service_name, metric_type, resource_fingerprint, series_fingerprint, minute+SQL++ }+ projection "projection_series_activity" {+ query = <<SQL+SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+GROUP BY+ team_id, service_name, metric_name, metric_type, resource_fingerprint, series_fingerprint, hour+SQL++ }+ engine "replicated_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics2"+ replica_name = "{replica}-{shard}"+ }+ }++ table "metrics2_input" {+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ engine "null" {+ }+ }++ table "metrics_distributed" {+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"}column "observed_timestamp" {type = "DateTime64(6)"}+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }column "created_at" {type = "DateTime64(6)"materialized = "now()"@@ -1293,9 +1996,6 @@ SQLcolumn "service_name" {type = "LowCardinality(String)"}- column "metric_name" {- type = "LowCardinality(String)"- }column "metric_type" {type = "LowCardinality(String)"}@@ -1314,6 +2014,19 @@ SQLcolumn "histogram_counts" {type = "Array(UInt64)"}+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }column "unit" {type = "LowCardinality(String)"}@@ -1324,77 +2037,22 @@ SQLtype = "Bool"default = "false"}- 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_map_float" {- type = "Map(LowCardinality(String), Float64)"- }- column "time_minute" {- type = "DateTime"- alias = "toStartOfMinute(timestamp)"- }- column "attributes" {- type = "Map(String, String)"- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"- }- index "idx_metric_name_set" {- expr = "metric_name"- type = "set(100)"- granularity = 1- }- index "idx_metric_type_set" {- expr = "metric_type"- type = "set(10)"- granularity = 1- }- index "idx_attributes_str_keys" {- expr = "mapKeys(attributes_map_str)"- type = "bloom_filter(0.01)"- granularity = 1- }- index "idx_attributes_str_values" {- expr = "mapValues(attributes_map_str)"- type = "bloom_filter(0.001)"- granularity = 1+ column "_partition" {+ type = "UInt32"}- index "idx_observed_minmax" {- expr = "observed_timestamp"- type = "minmax"- granularity = 1+ column "_topic" {+ type = "String"}- projection "projection_aggregate_counts" {- query = <<SQL-SELECT- team_id,- time_bucket,- toStartOfMinute(timestamp),- service_name,- metric_name,- metric_type,- resource_fingerprint,- count() AS event_count,- sum(value) AS total_value,- min(value) AS min_value,- max(value) AS max_value-GROUP BY- team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint-SQL-+ column "_offset" {+ type = "UInt64"}- engine "replicated_merge_tree" {- zoo_path = "/clickhouse/tables/logs/{shard}/posthog.metrics1"- replica_name = "{replica}"+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metrics2"}}@@ -2481,17 +3139,155 @@ SQLcolumn "max_offset" {type = "SimpleAggregateFunction(max, UInt64)"}- column "max_observed_timestamp" {- type = "SimpleAggregateFunction(max, DateTime64(6))"+ 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_metrics_avro2_mv" {+ to_table = "posthog.metrics2_input"+ query = <<SQL+SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp+ + toIntervalDay(+ assumeNotNull(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(90))+ )+ )+ ) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ toBool(ifNull(has_labels, 1)) AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS resource_attributes,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS attributes,+ _partition,+ _topic,+ _offset+FROM posthog.kafka_metrics_avro2+WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0+SQL++ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "String"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "String"+ }+ column "metric_type" {+ type = "String"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "String"+ }+ column "aggregation_temporality" {+ type = "String"+ }+ column "is_monotonic" {+ type = "UInt8"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(String, String)"}- column "max_timestamp" {- type = "SimpleAggregateFunction(max, DateTime64(6))"+ column "attributes" {+ type = "Map(String, String)"}- column "max_created_at" {- type = "SimpleAggregateFunction(max, DateTime)"+ column "_partition" {+ type = "UInt64"}- column "max_lag" {- type = "SimpleAggregateFunction(max, Decimal(18, 6))"+ column "_topic" {+ type = "LowCardinality(String)"+ }+ column "_offset" {+ type = "UInt64"}}@@ -3157,6 +3953,293 @@ SQL}}+ materialized_view "metrics2_input_to_metric_attributes" {+ to_table = "posthog.metric_attributes2"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_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 "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 "metrics2_input_to_metric_series" {+ to_table = "posthog.metric_series2"+ query = <<SQL+SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM posthog.metrics2_input+WHERE has_labels+SQL++ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ }++ materialized_view "metrics2_input_to_metrics" {+ to_table = "posthog.metrics2"+ query = <<SQL+SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM posthog.metrics2_input+SQL++ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ }++ materialized_view "metrics2_input_to_resource_attributes" {+ to_table = "posthog.metric_attributes2"+ query = <<SQL+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_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 "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 "ops_query_log_archive_mv" {to_table = "posthog.writable_query_log_archive"query = <<SQLdiff --git a/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl b/posthog/clickhouse/hcl/roles/logs/dev/patches.hclindex bc5de0715382..a78d38135f23 100644--- a/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl+++ b/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl@@ -32,6 +32,22 @@ database "posthog" {}}+ patch_table "kafka_metrics_avro2" {+ engine "kafka" {+ collection = "warpstream_metrics"+ topic_list = "clickhouse_metrics"+ group_name = "clickhouse-metrics-avro2"+ format = "Avro"+ num_consumers = 2+ max_block_size = 4096+ skip_broken_messages = 100+ poll_timeout_ms = 10000+ poll_max_batch_size = 4096+ flush_interval_ms = 10000+ thread_per_consumer = true+ }+ }+patch_table "trace_attributes_distributed" {engine "distributed" {cluster_name = "logs"diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/sql/kafka_metrics_avro2_mv.sql b/posthog/clickhouse/hcl/roles/logs/metrics/sql/kafka_metrics_avro2_mv.sqlnew file mode 100644index 000000000000..a4d0db8429eb--- /dev/null+++ b/posthog/clickhouse/hcl/roles/logs/metrics/sql/kafka_metrics_avro2_mv.sql@@ -0,0 +1,33 @@+SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp + toIntervalDay(assumeNotNull(if((retention_days IS NOT NULL) AND (retention_days > 0), retention_days, toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(90))))) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ toBool(ifNull(has_labels, 1)) AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if(toBool(ifNull(has_labels, 1)), mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)), CAST(map(), 'Map(String, String)')) AS resource_attributes,+ if(toBool(ifNull(has_labels, 1)), mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)), CAST(map(), 'Map(String, String)')) AS attributes,+ _partition,+ _topic,+ _offset+FROM posthog.kafka_metrics_avro2+WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_attributes.sql b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_attributes.sqlnew file mode 100644index 000000000000..291d7f196d2c--- /dev/null+++ b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_attributes.sql@@ -0,0 +1,27 @@+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes+ )diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_series.sql b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_series.sqlnew file mode 100644index 000000000000..43e893aebd5c--- /dev/null+++ b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metric_series.sql@@ -0,0 +1,16 @@+SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM posthog.metrics2_input+WHERE has_labelsdiff --git a/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metrics.sql b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metrics.sqlnew file mode 100644index 000000000000..b2742dde15db--- /dev/null+++ b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_metrics.sql@@ -0,0 +1,27 @@+SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM posthog.metrics2_inputdiff --git a/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_resource_attributes.sql b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_resource_attributes.sqlnew file mode 100644index 000000000000..df4d039e6caa--- /dev/null+++ b/posthog/clickhouse/hcl/roles/logs/metrics/sql/metrics2_input_to_resource_attributes.sql@@ -0,0 +1,27 @@+SELECT+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes+ )diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/tables.hcl b/posthog/clickhouse/hcl/roles/logs/metrics/tables.hclindex 8aa1e5cbef00..457fd7883462 100644--- a/posthog/clickhouse/hcl/roles/logs/metrics/tables.hcl+++ b/posthog/clickhouse/hcl/roles/logs/metrics/tables.hcl@@ -627,4 +627,917 @@ SQLtype = "DateTime64(6)"}}++ table "kafka_metrics_avro2" {+ column "uuid" {+ type = "String"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Nullable(Int32)"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "Nullable(String)"+ }+ column "metric_name" {+ type = "Nullable(String)"+ }+ column "metric_type" {+ type = "Nullable(String)"+ }+ column "value" {+ type = "Nullable(Float64)"+ }+ column "count" {+ type = "Nullable(Int64)"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(Int64)"+ }+ column "unit" {+ type = "Nullable(String)"+ }+ column "aggregation_temporality" {+ type = "Nullable(String)"+ }+ column "is_monotonic" {+ type = "Nullable(UInt8)"+ }+ column "resource_attributes" {+ type = "Map(String, String)"+ }+ column "instrumentation_scope" {+ type = "Nullable(String)"+ }+ column "attributes" {+ type = "Map(String, String)"+ }+ column "series_fingerprint" {+ type = "Nullable(Int64)"+ }+ column "has_labels" {+ type = "Nullable(UInt8)"+ }+ column "retention_days" {+ type = "Nullable(Int32)"+ }+ engine "kafka" {+ collection = "warpstream_metrics"+ topic_list = "clickhouse_metrics"+ group_name = "clickhouse-metrics-avro2"+ format = "Avro"+ num_consumers = 8+ skip_broken_messages = 100+ poll_timeout_ms = 3000+ poll_max_batch_size = 1000+ thread_per_consumer = true+ }+ settings = {+ input_format_avro_allow_missing_fields = "1"+ }+ }+ materialized_view "kafka_metrics_avro2_mv" {+ to_table = "posthog.metrics2_input"+ query = file("sql/kafka_metrics_avro2_mv.sql")+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "String"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "String"+ }+ column "metric_type" {+ type = "String"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "String"+ }+ column "aggregation_temporality" {+ type = "String"+ }+ column "is_monotonic" {+ type = "UInt8"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(String, String)"+ }+ column "attributes" {+ type = "Map(String, String)"+ }+ column "_partition" {+ type = "UInt64"+ }+ column "_topic" {+ type = "LowCardinality(String)"+ }+ column "_offset" {+ type = "UInt64"+ }+ }+ table "metrics2_input" {+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ engine "null" {}+ }+ table "metrics2" {+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint", "timestamp"]+ partition_by = "toDate(original_expiry_timestamp)"+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"+ index_granularity_bytes = "104857600"+ ttl_only_drop_parts = "1"+ }+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ index "idx_metric_type_set" {+ expr = "metric_type"+ type = "set(10)"+ granularity = 1+ }+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1+ }+ index "idx_trace_id_bf" {+ expr = "trace_id"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_observed_minmax" {+ expr = "observed_timestamp"+ type = "minmax"+ granularity = 1+ }+ projection "projection_series_minute" {+ query = <<SQL+SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+GROUP BY+ team_id, metric_name, service_name, metric_type, resource_fingerprint, series_fingerprint, minute+SQL++ }+ projection "projection_series_activity" {+ query = <<SQL+SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+GROUP BY+ team_id, service_name, metric_name, metric_type, resource_fingerprint, series_fingerprint, hour+SQL++ }+ engine "replicated_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics2"+ replica_name = "{replica}-{shard}"+ }+ }+ table "metric_series2" {+ order_by = ["team_id", "metric_name", "series_fingerprint"]+ ttl = "original_expiry_timestamp"+ settings = {+ index_granularity = "8192"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ materialized = "cityHash64(resource_attributes)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ index "idx_service_set" {+ expr = "service_name"+ type = "set(1000)"+ granularity = 1+ }+ index "idx_resource_fingerprint" {+ expr = "resource_fingerprint"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attr_keys" {+ expr = "mapKeys(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attr_values" {+ expr = "mapValues(attributes)"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ engine "replicated_replacing_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"+ replica_name = "{replica}-{shard}"+ version_column = "last_seen"+ }+ }+ table "metric_attributes2" {+ order_by = ["team_id", "attribute_type", "time_bucket", "attribute_key", "attribute_value"]+ partition_by = "toDate(original_expiry_time_bucket)"+ ttl = "original_expiry_time_bucket"+ settings = {+ index_granularity = "8192"+ ttl_only_drop_parts = "1"+ }+ 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 "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 = 1+ }+ index "idx_attribute_value" {+ expr = "attribute_value"+ type = "bloom_filter(0.01)"+ granularity = 1+ }+ index "idx_attribute_key_n3" {+ expr = "attribute_key"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ index "idx_attribute_value_n3" {+ expr = "attribute_value"+ type = "ngrambf_v1(3, 32768, 3, 0)"+ granularity = 1+ }+ engine "replicated_aggregating_merge_tree" {+ zoo_path = "/clickhouse/tables/noshard/posthog.metric_attributes2"+ replica_name = "{replica}-{shard}"+ }+ }+ table "metrics_distributed" {+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "time_bucket" {+ type = "DateTime"+ materialized = "toStartOfHour(timestamp)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ default = "0"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ codec = "DoubleDelta"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "created_at" {+ type = "DateTime64(6)"+ materialized = "now()"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ codec = "Gorilla(8)"+ }+ column "count" {+ type = "UInt64"+ default = "1"+ codec = "T64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ default = "false"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ engine "distributed" {+ cluster_name = "posthog_single_shard"+ remote_database = "posthog"+ remote_table = "metrics2"+ }+ }+ table "metric_series_distributed" {+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ codec = "Delta(8), Default"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ default = "false"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ materialized = "cityHash64(resource_attributes)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ engine "distributed" {+ cluster_name = "posthog_single_shard"+ remote_database = "posthog"+ remote_table = "metric_series2"+ }+ }+ table "metric_attributes_distributed" {+ 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 "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 = "metric_attributes2"+ }+ }+ materialized_view "metrics2_input_to_metrics" {+ to_table = "posthog.metrics2"+ query = file("sql/metrics2_input_to_metrics.sql")+ column "uuid" {+ type = "String"+ }+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "resource_fingerprint" {+ type = "UInt64"+ }+ column "timestamp" {+ type = "DateTime64(6)"+ }+ column "observed_timestamp" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "value" {+ type = "Float64"+ }+ column "count" {+ type = "UInt64"+ }+ column "histogram_bounds" {+ type = "Array(Float64)"+ }+ column "histogram_counts" {+ type = "Array(UInt64)"+ }+ column "trace_id" {+ type = "String"+ }+ column "span_id" {+ type = "String"+ }+ column "trace_flags" {+ type = "Int32"+ }+ column "has_labels" {+ type = "Bool"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "_partition" {+ type = "UInt32"+ }+ column "_topic" {+ type = "String"+ }+ column "_offset" {+ type = "UInt64"+ }+ }+ materialized_view "metrics2_input_to_metric_series" {+ to_table = "posthog.metric_series2"+ query = file("sql/metrics2_input_to_metric_series.sql")+ column "team_id" {+ type = "Int32"+ }+ column "metric_name" {+ type = "LowCardinality(String)"+ }+ column "series_fingerprint" {+ type = "UInt64"+ }+ column "metric_type" {+ type = "LowCardinality(String)"+ }+ column "unit" {+ type = "LowCardinality(String)"+ }+ column "aggregation_temporality" {+ type = "LowCardinality(String)"+ }+ column "is_monotonic" {+ type = "Bool"+ }+ column "service_name" {+ type = "LowCardinality(String)"+ }+ column "instrumentation_scope" {+ type = "String"+ }+ column "resource_attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "attributes" {+ type = "Map(LowCardinality(String), String)"+ }+ column "last_seen" {+ type = "DateTime64(6)"+ }+ column "original_expiry_timestamp" {+ type = "DateTime64(6)"+ }+ }+ materialized_view "metrics2_input_to_metric_attributes" {+ to_table = "posthog.metric_attributes2"+ query = file("sql/metrics2_input_to_metric_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 "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 "metrics2_input_to_resource_attributes" {+ to_table = "posthog.metric_attributes2"+ query = file("sql/metrics2_input_to_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 "attribute_key" {+ type = "LowCardinality(String)"+ }+ column "attribute_value" {+ type = "String"+ }+ column "attribute_type" {+ type = "LowCardinality(String)"+ }+ column "attribute_count" {+ type = "SimpleAggregateFunction(sum, UInt64)"+ }+ }}diff --git a/posthog/clickhouse/hcl/roles/logs/shared/patches.hcl b/posthog/clickhouse/hcl/roles/logs/shared/patches.hclindex e69cc7865304..783b83970b45 100644--- a/posthog/clickhouse/hcl/roles/logs/shared/patches.hcl+++ b/posthog/clickhouse/hcl/roles/logs/shared/patches.hcl@@ -93,9 +93,37 @@ database "posthog" {}}- # The metrics ingest chain (roles/logs/metrics) is declared in the local shape+ patch_table "metrics_distributed" {+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metrics2"+ }+ }++ patch_table "metric_series_distributed" {+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metric_series2"+ }+ }++ patch_table "metric_attributes_distributed" {+ engine "distributed" {+ cluster_name = "logs"+ remote_database = "posthog"+ remote_table = "metric_attributes2"+ }+ }++ # The v1 metrics ingest chain (roles/logs/metrics) is declared in the local shape# (noshard ZK paths, posthog_single_shard reader); the cloud envs keep their- # per-shard logs-cluster paths and read through the logs cluster.+ # per-shard logs-cluster paths for metrics1, metric_attributes and+ # metrics_kafka_metrics, and read through the logs cluster. The v2 chain+ # (metrics2_input, metrics2, metric_series2, metric_attributes2) stays on the+ # noshard path in every env, like metric_samples1 and metric_series1, and only+ # its readers are patched above.patch_table "metrics" {engine "distributed" {cluster_name = "logs"diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sqlindex 21860a6fbcc1..693bbeda924f 100644--- a/posthog/clickhouse/hcl/sql/dev/logs.sql+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql@@ -23,6 +23,30 @@ CREATE TABLE posthog.kafka_metrics_avro (attributes Map(String, String),series_fingerprint Nullable(Int64)) ENGINE = Kafka(warpstream_metrics) SETTINGS kafka_flush_interval_ms = 10000, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro-new', kafka_max_block_size = 4096, kafka_num_consumers = 2, kafka_poll_max_batch_size = 4096, kafka_poll_timeout_ms = 10000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';+CREATE TABLE posthog.kafka_metrics_avro2 (+ uuid String,+ trace_id String,+ span_id String,+ trace_flags Nullable(Int32),+ timestamp DateTime64(6),+ observed_timestamp DateTime64(6),+ service_name Nullable(String),+ metric_name Nullable(String),+ metric_type Nullable(String),+ value Nullable(Float64),+ count Nullable(Int64),+ histogram_bounds Array(Float64),+ histogram_counts Array(Int64),+ unit Nullable(String),+ aggregation_temporality Nullable(String),+ is_monotonic Nullable(UInt8),+ resource_attributes Map(String, String),+ instrumentation_scope Nullable(String),+ attributes Map(String, String),+ series_fingerprint Nullable(Int64),+ has_labels Nullable(UInt8),+ retention_days Nullable(Int32)+) ENGINE = Kafka(warpstream_metrics) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_flush_interval_ms = 10000, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro2', kafka_max_block_size = 4096, kafka_num_consumers = 2, kafka_poll_max_batch_size = 4096, kafka_poll_timeout_ms = 10000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';CREATE TABLE posthog.kafka_trace_spans_avro (uuid String,trace_id String,@@ -265,6 +289,30 @@ CREATE TABLE posthog.metric_attributes (INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/logs/{shard}/posthog.metric_attributes', '{replica}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(time_bucket) SETTINGS deduplicate_merge_projection_mode = 'drop', index_granularity = 8192;+CREATE TABLE posthog.metric_attributes2 (+ team_id Int32,+ time_bucket DateTime64(0),+ original_expiry_time_bucket DateTime64(0),+ service_name LowCardinality(String),+ 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 1,+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metric_attributes_distributed (+ team_id Int32,+ time_bucket DateTime64(0),+ original_expiry_time_bucket DateTime64(0),+ service_name LowCardinality(String),+ attribute_key LowCardinality(String),+ attribute_value String,+ attribute_type LowCardinality(String),+ attribute_count SimpleAggregateFunction(sum, UInt64)+) ENGINE = Distributed('logs', 'posthog', 'metric_attributes2');CREATE TABLE posthog.metric_samples1 (team_id Int32,metric_name LowCardinality(String),@@ -295,6 +343,42 @@ CREATE TABLE posthog.metric_series1 (INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series1', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL toDateTime(last_seen) + toIntervalDay(90) SETTINGS index_granularity = 8192;+CREATE TABLE posthog.metric_series2 (+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ metric_type LowCardinality(String),+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ service_name LowCardinality(String),+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ attributes Map(LowCardinality(String), String),+ last_seen DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;+CREATE TABLE posthog.metric_series_distributed (+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ metric_type LowCardinality(String),+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ service_name LowCardinality(String),+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ attributes Map(LowCardinality(String), String),+ last_seen DateTime64(6),+ original_expiry_timestamp DateTime64(6)+) ENGINE = Distributed('logs', 'posthog', 'metric_series2');CREATE TABLE posthog.metrics1 (time_bucket DateTime MATERIALIZED toStartOfDay(timestamp),uuid String,@@ -342,6 +426,126 @@ CREATE TABLE posthog.metrics1 (GROUP BYteam_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint)) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.metrics1', '{replica}') ORDER BY (team_id, time_bucket, service_name, metric_name, resource_fingerprint, timestamp) PARTITION BY toDate(timestamp) SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metrics2 (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ resource_fingerprint UInt64 DEFAULT 0,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64 CODEC(Gorilla(8)),+ count UInt64 DEFAULT 1 CODEC(T64),+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool DEFAULT false,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ instrumentation_scope String,+ _partition UInt32,+ _topic String,+ _offset UInt64,+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_trace_id_bf trace_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,+ PROJECTION projection_series_minute (SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+GROUP BY+ team_id, metric_name, service_name, metric_type, resource_fingerprint, series_fingerprint, minute),+ PROJECTION projection_series_activity (SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+GROUP BY+ team_id, service_name, metric_name, metric_type, resource_fingerprint, series_fingerprint, hour)+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.metrics2', '{replica}-{shard}') ORDER BY (team_id, metric_name, time_bucket, series_fingerprint, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metrics2_input (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64,+ resource_fingerprint UInt64,+ timestamp DateTime64(6),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64,+ count UInt64,+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool,+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ attributes Map(LowCardinality(String), String),+ _partition UInt32,+ _topic String,+ _offset UInt64+) ENGINE = Null();+CREATE TABLE posthog.metrics_distributed (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ resource_fingerprint UInt64 DEFAULT 0,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64 CODEC(Gorilla(8)),+ count UInt64 DEFAULT 1 CODEC(T64),+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool DEFAULT false,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ instrumentation_scope String,+ _partition UInt32,+ _topic String,+ _offset UInt64+) ENGINE = Distributed('logs', 'posthog', 'metrics2');CREATE TABLE posthog.metrics_kafka_metrics (_partition UInt32,_topic String,@@ -656,6 +860,56 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_kafka_metrics_mv TO posthog.logFROM posthog.logs34GROUP BY_partition, _topic;+CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp+ + toIntervalDay(+ assumeNotNull(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(90))+ )+ )+ ) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ toBool(ifNull(has_labels, 1)) AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS resource_attributes,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS attributes,+ _partition,+ _topic,+ _offset+FROM posthog.kafka_metrics_avro2+WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0;CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro_kafka_metrics_mv TO posthog.metrics_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,@@ -900,6 +1154,103 @@ FROMGROUP BYteam_id, time_bucket, service_name, resource_fingerprint, resource_attributes);+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_attributes TO posthog.metric_attributes2 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), 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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes+ );+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metric_series2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM posthog.metrics2_input+WHERE has_labels;+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metrics TO posthog.metrics2 (uuid String, team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name LowCardinality(String), metric_type LowCardinality(String), value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, instrumentation_scope String, _partition UInt32, _topic String, _offset UInt64) AS SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM posthog.metrics2_input;+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_resource_attributes TO posthog.metric_attributes2 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), 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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes+ );CREATE MATERIALIZED VIEW posthog.ops_query_log_archive_mv TO posthog.writable_query_log_archive (hostname LowCardinality(String), user LowCardinality(String), query_id String, initial_query_id String, is_initial_query UInt8, type Enum8('QueryStart'=1, 'QueryFinish'=2, 'ExceptionBeforeStart'=3, 'ExceptionWhileProcessing'=4), event_date Date, event_time DateTime, event_time_microseconds DateTime64(6), query_start_time DateTime, query_start_time_microseconds DateTime64(6), query_duration_ms UInt64, read_rows UInt64, read_bytes UInt64, written_rows UInt64, written_bytes UInt64, result_rows UInt64, result_bytes UInt64, memory_usage UInt64, peak_threads_usage UInt64, current_database LowCardinality(String), query String, formatted_query String, normalized_query_hash UInt64, query_kind LowCardinality(String), exception_code Int32, exception String, stack_trace String, team_id Int64, log_comment String, ProfileEvents Map(LowCardinality(String), UInt64)) AS SELECThostname,user,diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sqlindex 86e506e0d9b1..9b1986e3eef5 100644--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql@@ -23,6 +23,30 @@ CREATE TABLE posthog.kafka_metrics_avro (attributes Map(String, String),series_fingerprint Nullable(Int64)) ENGINE = Kafka(warpstream_metrics) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-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 = 'clickhouse_metrics';+CREATE TABLE posthog.kafka_metrics_avro2 (+ uuid String,+ trace_id String,+ span_id String,+ trace_flags Nullable(Int32),+ timestamp DateTime64(6),+ observed_timestamp DateTime64(6),+ service_name Nullable(String),+ metric_name Nullable(String),+ metric_type Nullable(String),+ value Nullable(Float64),+ count Nullable(Int64),+ histogram_bounds Array(Float64),+ histogram_counts Array(Int64),+ unit Nullable(String),+ aggregation_temporality Nullable(String),+ is_monotonic Nullable(UInt8),+ resource_attributes Map(String, String),+ instrumentation_scope Nullable(String),+ attributes Map(String, String),+ series_fingerprint Nullable(Int64),+ has_labels Nullable(UInt8),+ retention_days Nullable(Int32)+) ENGINE = Kafka(warpstream_metrics) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro2', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';CREATE TABLE posthog.kafka_trace_spans_avro (uuid String,trace_id String,@@ -333,6 +357,30 @@ CREATE TABLE posthog.metric_attributes (INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes', '{replica}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(time_bucket) SETTINGS deduplicate_merge_projection_mode = 'drop', index_granularity = 8192;+CREATE TABLE posthog.metric_attributes2 (+ team_id Int32,+ time_bucket DateTime64(0),+ original_expiry_time_bucket DateTime64(0),+ service_name LowCardinality(String),+ 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 1,+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metric_attributes_distributed (+ team_id Int32,+ time_bucket DateTime64(0),+ original_expiry_time_bucket DateTime64(0),+ service_name LowCardinality(String),+ attribute_key LowCardinality(String),+ attribute_value String,+ attribute_type LowCardinality(String),+ attribute_count SimpleAggregateFunction(sum, UInt64)+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'metric_attributes2');CREATE TABLE posthog.metric_samples1 (team_id Int32,metric_name LowCardinality(String),@@ -363,6 +411,42 @@ CREATE TABLE posthog.metric_series1 (INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series1', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL toDateTime(last_seen) + toIntervalDay(90) SETTINGS index_granularity = 8192;+CREATE TABLE posthog.metric_series2 (+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ metric_type LowCardinality(String),+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ service_name LowCardinality(String),+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ attributes Map(LowCardinality(String), String),+ last_seen DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;+CREATE TABLE posthog.metric_series_distributed (+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ metric_type LowCardinality(String),+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ service_name LowCardinality(String),+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ attributes Map(LowCardinality(String), String),+ last_seen DateTime64(6),+ original_expiry_timestamp DateTime64(6)+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'metric_series2');CREATE TABLE posthog.metrics1 (time_bucket DateTime MATERIALIZED toStartOfDay(timestamp),uuid String,@@ -410,6 +494,126 @@ CREATE TABLE posthog.metrics1 (GROUP BYteam_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint)) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.metrics1', '{replica}') ORDER BY (team_id, time_bucket, service_name, metric_name, resource_fingerprint, timestamp) PARTITION BY toDate(timestamp) SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metrics2 (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ resource_fingerprint UInt64 DEFAULT 0,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64 CODEC(Gorilla(8)),+ count UInt64 DEFAULT 1 CODEC(T64),+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool DEFAULT false,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ instrumentation_scope String,+ _partition UInt32,+ _topic String,+ _offset UInt64,+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_trace_id_bf trace_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,+ PROJECTION projection_series_minute (SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+GROUP BY+ team_id, metric_name, service_name, metric_type, resource_fingerprint, series_fingerprint, minute),+ PROJECTION projection_series_activity (SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+GROUP BY+ team_id, service_name, metric_name, metric_type, resource_fingerprint, series_fingerprint, hour)+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.metrics2', '{replica}-{shard}') ORDER BY (team_id, metric_name, time_bucket, series_fingerprint, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metrics2_input (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64,+ resource_fingerprint UInt64,+ timestamp DateTime64(6),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64,+ count UInt64,+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool,+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ attributes Map(LowCardinality(String), String),+ _partition UInt32,+ _topic String,+ _offset UInt64+) ENGINE = Null();+CREATE TABLE posthog.metrics_distributed (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ resource_fingerprint UInt64 DEFAULT 0,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64 CODEC(Gorilla(8)),+ count UInt64 DEFAULT 1 CODEC(T64),+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool DEFAULT false,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ instrumentation_scope String,+ _partition UInt32,+ _topic String,+ _offset UInt64+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'metrics2');CREATE TABLE posthog.metrics_kafka_metrics (_partition UInt32,_topic String,@@ -696,6 +900,56 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_kafka_metrics_mv TO posthog.logFROM posthog.logs34GROUP BY_partition, _topic;+CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp+ + toIntervalDay(+ assumeNotNull(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(90))+ )+ )+ ) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ toBool(ifNull(has_labels, 1)) AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS resource_attributes,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS attributes,+ _partition,+ _topic,+ _offset+FROM posthog.kafka_metrics_avro2+WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0;CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro_kafka_metrics_mv TO posthog.metrics_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,@@ -995,6 +1249,103 @@ FROMGROUP BYteam_id, time_bucket, service_name, resource_fingerprint, resource_attributes);+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_attributes TO posthog.metric_attributes2 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), 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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes+ );+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metric_series2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM posthog.metrics2_input+WHERE has_labels;+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metrics TO posthog.metrics2 (uuid String, team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name LowCardinality(String), metric_type LowCardinality(String), value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, instrumentation_scope String, _partition UInt32, _topic String, _offset UInt64) AS SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM posthog.metrics2_input;+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_resource_attributes TO posthog.metric_attributes2 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), 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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_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 SELECTteam_id,original_expiry_time_bucket,diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sqlindex 0d20be3fae67..73e7a8579fc8 100644--- a/posthog/clickhouse/hcl/sql/local-single/all.sql+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql@@ -616,6 +616,30 @@ CREATE TABLE posthog.kafka_metrics_avro (attributes Map(String, String),series_fingerprint Nullable(Int64)) ENGINE = Kafka(warpstream_metrics) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-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 = 'clickhouse_metrics';+CREATE TABLE posthog.kafka_metrics_avro2 (+ uuid String,+ trace_id String,+ span_id String,+ trace_flags Nullable(Int32),+ timestamp DateTime64(6),+ observed_timestamp DateTime64(6),+ service_name Nullable(String),+ metric_name Nullable(String),+ metric_type Nullable(String),+ value Nullable(Float64),+ count Nullable(Int64),+ histogram_bounds Array(Float64),+ histogram_counts Array(Int64),+ unit Nullable(String),+ aggregation_temporality Nullable(String),+ is_monotonic Nullable(UInt8),+ resource_attributes Map(String, String),+ instrumentation_scope Nullable(String),+ attributes Map(String, String),+ series_fingerprint Nullable(Int64),+ has_labels Nullable(UInt8),+ retention_days Nullable(Int32)+) ENGINE = Kafka(warpstream_metrics) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro2', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';CREATE TABLE posthog.kafka_performance_events (uuid UUID,session_id String,@@ -1223,6 +1247,30 @@ CREATE TABLE posthog.metric_attributes (INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes', '{replica}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(time_bucket) SETTINGS deduplicate_merge_projection_mode = 'drop', index_granularity = 8192;+CREATE TABLE posthog.metric_attributes2 (+ team_id Int32,+ time_bucket DateTime64(0),+ original_expiry_time_bucket DateTime64(0),+ service_name LowCardinality(String),+ 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 1,+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metric_attributes_distributed (+ team_id Int32,+ time_bucket DateTime64(0),+ original_expiry_time_bucket DateTime64(0),+ service_name LowCardinality(String),+ attribute_key LowCardinality(String),+ attribute_value String,+ attribute_type LowCardinality(String),+ attribute_count SimpleAggregateFunction(sum, UInt64)+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'metric_attributes2');CREATE TABLE posthog.metric_samples1 (team_id Int32,metric_name LowCardinality(String),@@ -1253,6 +1301,42 @@ CREATE TABLE posthog.metric_series1 (INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series1', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL toDateTime(last_seen) + toIntervalDay(90) SETTINGS index_granularity = 8192;+CREATE TABLE posthog.metric_series2 (+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ metric_type LowCardinality(String),+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ service_name LowCardinality(String),+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ attributes Map(LowCardinality(String), String),+ last_seen DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;+CREATE TABLE posthog.metric_series_distributed (+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ metric_type LowCardinality(String),+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ service_name LowCardinality(String),+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ attributes Map(LowCardinality(String), String),+ last_seen DateTime64(6),+ original_expiry_timestamp DateTime64(6)+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'metric_series2');CREATE TABLE posthog.metrics1 (time_bucket DateTime MATERIALIZED toStartOfDay(timestamp),uuid String,@@ -1300,6 +1384,126 @@ CREATE TABLE posthog.metrics1 (GROUP BYteam_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint)) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.metrics1', '{replica}') ORDER BY (team_id, time_bucket, service_name, metric_name, resource_fingerprint, timestamp) PARTITION BY toDate(timestamp) SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metrics2 (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ resource_fingerprint UInt64 DEFAULT 0,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64 CODEC(Gorilla(8)),+ count UInt64 DEFAULT 1 CODEC(T64),+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool DEFAULT false,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ instrumentation_scope String,+ _partition UInt32,+ _topic String,+ _offset UInt64,+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_trace_id_bf trace_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,+ PROJECTION projection_series_minute (SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+GROUP BY+ team_id, metric_name, service_name, metric_type, resource_fingerprint, series_fingerprint, minute),+ PROJECTION projection_series_activity (SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+GROUP BY+ team_id, service_name, metric_name, metric_type, resource_fingerprint, series_fingerprint, hour)+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.metrics2', '{replica}-{shard}') ORDER BY (team_id, metric_name, time_bucket, series_fingerprint, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metrics2_input (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64,+ resource_fingerprint UInt64,+ timestamp DateTime64(6),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64,+ count UInt64,+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool,+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ attributes Map(LowCardinality(String), String),+ _partition UInt32,+ _topic String,+ _offset UInt64+) ENGINE = Null();+CREATE TABLE posthog.metrics_distributed (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ resource_fingerprint UInt64 DEFAULT 0,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64 CODEC(Gorilla(8)),+ count UInt64 DEFAULT 1 CODEC(T64),+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool DEFAULT false,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ instrumentation_scope String,+ _partition UInt32,+ _topic String,+ _offset UInt64+) ENGINE = Distributed('posthog_single_shard', 'posthog', 'metrics2');CREATE TABLE posthog.metrics_kafka_metrics (_partition UInt32,_topic String,@@ -4524,6 +4728,56 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_kafka_metrics_mv TO posthog.logFROM posthog.logs34GROUP BY_partition, _topic;+CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp+ + toIntervalDay(+ assumeNotNull(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(90))+ )+ )+ ) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ toBool(ifNull(has_labels, 1)) AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS resource_attributes,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS attributes,+ _partition,+ _topic,+ _offset+FROM posthog.kafka_metrics_avro2+WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0;CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro_kafka_metrics_mv TO posthog.metrics_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,@@ -4868,6 +5122,103 @@ FROMGROUP BYteam_id, time_bucket, service_name, resource_fingerprint, resource_attributes);+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_attributes TO posthog.metric_attributes2 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), 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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes+ );+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metric_series2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM posthog.metrics2_input+WHERE has_labels;+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metrics TO posthog.metrics2 (uuid String, team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name LowCardinality(String), metric_type LowCardinality(String), value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, instrumentation_scope String, _partition UInt32, _topic String, _offset UInt64) AS SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM posthog.metrics2_input;+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_resource_attributes TO posthog.metric_attributes2 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), 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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes+ );CREATE MATERIALIZED VIEW posthog.performance_events_mv TO posthog.writeable_performance_events (uuid UUID, session_id String, window_id String, pageview_id String, distinct_id String, timestamp DateTime64(3), time_origin DateTime64(3, 'UTC'), entry_type LowCardinality(String), name String, team_id Int64, current_url String, start_time Float64, duration Float64, redirect_start Float64, redirect_end Float64, worker_start Float64, fetch_start Float64, domain_lookup_start Float64, domain_lookup_end Float64, connect_start Float64, secure_connection_start Float64, connect_end Float64, request_start Float64, response_start Float64, response_end Float64, decoded_body_size Int64, encoded_body_size Int64, initiator_type LowCardinality(String), next_hop_protocol LowCardinality(String), render_blocking_status LowCardinality(String), response_status Int64, transfer_size Int64, largest_contentful_paint_element String, largest_contentful_paint_render_time Float64, largest_contentful_paint_load_time Float64, largest_contentful_paint_size Float64, largest_contentful_paint_id String, largest_contentful_paint_url String, dom_complete Float64, dom_content_loaded_event Float64, dom_interactive Float64, load_event_end Float64, load_event_start Float64, redirect_count Int64, navigation_type LowCardinality(String), unload_event_end Float64, unload_event_start Float64, _timestamp Nullable(DateTime), _offset UInt64, _partition UInt64) AS SELECTuuid,session_id,diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sqlindex b98b07397375..559aae1ff213 100644--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql@@ -23,6 +23,30 @@ CREATE TABLE posthog.kafka_metrics_avro (attributes Map(String, String),series_fingerprint Nullable(Int64)) ENGINE = Kafka(warpstream_metrics) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-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 = 'clickhouse_metrics';+CREATE TABLE posthog.kafka_metrics_avro2 (+ uuid String,+ trace_id String,+ span_id String,+ trace_flags Nullable(Int32),+ timestamp DateTime64(6),+ observed_timestamp DateTime64(6),+ service_name Nullable(String),+ metric_name Nullable(String),+ metric_type Nullable(String),+ value Nullable(Float64),+ count Nullable(Int64),+ histogram_bounds Array(Float64),+ histogram_counts Array(Int64),+ unit Nullable(String),+ aggregation_temporality Nullable(String),+ is_monotonic Nullable(UInt8),+ resource_attributes Map(String, String),+ instrumentation_scope Nullable(String),+ attributes Map(String, String),+ series_fingerprint Nullable(Int64),+ has_labels Nullable(UInt8),+ retention_days Nullable(Int32)+) ENGINE = Kafka(warpstream_metrics) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro2', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';CREATE TABLE posthog.kafka_trace_spans_avro (uuid String,trace_id String,@@ -265,6 +289,30 @@ CREATE TABLE posthog.metric_attributes (INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/logs/{shard}/posthog.metric_attributes', '{replica}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(time_bucket) SETTINGS deduplicate_merge_projection_mode = 'drop', index_granularity = 8192;+CREATE TABLE posthog.metric_attributes2 (+ team_id Int32,+ time_bucket DateTime64(0),+ original_expiry_time_bucket DateTime64(0),+ service_name LowCardinality(String),+ 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 1,+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metric_attributes_distributed (+ team_id Int32,+ time_bucket DateTime64(0),+ original_expiry_time_bucket DateTime64(0),+ service_name LowCardinality(String),+ attribute_key LowCardinality(String),+ attribute_value String,+ attribute_type LowCardinality(String),+ attribute_count SimpleAggregateFunction(sum, UInt64)+) ENGINE = Distributed('logs', 'posthog', 'metric_attributes2');CREATE TABLE posthog.metric_samples1 (team_id Int32,metric_name LowCardinality(String),@@ -295,6 +343,42 @@ CREATE TABLE posthog.metric_series1 (INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series1', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL toDateTime(last_seen) + toIntervalDay(90) SETTINGS index_granularity = 8192;+CREATE TABLE posthog.metric_series2 (+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ metric_type LowCardinality(String),+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ service_name LowCardinality(String),+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ attributes Map(LowCardinality(String), String),+ last_seen DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;+CREATE TABLE posthog.metric_series_distributed (+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ metric_type LowCardinality(String),+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ service_name LowCardinality(String),+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ attributes Map(LowCardinality(String), String),+ last_seen DateTime64(6),+ original_expiry_timestamp DateTime64(6)+) ENGINE = Distributed('logs', 'posthog', 'metric_series2');CREATE TABLE posthog.metrics1 (time_bucket DateTime MATERIALIZED toStartOfDay(timestamp),uuid String,@@ -342,6 +426,126 @@ CREATE TABLE posthog.metrics1 (GROUP BYteam_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint)) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.metrics1', '{replica}') ORDER BY (team_id, time_bucket, service_name, metric_name, resource_fingerprint, timestamp) PARTITION BY toDate(timestamp) SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metrics2 (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ resource_fingerprint UInt64 DEFAULT 0,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64 CODEC(Gorilla(8)),+ count UInt64 DEFAULT 1 CODEC(T64),+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool DEFAULT false,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ instrumentation_scope String,+ _partition UInt32,+ _topic String,+ _offset UInt64,+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_trace_id_bf trace_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,+ PROJECTION projection_series_minute (SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+GROUP BY+ team_id, metric_name, service_name, metric_type, resource_fingerprint, series_fingerprint, minute),+ PROJECTION projection_series_activity (SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+GROUP BY+ team_id, service_name, metric_name, metric_type, resource_fingerprint, series_fingerprint, hour)+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.metrics2', '{replica}-{shard}') ORDER BY (team_id, metric_name, time_bucket, series_fingerprint, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metrics2_input (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64,+ resource_fingerprint UInt64,+ timestamp DateTime64(6),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64,+ count UInt64,+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool,+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ attributes Map(LowCardinality(String), String),+ _partition UInt32,+ _topic String,+ _offset UInt64+) ENGINE = Null();+CREATE TABLE posthog.metrics_distributed (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ resource_fingerprint UInt64 DEFAULT 0,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64 CODEC(Gorilla(8)),+ count UInt64 DEFAULT 1 CODEC(T64),+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool DEFAULT false,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ instrumentation_scope String,+ _partition UInt32,+ _topic String,+ _offset UInt64+) ENGINE = Distributed('logs', 'posthog', 'metrics2');CREATE TABLE posthog.metrics_kafka_metrics (_partition UInt32,_topic String,@@ -671,6 +875,56 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_kafka_metrics_mv TO posthog.logFROM posthog.logs34GROUP BY_partition, _topic;+CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp+ + toIntervalDay(+ assumeNotNull(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(90))+ )+ )+ ) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ toBool(ifNull(has_labels, 1)) AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS resource_attributes,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS attributes,+ _partition,+ _topic,+ _offset+FROM posthog.kafka_metrics_avro2+WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0;CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro_kafka_metrics_mv TO posthog.metrics_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,@@ -915,6 +1169,103 @@ FROMGROUP BYteam_id, time_bucket, service_name, resource_fingerprint, resource_attributes);+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_attributes TO posthog.metric_attributes2 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), 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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes+ );+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metric_series2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM posthog.metrics2_input+WHERE has_labels;+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metrics TO posthog.metrics2 (uuid String, team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name LowCardinality(String), metric_type LowCardinality(String), value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, instrumentation_scope String, _partition UInt32, _topic String, _offset UInt64) AS SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM posthog.metrics2_input;+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_resource_attributes TO posthog.metric_attributes2 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), 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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes+ );CREATE MATERIALIZED VIEW posthog.ops_query_log_archive_mv TO posthog.writable_query_log_archive (hostname LowCardinality(String), user LowCardinality(String), query_id String, initial_query_id String, is_initial_query UInt8, type Enum8('QueryStart'=1, 'QueryFinish'=2, 'ExceptionBeforeStart'=3, 'ExceptionWhileProcessing'=4), event_date Date, event_time DateTime, event_time_microseconds DateTime64(6), query_start_time DateTime, query_start_time_microseconds DateTime64(6), query_duration_ms UInt64, read_rows UInt64, read_bytes UInt64, written_rows UInt64, written_bytes UInt64, result_rows UInt64, result_bytes UInt64, memory_usage UInt64, peak_threads_usage UInt64, current_database LowCardinality(String), query String, formatted_query String, normalized_query_hash UInt64, query_kind LowCardinality(String), exception_code Int32, exception String, stack_trace String, team_id Int64, log_comment String, ProfileEvents Map(LowCardinality(String), UInt64)) AS SELECThostname,user,diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sqlindex 50c926d09861..86d8461ff917 100644--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql@@ -23,6 +23,30 @@ CREATE TABLE posthog.kafka_metrics_avro (attributes Map(String, String),series_fingerprint Nullable(Int64)) ENGINE = Kafka(warpstream_metrics) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-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 = 'clickhouse_metrics';+CREATE TABLE posthog.kafka_metrics_avro2 (+ uuid String,+ trace_id String,+ span_id String,+ trace_flags Nullable(Int32),+ timestamp DateTime64(6),+ observed_timestamp DateTime64(6),+ service_name Nullable(String),+ metric_name Nullable(String),+ metric_type Nullable(String),+ value Nullable(Float64),+ count Nullable(Int64),+ histogram_bounds Array(Float64),+ histogram_counts Array(Int64),+ unit Nullable(String),+ aggregation_temporality Nullable(String),+ is_monotonic Nullable(UInt8),+ resource_attributes Map(String, String),+ instrumentation_scope Nullable(String),+ attributes Map(String, String),+ series_fingerprint Nullable(Int64),+ has_labels Nullable(UInt8),+ retention_days Nullable(Int32)+) ENGINE = Kafka(warpstream_metrics) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro2', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';CREATE TABLE posthog.kafka_trace_spans_avro (uuid String,trace_id String,@@ -265,6 +289,30 @@ CREATE TABLE posthog.metric_attributes (INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/logs/{shard}/posthog.metric_attributes', '{replica}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(time_bucket) SETTINGS deduplicate_merge_projection_mode = 'drop', index_granularity = 8192;+CREATE TABLE posthog.metric_attributes2 (+ team_id Int32,+ time_bucket DateTime64(0),+ original_expiry_time_bucket DateTime64(0),+ service_name LowCardinality(String),+ 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 1,+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metric_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metric_attributes_distributed (+ team_id Int32,+ time_bucket DateTime64(0),+ original_expiry_time_bucket DateTime64(0),+ service_name LowCardinality(String),+ attribute_key LowCardinality(String),+ attribute_value String,+ attribute_type LowCardinality(String),+ attribute_count SimpleAggregateFunction(sum, UInt64)+) ENGINE = Distributed('logs', 'posthog', 'metric_attributes2');CREATE TABLE posthog.metric_samples1 (team_id Int32,metric_name LowCardinality(String),@@ -295,6 +343,42 @@ CREATE TABLE posthog.metric_series1 (INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series1', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL toDateTime(last_seen) + toIntervalDay(90) SETTINGS index_granularity = 8192;+CREATE TABLE posthog.metric_series2 (+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ metric_type LowCardinality(String),+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ service_name LowCardinality(String),+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ attributes Map(LowCardinality(String), String),+ last_seen DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;+CREATE TABLE posthog.metric_series_distributed (+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ metric_type LowCardinality(String),+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ service_name LowCardinality(String),+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),+ attributes Map(LowCardinality(String), String),+ last_seen DateTime64(6),+ original_expiry_timestamp DateTime64(6)+) ENGINE = Distributed('logs', 'posthog', 'metric_series2');CREATE TABLE posthog.metrics1 (time_bucket DateTime MATERIALIZED toStartOfDay(timestamp),uuid String,@@ -342,6 +426,126 @@ CREATE TABLE posthog.metrics1 (GROUP BYteam_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint)) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.metrics1', '{replica}') ORDER BY (team_id, time_bucket, service_name, metric_name, resource_fingerprint, timestamp) PARTITION BY toDate(timestamp) SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metrics2 (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ resource_fingerprint UInt64 DEFAULT 0,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64 CODEC(Gorilla(8)),+ count UInt64 DEFAULT 1 CODEC(T64),+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool DEFAULT false,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ instrumentation_scope String,+ _partition UInt32,+ _topic String,+ _offset UInt64,+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_trace_id_bf trace_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,+ PROJECTION projection_series_minute (SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+GROUP BY+ team_id, metric_name, service_name, metric_type, resource_fingerprint, series_fingerprint, minute),+ PROJECTION projection_series_activity (SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+GROUP BY+ team_id, service_name, metric_name, metric_type, resource_fingerprint, series_fingerprint, hour)+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.metrics2', '{replica}-{shard}') ORDER BY (team_id, metric_name, time_bucket, series_fingerprint, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;+CREATE TABLE posthog.metrics2_input (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ series_fingerprint UInt64,+ resource_fingerprint UInt64,+ timestamp DateTime64(6),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64,+ count UInt64,+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool,+ instrumentation_scope String,+ resource_attributes Map(LowCardinality(String), String),+ attributes Map(LowCardinality(String), String),+ _partition UInt32,+ _topic String,+ _offset UInt64+) ENGINE = Null();+CREATE TABLE posthog.metrics_distributed (+ uuid String,+ team_id Int32,+ metric_name LowCardinality(String),+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),+ series_fingerprint UInt64 CODEC(Delta(8), Default),+ resource_fingerprint UInt64 DEFAULT 0,+ timestamp DateTime64(6) CODEC(DoubleDelta),+ observed_timestamp DateTime64(6),+ original_expiry_timestamp DateTime64(6),+ created_at DateTime64(6) MATERIALIZED now(),+ service_name LowCardinality(String),+ metric_type LowCardinality(String),+ value Float64 CODEC(Gorilla(8)),+ count UInt64 DEFAULT 1 CODEC(T64),+ histogram_bounds Array(Float64),+ histogram_counts Array(UInt64),+ trace_id String,+ span_id String,+ trace_flags Int32,+ has_labels Bool DEFAULT false,+ unit LowCardinality(String),+ aggregation_temporality LowCardinality(String),+ is_monotonic Bool DEFAULT false,+ instrumentation_scope String,+ _partition UInt32,+ _topic String,+ _offset UInt64+) ENGINE = Distributed('logs', 'posthog', 'metrics2');CREATE TABLE posthog.metrics_kafka_metrics (_partition UInt32,_topic String,@@ -665,6 +869,56 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs_avro_kafka_metrics_mv TO posthog.logFROM posthog.logs34GROUP BY_partition, _topic;+CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp+ + toIntervalDay(+ assumeNotNull(+ if(+ (retention_days IS NOT NULL) AND (retention_days > 0),+ retention_days,+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(90))+ )+ )+ ) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ toBool(ifNull(has_labels, 1)) AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS resource_attributes,+ if(+ toBool(ifNull(has_labels, 1)),+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)),+ CAST(map(), 'Map(String, String)')+ ) AS attributes,+ _partition,+ _topic,+ _offset+FROM posthog.kafka_metrics_avro2+WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0;CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro_kafka_metrics_mv TO posthog.metrics_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,@@ -909,6 +1163,103 @@ FROMGROUP BYteam_id, time_bucket, service_name, resource_fingerprint, resource_attributes);+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_attributes TO posthog.metric_attributes2 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), 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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes+ );+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metric_series TO posthog.metric_series2 (team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, metric_type LowCardinality(String), unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, service_name LowCardinality(String), instrumentation_scope String, resource_attributes Map(LowCardinality(String), String), attributes Map(LowCardinality(String), String), last_seen DateTime64(6), original_expiry_timestamp DateTime64(6)) AS SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM posthog.metrics2_input+WHERE has_labels;+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_metrics TO posthog.metrics2 (uuid String, team_id Int32, metric_name LowCardinality(String), series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name LowCardinality(String), metric_type LowCardinality(String), value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit LowCardinality(String), aggregation_temporality LowCardinality(String), is_monotonic Bool, instrumentation_scope String, _partition UInt32, _topic String, _offset UInt64) AS SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM posthog.metrics2_input;+CREATE MATERIALIZED VIEW posthog.metrics2_input_to_resource_attributes TO posthog.metric_attributes2 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), 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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes+ );CREATE MATERIALIZED VIEW posthog.ops_query_log_archive_mv TO posthog.writable_query_log_archive (hostname LowCardinality(String), user LowCardinality(String), query_id String, initial_query_id String, is_initial_query UInt8, type Enum8('QueryStart'=1, 'QueryFinish'=2, 'ExceptionBeforeStart'=3, 'ExceptionWhileProcessing'=4), event_date Date, event_time DateTime, event_time_microseconds DateTime64(6), query_start_time DateTime, query_start_time_microseconds DateTime64(6), query_duration_ms UInt64, read_rows UInt64, read_bytes UInt64, written_rows UInt64, written_bytes UInt64, result_rows UInt64, result_bytes UInt64, memory_usage UInt64, peak_threads_usage UInt64, current_database LowCardinality(String), query String, formatted_query String, normalized_query_hash UInt64, query_kind LowCardinality(String), exception_code Int32, exception String, stack_trace String, team_id Int64, log_comment String, ProfileEvents Map(LowCardinality(String), UInt64)) AS SELECThostname,user,diff --git a/posthog/clickhouse/metrics/__init__.py b/posthog/clickhouse/metrics/__init__.pyindex 28a9478b1a73..42753f78b11a 100644--- a/posthog/clickhouse/metrics/__init__.py+++ b/posthog/clickhouse/metrics/__init__.py@@ -19,8 +19,36 @@ from .metrics1 import (METRICS1_TABLE_SQL,METRICS_DISTRIBUTED_TABLE_SQL,)+from .metrics2 import (+ KAFKA_METRICS_AVRO2_MV,+ KAFKA_METRICS_AVRO2_TABLE_SQL,+ METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL,+ METRIC_ATTRIBUTES2_TABLE_SQL,+ METRIC_SERIES2_DISTRIBUTED_TABLE_SQL,+ METRIC_SERIES2_TABLE_SQL,+ METRICS2_DISTRIBUTED_TABLE_SQL,+ METRICS2_INPUT_TABLE_SQL,+ METRICS2_INPUT_TO_METRIC_ATTRIBUTES_MV,+ METRICS2_INPUT_TO_METRIC_SERIES_MV,+ METRICS2_INPUT_TO_METRICS_MV,+ METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV,+ METRICS2_TABLE_SQL,+)__all__ = [+ "KAFKA_METRICS_AVRO2_MV",+ "KAFKA_METRICS_AVRO2_TABLE_SQL",+ "METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL",+ "METRIC_ATTRIBUTES2_TABLE_SQL",+ "METRIC_SERIES2_DISTRIBUTED_TABLE_SQL",+ "METRIC_SERIES2_TABLE_SQL",+ "METRICS2_DISTRIBUTED_TABLE_SQL",+ "METRICS2_INPUT_TABLE_SQL",+ "METRICS2_INPUT_TO_METRICS_MV",+ "METRICS2_TABLE_SQL",+ "METRICS2_INPUT_TO_METRIC_ATTRIBUTES_MV",+ "METRICS2_INPUT_TO_METRIC_SERIES_MV",+ "METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV","METRICS1_TABLE_SQL","METRICS_DISTRIBUTED_TABLE_SQL","METRIC_ATTRIBUTES_MV",diff --git a/posthog/clickhouse/metrics/metrics2.py b/posthog/clickhouse/metrics/metrics2.pynew file mode 100644index 000000000000..433cebad6521--- /dev/null+++ b/posthog/clickhouse/metrics/metrics2.py@@ -0,0 +1,436 @@+from django.conf import settings++from posthog.clickhouse.kafka_engine import kafka_engine+from posthog.clickhouse.table_engines import (+ AggregatingMergeTree,+ Distributed,+ MergeTreeEngine,+ ReplacingMergeTree,+ ReplicationScheme,+)++from .kafka_metrics import KAFKA_NAMED_COLLECTION, KAFKA_TOPIC++KAFKA_TABLE_NAME = "kafka_metrics_avro2"+KAFKA_GROUP = "clickhouse-metrics-avro2"++METRICS2_INPUT_TABLE_NAME = "metrics2_input"+METRICS2_TABLE_NAME = "metrics2"+METRICS_DISTRIBUTED_TABLE_NAME = "metrics_distributed"+METRIC_SERIES2_TABLE_NAME = "metric_series2"+METRIC_SERIES_DISTRIBUTED_TABLE_NAME = "metric_series_distributed"+METRIC_ATTRIBUTES2_TABLE_NAME = "metric_attributes2"+METRIC_ATTRIBUTES_DISTRIBUTED_TABLE_NAME = "metric_attributes_distributed"++DEFAULT_RETENTION_DAYS = 90+++def _db() -> str:+ return settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE+++def KAFKA_METRICS_AVRO2_TABLE_SQL() -> str:+ return f"""+CREATE TABLE IF NOT EXISTS {_db()}.{KAFKA_TABLE_NAME}+(+ `uuid` String,+ `trace_id` String,+ `span_id` String,+ `trace_flags` Nullable(Int32),+ `timestamp` DateTime64(6),+ `observed_timestamp` DateTime64(6),+ `service_name` Nullable(String),+ `metric_name` Nullable(String),+ `metric_type` Nullable(String),+ `value` Nullable(Float64),+ `count` Nullable(Int64),+ `histogram_bounds` Array(Float64),+ `histogram_counts` Array(Int64),+ `unit` Nullable(String),+ `aggregation_temporality` Nullable(String),+ `is_monotonic` Nullable(UInt8),+ `resource_attributes` Map(String, String),+ `instrumentation_scope` Nullable(String),+ `attributes` Map(String, String),+ `series_fingerprint` Nullable(Int64),+ `has_labels` Nullable(UInt8),+ `retention_days` Nullable(Int32)+)+ENGINE = {kafka_engine(topic=KAFKA_TOPIC, group=KAFKA_GROUP, serialization="Avro", named_collection=KAFKA_NAMED_COLLECTION)}+SETTINGS+ kafka_skip_broken_messages = 100,+ kafka_thread_per_consumer = 1,+ kafka_num_consumers = 8,+ kafka_poll_timeout_ms = 3000,+ kafka_poll_max_batch_size = 1000,+ input_format_avro_allow_missing_fields = 1+"""+++def METRICS2_INPUT_TABLE_SQL() -> str:+ return f"""+CREATE TABLE IF NOT EXISTS {_db()}.{METRICS2_INPUT_TABLE_NAME}+(+ `uuid` String,+ `team_id` Int32,+ `metric_name` LowCardinality(String),+ `series_fingerprint` UInt64,+ `resource_fingerprint` UInt64,+ `timestamp` DateTime64(6),+ `observed_timestamp` DateTime64(6),+ `original_expiry_timestamp` DateTime64(6),+ `service_name` LowCardinality(String),+ `metric_type` LowCardinality(String),+ `value` Float64,+ `count` UInt64,+ `histogram_bounds` Array(Float64),+ `histogram_counts` Array(UInt64),+ `trace_id` String,+ `span_id` String,+ `trace_flags` Int32,+ `has_labels` Bool,+ `unit` LowCardinality(String),+ `aggregation_temporality` LowCardinality(String),+ `is_monotonic` Bool,+ `instrumentation_scope` String,+ `resource_attributes` Map(LowCardinality(String), String),+ `attributes` Map(LowCardinality(String), String),+ `_partition` UInt32,+ `_topic` String,+ `_offset` UInt64+)+ENGINE = Null+"""+++def METRICS2_TABLE_SQL() -> str:+ return f"""+CREATE TABLE IF NOT EXISTS {_db()}.{METRICS2_TABLE_NAME}+(+ `uuid` String,+ `team_id` Int32,+ `metric_name` LowCardinality(String),+ `time_bucket` DateTime MATERIALIZED toStartOfHour(timestamp),+ `series_fingerprint` UInt64 CODEC(Delta, Default),+ `resource_fingerprint` UInt64 DEFAULT 0,+ `timestamp` DateTime64(6) CODEC(DoubleDelta),+ `observed_timestamp` DateTime64(6),+ `original_expiry_timestamp` DateTime64(6),+ `created_at` DateTime64(6) MATERIALIZED now(),+ `service_name` LowCardinality(String),+ `metric_type` LowCardinality(String),+ `value` Float64 CODEC(Gorilla),+ `count` UInt64 DEFAULT 1 CODEC(T64),+ `histogram_bounds` Array(Float64),+ `histogram_counts` Array(UInt64),+ `trace_id` String,+ `span_id` String,+ `trace_flags` Int32,+ `has_labels` Bool DEFAULT false,+ `unit` LowCardinality(String),+ `aggregation_temporality` LowCardinality(String),+ `is_monotonic` Bool DEFAULT false,+ `instrumentation_scope` String,+ `_partition` UInt32,+ `_topic` String,+ `_offset` UInt64,+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_trace_id_bf trace_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,+ PROJECTION projection_series_minute+ (+ SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+ GROUP BY+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ minute+ ),+ PROJECTION projection_series_activity+ (+ SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+ GROUP BY+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ hour+ )+)+ENGINE = {MergeTreeEngine(METRICS2_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}+PARTITION BY toDate(original_expiry_timestamp)+ORDER BY (team_id, metric_name, time_bucket, series_fingerprint, timestamp)+TTL original_expiry_timestamp+SETTINGS+ index_granularity_bytes = 104857600,+ index_granularity = 8192,+ ttl_only_drop_parts = 1+"""+++def METRIC_SERIES2_TABLE_SQL() -> str:+ return f"""+CREATE TABLE IF NOT EXISTS {_db()}.{METRIC_SERIES2_TABLE_NAME}+(+ `team_id` Int32,+ `metric_name` LowCardinality(String),+ `series_fingerprint` UInt64 CODEC(Delta, Default),+ `metric_type` LowCardinality(String),+ `unit` LowCardinality(String),+ `aggregation_temporality` LowCardinality(String),+ `is_monotonic` Bool DEFAULT false,+ `service_name` LowCardinality(String),+ `instrumentation_scope` String,+ `resource_attributes` Map(LowCardinality(String), String),+ `resource_fingerprint` UInt64 MATERIALIZED cityHash64(resource_attributes),+ `attributes` Map(LowCardinality(String), String),+ `last_seen` DateTime64(6),+ `original_expiry_timestamp` DateTime64(6),+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1+)+ENGINE = {ReplacingMergeTree(METRIC_SERIES2_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED, ver="last_seen")}+ORDER BY (team_id, metric_name, series_fingerprint)+TTL original_expiry_timestamp+SETTINGS index_granularity = 8192+"""+++def METRIC_ATTRIBUTES2_TABLE_SQL() -> str:+ return f"""+CREATE TABLE IF NOT EXISTS {_db()}.{METRIC_ATTRIBUTES2_TABLE_NAME}+(+ `team_id` Int32,+ `time_bucket` DateTime64(0),+ `original_expiry_time_bucket` DateTime64(0),+ `service_name` LowCardinality(String),+ `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 1,+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1+)+ENGINE = {AggregatingMergeTree(METRIC_ATTRIBUTES2_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}+PARTITION BY toDate(original_expiry_time_bucket)+ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value)+TTL original_expiry_time_bucket+SETTINGS+ index_granularity = 8192,+ ttl_only_drop_parts = 1+"""+++def _distributed_sql(distributed_name: str, data_table: str) -> str:+ return "CREATE TABLE IF NOT EXISTS {database}.{distributed} AS {database}.{table_name} ENGINE = {engine}".format(+ distributed=distributed_name,+ table_name=data_table,+ engine=Distributed(data_table=data_table, cluster=settings.CLICKHOUSE_LOGS_CLUSTER),+ database=_db(),+ )+++def METRICS2_DISTRIBUTED_TABLE_SQL() -> str:+ return _distributed_sql(METRICS_DISTRIBUTED_TABLE_NAME, METRICS2_TABLE_NAME)+++def METRIC_SERIES2_DISTRIBUTED_TABLE_SQL() -> str:+ return _distributed_sql(METRIC_SERIES_DISTRIBUTED_TABLE_NAME, METRIC_SERIES2_TABLE_NAME)+++def METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL() -> str:+ return _distributed_sql(METRIC_ATTRIBUTES_DISTRIBUTED_TABLE_NAME, METRIC_ATTRIBUTES2_TABLE_NAME)+++def KAFKA_METRICS_AVRO2_MV() -> str:+ db = _db()+ sorted_resource_attributes = "mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))"+ sorted_attributes = "mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes))"+ labelled = "toBool(ifNull(has_labels, 1))"+ return f"""+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{KAFKA_TABLE_NAME}_mv TO {db}.{METRICS2_INPUT_TABLE_NAME}+AS SELECT+ uuid,+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,+ ifNull(metric_name, '') AS metric_name,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,+ cityHash64({sorted_resource_attributes}) AS resource_fingerprint,+ timestamp,+ observed_timestamp,+ observed_timestamp + toIntervalDay(assumeNotNull(if((retention_days IS NOT NULL) AND (retention_days > 0), retention_days, toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32({DEFAULT_RETENTION_DAYS}))))) AS original_expiry_timestamp,+ ifNull(service_name, '') AS service_name,+ ifNull(metric_type, '') AS metric_type,+ ifNull(value, 0) AS value,+ toUInt64(ifNull(count, 1)) AS count,+ histogram_bounds,+ arrayMap(x -> toUInt64(x), histogram_counts) AS histogram_counts,+ trace_id,+ span_id,+ ifNull(trace_flags, 0) AS trace_flags,+ {labelled} AS has_labels,+ ifNull(unit, '') AS unit,+ ifNull(aggregation_temporality, '') AS aggregation_temporality,+ ifNull(is_monotonic, 0) AS is_monotonic,+ ifNull(instrumentation_scope, '') AS instrumentation_scope,+ if({labelled}, {sorted_resource_attributes}, CAST(map(), 'Map(String, String)')) AS resource_attributes,+ if({labelled}, {sorted_attributes}, CAST(map(), 'Map(String, String)')) AS attributes,+ _partition,+ _topic,+ _offset+FROM {db}.{KAFKA_TABLE_NAME}+WHERE {KAFKA_TABLE_NAME}.series_fingerprint IS NOT NULL+SETTINGS+ min_insert_block_size_rows = 0,+ min_insert_block_size_bytes = 0+"""+++def METRICS2_INPUT_TO_METRICS_MV() -> str:+ db = _db()+ return f"""+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{METRICS2_INPUT_TABLE_NAME}_to_metrics TO {db}.{METRICS2_TABLE_NAME}+AS SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+FROM {db}.{METRICS2_INPUT_TABLE_NAME}+"""+++def METRICS2_INPUT_TO_METRIC_SERIES_MV() -> str:+ db = _db()+ return f"""+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{METRICS2_INPUT_TABLE_NAME}_to_metric_series TO {db}.{METRIC_SERIES2_TABLE_NAME}+AS SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+FROM {db}.{METRICS2_INPUT_TABLE_NAME}+WHERE has_labels+"""+++def _attributes_mv(view_suffix: str, source_map: str, attribute_type: str, filter_long_pairs: bool) -> str:+ db = _db()+ attributes_expr = (+ f"mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), {source_map})"+ if filter_long_pairs+ else source_map+ )+ return f"""+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{METRICS2_INPUT_TABLE_NAME}_to_{view_suffix} TO {db}.{METRIC_ATTRIBUTES2_TABLE_NAME}+(+ `team_id` Int32,+ `time_bucket` DateTime64(0),+ `original_expiry_time_bucket` DateTime64(0),+ `service_name` LowCardinality(String),+ `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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+FROM+(+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ {attributes_expr} AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ '{attribute_type}' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM {db}.{METRICS2_INPUT_TABLE_NAME}+ WHERE has_labels+ GROUP BY+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ filtered_attributes+)+"""+++def METRICS2_INPUT_TO_METRIC_ATTRIBUTES_MV() -> str:+ return _attributes_mv("metric_attributes", "attributes", "metric", filter_long_pairs=True)+++def METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV() -> str:+ return _attributes_mv("resource_attributes", "resource_attributes", "resource", filter_long_pairs=False)diff --git a/posthog/clickhouse/migrations/0314_metrics2_chain.py b/posthog/clickhouse/migrations/0314_metrics2_chain.pynew file mode 100644index 000000000000..2c5762f4de13--- /dev/null+++ b/posthog/clickhouse/migrations/0314_metrics2_chain.py@@ -0,0 +1,33 @@+from posthog.clickhouse.client.connection import NodeRole+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions+from posthog.clickhouse.metrics import (+ KAFKA_METRICS_AVRO2_MV,+ KAFKA_METRICS_AVRO2_TABLE_SQL,+ METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL,+ METRIC_ATTRIBUTES2_TABLE_SQL,+ METRIC_SERIES2_DISTRIBUTED_TABLE_SQL,+ METRIC_SERIES2_TABLE_SQL,+ METRICS2_DISTRIBUTED_TABLE_SQL,+ METRICS2_INPUT_TABLE_SQL,+ METRICS2_INPUT_TO_METRIC_ATTRIBUTES_MV,+ METRICS2_INPUT_TO_METRIC_SERIES_MV,+ METRICS2_INPUT_TO_METRICS_MV,+ METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV,+ METRICS2_TABLE_SQL,+)++operations = [+ run_sql_with_exceptions(METRICS2_INPUT_TABLE_SQL(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(METRICS2_TABLE_SQL(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(METRIC_SERIES2_TABLE_SQL(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(METRIC_ATTRIBUTES2_TABLE_SQL(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(METRICS2_DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(METRIC_SERIES2_DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(METRICS2_INPUT_TO_METRICS_MV(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(METRICS2_INPUT_TO_METRIC_SERIES_MV(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(METRICS2_INPUT_TO_METRIC_ATTRIBUTES_MV(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(KAFKA_METRICS_AVRO2_TABLE_SQL(), node_roles=[NodeRole.LOGS]),+ run_sql_with_exceptions(KAFKA_METRICS_AVRO2_MV(), node_roles=[NodeRole.LOGS]),+]diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txtindex e86622ea942d..d82a360f9d33 100644--- a/posthog/clickhouse/migrations/max_migration.txt+++ b/posthog/clickhouse/migrations/max_migration.txt@@ -1 +1 @@-0313_logs_pattern_buckets+0314_metrics2_chaindiff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.pyindex f15b49a51d94..2e4655b992b7 100644--- a/posthog/clickhouse/schema.py+++ b/posthog/clickhouse/schema.py@@ -34,14 +34,25 @@ from posthog.clickhouse.logs import (LOGS_VOLUME_BUCKETS_TABLE_SQL,)from posthog.clickhouse.metrics import (+ METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL,+ METRIC_ATTRIBUTES2_TABLE_SQL,METRIC_ATTRIBUTES_MV,METRIC_ATTRIBUTES_TABLE_SQL,METRIC_RESOURCE_ATTRIBUTES_MV,METRIC_SAMPLES_DISTRIBUTED_TABLE_SQL,METRIC_SAMPLES_TABLE_SQL,+ METRIC_SERIES2_DISTRIBUTED_TABLE_SQL,+ METRIC_SERIES2_TABLE_SQL,METRIC_SERIES_DISTRIBUTED_TABLE_SQL,METRIC_SERIES_TABLE_SQL,METRICS1_TABLE_SQL,+ METRICS2_DISTRIBUTED_TABLE_SQL,+ METRICS2_INPUT_TABLE_SQL,+ METRICS2_INPUT_TO_METRIC_ATTRIBUTES_MV,+ METRICS2_INPUT_TO_METRIC_SERIES_MV,+ METRICS2_INPUT_TO_METRICS_MV,+ METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV,+ METRICS2_TABLE_SQL,METRICS_DISTRIBUTED_TABLE_SQL,)from posthog.clickhouse.plugin_log_entries import (@@ -440,6 +451,10 @@ CREATE_MERGETREE_TABLE_QUERIES = (METRIC_SERIES_TABLE_SQL,METRIC_SAMPLES_TABLE_SQL,METRIC_ATTRIBUTES_TABLE_SQL,+ METRICS2_INPUT_TABLE_SQL,+ METRICS2_TABLE_SQL,+ METRIC_SERIES2_TABLE_SQL,+ METRIC_ATTRIBUTES2_TABLE_SQL,TOPHOG_DATA_TABLE_SQL,BILLING_USAGE_RECORDS_DATA_TABLE_SQL,AI_EVENTS_DATA_TABLE_SQL,@@ -527,6 +542,9 @@ CREATE_DISTRIBUTED_TABLE_QUERIES = (METRICS_DISTRIBUTED_TABLE_SQL,METRIC_SERIES_DISTRIBUTED_TABLE_SQL,METRIC_SAMPLES_DISTRIBUTED_TABLE_SQL,+ METRICS2_DISTRIBUTED_TABLE_SQL,+ METRIC_SERIES2_DISTRIBUTED_TABLE_SQL,+ METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL,WRITABLE_TOPHOG_TABLE_SQL,DISTRIBUTED_TOPHOG_TABLE_SQL,DISTRIBUTED_BILLING_USAGE_RECORDS_TABLE_SQL,@@ -614,6 +632,10 @@ CREATE_MV_TABLE_QUERIES = (LOGS34_TO_VOLUME_BUCKETS_MV,METRIC_ATTRIBUTES_MV,METRIC_RESOURCE_ATTRIBUTES_MV,+ METRICS2_INPUT_TO_METRICS_MV,+ METRICS2_INPUT_TO_METRIC_SERIES_MV,+ METRICS2_INPUT_TO_METRIC_ATTRIBUTES_MV,+ METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV,TOPHOG_MV_SQL,BILLING_USAGE_RECORDS_MV_SQL,AI_EVENTS_MV_SQL,diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambrindex 48436c80fc4d..afd7f9a44610 100644--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr@@ -4067,6 +4067,34 @@'''# ---+# name: test_create_table_query[metric_attributes2]+ '''++ CREATE TABLE IF NOT EXISTS posthog_test.metric_attributes2+ (+ `team_id` Int32,+ `time_bucket` DateTime64(0),+ `original_expiry_time_bucket` DateTime64(0),+ `service_name` LowCardinality(String),+ `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 1,+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1+ )+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_attributes2', '{replica}-{shard}')+ PARTITION BY toDate(original_expiry_time_bucket)+ ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value)+ TTL original_expiry_time_bucket+ SETTINGS+ index_granularity = 8192,+ ttl_only_drop_parts = 1++ '''+# ---# name: test_create_table_query[metric_attributes]'''@@ -4094,6 +4122,9 @@'''# ---+# name: test_create_table_query[metric_attributes_distributed]+ "CREATE TABLE IF NOT EXISTS posthog_test.metric_attributes_distributed AS posthog_test.metric_attributes2 ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'metric_attributes2')"+# ---# name: test_create_table_query[metric_samples1]'''@@ -4150,9 +4181,43 @@'''# ---+# name: test_create_table_query[metric_series2]+ '''++ CREATE TABLE IF NOT EXISTS posthog_test.metric_series2+ (+ `team_id` Int32,+ `metric_name` LowCardinality(String),+ `series_fingerprint` UInt64 CODEC(Delta, Default),+ `metric_type` LowCardinality(String),+ `unit` LowCardinality(String),+ `aggregation_temporality` LowCardinality(String),+ `is_monotonic` Bool DEFAULT false,+ `service_name` LowCardinality(String),+ `instrumentation_scope` String,+ `resource_attributes` Map(LowCardinality(String), String),+ `resource_fingerprint` UInt64 MATERIALIZED cityHash64(resource_attributes),+ `attributes` Map(LowCardinality(String), String),+ `last_seen` DateTime64(6),+ `original_expiry_timestamp` DateTime64(6),+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1+ )+ ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_series2', '{replica}-{shard}', last_seen)+ ORDER BY (team_id, metric_name, series_fingerprint)+ TTL original_expiry_timestamp+ SETTINGS index_granularity = 8192++ '''+# ---# name: test_create_table_query[metric_series]"CREATE TABLE IF NOT EXISTS posthog_test.metric_series AS posthog_test.metric_series1 ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'metric_series1')"# ---+# name: test_create_table_query[metric_series_distributed]+ "CREATE TABLE IF NOT EXISTS posthog_test.metric_series_distributed AS posthog_test.metric_series2 ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'metric_series2')"+# ---# name: test_create_table_query[metrics1]'''@@ -4317,6 +4382,291 @@'''# ---+# name: test_create_table_query[metrics2]+ '''++ CREATE TABLE IF NOT EXISTS posthog_test.metrics2+ (+ `uuid` String,+ `team_id` Int32,+ `metric_name` LowCardinality(String),+ `time_bucket` DateTime MATERIALIZED toStartOfHour(timestamp),+ `series_fingerprint` UInt64 CODEC(Delta, Default),+ `resource_fingerprint` UInt64 DEFAULT 0,+ `timestamp` DateTime64(6) CODEC(DoubleDelta),+ `observed_timestamp` DateTime64(6),+ `original_expiry_timestamp` DateTime64(6),+ `created_at` DateTime64(6) MATERIALIZED now(),+ `service_name` LowCardinality(String),+ `metric_type` LowCardinality(String),+ `value` Float64 CODEC(Gorilla),+ `count` UInt64 DEFAULT 1 CODEC(T64),+ `histogram_bounds` Array(Float64),+ `histogram_counts` Array(UInt64),+ `trace_id` String,+ `span_id` String,+ `trace_flags` Int32,+ `has_labels` Bool DEFAULT false,+ `unit` LowCardinality(String),+ `aggregation_temporality` LowCardinality(String),+ `is_monotonic` Bool DEFAULT false,+ `instrumentation_scope` String,+ `_partition` UInt32,+ `_topic` String,+ `_offset` UInt64,+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_trace_id_bf trace_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,+ PROJECTION projection_series_minute+ (+ SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+ GROUP BY+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ minute+ ),+ PROJECTION projection_series_activity+ (+ SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+ GROUP BY+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ hour+ )+ )+ ENGINE = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics2', '{replica}-{shard}')+ PARTITION BY toDate(original_expiry_timestamp)+ ORDER BY (team_id, metric_name, time_bucket, series_fingerprint, timestamp)+ TTL original_expiry_timestamp+ SETTINGS+ index_granularity_bytes = 104857600,+ index_granularity = 8192,+ ttl_only_drop_parts = 1++ '''+# ---+# name: test_create_table_query[metrics2_input]+ '''++ CREATE TABLE IF NOT EXISTS posthog_test.metrics2_input+ (+ `uuid` String,+ `team_id` Int32,+ `metric_name` LowCardinality(String),+ `series_fingerprint` UInt64,+ `resource_fingerprint` UInt64,+ `timestamp` DateTime64(6),+ `observed_timestamp` DateTime64(6),+ `original_expiry_timestamp` DateTime64(6),+ `service_name` LowCardinality(String),+ `metric_type` LowCardinality(String),+ `value` Float64,+ `count` UInt64,+ `histogram_bounds` Array(Float64),+ `histogram_counts` Array(UInt64),+ `trace_id` String,+ `span_id` String,+ `trace_flags` Int32,+ `has_labels` Bool,+ `unit` LowCardinality(String),+ `aggregation_temporality` LowCardinality(String),+ `is_monotonic` Bool,+ `instrumentation_scope` String,+ `resource_attributes` Map(LowCardinality(String), String),+ `attributes` Map(LowCardinality(String), String),+ `_partition` UInt32,+ `_topic` String,+ `_offset` UInt64+ )+ ENGINE = Null++ '''+# ---+# name: test_create_table_query[metrics2_input_to_metric_attributes]+ '''++ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics2_input_to_metric_attributes TO posthog_test.metric_attributes2+ (+ `team_id` Int32,+ `time_bucket` DateTime64(0),+ `original_expiry_time_bucket` DateTime64(0),+ `service_name` LowCardinality(String),+ `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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+ FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'metric' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog_test.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ filtered_attributes+ )++ '''+# ---+# name: test_create_table_query[metrics2_input_to_metric_series]+ '''++ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics2_input_to_metric_series TO posthog_test.metric_series2+ AS SELECT+ team_id,+ metric_name,+ series_fingerprint,+ metric_type,+ unit,+ aggregation_temporality,+ is_monotonic,+ service_name,+ instrumentation_scope,+ resource_attributes,+ attributes,+ timestamp AS last_seen,+ original_expiry_timestamp+ FROM posthog_test.metrics2_input+ WHERE has_labels++ '''+# ---+# name: test_create_table_query[metrics2_input_to_metrics]+ '''++ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics2_input_to_metrics TO posthog_test.metrics2+ AS SELECT+ uuid,+ team_id,+ metric_name,+ series_fingerprint,+ resource_fingerprint,+ timestamp,+ observed_timestamp,+ original_expiry_timestamp,+ service_name,+ metric_type,+ value,+ count,+ histogram_bounds,+ histogram_counts,+ trace_id,+ span_id,+ trace_flags,+ has_labels,+ unit,+ aggregation_temporality,+ is_monotonic,+ instrumentation_scope,+ _partition,+ _topic,+ _offset+ FROM posthog_test.metrics2_input++ '''+# ---+# name: test_create_table_query[metrics2_input_to_resource_attributes]+ '''++ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics2_input_to_resource_attributes TO posthog_test.metric_attributes2+ (+ `team_id` Int32,+ `time_bucket` DateTime64(0),+ `original_expiry_time_bucket` DateTime64(0),+ `service_name` LowCardinality(String),+ `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,+ attribute_key,+ attribute_value,+ attribute_type,+ attribute_count+ FROM+ (+ SELECT+ team_id AS team_id,+ toStartOfInterval(timestamp, toIntervalHour(1)) AS time_bucket,+ toStartOfInterval(original_expiry_timestamp, toIntervalHour(1)) AS original_expiry_time_bucket,+ service_name AS service_name,+ resource_attributes AS filtered_attributes,+ arrayJoin(filtered_attributes) AS attribute,+ 'resource' AS attribute_type,+ attribute.1 AS attribute_key,+ attribute.2 AS attribute_value,+ sumSimpleState(1) AS attribute_count+ FROM posthog_test.metrics2_input+ WHERE has_labels+ GROUP BY+ team_id,+ time_bucket,+ original_expiry_time_bucket,+ service_name,+ filtered_attributes+ )++ '''+# ---# name: test_create_table_query[metrics]'''@@ -4324,6 +4674,9 @@'''# ---+# name: test_create_table_query[metrics_distributed]+ "CREATE TABLE IF NOT EXISTS posthog_test.metrics_distributed AS posthog_test.metrics2 ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'metrics2')"+# ---# name: test_create_table_query[ops_query_log_archive_mv]'''CREATE MATERIALIZED VIEW IF NOT EXISTS ops_query_log_archive_mv@@ -10858,6 +11211,34 @@'''# ---+# name: test_create_table_query_replicated_and_storage[metric_attributes2]+ '''++ CREATE TABLE IF NOT EXISTS posthog_test.metric_attributes2+ (+ `team_id` Int32,+ `time_bucket` DateTime64(0),+ `original_expiry_time_bucket` DateTime64(0),+ `service_name` LowCardinality(String),+ `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 1,+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1,+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 1+ )+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_attributes2', '{replica}-{shard}')+ PARTITION BY toDate(original_expiry_time_bucket)+ ORDER BY (team_id, attribute_type, time_bucket, attribute_key, attribute_value)+ TTL original_expiry_time_bucket+ SETTINGS+ index_granularity = 8192,+ ttl_only_drop_parts = 1++ '''+# ---# name: test_create_table_query_replicated_and_storage[metric_attributes]'''@@ -10938,6 +11319,37 @@'''# ---+# name: test_create_table_query_replicated_and_storage[metric_series2]+ '''++ CREATE TABLE IF NOT EXISTS posthog_test.metric_series2+ (+ `team_id` Int32,+ `metric_name` LowCardinality(String),+ `series_fingerprint` UInt64 CODEC(Delta, Default),+ `metric_type` LowCardinality(String),+ `unit` LowCardinality(String),+ `aggregation_temporality` LowCardinality(String),+ `is_monotonic` Bool DEFAULT false,+ `service_name` LowCardinality(String),+ `instrumentation_scope` String,+ `resource_attributes` Map(LowCardinality(String), String),+ `resource_fingerprint` UInt64 MATERIALIZED cityHash64(resource_attributes),+ `attributes` Map(LowCardinality(String), String),+ `last_seen` DateTime64(6),+ `original_expiry_timestamp` DateTime64(6),+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1+ )+ ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_series2', '{replica}-{shard}', last_seen)+ ORDER BY (team_id, metric_name, series_fingerprint)+ TTL original_expiry_timestamp+ SETTINGS index_granularity = 8192++ '''+# ---# name: test_create_table_query_replicated_and_storage[metrics1]'''@@ -11009,6 +11421,138 @@'''# ---+# name: test_create_table_query_replicated_and_storage[metrics2]+ '''++ CREATE TABLE IF NOT EXISTS posthog_test.metrics2+ (+ `uuid` String,+ `team_id` Int32,+ `metric_name` LowCardinality(String),+ `time_bucket` DateTime MATERIALIZED toStartOfHour(timestamp),+ `series_fingerprint` UInt64 CODEC(Delta, Default),+ `resource_fingerprint` UInt64 DEFAULT 0,+ `timestamp` DateTime64(6) CODEC(DoubleDelta),+ `observed_timestamp` DateTime64(6),+ `original_expiry_timestamp` DateTime64(6),+ `created_at` DateTime64(6) MATERIALIZED now(),+ `service_name` LowCardinality(String),+ `metric_type` LowCardinality(String),+ `value` Float64 CODEC(Gorilla),+ `count` UInt64 DEFAULT 1 CODEC(T64),+ `histogram_bounds` Array(Float64),+ `histogram_counts` Array(UInt64),+ `trace_id` String,+ `span_id` String,+ `trace_flags` Int32,+ `has_labels` Bool DEFAULT false,+ `unit` LowCardinality(String),+ `aggregation_temporality` LowCardinality(String),+ `is_monotonic` Bool DEFAULT false,+ `instrumentation_scope` String,+ `_partition` UInt32,+ `_topic` String,+ `_offset` UInt64,+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,+ INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,+ INDEX idx_trace_id_bf trace_id TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,+ PROJECTION projection_series_minute+ (+ SELECT+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfMinute(timestamp) AS minute,+ count() AS sample_count,+ sum(value) AS total_value,+ min(value) AS min_value,+ max(value) AS max_value,+ argMin(value, timestamp) AS first_value,+ argMax(value, timestamp) AS last_value+ GROUP BY+ team_id,+ metric_name,+ service_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ minute+ ),+ PROJECTION projection_series_activity+ (+ SELECT+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ toStartOfHour(timestamp) AS hour,+ count() AS sample_count,+ max(timestamp) AS last_seen+ GROUP BY+ team_id,+ service_name,+ metric_name,+ metric_type,+ resource_fingerprint,+ series_fingerprint,+ hour+ )+ )+ ENGINE = ReplicatedMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics2', '{replica}-{shard}')+ PARTITION BY toDate(original_expiry_timestamp)+ ORDER BY (team_id, metric_name, time_bucket, series_fingerprint, timestamp)+ TTL original_expiry_timestamp+ SETTINGS+ index_granularity_bytes = 104857600,+ index_granularity = 8192,+ ttl_only_drop_parts = 1++ '''+# ---+# name: test_create_table_query_replicated_and_storage[metrics2_input]+ '''++ CREATE TABLE IF NOT EXISTS posthog_test.metrics2_input+ (+ `uuid` String,+ `team_id` Int32,+ `metric_name` LowCardinality(String),+ `series_fingerprint` UInt64,+ `resource_fingerprint` UInt64,+ `timestamp` DateTime64(6),+ `observed_timestamp` DateTime64(6),+ `original_expiry_timestamp` DateTime64(6),+ `service_name` LowCardinality(String),+ `metric_type` LowCardinality(String),+ `value` Float64,+ `count` UInt64,+ `histogram_bounds` Array(Float64),+ `histogram_counts` Array(UInt64),+ `trace_id` String,+ `span_id` String,+ `trace_flags` Int32,+ `has_labels` Bool,+ `unit` LowCardinality(String),+ `aggregation_temporality` LowCardinality(String),+ `is_monotonic` Bool,+ `instrumentation_scope` String,+ `resource_attributes` Map(LowCardinality(String), String),+ `attributes` Map(LowCardinality(String), String),+ `_partition` UInt32,+ `_topic` String,+ `_offset` UInt64+ )+ ENGINE = Null++ '''+# ---# name: test_create_table_query_replicated_and_storage[partitioned_sharded_posthog_document_embeddings]'''