Skip to content

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

feat(metrics): add metrics4 tables and update sample expiry (#102433)

Frank Hamande992cdc83fc2
diff --git a/posthog/clickhouse/client/connection.py b/posthog/clickhouse/client/connection.py
index d25df8c91f11..236bd0ced3c8 100644
--- a/posthog/clickhouse/client/connection.py
+++ b/posthog/clickhouse/client/connection.py
@@ -40,6 +40,7 @@ class NodeRole(StrEnum):
LOGS = "logs"
# Below nodes are part of separate clusters.
+ APM = "apm"
AI_EVENTS = "ai_events"
AUX = "aux"
BATCH_EXPORTS = "batch_exports"
diff --git a/posthog/clickhouse/hcl/golden/dev/apm.hcl b/posthog/clickhouse/hcl/golden/dev/apm.hcl
index 111791677da6..bdfbb69df7c9 100644
--- a/posthog/clickhouse/hcl/golden/dev/apm.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/apm.hcl
@@ -146,6 +146,90 @@ database "posthog" {
}
}
+ table "kafka_metrics_avro4" {
+ 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-avro4"
+ format = "Avro"
+ num_consumers = 4
+ skip_broken_messages = 100
+ poll_timeout_ms = 10000
+ poll_max_batch_size = 1000
+ flush_interval_ms = 10000
+ thread_per_consumer = true
+ }
+ }
+
table "kafka_trace_spans_avro" {
settings = {
input_format_avro_allow_missing_fields = "1"
@@ -227,6 +311,92 @@ database "posthog" {
}
}
+ table "metrics4_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 "writable_logs34" {
settings = {
background_insert_batch = "1"
@@ -529,98 +699,289 @@ database "posthog" {
}
}
- table "writable_metrics_kafka_metrics" {
- column "_partition" {
- type = "UInt32"
+ table "writable_metrics4_attributes" {
+ column "team_id" {
+ type = "Int32"
}
- column "_topic" {
- type = "String"
+ column "metric_name" {
+ type = "LowCardinality(String)"
}
- column "max_offset" {
- type = "SimpleAggregateFunction(max, UInt64)"
+ column "time_bucket" {
+ type = "DateTime64(0)"
}
- column "max_observed_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(9))"
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
}
- column "max_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(9))"
+ column "service_name" {
+ type = "LowCardinality(String)"
}
- column "max_created_at" {
- type = "SimpleAggregateFunction(max, DateTime64(9))"
+ column "attribute_key" {
+ type = "LowCardinality(String)"
}
- column "max_lag" {
- type = "SimpleAggregateFunction(max, UInt64)"
+ 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 = "metrics_kafka_metrics"
+ remote_table = "metrics4_attributes"
}
}
- table "writable_query_log_archive" {
- column "hostname" {
- type = "LowCardinality(String)"
+ table "writable_metrics4_names" {
+ column "team_id" {
+ type = "Int32"
}
- column "user" {
+ column "metric_name" {
type = "LowCardinality(String)"
}
- column "query_id" {
- type = "String"
- }
- column "initial_query_id" {
- type = "String"
+ column "time_bucket" {
+ type = "DateTime64(0)"
}
- column "is_initial_query" {
- type = "UInt8"
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
}
- column "type" {
- type = "Enum8('QueryStart'=1, 'QueryFinish'=2, 'ExceptionBeforeStart'=3, 'ExceptionWhileProcessing'=4)"
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
}
- column "event_date" {
- type = "Date"
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_names"
}
- column "event_time" {
- type = "DateTime"
+ }
+
+ table "writable_metrics4_samples" {
+ column "team_id" {
+ type = "Int32"
}
- column "event_time_microseconds" {
- type = "DateTime64(6)"
+ column "metric_name" {
+ type = "LowCardinality(String)"
}
- column "query_start_time" {
+ column "time_bucket" {
type = "DateTime"
}
- column "query_start_time_microseconds" {
- type = "DateTime64(6)"
- }
- column "query_duration_ms" {
+ column "series_fingerprint" {
type = "UInt64"
}
- column "read_rows" {
- type = "UInt64"
+ column "original_expiry_date" {
+ type = "Date32"
}
- column "read_bytes" {
- type = "UInt64"
+ column "resource_fingerprint" {
+ type = "SimpleAggregateFunction(any, UInt64)"
}
- column "written_rows" {
- type = "UInt64"
+ column "service_name" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
}
- column "written_bytes" {
- type = "UInt64"
+ column "metric_type" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
}
- column "result_rows" {
- type = "UInt64"
+ column "unit" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
}
- column "result_bytes" {
- type = "UInt64"
+ column "aggregation_temporality" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
}
- column "memory_usage" {
- type = "UInt64"
+ column "is_monotonic" {
+ type = "SimpleAggregateFunction(max, UInt8)"
}
- column "peak_threads_usage" {
- type = "UInt64"
+ column "has_labels" {
+ type = "SimpleAggregateFunction(max, UInt8)"
}
- column "current_database" {
- type = "LowCardinality(String)"
+ column "instrumentation_scope" {
+ type = "SimpleAggregateFunction(any, String)"
+ }
+ column "histogram_bounds" {
+ type = "SimpleAggregateFunction(anyLast, Array(Float64))"
+ }
+ column "_topic" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ }
+ column "observed_timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ }
+ column "value_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Float64))"
+ }
+ column "count_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64))"
+ }
+ column "histogram_counts_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64)))"
+ }
+ column "trace_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "span_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "trace_flags_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_samples"
+ }
+ }
+
+ table "writable_metrics4_series" {
+ 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"
+ 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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfHour(timestamp)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_series"
+ }
+ }
+
+ table "writable_metrics_kafka_metrics" {
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics_kafka_metrics"
+ }
+ }
+
+ table "writable_query_log_archive" {
+ column "hostname" {
+ type = "LowCardinality(String)"
+ }
+ column "user" {
+ type = "LowCardinality(String)"
+ }
+ column "query_id" {
+ type = "String"
+ }
+ column "initial_query_id" {
+ type = "String"
+ }
+ column "is_initial_query" {
+ type = "UInt8"
+ }
+ column "type" {
+ type = "Enum8('QueryStart'=1, 'QueryFinish'=2, 'ExceptionBeforeStart'=3, 'ExceptionWhileProcessing'=4)"
+ }
+ column "event_date" {
+ type = "Date"
+ }
+ column "event_time" {
+ type = "DateTime"
+ }
+ column "event_time_microseconds" {
+ type = "DateTime64(6)"
+ }
+ column "query_start_time" {
+ type = "DateTime"
+ }
+ column "query_start_time_microseconds" {
+ type = "DateTime64(6)"
+ }
+ column "query_duration_ms" {
+ type = "UInt64"
+ }
+ column "read_rows" {
+ type = "UInt64"
+ }
+ column "read_bytes" {
+ type = "UInt64"
+ }
+ column "written_rows" {
+ type = "UInt64"
+ }
+ column "written_bytes" {
+ type = "UInt64"
+ }
+ column "result_rows" {
+ type = "UInt64"
+ }
+ column "result_bytes" {
+ type = "UInt64"
+ }
+ column "memory_usage" {
+ type = "UInt64"
+ }
+ column "peak_threads_usage" {
+ type = "UInt64"
+ }
+ column "current_database" {
+ type = "LowCardinality(String)"
}
column "query" {
type = "String"
@@ -898,6 +1259,144 @@ SQL
}
}
+ materialized_view "kafka_metrics_avro4_mv" {
+ to_table = "posthog.metrics4_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,
+ timestamp
+ + toIntervalDay(
+ assumeNotNull(
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
+ )
+ )
+ ) 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_avro4
+WHERE kafka_metrics_avro4.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 "_offset" {
+ type = "UInt64"
+ }
+ }
+
materialized_view "kafka_metrics_avro_kafka_metrics_mv" {
to_table = "posthog.writable_metrics_kafka_metrics"
query = <<SQL
@@ -1053,6 +1552,329 @@ SQL
}
}
+ materialized_view "metrics4_input_to_metrics4_attributes" {
+ to_table = "posthog.writable_metrics4_attributes"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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 "metrics4_input_to_metrics4_names" {
+ to_table = "posthog.writable_metrics4_names"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics4_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_resource_attributes" {
+ to_table = "posthog.writable_metrics4_attributes"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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 "metrics4_input_to_metrics4_samples" {
+ to_table = "posthog.writable_metrics4_samples"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toDateTime(toStartOfHour(timestamp)) AS time_bucket,
+ series_fingerprint,
+ toDate32(original_expiry_timestamp) AS original_expiry_date,
+ any(resource_fingerprint) AS resource_fingerprint,
+ any(service_name) AS service_name,
+ any(metric_type) AS metric_type,
+ any(unit) AS unit,
+ any(aggregation_temporality) AS aggregation_temporality,
+ max(toUInt8(is_monotonic)) AS is_monotonic,
+ max(toUInt8(has_labels)) AS has_labels,
+ any(instrumentation_scope) AS instrumentation_scope,
+ anyLast(histogram_bounds) AS histogram_bounds,
+ any(_topic) AS _topic,
+ groupArray(10000)(timestamp) AS timestamp_arr,
+ groupArray(10000)(observed_timestamp) AS observed_timestamp_arr,
+ groupArray(10000)(value) AS value_arr,
+ groupArray(10000)(count) AS count_arr,
+ groupArray(10000)(histogram_counts) AS histogram_counts_arr,
+ groupArray(10000)(trace_id) AS trace_id_arr,
+ groupArray(10000)(span_id) AS span_id_arr,
+ groupArray(10000)(trace_flags) AS trace_flags_arr
+FROM posthog.metrics4_input
+GROUP BY
+ team_id, metric_name, time_bucket, series_fingerprint, original_expiry_date
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "metric_type" {
+ type = "String"
+ }
+ column "unit" {
+ type = "String"
+ }
+ column "aggregation_temporality" {
+ type = "String"
+ }
+ column "is_monotonic" {
+ type = "UInt8"
+ }
+ column "has_labels" {
+ type = "UInt8"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "histogram_bounds" {
+ type = "Array(Float64)"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "timestamp_arr" {
+ type = "Array(DateTime64(6))"
+ }
+ column "observed_timestamp_arr" {
+ type = "Array(DateTime64(6))"
+ }
+ column "value_arr" {
+ type = "Array(Float64)"
+ }
+ column "count_arr" {
+ type = "Array(UInt64)"
+ }
+ column "histogram_counts_arr" {
+ type = "Array(Array(UInt64))"
+ }
+ column "trace_id_arr" {
+ type = "Array(String)"
+ }
+ column "span_id_arr" {
+ type = "Array(String)"
+ }
+ column "trace_flags_arr" {
+ type = "Array(Int32)"
+ }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_series" {
+ to_table = "posthog.writable_metrics4_series"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp,
+ original_expiry_timestamp
+FROM posthog.metrics4_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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+
materialized_view "ops_query_log_archive_mv" {
to_table = "posthog.writable_query_log_archive"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index 65e3474727ec..26f92c6b1fc1 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -1962,6 +1962,293 @@ SQL
}
}
+ table "metrics4_attributes" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ 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 "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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.metrics4_attributes"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_names" {
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_samples" {
+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint"]
+ partition_by = "original_expiry_date"
+ ttl = "original_expiry_date"
+ settings = {
+ index_granularity = "128"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "SimpleAggregateFunction(any, UInt64)"
+ }
+ column "service_name" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "metric_type" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "unit" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "aggregation_temporality" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "is_monotonic" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "has_labels" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "instrumentation_scope" {
+ type = "SimpleAggregateFunction(any, String)"
+ }
+ column "histogram_bounds" {
+ type = "SimpleAggregateFunction(anyLast, Array(Float64))"
+ }
+ column "_topic" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "observed_timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "value_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Float64))"
+ codec = "Gorilla(8), Default"
+ }
+ column "count_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64))"
+ codec = "T64, Default"
+ }
+ column "histogram_counts_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64)))"
+ codec = "T64, Default"
+ }
+ column "trace_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "span_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "trace_flags_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))"
+ }
+ index "idx_metric_type_set" {
+ expr = "metric_type"
+ type = "set(10)"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_trace_id_bf" {
+ expr = "trace_id_arr"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_samples"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_series" {
+ order_by = ["team_id", "metric_name", "series_fingerprint", "time_bucket"]
+ partition_by = "toStartOfWeek(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "1024"
+ ttl_only_drop_parts = "1"
+ }
+ 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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfHour(timestamp)"
+ }
+ 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
+ }
+ index "idx_timestamp_minmax" {
+ expr = "timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_series"
+ replica_name = "{replica}-{shard}"
+ version_column = "timestamp"
+ }
+ }
+
table "metrics_distributed" {
column "team_id" {
type = "Int32"
@@ -3149,13 +3436,13 @@ SELECT
cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,
timestamp,
observed_timestamp,
- observed_timestamp
+ 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))
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
)
)
) AS original_expiry_timestamp,
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index a4cf924c439a..e6a4c3ce72ac 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -2530,6 +2530,293 @@ SQL
}
}
+ table "metrics4_attributes" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ 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 "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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.metrics4_attributes"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_names" {
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_samples" {
+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint"]
+ partition_by = "original_expiry_date"
+ ttl = "original_expiry_date"
+ settings = {
+ index_granularity = "128"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "SimpleAggregateFunction(any, UInt64)"
+ }
+ column "service_name" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "metric_type" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "unit" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "aggregation_temporality" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "is_monotonic" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "has_labels" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "instrumentation_scope" {
+ type = "SimpleAggregateFunction(any, String)"
+ }
+ column "histogram_bounds" {
+ type = "SimpleAggregateFunction(anyLast, Array(Float64))"
+ }
+ column "_topic" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "observed_timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "value_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Float64))"
+ codec = "Gorilla(8), Default"
+ }
+ column "count_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64))"
+ codec = "T64, Default"
+ }
+ column "histogram_counts_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64)))"
+ codec = "T64, Default"
+ }
+ column "trace_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "span_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "trace_flags_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))"
+ }
+ index "idx_metric_type_set" {
+ expr = "metric_type"
+ type = "set(10)"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_trace_id_bf" {
+ expr = "trace_id_arr"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_samples"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_series" {
+ order_by = ["team_id", "metric_name", "series_fingerprint", "time_bucket"]
+ partition_by = "toStartOfWeek(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "1024"
+ ttl_only_drop_parts = "1"
+ }
+ 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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfHour(timestamp)"
+ }
+ 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
+ }
+ index "idx_timestamp_minmax" {
+ expr = "timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_series"
+ replica_name = "{replica}-{shard}"
+ version_column = "timestamp"
+ }
+ }
+
table "metrics_distributed" {
column "team_id" {
type = "Int32"
@@ -3633,13 +3920,13 @@ SELECT
cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,
timestamp,
observed_timestamp,
- observed_timestamp
+ 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))
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
)
)
) AS original_expiry_timestamp,
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index 1767d1472a61..641e54e0f3d6 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -3728,6 +3728,89 @@ database "posthog" {
}
}
+ table "kafka_metrics_avro4" {
+ 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-avro4"
+ format = "Avro"
+ num_consumers = 1
+ 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"
@@ -7404,7 +7487,14 @@ SQL
}
}
- table "metrics_distributed" {
+ table "metrics4_attributes" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ 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"
}
@@ -7412,20 +7502,70 @@ SQL
type = "LowCardinality(String)"
}
column "time_bucket" {
- type = "DateTime"
- materialized = "toStartOfHour(timestamp)"
+ 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.metrics4_attributes"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_input" {
+ column "uuid" {
+ type = "String"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
}
column "series_fingerprint" {
- type = "UInt64"
- codec = "Delta(8), Default"
+ type = "UInt64"
}
column "resource_fingerprint" {
- type = "UInt64"
- default = "0"
+ type = "UInt64"
}
column "timestamp" {
- type = "DateTime64(6)"
- codec = "DoubleDelta"
+ type = "DateTime64(6)"
}
column "observed_timestamp" {
type = "DateTime64(6)"
@@ -7433,10 +7573,6 @@ SQL
column "original_expiry_timestamp" {
type = "DateTime64(6)"
}
- column "created_at" {
- type = "DateTime64(6)"
- materialized = "now()"
- }
column "service_name" {
type = "LowCardinality(String)"
}
@@ -7444,13 +7580,10 @@ SQL
type = "LowCardinality(String)"
}
column "value" {
- type = "Float64"
- codec = "Gorilla(8)"
+ type = "Float64"
}
column "count" {
- type = "UInt64"
- default = "1"
- codec = "T64"
+ type = "UInt64"
}
column "histogram_bounds" {
type = "Array(Float64)"
@@ -7468,8 +7601,7 @@ SQL
type = "Int32"
}
column "has_labels" {
- type = "Bool"
- default = "false"
+ type = "Bool"
}
column "unit" {
type = "LowCardinality(String)"
@@ -7478,12 +7610,17 @@ SQL
type = "LowCardinality(String)"
}
column "is_monotonic" {
- type = "Bool"
- default = "false"
+ 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"
}
@@ -7493,87 +7630,406 @@ SQL
column "_offset" {
type = "UInt64"
}
- engine "distributed" {
- cluster_name = "posthog_single_shard"
- remote_database = "posthog"
- remote_table = "metrics2"
+ engine "null" {
}
}
- table "metrics_kafka_metrics" {
- order_by = ["_topic", "_partition"]
+ table "metrics4_names" {
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_timestamp"
settings = {
index_granularity = "8192"
}
- column "_partition" {
- type = "UInt32"
- }
- column "_topic" {
- type = "String"
- }
- column "max_offset" {
- type = "SimpleAggregateFunction(max, UInt64)"
+ column "team_id" {
+ type = "Int32"
}
- column "max_observed_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(9))"
+ column "metric_name" {
+ type = "LowCardinality(String)"
}
- column "max_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(9))"
+ column "time_bucket" {
+ type = "DateTime64(0)"
}
- column "max_created_at" {
- type = "SimpleAggregateFunction(max, DateTime64(9))"
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
}
- column "max_lag" {
- type = "SimpleAggregateFunction(max, UInt64)"
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
}
engine "replicated_aggregating_merge_tree" {
- zoo_path = "/clickhouse/tables/noshard/posthog.metrics_kafka_metrics"
- replica_name = "{replica}"
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
+ replica_name = "{replica}-{shard}"
}
}
- table "partitioned_sharded_posthog_document_embeddings" {
- order_by = ["team_id", "toDate(timestamp)", "product", "document_type", "model_name", "rendering", "cityHash64(document_id)"]
- partition_by = "toMonday(timestamp)"
- ttl = "timestamp + toIntervalMonth(3)"
+ table "metrics4_samples" {
+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint"]
+ partition_by = "original_expiry_date"
+ ttl = "original_expiry_date"
settings = {
- index_granularity = "512"
+ index_granularity = "128"
ttl_only_drop_parts = "1"
}
column "team_id" {
- type = "Int64"
+ type = "Int32"
}
- column "product" {
+ column "metric_name" {
type = "LowCardinality(String)"
}
- column "document_type" {
- type = "LowCardinality(String)"
+ column "time_bucket" {
+ type = "DateTime"
}
- column "model_name" {
- type = "LowCardinality(String)"
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
}
- column "rendering" {
- type = "LowCardinality(String)"
+ column "original_expiry_date" {
+ type = "Date32"
}
- column "document_id" {
- type = "String"
+ column "resource_fingerprint" {
+ type = "SimpleAggregateFunction(any, UInt64)"
}
- column "timestamp" {
- type = "DateTime64(3, 'UTC')"
+ column "service_name" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
}
- column "inserted_at" {
- type = "DateTime64(3, 'UTC')"
+ column "metric_type" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
}
- column "content" {
- type = "String"
- default = "''"
+ column "unit" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
}
- column "metadata" {
- type = "String"
- default = "'{}'"
+ column "aggregation_temporality" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
}
- column "embedding" {
- type = "Array(Float64)"
+ column "is_monotonic" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "has_labels" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "instrumentation_scope" {
+ type = "SimpleAggregateFunction(any, String)"
+ }
+ column "histogram_bounds" {
+ type = "SimpleAggregateFunction(anyLast, Array(Float64))"
+ }
+ column "_topic" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "observed_timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "value_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Float64))"
+ codec = "Gorilla(8), Default"
+ }
+ column "count_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64))"
+ codec = "T64, Default"
+ }
+ column "histogram_counts_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64)))"
+ codec = "T64, Default"
+ }
+ column "trace_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "span_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "trace_flags_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))"
+ }
+ index "idx_metric_type_set" {
+ expr = "metric_type"
+ type = "set(10)"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_trace_id_bf" {
+ expr = "trace_id_arr"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_samples"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_series" {
+ order_by = ["team_id", "metric_name", "series_fingerprint", "time_bucket"]
+ partition_by = "toStartOfWeek(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "1024"
+ ttl_only_drop_parts = "1"
+ }
+ 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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfHour(timestamp)"
+ }
+ 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
+ }
+ index "idx_timestamp_minmax" {
+ expr = "timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_series"
+ replica_name = "{replica}-{shard}"
+ version_column = "timestamp"
+ }
+ }
+
+ table "metrics_distributed" {
+ 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 "metrics_kafka_metrics" {
+ order_by = ["_topic", "_partition"]
+ settings = {
+ index_granularity = "8192"
+ }
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics_kafka_metrics"
+ replica_name = "{replica}"
+ }
+ }
+
+ table "partitioned_sharded_posthog_document_embeddings" {
+ order_by = ["team_id", "toDate(timestamp)", "product", "document_type", "model_name", "rendering", "cityHash64(document_id)"]
+ partition_by = "toMonday(timestamp)"
+ ttl = "timestamp + toIntervalMonth(3)"
+ settings = {
+ index_granularity = "512"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int64"
+ }
+ column "product" {
+ type = "LowCardinality(String)"
+ }
+ column "document_type" {
+ type = "LowCardinality(String)"
+ }
+ column "model_name" {
+ type = "LowCardinality(String)"
+ }
+ column "rendering" {
+ type = "LowCardinality(String)"
+ }
+ column "document_id" {
+ type = "String"
+ }
+ column "timestamp" {
+ type = "DateTime64(3, 'UTC')"
+ }
+ column "inserted_at" {
+ type = "DateTime64(3, 'UTC')"
+ }
+ column "content" {
+ type = "String"
+ default = "''"
+ }
+ column "metadata" {
+ type = "String"
+ default = "'{}'"
+ }
+ column "embedding" {
+ type = "Array(Float64)"
}
column "_timestamp" {
type = "DateTime"
@@ -17373,12 +17829,247 @@ SQL
column "severity_text" {
type = "LowCardinality(String)"
}
- column "severity_number" {
- type = "Int32"
+ column "severity_number" {
+ type = "Int32"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "event_name" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "level" {
+ type = "String"
+ alias = "severity_text"
+ }
+ column "mat_body_ipv4_matches" {
+ type = "Array(String)"
+ alias = "extractAll(body, '(\\\\d\\\\.((25[0-5]|(2[0-4]|1(0, 1)[0-9])(0, 1)[0-9])\\\\.)(2, 2)([0-9]))')"
+ }
+ column "time_minute" {
+ type = "DateTime"
+ alias = "toStartOfMinute(timestamp)"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"
+ }
+ column "attributes_map_float" {
+ type = "Map(LowCardinality(String), Float64)"
+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str))"
+ }
+ column "attributes_map_datetime" {
+ type = "Map(LowCardinality(String), DateTime64(6))"
+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"
+ }
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_bytes_uncompressed" {
+ type = "UInt64"
+ }
+ column "_bytes_compressed" {
+ type = "UInt64"
+ }
+ column "_record_count" {
+ type = "UInt64"
+ }
+ column "pattern" {
+ type = "String"
+ }
+ column "pattern_version" {
+ type = "UInt8"
+ }
+ engine "distributed" {
+ cluster_name = "posthog_single_shard"
+ remote_database = "posthog"
+ remote_table = "logs34"
+ }
+ }
+
+ table "writable_metrics4_attributes" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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 = "metrics4_attributes"
+ }
+ }
+
+ table "writable_metrics4_names" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_names"
+ }
+ }
+
+ table "writable_metrics4_samples" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "SimpleAggregateFunction(any, UInt64)"
+ }
+ column "service_name" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "metric_type" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "unit" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "aggregation_temporality" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "is_monotonic" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "has_labels" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "instrumentation_scope" {
+ type = "SimpleAggregateFunction(any, String)"
+ }
+ column "histogram_bounds" {
+ type = "SimpleAggregateFunction(anyLast, Array(Float64))"
+ }
+ column "_topic" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ }
+ column "observed_timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ }
+ column "value_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Float64))"
+ }
+ column "count_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64))"
+ }
+ column "histogram_counts_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64)))"
+ }
+ column "trace_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "span_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "trace_flags_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_samples"
+ }
+ }
+
+ table "writable_metrics4_series" {
+ 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"
+ default = "false"
}
column "service_name" {
type = "LowCardinality(String)"
}
+ column "instrumentation_scope" {
+ type = "String"
+ }
column "resource_attributes" {
type = "Map(LowCardinality(String), String)"
}
@@ -17386,67 +18077,23 @@ SQL
type = "UInt64"
materialized = "cityHash64(resource_attributes)"
}
- column "instrumentation_scope" {
- type = "String"
- }
- column "event_name" {
- type = "String"
- }
- column "attributes_map_str" {
- type = "Map(LowCardinality(String), String)"
- }
- column "level" {
- type = "String"
- alias = "severity_text"
- }
- column "mat_body_ipv4_matches" {
- type = "Array(String)"
- alias = "extractAll(body, '(\\\\d\\\\.((25[0-5]|(2[0-4]|1(0, 1)[0-9])(0, 1)[0-9])\\\\.)(2, 2)([0-9]))')"
- }
- column "time_minute" {
- type = "DateTime"
- alias = "toStartOfMinute(timestamp)"
- }
column "attributes" {
- type = "Map(LowCardinality(String), String)"
- alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"
- }
- column "attributes_map_float" {
- type = "Map(LowCardinality(String), Float64)"
- materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str))"
- }
- column "attributes_map_datetime" {
- type = "Map(LowCardinality(String), DateTime64(6))"
- materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"
- }
- column "_partition" {
- type = "UInt32"
- }
- column "_topic" {
- type = "String"
- }
- column "_offset" {
- type = "UInt64"
- }
- column "_bytes_uncompressed" {
- type = "UInt64"
- }
- column "_bytes_compressed" {
- type = "UInt64"
+ type = "Map(LowCardinality(String), String)"
}
- column "_record_count" {
- type = "UInt64"
+ column "timestamp" {
+ type = "DateTime64(6)"
}
- column "pattern" {
- type = "String"
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfHour(timestamp)"
}
- column "pattern_version" {
- type = "UInt8"
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
}
engine "distributed" {
- cluster_name = "posthog_single_shard"
+ cluster_name = "logs"
remote_database = "posthog"
- remote_table = "logs34"
+ remote_table = "metrics4_series"
}
}
@@ -20680,108 +21327,246 @@ SQL
column "_offset" {
type = "UInt64"
}
- column "_record_count" {
- type = "Int64"
+ column "_record_count" {
+ type = "Int64"
+ }
+ column "_bytes_uncompressed" {
+ type = "Nullable(Int64)"
+ }
+ column "_bytes_compressed" {
+ type = "Nullable(Int64)"
+ }
+ column "pattern" {
+ type = "String"
+ }
+ column "pattern_version" {
+ type = "UInt8"
+ }
+ }
+
+ materialized_view "kafka_logs_avro_billing_metrics_mv" {
+ to_table = "posthog.logs_billing_metrics"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ sumSimpleState(_bytes_uncompressed) AS bytes_uncompressed,
+ sumSimpleState(_bytes_compressed) AS bytes_compressed,
+ sumSimpleState(1) AS record_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalMinute(1)) AS time_bucket,
+ service_name AS service_name,
+ _bytes_uncompressed,
+ _bytes_compressed
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "bytes_uncompressed" {
+ type = "SimpleAggregateFunction(sum, Float64)"
+ }
+ column "bytes_compressed" {
+ type = "SimpleAggregateFunction(sum, Float64)"
+ }
+ column "record_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "kafka_logs_avro_kafka_metrics_mv" {
+ to_table = "posthog.logs_kafka_metrics"
+ query = <<SQL
+SELECT
+ _partition,
+ _topic,
+ maxSimpleState(_offset) AS max_offset,
+ maxSimpleState(observed_timestamp) AS max_observed_timestamp,
+ maxSimpleState(timestamp) AS max_timestamp,
+ maxSimpleState(now()) AS max_created_at,
+ maxSimpleState(now() - observed_timestamp) AS max_lag
+FROM posthog.logs34
+GROUP BY
+ _partition, _topic
+SQL
+
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime)"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, Decimal(18, 6))"
+ }
+ }
+
+ materialized_view "kafka_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,
+ timestamp
+ + toIntervalDay(
+ assumeNotNull(
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
+ )
+ )
+ ) 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 "_bytes_uncompressed" {
- type = "Nullable(Int64)"
+ column "histogram_bounds" {
+ type = "Array(Float64)"
}
- column "_bytes_compressed" {
- type = "Nullable(Int64)"
+ column "histogram_counts" {
+ type = "Array(UInt64)"
}
- column "pattern" {
+ column "trace_id" {
type = "String"
}
- column "pattern_version" {
- type = "UInt8"
+ column "span_id" {
+ type = "String"
}
- }
-
- materialized_view "kafka_logs_avro_billing_metrics_mv" {
- to_table = "posthog.logs_billing_metrics"
- query = <<SQL
-SELECT
- team_id,
- time_bucket,
- service_name,
- sumSimpleState(_bytes_uncompressed) AS bytes_uncompressed,
- sumSimpleState(_bytes_compressed) AS bytes_compressed,
- sumSimpleState(1) AS record_count
-FROM
- (
- SELECT
- team_id,
- toStartOfInterval(timestamp, toIntervalMinute(1)) AS time_bucket,
- service_name AS service_name,
- _bytes_uncompressed,
- _bytes_compressed
- FROM posthog.logs34
- )
-GROUP BY
- team_id, time_bucket, service_name
-SQL
-
- column "team_id" {
+ column "trace_flags" {
type = "Int32"
}
- column "time_bucket" {
- type = "DateTime"
- }
- column "service_name" {
- type = "LowCardinality(String)"
- }
- column "bytes_uncompressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
+ column "has_labels" {
+ type = "Bool"
}
- column "bytes_compressed" {
- type = "SimpleAggregateFunction(sum, Float64)"
+ column "unit" {
+ type = "String"
}
- column "record_count" {
- type = "SimpleAggregateFunction(sum, UInt64)"
+ column "aggregation_temporality" {
+ type = "String"
}
- }
-
- materialized_view "kafka_logs_avro_kafka_metrics_mv" {
- to_table = "posthog.logs_kafka_metrics"
- query = <<SQL
-SELECT
- _partition,
- _topic,
- maxSimpleState(_offset) AS max_offset,
- maxSimpleState(observed_timestamp) AS max_observed_timestamp,
- maxSimpleState(timestamp) AS max_timestamp,
- maxSimpleState(now()) AS max_created_at,
- maxSimpleState(now() - observed_timestamp) AS max_lag
-FROM posthog.logs34
-GROUP BY
- _partition, _topic
-SQL
-
- column "_partition" {
- type = "UInt32"
+ column "is_monotonic" {
+ type = "UInt8"
}
- column "_topic" {
+ column "instrumentation_scope" {
type = "String"
}
- column "max_offset" {
- type = "SimpleAggregateFunction(max, UInt64)"
+ column "resource_attributes" {
+ type = "Map(String, String)"
}
- column "max_observed_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(6))"
+ column "attributes" {
+ type = "Map(String, String)"
}
- column "max_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(6))"
+ column "_partition" {
+ type = "UInt64"
}
- column "max_created_at" {
- type = "SimpleAggregateFunction(max, DateTime)"
+ column "_topic" {
+ type = "LowCardinality(String)"
}
- column "max_lag" {
- type = "SimpleAggregateFunction(max, Decimal(18, 6))"
+ column "_offset" {
+ type = "UInt64"
}
}
- materialized_view "kafka_metrics_avro2_mv" {
- to_table = "posthog.metrics2_input"
+ materialized_view "kafka_metrics_avro4_mv" {
+ to_table = "posthog.metrics4_input"
query = <<SQL
SELECT
uuid,
@@ -20791,13 +21576,13 @@ SELECT
cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,
timestamp,
observed_timestamp,
- observed_timestamp
+ 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))
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
)
)
) AS original_expiry_timestamp,
@@ -20828,8 +21613,8 @@ SELECT
_partition,
_topic,
_offset
-FROM posthog.kafka_metrics_avro2
-WHERE kafka_metrics_avro2.series_fingerprint IS NOT NULL
+FROM posthog.kafka_metrics_avro4
+WHERE kafka_metrics_avro4.series_fingerprint IS NOT NULL
SETTINGS
min_insert_block_size_rows = 0,
min_insert_block_size_bytes = 0
@@ -21863,79 +22648,201 @@ SQL
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 "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 "metrics2_input_to_resource_attributes3" {
+ to_table = "posthog.metric_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
}
- column "original_expiry_timestamp" {
- type = "DateTime64(6)"
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
}
column "service_name" {
type = "LowCardinality(String)"
}
- column "metric_type" {
+ column "attribute_key" {
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" {
+ column "attribute_value" {
type = "String"
}
- column "trace_flags" {
- type = "Int32"
- }
- column "has_labels" {
- type = "Bool"
- }
- column "unit" {
- type = "LowCardinality(String)"
- }
- column "aggregation_temporality" {
+ column "attribute_type" {
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"
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
}
}
- materialized_view "metrics2_input_to_resource_attributes" {
- to_table = "posthog.metric_attributes2"
+ materialized_view "metrics4_input_to_metrics4_attributes" {
+ to_table = "posthog.writable_metrics4_attributes"
query = <<SQL
SELECT
team_id,
+ metric_name,
time_bucket,
original_expiry_time_bucket,
service_name,
@@ -21947,25 +22854,29 @@ FROM
(
SELECT
team_id AS team_id,
+ metric_name AS metric_name,
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,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS filtered_attributes,
arrayJoin(filtered_attributes) AS attribute,
- 'resource' AS attribute_type,
+ 'metric' AS attribute_type,
attribute.1 AS attribute_key,
attribute.2 AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.metrics2_input
+ FROM posthog.metrics4_input
WHERE has_labels
GROUP BY
- team_id, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
)
SQL
column "team_id" {
type = "Int32"
}
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
column "time_bucket" {
type = "DateTime64(0)"
}
@@ -21989,8 +22900,40 @@ SQL
}
}
- materialized_view "metrics2_input_to_resource_attributes3" {
- to_table = "posthog.metric_attributes3"
+ materialized_view "metrics4_input_to_metrics4_names" {
+ to_table = "posthog.writable_metrics4_names"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics4_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_resource_attributes" {
+ to_table = "posthog.writable_metrics4_attributes"
query = <<SQL
SELECT
team_id,
@@ -22016,7 +22959,7 @@ FROM
attribute.1 AS attribute_key,
attribute.2 AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.metrics2_input
+ FROM posthog.metrics4_input
WHERE has_labels
GROUP BY
team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
@@ -22052,6 +22995,171 @@ SQL
}
}
+ materialized_view "metrics4_input_to_metrics4_samples" {
+ to_table = "posthog.writable_metrics4_samples"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toDateTime(toStartOfHour(timestamp)) AS time_bucket,
+ series_fingerprint,
+ toDate32(original_expiry_timestamp) AS original_expiry_date,
+ any(resource_fingerprint) AS resource_fingerprint,
+ any(service_name) AS service_name,
+ any(metric_type) AS metric_type,
+ any(unit) AS unit,
+ any(aggregation_temporality) AS aggregation_temporality,
+ max(toUInt8(is_monotonic)) AS is_monotonic,
+ max(toUInt8(has_labels)) AS has_labels,
+ any(instrumentation_scope) AS instrumentation_scope,
+ anyLast(histogram_bounds) AS histogram_bounds,
+ any(_topic) AS _topic,
+ groupArray(10000)(timestamp) AS timestamp_arr,
+ groupArray(10000)(observed_timestamp) AS observed_timestamp_arr,
+ groupArray(10000)(value) AS value_arr,
+ groupArray(10000)(count) AS count_arr,
+ groupArray(10000)(histogram_counts) AS histogram_counts_arr,
+ groupArray(10000)(trace_id) AS trace_id_arr,
+ groupArray(10000)(span_id) AS span_id_arr,
+ groupArray(10000)(trace_flags) AS trace_flags_arr
+FROM posthog.metrics4_input
+GROUP BY
+ team_id, metric_name, time_bucket, series_fingerprint, original_expiry_date
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "metric_type" {
+ type = "String"
+ }
+ column "unit" {
+ type = "String"
+ }
+ column "aggregation_temporality" {
+ type = "String"
+ }
+ column "is_monotonic" {
+ type = "UInt8"
+ }
+ column "has_labels" {
+ type = "UInt8"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "histogram_bounds" {
+ type = "Array(Float64)"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "timestamp_arr" {
+ type = "Array(DateTime64(6))"
+ }
+ column "observed_timestamp_arr" {
+ type = "Array(DateTime64(6))"
+ }
+ column "value_arr" {
+ type = "Array(Float64)"
+ }
+ column "count_arr" {
+ type = "Array(UInt64)"
+ }
+ column "histogram_counts_arr" {
+ type = "Array(Array(UInt64))"
+ }
+ column "trace_id_arr" {
+ type = "Array(String)"
+ }
+ column "span_id_arr" {
+ type = "Array(String)"
+ }
+ column "trace_flags_arr" {
+ type = "Array(Int32)"
+ }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_series" {
+ to_table = "posthog.writable_metrics4_series"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp,
+ original_expiry_timestamp
+FROM posthog.metrics4_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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+
materialized_view "ops_query_log_archive_mv" {
to_table = "posthog.writable_query_log_archive"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl b/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl
index 54a71e8ea712..8faa280679b3 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/apm.hcl
@@ -1,4 +1,366 @@
database "posthog" {
+ table "kafka_metrics_avro4" {
+ 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-avro4"
+ format = "Avro"
+ num_consumers = 8
+ max_block_size = 1000000
+ skip_broken_messages = 100
+ poll_timeout_ms = 3000
+ poll_max_batch_size = 1000
+ flush_interval_ms = 30000
+ thread_per_consumer = true
+ }
+ }
+
+ table "metrics4_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 "writable_metrics4_attributes" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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 = "metrics4_attributes"
+ }
+ }
+
+ table "writable_metrics4_names" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_names"
+ }
+ }
+
+ table "writable_metrics4_samples" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "SimpleAggregateFunction(any, UInt64)"
+ }
+ column "service_name" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "metric_type" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "unit" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "aggregation_temporality" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "is_monotonic" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "has_labels" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "instrumentation_scope" {
+ type = "SimpleAggregateFunction(any, String)"
+ }
+ column "histogram_bounds" {
+ type = "SimpleAggregateFunction(anyLast, Array(Float64))"
+ }
+ column "_topic" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ }
+ column "observed_timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ }
+ column "value_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Float64))"
+ }
+ column "count_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64))"
+ }
+ column "histogram_counts_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64)))"
+ }
+ column "trace_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "span_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "trace_flags_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_samples"
+ }
+ }
+
+ table "writable_metrics4_series" {
+ 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"
+ 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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfHour(timestamp)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_series"
+ }
+ }
+
table "writable_query_log_archive" {
column "hostname" {
type = "LowCardinality(String)"
@@ -100,6 +462,467 @@ database "posthog" {
}
}
+ materialized_view "kafka_metrics_avro4_mv" {
+ to_table = "posthog.metrics4_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,
+ timestamp
+ + toIntervalDay(
+ assumeNotNull(
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
+ )
+ )
+ ) 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_avro4
+WHERE kafka_metrics_avro4.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 "_offset" {
+ type = "UInt64"
+ }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_attributes" {
+ to_table = "posthog.writable_metrics4_attributes"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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 "metrics4_input_to_metrics4_names" {
+ to_table = "posthog.writable_metrics4_names"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics4_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_resource_attributes" {
+ to_table = "posthog.writable_metrics4_attributes"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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 "metrics4_input_to_metrics4_samples" {
+ to_table = "posthog.writable_metrics4_samples"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toDateTime(toStartOfHour(timestamp)) AS time_bucket,
+ series_fingerprint,
+ toDate32(original_expiry_timestamp) AS original_expiry_date,
+ any(resource_fingerprint) AS resource_fingerprint,
+ any(service_name) AS service_name,
+ any(metric_type) AS metric_type,
+ any(unit) AS unit,
+ any(aggregation_temporality) AS aggregation_temporality,
+ max(toUInt8(is_monotonic)) AS is_monotonic,
+ max(toUInt8(has_labels)) AS has_labels,
+ any(instrumentation_scope) AS instrumentation_scope,
+ anyLast(histogram_bounds) AS histogram_bounds,
+ any(_topic) AS _topic,
+ groupArray(10000)(timestamp) AS timestamp_arr,
+ groupArray(10000)(observed_timestamp) AS observed_timestamp_arr,
+ groupArray(10000)(value) AS value_arr,
+ groupArray(10000)(count) AS count_arr,
+ groupArray(10000)(histogram_counts) AS histogram_counts_arr,
+ groupArray(10000)(trace_id) AS trace_id_arr,
+ groupArray(10000)(span_id) AS span_id_arr,
+ groupArray(10000)(trace_flags) AS trace_flags_arr
+FROM posthog.metrics4_input
+GROUP BY
+ team_id, metric_name, time_bucket, series_fingerprint, original_expiry_date
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "metric_type" {
+ type = "String"
+ }
+ column "unit" {
+ type = "String"
+ }
+ column "aggregation_temporality" {
+ type = "String"
+ }
+ column "is_monotonic" {
+ type = "UInt8"
+ }
+ column "has_labels" {
+ type = "UInt8"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "histogram_bounds" {
+ type = "Array(Float64)"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "timestamp_arr" {
+ type = "Array(DateTime64(6))"
+ }
+ column "observed_timestamp_arr" {
+ type = "Array(DateTime64(6))"
+ }
+ column "value_arr" {
+ type = "Array(Float64)"
+ }
+ column "count_arr" {
+ type = "Array(UInt64)"
+ }
+ column "histogram_counts_arr" {
+ type = "Array(Array(UInt64))"
+ }
+ column "trace_id_arr" {
+ type = "Array(String)"
+ }
+ column "span_id_arr" {
+ type = "Array(String)"
+ }
+ column "trace_flags_arr" {
+ type = "Array(Int32)"
+ }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_series" {
+ to_table = "posthog.writable_metrics4_series"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp,
+ original_expiry_timestamp
+FROM posthog.metrics4_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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+
materialized_view "ops_query_log_archive_mv" {
to_table = "posthog.writable_query_log_archive"
query = <<SQL
@@ -310,3 +1133,39 @@ SQL
}
}
+
+named_collection "msk_cluster" {
+ external = true
+}
+
+named_collection "warpstream_calculated_events" {
+ external = true
+}
+
+named_collection "warpstream_cyclotron" {
+ external = true
+}
+
+named_collection "warpstream_ingestion" {
+ external = true
+}
+
+named_collection "warpstream_logs" {
+ external = true
+}
+
+named_collection "warpstream_metrics" {
+ external = true
+}
+
+named_collection "warpstream_replay" {
+ external = true
+}
+
+named_collection "warpstream_shared" {
+ external = true
+}
+
+named_collection "warpstream_traces" {
+ external = true
+}
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index 79aace874660..2974525976cd 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -2116,6 +2116,293 @@ SQL
}
}
+ table "metrics4_attributes" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ 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 "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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.metrics4_attributes"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_names" {
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_samples" {
+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint"]
+ partition_by = "original_expiry_date"
+ ttl = "original_expiry_date"
+ settings = {
+ index_granularity = "128"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "SimpleAggregateFunction(any, UInt64)"
+ }
+ column "service_name" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "metric_type" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "unit" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "aggregation_temporality" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "is_monotonic" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "has_labels" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "instrumentation_scope" {
+ type = "SimpleAggregateFunction(any, String)"
+ }
+ column "histogram_bounds" {
+ type = "SimpleAggregateFunction(anyLast, Array(Float64))"
+ }
+ column "_topic" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "observed_timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "value_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Float64))"
+ codec = "Gorilla(8), Default"
+ }
+ column "count_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64))"
+ codec = "T64, Default"
+ }
+ column "histogram_counts_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64)))"
+ codec = "T64, Default"
+ }
+ column "trace_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "span_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "trace_flags_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))"
+ }
+ index "idx_metric_type_set" {
+ expr = "metric_type"
+ type = "set(10)"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_trace_id_bf" {
+ expr = "trace_id_arr"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_samples"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_series" {
+ order_by = ["team_id", "metric_name", "series_fingerprint", "time_bucket"]
+ partition_by = "toStartOfWeek(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "1024"
+ ttl_only_drop_parts = "1"
+ }
+ 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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfHour(timestamp)"
+ }
+ 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
+ }
+ index "idx_timestamp_minmax" {
+ expr = "timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_series"
+ replica_name = "{replica}-{shard}"
+ version_column = "timestamp"
+ }
+ }
+
table "metrics_distributed" {
column "team_id" {
type = "Int32"
@@ -3338,13 +3625,13 @@ SELECT
cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,
timestamp,
observed_timestamp,
- observed_timestamp
+ 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))
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
)
)
) AS original_expiry_timestamp,
diff --git a/posthog/clickhouse/hcl/golden/prod-us/apm.hcl b/posthog/clickhouse/hcl/golden/prod-us/apm.hcl
index 54a71e8ea712..8faa280679b3 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/apm.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/apm.hcl
@@ -1,4 +1,366 @@
database "posthog" {
+ table "kafka_metrics_avro4" {
+ 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-avro4"
+ format = "Avro"
+ num_consumers = 8
+ max_block_size = 1000000
+ skip_broken_messages = 100
+ poll_timeout_ms = 3000
+ poll_max_batch_size = 1000
+ flush_interval_ms = 30000
+ thread_per_consumer = true
+ }
+ }
+
+ table "metrics4_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 "writable_metrics4_attributes" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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 = "metrics4_attributes"
+ }
+ }
+
+ table "writable_metrics4_names" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_names"
+ }
+ }
+
+ table "writable_metrics4_samples" {
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "SimpleAggregateFunction(any, UInt64)"
+ }
+ column "service_name" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "metric_type" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "unit" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "aggregation_temporality" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "is_monotonic" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "has_labels" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "instrumentation_scope" {
+ type = "SimpleAggregateFunction(any, String)"
+ }
+ column "histogram_bounds" {
+ type = "SimpleAggregateFunction(anyLast, Array(Float64))"
+ }
+ column "_topic" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ }
+ column "observed_timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ }
+ column "value_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Float64))"
+ }
+ column "count_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64))"
+ }
+ column "histogram_counts_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64)))"
+ }
+ column "trace_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "span_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "trace_flags_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_samples"
+ }
+ }
+
+ table "writable_metrics4_series" {
+ 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"
+ 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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfHour(timestamp)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_series"
+ }
+ }
+
table "writable_query_log_archive" {
column "hostname" {
type = "LowCardinality(String)"
@@ -100,6 +462,467 @@ database "posthog" {
}
}
+ materialized_view "kafka_metrics_avro4_mv" {
+ to_table = "posthog.metrics4_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,
+ timestamp
+ + toIntervalDay(
+ assumeNotNull(
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
+ )
+ )
+ ) 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_avro4
+WHERE kafka_metrics_avro4.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 "_offset" {
+ type = "UInt64"
+ }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_attributes" {
+ to_table = "posthog.writable_metrics4_attributes"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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 "metrics4_input_to_metrics4_names" {
+ to_table = "posthog.writable_metrics4_names"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics4_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_resource_attributes" {
+ to_table = "posthog.writable_metrics4_attributes"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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 "metrics4_input_to_metrics4_samples" {
+ to_table = "posthog.writable_metrics4_samples"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ toDateTime(toStartOfHour(timestamp)) AS time_bucket,
+ series_fingerprint,
+ toDate32(original_expiry_timestamp) AS original_expiry_date,
+ any(resource_fingerprint) AS resource_fingerprint,
+ any(service_name) AS service_name,
+ any(metric_type) AS metric_type,
+ any(unit) AS unit,
+ any(aggregation_temporality) AS aggregation_temporality,
+ max(toUInt8(is_monotonic)) AS is_monotonic,
+ max(toUInt8(has_labels)) AS has_labels,
+ any(instrumentation_scope) AS instrumentation_scope,
+ anyLast(histogram_bounds) AS histogram_bounds,
+ any(_topic) AS _topic,
+ groupArray(10000)(timestamp) AS timestamp_arr,
+ groupArray(10000)(observed_timestamp) AS observed_timestamp_arr,
+ groupArray(10000)(value) AS value_arr,
+ groupArray(10000)(count) AS count_arr,
+ groupArray(10000)(histogram_counts) AS histogram_counts_arr,
+ groupArray(10000)(trace_id) AS trace_id_arr,
+ groupArray(10000)(span_id) AS span_id_arr,
+ groupArray(10000)(trace_flags) AS trace_flags_arr
+FROM posthog.metrics4_input
+GROUP BY
+ team_id, metric_name, time_bucket, series_fingerprint, original_expiry_date
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "metric_type" {
+ type = "String"
+ }
+ column "unit" {
+ type = "String"
+ }
+ column "aggregation_temporality" {
+ type = "String"
+ }
+ column "is_monotonic" {
+ type = "UInt8"
+ }
+ column "has_labels" {
+ type = "UInt8"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "histogram_bounds" {
+ type = "Array(Float64)"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "timestamp_arr" {
+ type = "Array(DateTime64(6))"
+ }
+ column "observed_timestamp_arr" {
+ type = "Array(DateTime64(6))"
+ }
+ column "value_arr" {
+ type = "Array(Float64)"
+ }
+ column "count_arr" {
+ type = "Array(UInt64)"
+ }
+ column "histogram_counts_arr" {
+ type = "Array(Array(UInt64))"
+ }
+ column "trace_id_arr" {
+ type = "Array(String)"
+ }
+ column "span_id_arr" {
+ type = "Array(String)"
+ }
+ column "trace_flags_arr" {
+ type = "Array(Int32)"
+ }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_series" {
+ to_table = "posthog.writable_metrics4_series"
+ query = <<SQL
+SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp,
+ original_expiry_timestamp
+FROM posthog.metrics4_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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+
materialized_view "ops_query_log_archive_mv" {
to_table = "posthog.writable_query_log_archive"
query = <<SQL
@@ -310,3 +1133,39 @@ SQL
}
}
+
+named_collection "msk_cluster" {
+ external = true
+}
+
+named_collection "warpstream_calculated_events" {
+ external = true
+}
+
+named_collection "warpstream_cyclotron" {
+ external = true
+}
+
+named_collection "warpstream_ingestion" {
+ external = true
+}
+
+named_collection "warpstream_logs" {
+ external = true
+}
+
+named_collection "warpstream_metrics" {
+ external = true
+}
+
+named_collection "warpstream_replay" {
+ external = true
+}
+
+named_collection "warpstream_shared" {
+ external = true
+}
+
+named_collection "warpstream_traces" {
+ external = true
+}
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index 49b28da0e114..147b95c3400d 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -2116,6 +2116,293 @@ SQL
}
}
+ table "metrics4_attributes" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ 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 "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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.metrics4_attributes"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_names" {
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_samples" {
+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint"]
+ partition_by = "original_expiry_date"
+ ttl = "original_expiry_date"
+ settings = {
+ index_granularity = "128"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "SimpleAggregateFunction(any, UInt64)"
+ }
+ column "service_name" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "metric_type" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "unit" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "aggregation_temporality" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "is_monotonic" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "has_labels" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "instrumentation_scope" {
+ type = "SimpleAggregateFunction(any, String)"
+ }
+ column "histogram_bounds" {
+ type = "SimpleAggregateFunction(anyLast, Array(Float64))"
+ }
+ column "_topic" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "observed_timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "value_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Float64))"
+ codec = "Gorilla(8), Default"
+ }
+ column "count_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64))"
+ codec = "T64, Default"
+ }
+ column "histogram_counts_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64)))"
+ codec = "T64, Default"
+ }
+ column "trace_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "span_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "trace_flags_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))"
+ }
+ index "idx_metric_type_set" {
+ expr = "metric_type"
+ type = "set(10)"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_trace_id_bf" {
+ expr = "trace_id_arr"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_samples"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_series" {
+ order_by = ["team_id", "metric_name", "series_fingerprint", "time_bucket"]
+ partition_by = "toStartOfWeek(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "1024"
+ ttl_only_drop_parts = "1"
+ }
+ 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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfHour(timestamp)"
+ }
+ 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
+ }
+ index "idx_timestamp_minmax" {
+ expr = "timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_series"
+ replica_name = "{replica}-{shard}"
+ version_column = "timestamp"
+ }
+ }
+
table "metrics_distributed" {
column "team_id" {
type = "Int32"
@@ -3320,13 +3607,13 @@ SELECT
cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,
timestamp,
observed_timestamp,
- observed_timestamp
+ 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))
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
)
)
) AS original_expiry_timestamp,
diff --git a/posthog/clickhouse/hcl/manifest.hcl b/posthog/clickhouse/hcl/manifest.hcl
index 6c88a67e2fe6..d9800feb2220 100644
--- a/posthog/clickhouse/hcl/manifest.hcl
+++ b/posthog/clickhouse/hcl/manifest.hcl
@@ -42,15 +42,14 @@ role "logs" {
env "prod-eu" { layers = ["roles/shared", "roles/coshared/named_collections", "roles/coshared/apm_ingest", "roles/coshared/custom_metrics", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/metrics", "roles/logs/shared", "roles/logs/prod", "roles/logs/prod-eu"] }
}
-# APM satellite: the ingestion-apm nodes. Every env runs the shared custom_metrics
-# suite and writes into the query log archive, but not the query_log_archive reader
-# itself. dev additionally runs the APM ingest chain -- the Kafka consumers for logs,
-# traces and metrics and the writable proxies they feed -- which the prod envs still
-# run on their logs nodes.
+# APM satellite: the ingestion-apm nodes. Every environment runs the shared
+# custom_metrics suite and writes to the query log archive. Every environment also
+# runs the metrics4 ingest chain. Development runs the legacy APM ingest chain too.
+# The production logs nodes still run that legacy chain.
role "apm" {
- env "dev" { layers = ["roles/shared/qla_write.hcl", "roles/coshared/named_collections", "roles/coshared/custom_metrics", "roles/coshared/apm_ingest", "roles/coshared/logs_avro_ingest", "roles/apm/dev"] }
- env "prod-us" { layers = ["roles/shared/qla_write.hcl", "roles/coshared/custom_metrics"] }
- env "prod-eu" { layers = ["roles/shared/qla_write.hcl", "roles/coshared/custom_metrics"] }
+ env "dev" { layers = ["roles/shared/qla_write.hcl", "roles/coshared/named_collections", "roles/coshared/custom_metrics", "roles/coshared/apm_ingest", "roles/coshared/logs_avro_ingest", "roles/apm/shared", "roles/apm/dev"] }
+ env "prod-us" { layers = ["roles/shared/qla_write.hcl", "roles/coshared/named_collections", "roles/coshared/custom_metrics", "roles/apm/shared"] }
+ env "prod-eu" { layers = ["roles/shared/qla_write.hcl", "roles/coshared/named_collections", "roles/coshared/custom_metrics", "roles/apm/shared"] }
}
# AI_EVENTS satellite (LLM analytics). local/hobby run the MSK variant
@@ -147,7 +146,7 @@ role "medium" {
# and not MULTINODE_CLICKHOUSE. Composed as the deduped union of the local-multi stacks it
# hosts, so any name two of those roles declare fails this load instead of drifting.
role "all" {
- env "local-single" { layers = ["roles/shared", "roles/coshared/named_collections", "roles/coshared/apm_ingest", "roles/coshared/logs_avro_ingest", "roles/coshared/custom_metrics", "roles/ops/shared", "roles/ops/local", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/metrics", "roles/logs/local", "roles/coshared/ai_events_data", "roles/ai_events/shared", "roles/ai_events/local", "roles/coshared/aux_data", "roles/coshared/person_property_mutations", "roles/coshared/aux_small", "roles/auxiliary/shared", "roles/auxiliary/local", "roles/coshared/sessions_data", "roles/coshared/tophog", "roles/coshared/events_recent", "roles/coshared/events_recent_write", "roles/coshared/batch_exports_data", "roles/coshared/ingestion_warnings_store", "roles/coshared/events_json_write", "roles/coshared/log_entries_write", "roles/coshared/session_replay_write", "roles/data/shared", "roles/data/common", "roles/data/local", "roles/ingestion_events/shared", "roles/ingestion_events/local", "roles/ingestion_events/local-single", "roles/ingestion_small/local", "roles/ingestion_medium/local"] }
+ env "local-single" { layers = ["roles/shared", "roles/coshared/named_collections", "roles/coshared/apm_ingest", "roles/coshared/logs_avro_ingest", "roles/coshared/custom_metrics", "roles/ops/shared", "roles/ops/local", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/metrics", "roles/logs/local", "roles/apm/shared", "roles/apm/local", "roles/coshared/ai_events_data", "roles/ai_events/shared", "roles/ai_events/local", "roles/coshared/aux_data", "roles/coshared/person_property_mutations", "roles/coshared/aux_small", "roles/auxiliary/shared", "roles/auxiliary/local", "roles/coshared/sessions_data", "roles/coshared/tophog", "roles/coshared/events_recent", "roles/coshared/events_recent_write", "roles/coshared/batch_exports_data", "roles/coshared/ingestion_warnings_store", "roles/coshared/events_json_write", "roles/coshared/log_entries_write", "roles/coshared/session_replay_write", "roles/data/shared", "roles/data/common", "roles/data/local", "roles/ingestion_events/shared", "roles/ingestion_events/local", "roles/ingestion_events/local-single", "roles/ingestion_small/local", "roles/ingestion_medium/local"] }
}
# role "endpoints" {
diff --git a/posthog/clickhouse/hcl/roles/apm/dev/tables.hcl b/posthog/clickhouse/hcl/roles/apm/dev/tables.hcl
index 2fdbf1268290..fab1314164d6 100644
--- a/posthog/clickhouse/hcl/roles/apm/dev/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/apm/dev/tables.hcl
@@ -374,6 +374,24 @@ database "posthog" {
}
}
+ patch_table "kafka_metrics_avro4" {
+ engine "kafka" {
+ collection = "warpstream_metrics"
+ topic_list = "clickhouse_metrics"
+ group_name = "clickhouse-metrics-avro4"
+ format = "Avro"
+ num_consumers = 4
+ skip_broken_messages = 100
+ poll_timeout_ms = 10000
+ poll_max_batch_size = 1000
+ flush_interval_ms = 10000
+ thread_per_consumer = true
+ }
+ settings = {
+ input_format_avro_allow_missing_fields = "1"
+ }
+ }
+
patch_table "kafka_trace_spans_avro" {
engine "kafka" {
collection = "warpstream_traces"
diff --git a/posthog/clickhouse/hcl/roles/apm/local/patches.hcl b/posthog/clickhouse/hcl/roles/apm/local/patches.hcl
new file mode 100644
index 000000000000..7659d61de110
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/apm/local/patches.hcl
@@ -0,0 +1,18 @@
+database "posthog" {
+ patch_table "kafka_metrics_avro4" {
+ engine "kafka" {
+ collection = "warpstream_metrics"
+ topic_list = "clickhouse_metrics"
+ group_name = "clickhouse-metrics-avro4"
+ format = "Avro"
+ num_consumers = 1
+ 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"
+ }
+ }
+}
diff --git a/posthog/clickhouse/hcl/roles/apm/shared/metrics4.hcl b/posthog/clickhouse/hcl/roles/apm/shared/metrics4.hcl
new file mode 100644
index 000000000000..7c68e89a1ef0
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/apm/shared/metrics4.hcl
@@ -0,0 +1,284 @@
+# The metrics4 ingest chain runs on the APM ingestion nodes.
+# Its Distributed tables write to the metrics4 data tables on the logs cluster.
+database "posthog" {
+ table "kafka_metrics_avro4" {
+ 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-avro4"
+ format = "Avro"
+ num_consumers = 8
+ max_block_size = 1000000
+ skip_broken_messages = 100
+ poll_timeout_ms = 3000
+ poll_max_batch_size = 1000
+ flush_interval_ms = 30000
+ thread_per_consumer = true
+ }
+ settings = {
+ input_format_avro_allow_missing_fields = "1"
+ }
+ }
+
+ table "metrics4_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 "writable_metrics4_samples" {
+ column "team_id" { type = "Int32" }
+ column "metric_name" { type = "LowCardinality(String)" }
+ column "time_bucket" { type = "DateTime" }
+ column "series_fingerprint" { type = "UInt64" }
+ column "original_expiry_date" { type = "Date32" }
+ column "resource_fingerprint" { type = "SimpleAggregateFunction(any, UInt64)" }
+ column "service_name" { type = "SimpleAggregateFunction(any, LowCardinality(String))" }
+ column "metric_type" { type = "SimpleAggregateFunction(any, LowCardinality(String))" }
+ column "unit" { type = "SimpleAggregateFunction(any, LowCardinality(String))" }
+ column "aggregation_temporality" { type = "SimpleAggregateFunction(any, LowCardinality(String))" }
+ column "is_monotonic" { type = "SimpleAggregateFunction(max, UInt8)" }
+ column "has_labels" { type = "SimpleAggregateFunction(max, UInt8)" }
+ column "instrumentation_scope" { type = "SimpleAggregateFunction(any, String)" }
+ column "histogram_bounds" { type = "SimpleAggregateFunction(anyLast, Array(Float64))" }
+ column "_topic" { type = "SimpleAggregateFunction(any, LowCardinality(String))" }
+ column "timestamp_arr" { type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))" }
+ column "observed_timestamp_arr" { type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))" }
+ column "value_arr" { type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Float64))" }
+ column "count_arr" { type = "SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64))" }
+ column "histogram_counts_arr" { type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64)))" }
+ column "trace_id_arr" { type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))" }
+ column "span_id_arr" { type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))" }
+ column "trace_flags_arr" { type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))" }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_samples"
+ }
+ }
+
+ table "writable_metrics4_series" {
+ 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"
+ 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 "timestamp" { type = "DateTime64(6)" }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfHour(timestamp)"
+ }
+ column "original_expiry_timestamp" { type = "DateTime64(6)" }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_series"
+ }
+ }
+
+ table "writable_metrics4_names" {
+ column "team_id" { type = "Int32" }
+ column "metric_name" { type = "LowCardinality(String)" }
+ column "time_bucket" { type = "DateTime64(0)" }
+ column "original_expiry_time_bucket" { type = "DateTime64(0)" }
+ column "original_expiry_timestamp" { type = "SimpleAggregateFunction(max, DateTime64(6))" }
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "metrics4_names"
+ }
+ }
+
+ table "writable_metrics4_attributes" {
+ column "team_id" { type = "Int32" }
+ column "metric_name" { type = "LowCardinality(String)" }
+ 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 = "metrics4_attributes"
+ }
+ }
+
+ materialized_view "kafka_metrics_avro4_mv" {
+ to_table = "posthog.metrics4_input"
+ query = file("sql/kafka_metrics_avro4_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" }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_samples" {
+ to_table = "posthog.writable_metrics4_samples"
+ query = file("sql/metrics4_input_to_metrics4_samples.sql")
+ column "team_id" { type = "Int32" }
+ column "metric_name" { type = "LowCardinality(String)" }
+ column "time_bucket" { type = "DateTime" }
+ column "series_fingerprint" { type = "UInt64" }
+ column "original_expiry_date" { type = "Date32" }
+ column "resource_fingerprint" { type = "UInt64" }
+ column "service_name" { type = "String" }
+ column "metric_type" { type = "String" }
+ column "unit" { type = "String" }
+ column "aggregation_temporality" { type = "String" }
+ column "is_monotonic" { type = "UInt8" }
+ column "has_labels" { type = "UInt8" }
+ column "instrumentation_scope" { type = "String" }
+ column "histogram_bounds" { type = "Array(Float64)" }
+ column "_topic" { type = "String" }
+ column "timestamp_arr" { type = "Array(DateTime64(6))" }
+ column "observed_timestamp_arr" { type = "Array(DateTime64(6))" }
+ column "value_arr" { type = "Array(Float64)" }
+ column "count_arr" { type = "Array(UInt64)" }
+ column "histogram_counts_arr" { type = "Array(Array(UInt64))" }
+ column "trace_id_arr" { type = "Array(String)" }
+ column "span_id_arr" { type = "Array(String)" }
+ column "trace_flags_arr" { type = "Array(Int32)" }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_series" {
+ to_table = "posthog.writable_metrics4_series"
+ query = file("sql/metrics4_input_to_metrics4_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 "timestamp" { type = "DateTime64(6)" }
+ column "original_expiry_timestamp" { type = "DateTime64(6)" }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_names" {
+ to_table = "posthog.writable_metrics4_names"
+ query = file("sql/metrics4_input_to_metrics4_names.sql")
+ column "team_id" { type = "Int32" }
+ column "metric_name" { type = "LowCardinality(String)" }
+ column "time_bucket" { type = "DateTime64(0)" }
+ column "original_expiry_time_bucket" { type = "DateTime64(0)" }
+ column "original_expiry_timestamp" { type = "SimpleAggregateFunction(max, DateTime64(6))" }
+ }
+
+ materialized_view "metrics4_input_to_metrics4_attributes" {
+ to_table = "posthog.writable_metrics4_attributes"
+ query = file("sql/metrics4_input_to_metrics4_attributes.sql")
+ column "team_id" { type = "Int32" }
+ column "metric_name" { type = "LowCardinality(String)" }
+ 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 "metrics4_input_to_metrics4_resource_attributes" {
+ to_table = "posthog.writable_metrics4_attributes"
+ query = file("sql/metrics4_input_to_metrics4_resource_attributes.sql")
+ column "team_id" { type = "Int32" }
+ column "metric_name" { type = "LowCardinality(String)" }
+ 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/apm/shared/sql/kafka_metrics_avro4_mv.sql b/posthog/clickhouse/hcl/roles/apm/shared/sql/kafka_metrics_avro4_mv.sql
new file mode 100644
index 000000000000..ce97139b2ac9
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/apm/shared/sql/kafka_metrics_avro4_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,
+ timestamp + toIntervalDay(assumeNotNull(if((retention_days IS NOT NULL) AND (retention_days > 0), retention_days, toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))))) 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_avro4
+WHERE kafka_metrics_avro4.series_fingerprint IS NOT NULL
+SETTINGS
+ min_insert_block_size_rows = 0,
+ min_insert_block_size_bytes = 0
diff --git a/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_attributes.sql b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_attributes.sql
new file mode 100644
index 000000000000..38f804057726
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_attributes.sql
@@ -0,0 +1,29 @@
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
diff --git a/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_names.sql b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_names.sql
new file mode 100644
index 000000000000..51cb46cc305c
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_names.sql
@@ -0,0 +1,10 @@
+SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics4_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket
diff --git a/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_resource_attributes.sql b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_resource_attributes.sql
new file mode 100644
index 000000000000..e1e0661ceba6
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_resource_attributes.sql
@@ -0,0 +1,29 @@
+SELECT
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ )
diff --git a/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_samples.sql b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_samples.sql
new file mode 100644
index 000000000000..4d9ccb9b5ef4
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_samples.sql
@@ -0,0 +1,31 @@
+SELECT
+ team_id,
+ metric_name,
+ toDateTime(toStartOfHour(timestamp)) AS time_bucket,
+ series_fingerprint,
+ toDate32(original_expiry_timestamp) AS original_expiry_date,
+ any(resource_fingerprint) AS resource_fingerprint,
+ any(service_name) AS service_name,
+ any(metric_type) AS metric_type,
+ any(unit) AS unit,
+ any(aggregation_temporality) AS aggregation_temporality,
+ max(toUInt8(is_monotonic)) AS is_monotonic,
+ max(toUInt8(has_labels)) AS has_labels,
+ any(instrumentation_scope) AS instrumentation_scope,
+ anyLast(histogram_bounds) AS histogram_bounds,
+ any(_topic) AS _topic,
+ groupArray(10000)(timestamp) AS timestamp_arr,
+ groupArray(10000)(observed_timestamp) AS observed_timestamp_arr,
+ groupArray(10000)(value) AS value_arr,
+ groupArray(10000)(count) AS count_arr,
+ groupArray(10000)(histogram_counts) AS histogram_counts_arr,
+ groupArray(10000)(trace_id) AS trace_id_arr,
+ groupArray(10000)(span_id) AS span_id_arr,
+ groupArray(10000)(trace_flags) AS trace_flags_arr
+FROM posthog.metrics4_input
+GROUP BY
+ team_id,
+ metric_name,
+ time_bucket,
+ series_fingerprint,
+ original_expiry_date
diff --git a/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_series.sql b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_series.sql
new file mode 100644
index 000000000000..a9a870fb5dbb
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/apm/shared/sql/metrics4_input_to_metrics4_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,
+ original_expiry_timestamp
+FROM posthog.metrics4_input
+WHERE has_labels
diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl b/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl
new file mode 100644
index 000000000000..ca4b54488b60
--- /dev/null
+++ b/posthog/clickhouse/hcl/roles/logs/metrics/metrics4.hcl
@@ -0,0 +1,293 @@
+# These tables store hourly data from metrics4_input.
+# metrics4_samples stores each sample field in a parallel array.
+# Each sample row stores at most 10,000 points for one series-hour.
+# The other tables store series, name, and attribute data.
+# posthog/clickhouse/metrics/metrics4.py defines the source schema.
+database "posthog" {
+ table "metrics4_attributes" {
+ order_by = ["team_id", "metric_name", "attribute_type", "time_bucket", "attribute_key", "attribute_value", "service_name", "original_expiry_time_bucket"]
+ 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 "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ 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.metrics4_attributes"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_names" {
+ order_by = ["team_id", "time_bucket", "metric_name", "original_expiry_time_bucket"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_names"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_samples" {
+ order_by = ["team_id", "metric_name", "time_bucket", "series_fingerprint"]
+ partition_by = "original_expiry_date"
+ ttl = "original_expiry_date"
+ settings = {
+ index_granularity = "128"
+ ttl_only_drop_parts = "1"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "LowCardinality(String)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ codec = "Delta(8), Default"
+ }
+ column "original_expiry_date" {
+ type = "Date32"
+ }
+ column "resource_fingerprint" {
+ type = "SimpleAggregateFunction(any, UInt64)"
+ }
+ column "service_name" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "metric_type" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "unit" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "aggregation_temporality" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "is_monotonic" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "has_labels" {
+ type = "SimpleAggregateFunction(max, UInt8)"
+ }
+ column "instrumentation_scope" {
+ type = "SimpleAggregateFunction(any, String)"
+ }
+ column "histogram_bounds" {
+ type = "SimpleAggregateFunction(anyLast, Array(Float64))"
+ }
+ column "_topic" {
+ type = "SimpleAggregateFunction(any, LowCardinality(String))"
+ }
+ column "timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "observed_timestamp_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6)))"
+ codec = "DoubleDelta, Default"
+ }
+ column "value_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Float64))"
+ codec = "Gorilla(8), Default"
+ }
+ column "count_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64))"
+ codec = "T64, Default"
+ }
+ column "histogram_counts_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64)))"
+ codec = "T64, Default"
+ }
+ column "trace_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "span_id_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(String))"
+ }
+ column "trace_flags_arr" {
+ type = "SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))"
+ }
+ index "idx_metric_type_set" {
+ expr = "metric_type"
+ type = "set(10)"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_trace_id_bf" {
+ expr = "trace_id_arr"
+ type = "bloom_filter(0.01)"
+ granularity = 1
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_samples"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ table "metrics4_series" {
+ order_by = ["team_id", "metric_name", "series_fingerprint", "time_bucket"]
+ partition_by = "toStartOfWeek(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ index_granularity = "1024"
+ ttl_only_drop_parts = "1"
+ }
+ 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 "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfHour(timestamp)"
+ }
+ 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
+ }
+ index "idx_timestamp_minmax" {
+ expr = "timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_time_bucket_minmax" {
+ expr = "time_bucket"
+ type = "minmax"
+ granularity = 1
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.metrics4_series"
+ replica_name = "{replica}-{shard}"
+ version_column = "timestamp"
+ }
+ }
+}
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.sql
index a4d0db8429eb..133358c3846f 100644
--- 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.sql
@@ -6,7 +6,7 @@ SELECT
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,
+ timestamp + toIntervalDay(assumeNotNull(if((retention_days IS NOT NULL) AND (retention_days > 0), retention_days, toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))))) AS original_expiry_timestamp,
ifNull(service_name, '') AS service_name,
ifNull(metric_type, '') AS metric_type,
ifNull(value, 0) AS value,
diff --git a/posthog/clickhouse/hcl/sql/dev/apm.sql b/posthog/clickhouse/hcl/sql/dev/apm.sql
index 244c9f73f20a..2c728cef8205 100644
--- a/posthog/clickhouse/hcl/sql/dev/apm.sql
+++ b/posthog/clickhouse/hcl/sql/dev/apm.sql
@@ -42,6 +42,30 @@ CREATE TABLE posthog.kafka_metrics_avro (
attributes Map(String, String),
series_fingerprint Nullable(Int64)
) 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-avro-new', kafka_num_consumers = 4, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 10000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';
+CREATE TABLE posthog.kafka_metrics_avro4 (
+ 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-avro4', kafka_num_consumers = 4, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 10000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';
CREATE TABLE posthog.kafka_trace_spans_avro (
uuid String,
trace_id String,
@@ -65,6 +89,35 @@ CREATE TABLE posthog.kafka_trace_spans_avro (
dropped_links_count Int32,
status_code Int32
) ENGINE = Kafka(warpstream_traces) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_flush_interval_ms = 10000, kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 4, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 10000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';
+CREATE TABLE posthog.metrics4_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.writable_logs34 (
time_bucket DateTime MATERIALIZED toStartOfDay(timestamp),
original_expiry_timestamp DateTime64(6),
@@ -154,6 +207,66 @@ CREATE TABLE posthog.writable_metrics1 (
time_minute DateTime ALIAS toStartOfMinute(timestamp),
attributes Map(String, String) ALIAS mapApply((k, v) -> (left(k, -5), v), attributes_map_str)
) ENGINE = Distributed('logs', 'posthog', 'metrics1');
+CREATE TABLE posthog.writable_metrics4_attributes (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ 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', 'metrics4_attributes');
+CREATE TABLE posthog.writable_metrics4_names (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
+CREATE TABLE posthog.writable_metrics4_samples (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime,
+ series_fingerprint UInt64,
+ original_expiry_date Date32,
+ resource_fingerprint SimpleAggregateFunction(any, UInt64),
+ service_name SimpleAggregateFunction(any, LowCardinality(String)),
+ metric_type SimpleAggregateFunction(any, LowCardinality(String)),
+ unit SimpleAggregateFunction(any, LowCardinality(String)),
+ aggregation_temporality SimpleAggregateFunction(any, LowCardinality(String)),
+ is_monotonic SimpleAggregateFunction(max, UInt8),
+ has_labels SimpleAggregateFunction(max, UInt8),
+ instrumentation_scope SimpleAggregateFunction(any, String),
+ histogram_bounds SimpleAggregateFunction(anyLast, Array(Float64)),
+ _topic SimpleAggregateFunction(any, LowCardinality(String)),
+ timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))),
+ observed_timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))),
+ value_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)),
+ count_arr SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)),
+ histogram_counts_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))),
+ trace_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ span_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ trace_flags_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_samples');
+CREATE TABLE posthog.writable_metrics4_series (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ series_fingerprint UInt64,
+ 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),
+ timestamp DateTime64(6),
+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),
+ original_expiry_timestamp DateTime64(6)
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_series');
CREATE TABLE posthog.writable_metrics_kafka_metrics (
_partition UInt32,
_topic String,
@@ -268,6 +381,56 @@ CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.writable_logs34
ifNull(pattern, '') AS pattern,
toUInt8(ifNull(pattern_version, 0)) AS pattern_version
FROM posthog.kafka_logs_avro;
+CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro4_mv TO posthog.metrics4_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT
+ uuid,
+ 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,
+ timestamp
+ + toIntervalDay(
+ assumeNotNull(
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
+ )
+ )
+ ) 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_avro4
+WHERE kafka_metrics_avro4.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.writable_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,
@@ -301,6 +464,117 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.writable_t
toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) AS _bytes_uncompressed,
toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) AS _bytes_compressed
FROM posthog.kafka_trace_spans_avro;
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), 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,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics4_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket;
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_resource_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), 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,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_samples TO posthog.writable_metrics4_samples (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime, series_fingerprint UInt64, original_expiry_date Date32, resource_fingerprint UInt64, service_name String, metric_type String, unit String, aggregation_temporality String, is_monotonic UInt8, has_labels UInt8, instrumentation_scope String, histogram_bounds Array(Float64), _topic String, timestamp_arr Array(DateTime64(6)), observed_timestamp_arr Array(DateTime64(6)), value_arr Array(Float64), count_arr Array(UInt64), histogram_counts_arr Array(Array(UInt64)), trace_id_arr Array(String), span_id_arr Array(String), trace_flags_arr Array(Int32)) AS SELECT
+ team_id,
+ metric_name,
+ toDateTime(toStartOfHour(timestamp)) AS time_bucket,
+ series_fingerprint,
+ toDate32(original_expiry_timestamp) AS original_expiry_date,
+ any(resource_fingerprint) AS resource_fingerprint,
+ any(service_name) AS service_name,
+ any(metric_type) AS metric_type,
+ any(unit) AS unit,
+ any(aggregation_temporality) AS aggregation_temporality,
+ max(toUInt8(is_monotonic)) AS is_monotonic,
+ max(toUInt8(has_labels)) AS has_labels,
+ any(instrumentation_scope) AS instrumentation_scope,
+ anyLast(histogram_bounds) AS histogram_bounds,
+ any(_topic) AS _topic,
+ groupArray(10000)(timestamp) AS timestamp_arr,
+ groupArray(10000)(observed_timestamp) AS observed_timestamp_arr,
+ groupArray(10000)(value) AS value_arr,
+ groupArray(10000)(count) AS count_arr,
+ groupArray(10000)(histogram_counts) AS histogram_counts_arr,
+ groupArray(10000)(trace_id) AS trace_id_arr,
+ groupArray(10000)(span_id) AS span_id_arr,
+ groupArray(10000)(trace_flags) AS trace_flags_arr
+FROM posthog.metrics4_input
+GROUP BY
+ team_id, metric_name, time_bucket, series_fingerprint, original_expiry_date;
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_series TO posthog.writable_metrics4_series (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), timestamp 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,
+ original_expiry_timestamp
+FROM posthog.metrics4_input
+WHERE has_labels;
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 SELECT
hostname,
user,
diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index 7ec74d1edffc..f91bbaf8f778 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -500,6 +500,79 @@ CREATE TABLE posthog.metrics2_input (
_topic String,
_offset UInt64
) ENGINE = Null();
+CREATE TABLE posthog.metrics4_attributes (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ 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.metrics4_attributes', '{replica}-{shard}') ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metrics4_names (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.metrics4_samples (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime,
+ series_fingerprint UInt64 CODEC(Delta(8), Default),
+ original_expiry_date Date32,
+ resource_fingerprint SimpleAggregateFunction(any, UInt64),
+ service_name SimpleAggregateFunction(any, LowCardinality(String)),
+ metric_type SimpleAggregateFunction(any, LowCardinality(String)),
+ unit SimpleAggregateFunction(any, LowCardinality(String)),
+ aggregation_temporality SimpleAggregateFunction(any, LowCardinality(String)),
+ is_monotonic SimpleAggregateFunction(max, UInt8),
+ has_labels SimpleAggregateFunction(max, UInt8),
+ instrumentation_scope SimpleAggregateFunction(any, String),
+ histogram_bounds SimpleAggregateFunction(anyLast, Array(Float64)),
+ _topic SimpleAggregateFunction(any, LowCardinality(String)),
+ timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ observed_timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ value_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)) CODEC(Gorilla(8), Default),
+ count_arr SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)) CODEC(T64, Default),
+ histogram_counts_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))) CODEC(T64, Default),
+ trace_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ span_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ trace_flags_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Int32)),
+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1,
+ INDEX idx_trace_id_bf trace_id_arr TYPE bloom_filter(0.01) GRANULARITY 1
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_samples', '{replica}-{shard}') ORDER BY (team_id, metric_name, time_bucket, series_fingerprint) PARTITION BY original_expiry_date TTL original_expiry_date SETTINGS index_granularity = 128, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metrics4_series (
+ 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),
+ timestamp DateTime64(6),
+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),
+ 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,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_series', '{replica}-{shard}', timestamp) ORDER BY (team_id, metric_name, series_fingerprint, time_bucket) PARTITION BY toStartOfWeek(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 1024, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metrics_distributed (
team_id Int32,
metric_name LowCardinality(String),
@@ -850,13 +923,13 @@ CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_inpu
cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,
timestamp,
observed_timestamp,
- observed_timestamp
+ 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))
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
)
)
) AS original_expiry_timestamp,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index a1b34f67f1a9..225dad4140aa 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -613,6 +613,79 @@ CREATE TABLE posthog.metrics2_input (
_topic String,
_offset UInt64
) ENGINE = Null();
+CREATE TABLE posthog.metrics4_attributes (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ 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.metrics4_attributes', '{replica}-{shard}') ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metrics4_names (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.metrics4_samples (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime,
+ series_fingerprint UInt64 CODEC(Delta(8), Default),
+ original_expiry_date Date32,
+ resource_fingerprint SimpleAggregateFunction(any, UInt64),
+ service_name SimpleAggregateFunction(any, LowCardinality(String)),
+ metric_type SimpleAggregateFunction(any, LowCardinality(String)),
+ unit SimpleAggregateFunction(any, LowCardinality(String)),
+ aggregation_temporality SimpleAggregateFunction(any, LowCardinality(String)),
+ is_monotonic SimpleAggregateFunction(max, UInt8),
+ has_labels SimpleAggregateFunction(max, UInt8),
+ instrumentation_scope SimpleAggregateFunction(any, String),
+ histogram_bounds SimpleAggregateFunction(anyLast, Array(Float64)),
+ _topic SimpleAggregateFunction(any, LowCardinality(String)),
+ timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ observed_timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ value_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)) CODEC(Gorilla(8), Default),
+ count_arr SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)) CODEC(T64, Default),
+ histogram_counts_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))) CODEC(T64, Default),
+ trace_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ span_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ trace_flags_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Int32)),
+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1,
+ INDEX idx_trace_id_bf trace_id_arr TYPE bloom_filter(0.01) GRANULARITY 1
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_samples', '{replica}-{shard}') ORDER BY (team_id, metric_name, time_bucket, series_fingerprint) PARTITION BY original_expiry_date TTL original_expiry_date SETTINGS index_granularity = 128, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metrics4_series (
+ 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),
+ timestamp DateTime64(6),
+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),
+ 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,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_series', '{replica}-{shard}', timestamp) ORDER BY (team_id, metric_name, series_fingerprint, time_bucket) PARTITION BY toStartOfWeek(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 1024, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metrics_distributed (
team_id Int32,
metric_name LowCardinality(String),
@@ -935,13 +1008,13 @@ CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_inpu
cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,
timestamp,
observed_timestamp,
- observed_timestamp
+ 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))
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
)
)
) AS original_expiry_timestamp,
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 580da75488ba..a9b7305ad905 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -635,6 +635,30 @@ CREATE TABLE posthog.kafka_metrics_avro2 (
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 = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';
+CREATE TABLE posthog.kafka_metrics_avro4 (
+ 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-avro4', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';
CREATE TABLE posthog.kafka_performance_events (
uuid UUID,
session_id String,
@@ -1504,6 +1528,108 @@ CREATE TABLE posthog.metrics2_input (
_topic String,
_offset UInt64
) ENGINE = Null();
+CREATE TABLE posthog.metrics4_attributes (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ 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.metrics4_attributes', '{replica}-{shard}') ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metrics4_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.metrics4_names (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.metrics4_samples (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime,
+ series_fingerprint UInt64 CODEC(Delta(8), Default),
+ original_expiry_date Date32,
+ resource_fingerprint SimpleAggregateFunction(any, UInt64),
+ service_name SimpleAggregateFunction(any, LowCardinality(String)),
+ metric_type SimpleAggregateFunction(any, LowCardinality(String)),
+ unit SimpleAggregateFunction(any, LowCardinality(String)),
+ aggregation_temporality SimpleAggregateFunction(any, LowCardinality(String)),
+ is_monotonic SimpleAggregateFunction(max, UInt8),
+ has_labels SimpleAggregateFunction(max, UInt8),
+ instrumentation_scope SimpleAggregateFunction(any, String),
+ histogram_bounds SimpleAggregateFunction(anyLast, Array(Float64)),
+ _topic SimpleAggregateFunction(any, LowCardinality(String)),
+ timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ observed_timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ value_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)) CODEC(Gorilla(8), Default),
+ count_arr SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)) CODEC(T64, Default),
+ histogram_counts_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))) CODEC(T64, Default),
+ trace_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ span_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ trace_flags_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Int32)),
+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1,
+ INDEX idx_trace_id_bf trace_id_arr TYPE bloom_filter(0.01) GRANULARITY 1
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_samples', '{replica}-{shard}') ORDER BY (team_id, metric_name, time_bucket, series_fingerprint) PARTITION BY original_expiry_date TTL original_expiry_date SETTINGS index_granularity = 128, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metrics4_series (
+ 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),
+ timestamp DateTime64(6),
+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),
+ 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,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_series', '{replica}-{shard}', timestamp) ORDER BY (team_id, metric_name, series_fingerprint, time_bucket) PARTITION BY toStartOfWeek(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 1024, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metrics_distributed (
team_id Int32,
metric_name LowCardinality(String),
@@ -3923,6 +4049,66 @@ CREATE TABLE posthog.writable_logs34 (
pattern String,
pattern_version UInt8
) ENGINE = Distributed('posthog_single_shard', 'posthog', 'logs34') SETTINGS background_insert_batch = 1;
+CREATE TABLE posthog.writable_metrics4_attributes (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ 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', 'metrics4_attributes');
+CREATE TABLE posthog.writable_metrics4_names (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
+CREATE TABLE posthog.writable_metrics4_samples (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime,
+ series_fingerprint UInt64,
+ original_expiry_date Date32,
+ resource_fingerprint SimpleAggregateFunction(any, UInt64),
+ service_name SimpleAggregateFunction(any, LowCardinality(String)),
+ metric_type SimpleAggregateFunction(any, LowCardinality(String)),
+ unit SimpleAggregateFunction(any, LowCardinality(String)),
+ aggregation_temporality SimpleAggregateFunction(any, LowCardinality(String)),
+ is_monotonic SimpleAggregateFunction(max, UInt8),
+ has_labels SimpleAggregateFunction(max, UInt8),
+ instrumentation_scope SimpleAggregateFunction(any, String),
+ histogram_bounds SimpleAggregateFunction(anyLast, Array(Float64)),
+ _topic SimpleAggregateFunction(any, LowCardinality(String)),
+ timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))),
+ observed_timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))),
+ value_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)),
+ count_arr SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)),
+ histogram_counts_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))),
+ trace_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ span_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ trace_flags_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_samples');
+CREATE TABLE posthog.writable_metrics4_series (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ series_fingerprint UInt64,
+ 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),
+ timestamp DateTime64(6),
+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),
+ original_expiry_timestamp DateTime64(6)
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_series');
CREATE TABLE posthog.writable_person (
id UUID,
created_at DateTime64(3),
@@ -4797,13 +4983,13 @@ CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_inpu
cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,
timestamp,
observed_timestamp,
- observed_timestamp
+ 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))
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
)
)
) AS original_expiry_timestamp,
@@ -4836,6 +5022,56 @@ CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_inpu
_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_avro4_mv TO posthog.metrics4_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT
+ uuid,
+ 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,
+ timestamp
+ + toIntervalDay(
+ assumeNotNull(
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
+ )
+ )
+ ) 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_avro4
+WHERE kafka_metrics_avro4.series_fingerprint IS NOT NULL
SETTINGS
min_insert_block_size_rows = 0,
min_insert_block_size_bytes = 0;
@@ -5244,6 +5480,117 @@ FROM
GROUP BY
team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
);
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), 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,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics4_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket;
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_resource_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), 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,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_samples TO posthog.writable_metrics4_samples (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime, series_fingerprint UInt64, original_expiry_date Date32, resource_fingerprint UInt64, service_name String, metric_type String, unit String, aggregation_temporality String, is_monotonic UInt8, has_labels UInt8, instrumentation_scope String, histogram_bounds Array(Float64), _topic String, timestamp_arr Array(DateTime64(6)), observed_timestamp_arr Array(DateTime64(6)), value_arr Array(Float64), count_arr Array(UInt64), histogram_counts_arr Array(Array(UInt64)), trace_id_arr Array(String), span_id_arr Array(String), trace_flags_arr Array(Int32)) AS SELECT
+ team_id,
+ metric_name,
+ toDateTime(toStartOfHour(timestamp)) AS time_bucket,
+ series_fingerprint,
+ toDate32(original_expiry_timestamp) AS original_expiry_date,
+ any(resource_fingerprint) AS resource_fingerprint,
+ any(service_name) AS service_name,
+ any(metric_type) AS metric_type,
+ any(unit) AS unit,
+ any(aggregation_temporality) AS aggregation_temporality,
+ max(toUInt8(is_monotonic)) AS is_monotonic,
+ max(toUInt8(has_labels)) AS has_labels,
+ any(instrumentation_scope) AS instrumentation_scope,
+ anyLast(histogram_bounds) AS histogram_bounds,
+ any(_topic) AS _topic,
+ groupArray(10000)(timestamp) AS timestamp_arr,
+ groupArray(10000)(observed_timestamp) AS observed_timestamp_arr,
+ groupArray(10000)(value) AS value_arr,
+ groupArray(10000)(count) AS count_arr,
+ groupArray(10000)(histogram_counts) AS histogram_counts_arr,
+ groupArray(10000)(trace_id) AS trace_id_arr,
+ groupArray(10000)(span_id) AS span_id_arr,
+ groupArray(10000)(trace_flags) AS trace_flags_arr
+FROM posthog.metrics4_input
+GROUP BY
+ team_id, metric_name, time_bucket, series_fingerprint, original_expiry_date;
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_series TO posthog.writable_metrics4_series (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), timestamp 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,
+ original_expiry_timestamp
+FROM posthog.metrics4_input
+WHERE has_labels;
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 SELECT
uuid,
session_id,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/apm.sql b/posthog/clickhouse/hcl/sql/prod-eu/apm.sql
index 2576ee8f100b..e904014a4ade 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/apm.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/apm.sql
@@ -1,6 +1,119 @@
-- AUTO-GENERATED from the declarative HCL by ops/gen-sql.sh — do not edit.
-- Full CREATE schema for the prod-eu/apm node. Apply to a fresh ClickHouse to build it.
+CREATE TABLE posthog.kafka_metrics_avro4 (
+ 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 = 30000, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro4', kafka_max_block_size = 1000000, 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.metrics4_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.writable_metrics4_attributes (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ 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', 'metrics4_attributes');
+CREATE TABLE posthog.writable_metrics4_names (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
+CREATE TABLE posthog.writable_metrics4_samples (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime,
+ series_fingerprint UInt64,
+ original_expiry_date Date32,
+ resource_fingerprint SimpleAggregateFunction(any, UInt64),
+ service_name SimpleAggregateFunction(any, LowCardinality(String)),
+ metric_type SimpleAggregateFunction(any, LowCardinality(String)),
+ unit SimpleAggregateFunction(any, LowCardinality(String)),
+ aggregation_temporality SimpleAggregateFunction(any, LowCardinality(String)),
+ is_monotonic SimpleAggregateFunction(max, UInt8),
+ has_labels SimpleAggregateFunction(max, UInt8),
+ instrumentation_scope SimpleAggregateFunction(any, String),
+ histogram_bounds SimpleAggregateFunction(anyLast, Array(Float64)),
+ _topic SimpleAggregateFunction(any, LowCardinality(String)),
+ timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))),
+ observed_timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))),
+ value_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)),
+ count_arr SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)),
+ histogram_counts_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))),
+ trace_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ span_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ trace_flags_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_samples');
+CREATE TABLE posthog.writable_metrics4_series (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ series_fingerprint UInt64,
+ 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),
+ timestamp DateTime64(6),
+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),
+ original_expiry_timestamp DateTime64(6)
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_series');
CREATE TABLE posthog.writable_query_log_archive (
hostname LowCardinality(String),
user LowCardinality(String),
@@ -34,6 +147,167 @@ CREATE TABLE posthog.writable_query_log_archive (
log_comment JSON(max_dynamic_paths=256, access_method LowCardinality(String), alert_config_id String, api_key_label String, api_key_mask String, batch_export_id String, chargeable Bool, client_query_id String, cohort_id Int64, `dagster.job_name` String, `dagster.run_id` String, `dagster.tags.owner` String, dashboard_id Int64, experiment_feature_flag_key String, experiment_id Int64, feature LowCardinality(String), id String, insight_id Int64, is_impersonated Bool, kind LowCardinality(String), name String, org_id String, person_on_events_mode LowCardinality(String), product LowCardinality(String), query_type LowCardinality(String), request_name String, route_id String, service_name String, session_id String, table_id String, team_id Int64, `temporal.activity_id` String, `temporal.activity_type` String, `temporal.attempt` Int64, `temporal.workflow_id` String, `temporal.workflow_namespace` String, `temporal.workflow_run_id` String, `temporal.workflow_type` String, user_id Int64, warehouse_query Bool, workflow LowCardinality(String), workload LowCardinality(String), SKIP cache_key, SKIP filter, SKIP hogql_features, SKIP http_referer, SKIP http_request_id, SKIP http_user_agent, SKIP query_settings, SKIP timings, SKIP user_email),
ProfileEvents Map(String, UInt64)
) ENGINE = Distributed('ops', 'posthog', 'query_log_archive_buffer');
+CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro4_mv TO posthog.metrics4_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT
+ uuid,
+ 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,
+ timestamp
+ + toIntervalDay(
+ assumeNotNull(
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
+ )
+ )
+ ) 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_avro4
+WHERE kafka_metrics_avro4.series_fingerprint IS NOT NULL
+SETTINGS
+ min_insert_block_size_rows = 0,
+ min_insert_block_size_bytes = 0;
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), 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,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics4_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket;
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_resource_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), 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,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_samples TO posthog.writable_metrics4_samples (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime, series_fingerprint UInt64, original_expiry_date Date32, resource_fingerprint UInt64, service_name String, metric_type String, unit String, aggregation_temporality String, is_monotonic UInt8, has_labels UInt8, instrumentation_scope String, histogram_bounds Array(Float64), _topic String, timestamp_arr Array(DateTime64(6)), observed_timestamp_arr Array(DateTime64(6)), value_arr Array(Float64), count_arr Array(UInt64), histogram_counts_arr Array(Array(UInt64)), trace_id_arr Array(String), span_id_arr Array(String), trace_flags_arr Array(Int32)) AS SELECT
+ team_id,
+ metric_name,
+ toDateTime(toStartOfHour(timestamp)) AS time_bucket,
+ series_fingerprint,
+ toDate32(original_expiry_timestamp) AS original_expiry_date,
+ any(resource_fingerprint) AS resource_fingerprint,
+ any(service_name) AS service_name,
+ any(metric_type) AS metric_type,
+ any(unit) AS unit,
+ any(aggregation_temporality) AS aggregation_temporality,
+ max(toUInt8(is_monotonic)) AS is_monotonic,
+ max(toUInt8(has_labels)) AS has_labels,
+ any(instrumentation_scope) AS instrumentation_scope,
+ anyLast(histogram_bounds) AS histogram_bounds,
+ any(_topic) AS _topic,
+ groupArray(10000)(timestamp) AS timestamp_arr,
+ groupArray(10000)(observed_timestamp) AS observed_timestamp_arr,
+ groupArray(10000)(value) AS value_arr,
+ groupArray(10000)(count) AS count_arr,
+ groupArray(10000)(histogram_counts) AS histogram_counts_arr,
+ groupArray(10000)(trace_id) AS trace_id_arr,
+ groupArray(10000)(span_id) AS span_id_arr,
+ groupArray(10000)(trace_flags) AS trace_flags_arr
+FROM posthog.metrics4_input
+GROUP BY
+ team_id, metric_name, time_bucket, series_fingerprint, original_expiry_date;
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_series TO posthog.writable_metrics4_series (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), timestamp 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,
+ original_expiry_timestamp
+FROM posthog.metrics4_input
+WHERE has_labels;
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 SELECT
hostname,
user,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index 1a44326bdac5..ba77f7b2b4d7 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -545,6 +545,79 @@ CREATE TABLE posthog.metrics2_input (
_topic String,
_offset UInt64
) ENGINE = Null();
+CREATE TABLE posthog.metrics4_attributes (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ 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.metrics4_attributes', '{replica}-{shard}') ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metrics4_names (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.metrics4_samples (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime,
+ series_fingerprint UInt64 CODEC(Delta(8), Default),
+ original_expiry_date Date32,
+ resource_fingerprint SimpleAggregateFunction(any, UInt64),
+ service_name SimpleAggregateFunction(any, LowCardinality(String)),
+ metric_type SimpleAggregateFunction(any, LowCardinality(String)),
+ unit SimpleAggregateFunction(any, LowCardinality(String)),
+ aggregation_temporality SimpleAggregateFunction(any, LowCardinality(String)),
+ is_monotonic SimpleAggregateFunction(max, UInt8),
+ has_labels SimpleAggregateFunction(max, UInt8),
+ instrumentation_scope SimpleAggregateFunction(any, String),
+ histogram_bounds SimpleAggregateFunction(anyLast, Array(Float64)),
+ _topic SimpleAggregateFunction(any, LowCardinality(String)),
+ timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ observed_timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ value_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)) CODEC(Gorilla(8), Default),
+ count_arr SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)) CODEC(T64, Default),
+ histogram_counts_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))) CODEC(T64, Default),
+ trace_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ span_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ trace_flags_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Int32)),
+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1,
+ INDEX idx_trace_id_bf trace_id_arr TYPE bloom_filter(0.01) GRANULARITY 1
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_samples', '{replica}-{shard}') ORDER BY (team_id, metric_name, time_bucket, series_fingerprint) PARTITION BY original_expiry_date TTL original_expiry_date SETTINGS index_granularity = 128, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metrics4_series (
+ 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),
+ timestamp DateTime64(6),
+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),
+ 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,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_series', '{replica}-{shard}', timestamp) ORDER BY (team_id, metric_name, series_fingerprint, time_bucket) PARTITION BY toStartOfWeek(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 1024, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metrics_distributed (
team_id Int32,
metric_name LowCardinality(String),
@@ -910,13 +983,13 @@ CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_inpu
cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,
timestamp,
observed_timestamp,
- observed_timestamp
+ 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))
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
)
)
) AS original_expiry_timestamp,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/apm.sql b/posthog/clickhouse/hcl/sql/prod-us/apm.sql
index 8a3171142b25..7180c4b290fa 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/apm.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/apm.sql
@@ -1,6 +1,119 @@
-- AUTO-GENERATED from the declarative HCL by ops/gen-sql.sh — do not edit.
-- Full CREATE schema for the prod-us/apm node. Apply to a fresh ClickHouse to build it.
+CREATE TABLE posthog.kafka_metrics_avro4 (
+ 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 = 30000, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro4', kafka_max_block_size = 1000000, 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.metrics4_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.writable_metrics4_attributes (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ 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', 'metrics4_attributes');
+CREATE TABLE posthog.writable_metrics4_names (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_names');
+CREATE TABLE posthog.writable_metrics4_samples (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime,
+ series_fingerprint UInt64,
+ original_expiry_date Date32,
+ resource_fingerprint SimpleAggregateFunction(any, UInt64),
+ service_name SimpleAggregateFunction(any, LowCardinality(String)),
+ metric_type SimpleAggregateFunction(any, LowCardinality(String)),
+ unit SimpleAggregateFunction(any, LowCardinality(String)),
+ aggregation_temporality SimpleAggregateFunction(any, LowCardinality(String)),
+ is_monotonic SimpleAggregateFunction(max, UInt8),
+ has_labels SimpleAggregateFunction(max, UInt8),
+ instrumentation_scope SimpleAggregateFunction(any, String),
+ histogram_bounds SimpleAggregateFunction(anyLast, Array(Float64)),
+ _topic SimpleAggregateFunction(any, LowCardinality(String)),
+ timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))),
+ observed_timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))),
+ value_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)),
+ count_arr SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)),
+ histogram_counts_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))),
+ trace_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ span_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ trace_flags_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_samples');
+CREATE TABLE posthog.writable_metrics4_series (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ series_fingerprint UInt64,
+ 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),
+ timestamp DateTime64(6),
+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),
+ original_expiry_timestamp DateTime64(6)
+) ENGINE = Distributed('logs', 'posthog', 'metrics4_series');
CREATE TABLE posthog.writable_query_log_archive (
hostname LowCardinality(String),
user LowCardinality(String),
@@ -34,6 +147,167 @@ CREATE TABLE posthog.writable_query_log_archive (
log_comment JSON(max_dynamic_paths=256, access_method LowCardinality(String), alert_config_id String, api_key_label String, api_key_mask String, batch_export_id String, chargeable Bool, client_query_id String, cohort_id Int64, `dagster.job_name` String, `dagster.run_id` String, `dagster.tags.owner` String, dashboard_id Int64, experiment_feature_flag_key String, experiment_id Int64, feature LowCardinality(String), id String, insight_id Int64, is_impersonated Bool, kind LowCardinality(String), name String, org_id String, person_on_events_mode LowCardinality(String), product LowCardinality(String), query_type LowCardinality(String), request_name String, route_id String, service_name String, session_id String, table_id String, team_id Int64, `temporal.activity_id` String, `temporal.activity_type` String, `temporal.attempt` Int64, `temporal.workflow_id` String, `temporal.workflow_namespace` String, `temporal.workflow_run_id` String, `temporal.workflow_type` String, user_id Int64, warehouse_query Bool, workflow LowCardinality(String), workload LowCardinality(String), SKIP cache_key, SKIP filter, SKIP hogql_features, SKIP http_referer, SKIP http_request_id, SKIP http_user_agent, SKIP query_settings, SKIP timings, SKIP user_email),
ProfileEvents Map(String, UInt64)
) ENGINE = Distributed('ops', 'posthog', 'query_log_archive_buffer');
+CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro4_mv TO posthog.metrics4_input (uuid String, team_id Int32, metric_name String, series_fingerprint UInt64, resource_fingerprint UInt64, timestamp DateTime64(6), observed_timestamp DateTime64(6), original_expiry_timestamp DateTime64(6), service_name String, metric_type String, value Float64, count UInt64, histogram_bounds Array(Float64), histogram_counts Array(UInt64), trace_id String, span_id String, trace_flags Int32, has_labels Bool, unit String, aggregation_temporality String, is_monotonic UInt8, instrumentation_scope String, resource_attributes Map(String, String), attributes Map(String, String), _partition UInt64, _topic LowCardinality(String), _offset UInt64) AS SELECT
+ uuid,
+ 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,
+ timestamp
+ + toIntervalDay(
+ assumeNotNull(
+ if(
+ (retention_days IS NOT NULL) AND (retention_days > 0),
+ retention_days,
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
+ )
+ )
+ ) 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_avro4
+WHERE kafka_metrics_avro4.series_fingerprint IS NOT NULL
+SETTINGS
+ min_insert_block_size_rows = 0,
+ min_insert_block_size_bytes = 0;
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), 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,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_names TO posthog.writable_metrics4_names (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))) AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM posthog.metrics4_input AS input
+WHERE has_labels
+GROUP BY
+ team_id, time_bucket, metric_name, original_expiry_time_bucket;
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_resource_attributes TO posthog.writable_metrics4_attributes (team_id Int32, metric_name LowCardinality(String), 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,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id, metric_name, time_bucket, original_expiry_time_bucket, service_name, filtered_attributes
+ );
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_samples TO posthog.writable_metrics4_samples (team_id Int32, metric_name LowCardinality(String), time_bucket DateTime, series_fingerprint UInt64, original_expiry_date Date32, resource_fingerprint UInt64, service_name String, metric_type String, unit String, aggregation_temporality String, is_monotonic UInt8, has_labels UInt8, instrumentation_scope String, histogram_bounds Array(Float64), _topic String, timestamp_arr Array(DateTime64(6)), observed_timestamp_arr Array(DateTime64(6)), value_arr Array(Float64), count_arr Array(UInt64), histogram_counts_arr Array(Array(UInt64)), trace_id_arr Array(String), span_id_arr Array(String), trace_flags_arr Array(Int32)) AS SELECT
+ team_id,
+ metric_name,
+ toDateTime(toStartOfHour(timestamp)) AS time_bucket,
+ series_fingerprint,
+ toDate32(original_expiry_timestamp) AS original_expiry_date,
+ any(resource_fingerprint) AS resource_fingerprint,
+ any(service_name) AS service_name,
+ any(metric_type) AS metric_type,
+ any(unit) AS unit,
+ any(aggregation_temporality) AS aggregation_temporality,
+ max(toUInt8(is_monotonic)) AS is_monotonic,
+ max(toUInt8(has_labels)) AS has_labels,
+ any(instrumentation_scope) AS instrumentation_scope,
+ anyLast(histogram_bounds) AS histogram_bounds,
+ any(_topic) AS _topic,
+ groupArray(10000)(timestamp) AS timestamp_arr,
+ groupArray(10000)(observed_timestamp) AS observed_timestamp_arr,
+ groupArray(10000)(value) AS value_arr,
+ groupArray(10000)(count) AS count_arr,
+ groupArray(10000)(histogram_counts) AS histogram_counts_arr,
+ groupArray(10000)(trace_id) AS trace_id_arr,
+ groupArray(10000)(span_id) AS span_id_arr,
+ groupArray(10000)(trace_flags) AS trace_flags_arr
+FROM posthog.metrics4_input
+GROUP BY
+ team_id, metric_name, time_bucket, series_fingerprint, original_expiry_date;
+CREATE MATERIALIZED VIEW posthog.metrics4_input_to_metrics4_series TO posthog.writable_metrics4_series (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), timestamp 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,
+ original_expiry_timestamp
+FROM posthog.metrics4_input
+WHERE has_labels;
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 SELECT
hostname,
user,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index b886a70d0a91..a435019af360 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -545,6 +545,79 @@ CREATE TABLE posthog.metrics2_input (
_topic String,
_offset UInt64
) ENGINE = Null();
+CREATE TABLE posthog.metrics4_attributes (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ 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.metrics4_attributes', '{replica}-{shard}') ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metrics4_names (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime64(0),
+ original_expiry_time_bucket DateTime64(0),
+ original_expiry_timestamp SimpleAggregateFunction(max, DateTime64(6))
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_names', '{replica}-{shard}') ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.metrics4_samples (
+ team_id Int32,
+ metric_name LowCardinality(String),
+ time_bucket DateTime,
+ series_fingerprint UInt64 CODEC(Delta(8), Default),
+ original_expiry_date Date32,
+ resource_fingerprint SimpleAggregateFunction(any, UInt64),
+ service_name SimpleAggregateFunction(any, LowCardinality(String)),
+ metric_type SimpleAggregateFunction(any, LowCardinality(String)),
+ unit SimpleAggregateFunction(any, LowCardinality(String)),
+ aggregation_temporality SimpleAggregateFunction(any, LowCardinality(String)),
+ is_monotonic SimpleAggregateFunction(max, UInt8),
+ has_labels SimpleAggregateFunction(max, UInt8),
+ instrumentation_scope SimpleAggregateFunction(any, String),
+ histogram_bounds SimpleAggregateFunction(anyLast, Array(Float64)),
+ _topic SimpleAggregateFunction(any, LowCardinality(String)),
+ timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ observed_timestamp_arr SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ value_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)) CODEC(Gorilla(8), Default),
+ count_arr SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)) CODEC(T64, Default),
+ histogram_counts_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))) CODEC(T64, Default),
+ trace_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ span_id_arr SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ trace_flags_arr SimpleAggregateFunction(groupArrayArray(10000), Array(Int32)),
+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1,
+ INDEX idx_trace_id_bf trace_id_arr TYPE bloom_filter(0.01) GRANULARITY 1
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_samples', '{replica}-{shard}') ORDER BY (team_id, metric_name, time_bucket, series_fingerprint) PARTITION BY original_expiry_date TTL original_expiry_date SETTINGS index_granularity = 128, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.metrics4_series (
+ 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),
+ timestamp DateTime64(6),
+ time_bucket DateTime MATERIALIZED toStartOfHour(timestamp),
+ 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,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metrics4_series', '{replica}-{shard}', timestamp) ORDER BY (team_id, metric_name, series_fingerprint, time_bucket) PARTITION BY toStartOfWeek(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS index_granularity = 1024, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metrics_distributed (
team_id Int32,
metric_name LowCardinality(String),
@@ -904,13 +977,13 @@ CREATE MATERIALIZED VIEW posthog.kafka_metrics_avro2_mv TO posthog.metrics2_inpu
cityHash64(mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes))) AS resource_fingerprint,
timestamp,
observed_timestamp,
- observed_timestamp
+ 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))
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))
)
)
) AS original_expiry_timestamp,
diff --git a/posthog/clickhouse/metrics/__init__.py b/posthog/clickhouse/metrics/__init__.py
index a8ea6f8e38a3..9043d3cb53de 100644
--- a/posthog/clickhouse/metrics/__init__.py
+++ b/posthog/clickhouse/metrics/__init__.py
@@ -43,6 +43,24 @@ from .metrics3 import (
METRICS2_INPUT_TO_METRIC_SERIES3_MV,
METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES3_MV,
)
+from .metrics4 import (
+ KAFKA_METRICS_AVRO4_MV,
+ KAFKA_METRICS_AVRO4_TABLE_SQL,
+ METRICS4_ATTRIBUTES_TABLE_SQL,
+ METRICS4_INPUT_TABLE_SQL,
+ METRICS4_INPUT_TO_METRICS4_ATTRIBUTES_MV,
+ METRICS4_INPUT_TO_METRICS4_NAMES_MV,
+ METRICS4_INPUT_TO_METRICS4_RESOURCE_ATTRIBUTES_MV,
+ METRICS4_INPUT_TO_METRICS4_SAMPLES_MV,
+ METRICS4_INPUT_TO_METRICS4_SERIES_MV,
+ METRICS4_NAMES_TABLE_SQL,
+ METRICS4_SAMPLES_TABLE_SQL,
+ METRICS4_SERIES_TABLE_SQL,
+ WRITABLE_METRICS4_ATTRIBUTES_TABLE_SQL,
+ WRITABLE_METRICS4_NAMES_TABLE_SQL,
+ WRITABLE_METRICS4_SAMPLES_TABLE_SQL,
+ WRITABLE_METRICS4_SERIES_TABLE_SQL,
+)
__all__ = [
"METRIC_ATTRIBUTES3_TABLE_SQL",
@@ -52,6 +70,22 @@ __all__ = [
"METRICS2_INPUT_TO_METRIC_NAMES3_MV",
"METRICS2_INPUT_TO_METRIC_SERIES3_MV",
"METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES3_MV",
+ "KAFKA_METRICS_AVRO4_MV",
+ "KAFKA_METRICS_AVRO4_TABLE_SQL",
+ "METRICS4_ATTRIBUTES_TABLE_SQL",
+ "METRICS4_INPUT_TABLE_SQL",
+ "METRICS4_INPUT_TO_METRICS4_ATTRIBUTES_MV",
+ "METRICS4_INPUT_TO_METRICS4_NAMES_MV",
+ "METRICS4_INPUT_TO_METRICS4_RESOURCE_ATTRIBUTES_MV",
+ "METRICS4_INPUT_TO_METRICS4_SAMPLES_MV",
+ "METRICS4_INPUT_TO_METRICS4_SERIES_MV",
+ "METRICS4_NAMES_TABLE_SQL",
+ "METRICS4_SAMPLES_TABLE_SQL",
+ "METRICS4_SERIES_TABLE_SQL",
+ "WRITABLE_METRICS4_ATTRIBUTES_TABLE_SQL",
+ "WRITABLE_METRICS4_NAMES_TABLE_SQL",
+ "WRITABLE_METRICS4_SAMPLES_TABLE_SQL",
+ "WRITABLE_METRICS4_SERIES_TABLE_SQL",
"KAFKA_METRICS_AVRO2_MV",
"KAFKA_METRICS_AVRO2_TABLE_SQL",
"METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL",
diff --git a/posthog/clickhouse/metrics/metrics2.py b/posthog/clickhouse/metrics/metrics2.py
index c6cd85cdc800..df8a9c3a4466 100644
--- a/posthog/clickhouse/metrics/metrics2.py
+++ b/posthog/clickhouse/metrics/metrics2.py
@@ -22,16 +22,28 @@ 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
+DEFAULT_RETENTION_DAYS = 30
def _db() -> str:
return settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
-def KAFKA_METRICS_AVRO2_TABLE_SQL() -> str:
+def kafka_metrics_avro_table_sql(
+ table_name: str,
+ group: str,
+ flush_interval_ms: int | None = None,
+ max_block_size: int | None = None,
+) -> str:
+ # The aggregation views group one insert block at a time, so a longer flush interval
+ # packs more samples of a series into each row before the merge.
+ block_settings = ""
+ if flush_interval_ms is not None:
+ block_settings += f"\n kafka_flush_interval_ms = {flush_interval_ms},"
+ if max_block_size is not None:
+ block_settings += f"\n kafka_max_block_size = {max_block_size},"
return f"""
-CREATE TABLE IF NOT EXISTS {_db()}.{KAFKA_TABLE_NAME}
+CREATE TABLE IF NOT EXISTS {_db()}.{table_name}
(
`uuid` String,
`trace_id` String,
@@ -56,20 +68,24 @@ CREATE TABLE IF NOT EXISTS {_db()}.{KAFKA_TABLE_NAME}
`has_labels` Nullable(UInt8),
`retention_days` Nullable(Int32)
)
-ENGINE = {kafka_engine(topic=KAFKA_TOPIC, group=KAFKA_GROUP, serialization="Avro", named_collection=KAFKA_NAMED_COLLECTION)}
+ENGINE = {kafka_engine(topic=KAFKA_TOPIC, group=group, serialization="Avro", named_collection=KAFKA_NAMED_COLLECTION)}
SETTINGS
kafka_skip_broken_messages = 100,
kafka_thread_per_consumer = 1,
kafka_num_consumers = {kafka_num_consumers(8)},
kafka_poll_timeout_ms = 3000,
- kafka_poll_max_batch_size = 1000,
+ kafka_poll_max_batch_size = 1000,{block_settings}
input_format_avro_allow_missing_fields = 1
"""
-def METRICS2_INPUT_TABLE_SQL() -> str:
+def KAFKA_METRICS_AVRO2_TABLE_SQL() -> str:
+ return kafka_metrics_avro_table_sql(KAFKA_TABLE_NAME, KAFKA_GROUP)
+
+
+def metrics_input_table_sql(table_name: str) -> str:
return f"""
-CREATE TABLE IF NOT EXISTS {_db()}.{METRICS2_INPUT_TABLE_NAME}
+CREATE TABLE IF NOT EXISTS {_db()}.{table_name}
(
`uuid` String,
`team_id` Int32,
@@ -103,6 +119,10 @@ ENGINE = Null
"""
+def METRICS2_INPUT_TABLE_SQL() -> str:
+ return metrics_input_table_sql(METRICS2_INPUT_TABLE_NAME)
+
+
def METRICS2_TABLE_SQL() -> str:
return f"""
CREATE TABLE IF NOT EXISTS {_db()}.{METRICS2_TABLE_NAME}
@@ -251,14 +271,15 @@ 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:
+def kafka_metrics_avro_mv_select(table_name: str) -> 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
+ # Use the sample timestamp so a late sample expires with its series.
+ # Capture replaces a timestamp when it differs from the ingest time by more than 24 hours.
+ # Thus, this view does not need another clock check.
+ return f"""SELECT
uuid,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
ifNull(metric_name, '') AS metric_name,
@@ -266,7 +287,7 @@ AS SELECT
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,
+ 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,
@@ -286,11 +307,22 @@ AS SELECT
_partition,
_topic,
_offset
-FROM {db}.{KAFKA_TABLE_NAME}
-WHERE {KAFKA_TABLE_NAME}.series_fingerprint IS NOT NULL
+FROM {db}.{table_name}
+WHERE {table_name}.series_fingerprint IS NOT NULL
SETTINGS
min_insert_block_size_rows = 0,
- min_insert_block_size_bytes = 0
+ min_insert_block_size_bytes = 0"""
+
+
+def KAFKA_METRICS_AVRO2_MV_SELECT() -> str:
+ return kafka_metrics_avro_mv_select(KAFKA_TABLE_NAME)
+
+
+def KAFKA_METRICS_AVRO2_MV() -> str:
+ db = _db()
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{KAFKA_TABLE_NAME}_mv TO {db}.{METRICS2_INPUT_TABLE_NAME}
+AS {KAFKA_METRICS_AVRO2_MV_SELECT()}
"""
diff --git a/posthog/clickhouse/metrics/metrics4.py b/posthog/clickhouse/metrics/metrics4.py
new file mode 100644
index 000000000000..6235ef28bcfa
--- /dev/null
+++ b/posthog/clickhouse/metrics/metrics4.py
@@ -0,0 +1,459 @@
+"""Define the metrics4 ClickHouse tables and materialized views.
+
+`metrics4_samples` groups points by series, hour, and expiry date. A complete
+merge produces one row for each key. The table stores each point field in a
+parallel array. The insert view and background merges keep at most 10,000 points
+in each row. Readers combine partial rows and sort the points by timestamp. The
+PromQL bridge already creates the same array shape from `metrics2`.
+
+After a merge, `metrics4_series` keeps one label row for each active series-hour
+in an expiry partition. `metrics4_names` stores hourly metric names.
+`metrics4_attributes` stores hourly metric and resource attributes. Readers use
+the last two tables for name and attribute selectors.
+
+`metrics4_input` supplies `original_expiry_timestamp` to each view. The Kafka
+view calculates this timestamp from the sample timestamp. The tables derive
+their expiry values from it. The default retention period is 30 days.
+
+The aggregate views process one input block at a time. The Kafka table flushes
+one block every 30 seconds. Larger blocks let `metrics4_samples` combine more
+points before it writes a part. `metrics4_series` uses an index granularity of
+1,024. This smaller granularity reduces the rows that a primary-key lookup reads
+from a new part.
+
+ClickHouse gives each parallel `groupArray` function the same input order for
+one block. Therefore, the same array index identifies all fields of one point.
+During a merge, ClickHouse uses the same row sequence for each array.
+`groupArrayArray` keeps at most 10,000 elements from that sequence. `ARRAY JOIN`
+requires all parallel arrays to have the same length.
+"""
+
+from django.conf import settings
+
+from posthog.clickhouse.table_engines import AggregatingMergeTree, Distributed, ReplacingMergeTree, ReplicationScheme
+
+from .metrics2 import kafka_metrics_avro_mv_select, kafka_metrics_avro_table_sql, metrics_input_table_sql
+
+KAFKA_METRICS4_TABLE_NAME = "kafka_metrics_avro4"
+KAFKA_METRICS4_GROUP = "clickhouse-metrics-avro4"
+KAFKA_METRICS4_FLUSH_INTERVAL_MS = 30_000
+KAFKA_METRICS4_MAX_BLOCK_SIZE = 1_000_000
+METRICS4_INPUT_TABLE_NAME = "metrics4_input"
+METRICS4_SAMPLES_TABLE_NAME = "metrics4_samples"
+METRICS4_SERIES_TABLE_NAME = "metrics4_series"
+METRICS4_NAMES_TABLE_NAME = "metrics4_names"
+METRICS4_ATTRIBUTES_TABLE_NAME = "metrics4_attributes"
+WRITABLE_METRICS4_SAMPLES_TABLE_NAME = "writable_metrics4_samples"
+WRITABLE_METRICS4_SERIES_TABLE_NAME = "writable_metrics4_series"
+WRITABLE_METRICS4_NAMES_TABLE_NAME = "writable_metrics4_names"
+WRITABLE_METRICS4_ATTRIBUTES_TABLE_NAME = "writable_metrics4_attributes"
+METRICS4_MAX_SAMPLES_PER_SERIES_HOUR = 10_000
+
+# Each tuple maps an input column to its metrics4 array element type.
+METRICS4_POINT_ARRAY_COLUMNS: tuple[tuple[str, str], ...] = (
+ ("timestamp", "DateTime64(6)"),
+ ("observed_timestamp", "DateTime64(6)"),
+ ("value", "Float64"),
+ ("count", "UInt64"),
+ ("histogram_counts", "Array(UInt64)"),
+ ("trace_id", "String"),
+ ("span_id", "String"),
+ ("trace_flags", "Int32"),
+)
+
+# ClickHouse applies each codec to the continuous stream of array elements.
+# Each array keeps its insert order. The delta codecs compress adjacent values.
+# T64 and Gorilla can process values without a sort.
+_ARRAY_CODECS: dict[str, str] = {
+ "timestamp": " CODEC(DoubleDelta, Default)",
+ "observed_timestamp": " CODEC(DoubleDelta, Default)",
+ "value": " CODEC(Gorilla, Default)",
+ "count": " CODEC(T64, Default)",
+ "histogram_counts": " CODEC(T64, Default)",
+}
+
+
+def _db() -> str:
+ return settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+
+
+def KAFKA_METRICS_AVRO4_TABLE_SQL() -> str:
+ return kafka_metrics_avro_table_sql(
+ KAFKA_METRICS4_TABLE_NAME,
+ KAFKA_METRICS4_GROUP,
+ flush_interval_ms=KAFKA_METRICS4_FLUSH_INTERVAL_MS,
+ max_block_size=KAFKA_METRICS4_MAX_BLOCK_SIZE,
+ )
+
+
+def METRICS4_INPUT_TABLE_SQL() -> str:
+ return metrics_input_table_sql(METRICS4_INPUT_TABLE_NAME)
+
+
+def KAFKA_METRICS_AVRO4_MV() -> str:
+ db = _db()
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{KAFKA_METRICS4_TABLE_NAME}_mv TO {db}.{METRICS4_INPUT_TABLE_NAME}
+AS {kafka_metrics_avro_mv_select(KAFKA_METRICS4_TABLE_NAME)}
+"""
+
+
+def _writable_table_sql(table_name: str, data_table_name: str, columns: str) -> str:
+ return f"""
+CREATE TABLE IF NOT EXISTS {_db()}.{table_name}
+(
+{columns}
+)
+ENGINE = {Distributed(data_table=data_table_name, cluster=settings.CLICKHOUSE_LOGS_CLUSTER)}
+"""
+
+
+def WRITABLE_METRICS4_SAMPLES_TABLE_SQL() -> str:
+ arrays = ",\n".join(
+ f" `{name}_arr` SimpleAggregateFunction(groupArrayArray({METRICS4_MAX_SAMPLES_PER_SERIES_HOUR}), Array({element_type}))"
+ for name, element_type in METRICS4_POINT_ARRAY_COLUMNS
+ )
+ return _writable_table_sql(
+ WRITABLE_METRICS4_SAMPLES_TABLE_NAME,
+ METRICS4_SAMPLES_TABLE_NAME,
+ f""" `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime,
+ `series_fingerprint` UInt64,
+ `original_expiry_date` Date32,
+ `resource_fingerprint` SimpleAggregateFunction(any, UInt64),
+ `service_name` SimpleAggregateFunction(any, LowCardinality(String)),
+ `metric_type` SimpleAggregateFunction(any, LowCardinality(String)),
+ `unit` SimpleAggregateFunction(any, LowCardinality(String)),
+ `aggregation_temporality` SimpleAggregateFunction(any, LowCardinality(String)),
+ `is_monotonic` SimpleAggregateFunction(max, UInt8),
+ `has_labels` SimpleAggregateFunction(max, UInt8),
+ `instrumentation_scope` SimpleAggregateFunction(any, String),
+ `histogram_bounds` SimpleAggregateFunction(anyLast, Array(Float64)),
+ `_topic` SimpleAggregateFunction(any, LowCardinality(String)),
+{arrays}""",
+ )
+
+
+def WRITABLE_METRICS4_SERIES_TABLE_SQL() -> str:
+ return _writable_table_sql(
+ WRITABLE_METRICS4_SERIES_TABLE_NAME,
+ METRICS4_SERIES_TABLE_NAME,
+ """ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `series_fingerprint` UInt64,
+ `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),
+ `timestamp` DateTime64(6),
+ `time_bucket` DateTime MATERIALIZED toStartOfHour(timestamp),
+ `original_expiry_timestamp` DateTime64(6)""",
+ )
+
+
+def WRITABLE_METRICS4_NAMES_TABLE_SQL() -> str:
+ return _writable_table_sql(
+ WRITABLE_METRICS4_NAMES_TABLE_NAME,
+ METRICS4_NAMES_TABLE_NAME,
+ """ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))""",
+ )
+
+
+def WRITABLE_METRICS4_ATTRIBUTES_TABLE_SQL() -> str:
+ return _writable_table_sql(
+ WRITABLE_METRICS4_ATTRIBUTES_TABLE_NAME,
+ METRICS4_ATTRIBUTES_TABLE_NAME,
+ """ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `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)""",
+ )
+
+
+def METRICS4_SAMPLES_TABLE_SQL() -> str:
+ arrays = ",\n".join(
+ f" `{name}_arr` SimpleAggregateFunction(groupArrayArray({METRICS4_MAX_SAMPLES_PER_SERIES_HOUR}), Array({element_type})){_ARRAY_CODECS.get(name, '')}"
+ for name, element_type in METRICS4_POINT_ARRAY_COLUMNS
+ )
+ # One merged row contains up to 10,000 points for one series-hour and expiry date.
+ # A 128-row granule can contain many series from one insert.
+ # A primary-key filter cannot skip individual rows in that granule.
+ # The granule can contain multiple metric names and time buckets.
+ # The time_bucket minmax index lets an hour filter skip the complete granule.
+ # The metrics2 table uses idx_timestamp_minmax for the same purpose.
+ return f"""
+CREATE TABLE IF NOT EXISTS {_db()}.{METRICS4_SAMPLES_TABLE_NAME}
+(
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime,
+ `series_fingerprint` UInt64 CODEC(Delta(8), Default),
+ `original_expiry_date` Date32,
+ `resource_fingerprint` SimpleAggregateFunction(any, UInt64),
+ `service_name` SimpleAggregateFunction(any, LowCardinality(String)),
+ `metric_type` SimpleAggregateFunction(any, LowCardinality(String)),
+ `unit` SimpleAggregateFunction(any, LowCardinality(String)),
+ `aggregation_temporality` SimpleAggregateFunction(any, LowCardinality(String)),
+ `is_monotonic` SimpleAggregateFunction(max, UInt8),
+ `has_labels` SimpleAggregateFunction(max, UInt8),
+ `instrumentation_scope` SimpleAggregateFunction(any, String),
+ `histogram_bounds` SimpleAggregateFunction(anyLast, Array(Float64)),
+ `_topic` SimpleAggregateFunction(any, LowCardinality(String)),
+{arrays},
+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1,
+ INDEX idx_trace_id_bf trace_id_arr TYPE bloom_filter(0.01) GRANULARITY 1
+)
+ENGINE = {AggregatingMergeTree(METRICS4_SAMPLES_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
+PARTITION BY original_expiry_date
+ORDER BY (team_id, metric_name, time_bucket, series_fingerprint)
+TTL original_expiry_date
+SETTINGS
+ index_granularity = 128,
+ ttl_only_drop_parts = 1
+"""
+
+
+def METRICS4_SERIES_TABLE_SQL() -> str:
+ return f"""
+CREATE TABLE IF NOT EXISTS {_db()}.{METRICS4_SERIES_TABLE_NAME}
+(
+ `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),
+ `timestamp` DateTime64(6),
+ `time_bucket` DateTime MATERIALIZED toStartOfHour(timestamp),
+ `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,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1
+)
+ENGINE = {ReplacingMergeTree(METRICS4_SERIES_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED, ver="timestamp")}
+PARTITION BY toStartOfWeek(original_expiry_timestamp)
+ORDER BY (team_id, metric_name, series_fingerprint, time_bucket)
+TTL original_expiry_timestamp
+SETTINGS
+ index_granularity = 1024,
+ ttl_only_drop_parts = 1
+"""
+
+
+def METRICS4_NAMES_TABLE_SQL() -> str:
+ return f"""
+CREATE TABLE IF NOT EXISTS {_db()}.{METRICS4_NAMES_TABLE_NAME}
+(
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+)
+ENGINE = {AggregatingMergeTree(METRICS4_NAMES_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
+PARTITION BY toDate(original_expiry_time_bucket)
+ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket)
+TTL original_expiry_timestamp
+SETTINGS index_granularity = 8192
+"""
+
+
+def METRICS4_ATTRIBUTES_TABLE_SQL() -> str:
+ return f"""
+CREATE TABLE IF NOT EXISTS {_db()}.{METRICS4_ATTRIBUTES_TABLE_NAME}
+(
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `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(METRICS4_ATTRIBUTES_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED)}
+PARTITION BY toDate(original_expiry_time_bucket)
+ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket)
+TTL original_expiry_time_bucket
+SETTINGS
+ index_granularity = 8192,
+ ttl_only_drop_parts = 1
+"""
+
+
+def METRICS4_INPUT_TO_METRICS4_SAMPLES_MV() -> str:
+ db = _db()
+ group_arrays = ",\n".join(
+ f" groupArray({METRICS4_MAX_SAMPLES_PER_SERIES_HOUR})({name}) AS {name}_arr"
+ for name, _ in METRICS4_POINT_ARRAY_COLUMNS
+ )
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{METRICS4_INPUT_TABLE_NAME}_to_{METRICS4_SAMPLES_TABLE_NAME} TO {db}.{WRITABLE_METRICS4_SAMPLES_TABLE_NAME}
+AS SELECT
+ team_id,
+ metric_name,
+ toDateTime(toStartOfHour(timestamp)) AS time_bucket,
+ series_fingerprint,
+ toDate32(original_expiry_timestamp) AS original_expiry_date,
+ any(resource_fingerprint) AS resource_fingerprint,
+ any(service_name) AS service_name,
+ any(metric_type) AS metric_type,
+ any(unit) AS unit,
+ any(aggregation_temporality) AS aggregation_temporality,
+ max(toUInt8(is_monotonic)) AS is_monotonic,
+ max(toUInt8(has_labels)) AS has_labels,
+ any(instrumentation_scope) AS instrumentation_scope,
+ anyLast(histogram_bounds) AS histogram_bounds,
+ any(_topic) AS _topic,
+{group_arrays}
+FROM {db}.{METRICS4_INPUT_TABLE_NAME}
+GROUP BY
+ team_id,
+ metric_name,
+ time_bucket,
+ series_fingerprint,
+ original_expiry_date
+"""
+
+
+def METRICS4_INPUT_TO_METRICS4_SERIES_MV() -> str:
+ db = _db()
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{METRICS4_INPUT_TABLE_NAME}_to_{METRICS4_SERIES_TABLE_NAME} TO {db}.{WRITABLE_METRICS4_SERIES_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,
+ original_expiry_timestamp
+FROM {db}.{METRICS4_INPUT_TABLE_NAME}
+WHERE has_labels
+"""
+
+
+def METRICS4_INPUT_TO_METRICS4_NAMES_MV() -> str:
+ db = _db()
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{METRICS4_INPUT_TABLE_NAME}_to_{METRICS4_NAMES_TABLE_NAME} TO {db}.{WRITABLE_METRICS4_NAMES_TABLE_NAME}
+(
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+)
+AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+FROM {db}.{METRICS4_INPUT_TABLE_NAME} AS input
+WHERE has_labels
+GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket
+"""
+
+
+def _metrics4_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}.{METRICS4_INPUT_TABLE_NAME}_to_{view_suffix} TO {db}.{WRITABLE_METRICS4_ATTRIBUTES_TABLE_NAME}
+(
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `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,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+(
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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}.{METRICS4_INPUT_TABLE_NAME}
+ WHERE has_labels
+ GROUP BY
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ filtered_attributes
+)
+"""
+
+
+def METRICS4_INPUT_TO_METRICS4_ATTRIBUTES_MV() -> str:
+ return _metrics4_attributes_mv(METRICS4_ATTRIBUTES_TABLE_NAME, "attributes", "metric", filter_long_pairs=True)
+
+
+def METRICS4_INPUT_TO_METRICS4_RESOURCE_ATTRIBUTES_MV() -> str:
+ return _metrics4_attributes_mv(
+ "metrics4_resource_attributes", "resource_attributes", "resource", filter_long_pairs=False
+ )
diff --git a/posthog/clickhouse/migrations/0327_metrics4.py b/posthog/clickhouse/migrations/0327_metrics4.py
new file mode 100644
index 000000000000..94c39e500e4c
--- /dev/null
+++ b/posthog/clickhouse/migrations/0327_metrics4.py
@@ -0,0 +1,51 @@
+from django.conf import settings
+
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.clickhouse.metrics import (
+ KAFKA_METRICS_AVRO4_MV,
+ KAFKA_METRICS_AVRO4_TABLE_SQL,
+ METRICS4_ATTRIBUTES_TABLE_SQL,
+ METRICS4_INPUT_TABLE_SQL,
+ METRICS4_INPUT_TO_METRICS4_ATTRIBUTES_MV,
+ METRICS4_INPUT_TO_METRICS4_NAMES_MV,
+ METRICS4_INPUT_TO_METRICS4_RESOURCE_ATTRIBUTES_MV,
+ METRICS4_INPUT_TO_METRICS4_SAMPLES_MV,
+ METRICS4_INPUT_TO_METRICS4_SERIES_MV,
+ METRICS4_NAMES_TABLE_SQL,
+ METRICS4_SAMPLES_TABLE_SQL,
+ METRICS4_SERIES_TABLE_SQL,
+ WRITABLE_METRICS4_ATTRIBUTES_TABLE_SQL,
+ WRITABLE_METRICS4_NAMES_TABLE_SQL,
+ WRITABLE_METRICS4_SAMPLES_TABLE_SQL,
+ WRITABLE_METRICS4_SERIES_TABLE_SQL,
+)
+from posthog.clickhouse.metrics.metrics2 import KAFKA_METRICS_AVRO2_MV_SELECT
+
+DB = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+
+operations = [
+ run_sql_with_exceptions(METRICS4_SAMPLES_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(METRICS4_SERIES_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(METRICS4_NAMES_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(METRICS4_ATTRIBUTES_TABLE_SQL(), node_roles=[NodeRole.LOGS]),
+ run_sql_with_exceptions(METRICS4_INPUT_TABLE_SQL(), node_roles=[NodeRole.APM]),
+ run_sql_with_exceptions(WRITABLE_METRICS4_SAMPLES_TABLE_SQL(), node_roles=[NodeRole.APM]),
+ run_sql_with_exceptions(WRITABLE_METRICS4_SERIES_TABLE_SQL(), node_roles=[NodeRole.APM]),
+ run_sql_with_exceptions(WRITABLE_METRICS4_NAMES_TABLE_SQL(), node_roles=[NodeRole.APM]),
+ run_sql_with_exceptions(WRITABLE_METRICS4_ATTRIBUTES_TABLE_SQL(), node_roles=[NodeRole.APM]),
+ run_sql_with_exceptions(METRICS4_INPUT_TO_METRICS4_SAMPLES_MV(), node_roles=[NodeRole.APM]),
+ run_sql_with_exceptions(METRICS4_INPUT_TO_METRICS4_SERIES_MV(), node_roles=[NodeRole.APM]),
+ run_sql_with_exceptions(METRICS4_INPUT_TO_METRICS4_NAMES_MV(), node_roles=[NodeRole.APM]),
+ run_sql_with_exceptions(METRICS4_INPUT_TO_METRICS4_ATTRIBUTES_MV(), node_roles=[NodeRole.APM]),
+ run_sql_with_exceptions(METRICS4_INPUT_TO_METRICS4_RESOURCE_ATTRIBUTES_MV(), node_roles=[NodeRole.APM]),
+ run_sql_with_exceptions(KAFKA_METRICS_AVRO4_TABLE_SQL(), node_roles=[NodeRole.APM]),
+ run_sql_with_exceptions(KAFKA_METRICS_AVRO4_MV(), node_roles=[NodeRole.APM]),
+ # Use the sample timestamp so a late sample expires with its series.
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.kafka_metrics_avro2_mv MODIFY QUERY\n{KAFKA_METRICS_AVRO2_MV_SELECT()}",
+ node_roles=[NodeRole.LOGS],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index d658262ea51a..77f13daaf2cc 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0326_web_sessions_preaggregated
+0327_metrics4
diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.py
index 657ed1732dce..0729033e8513 100644
--- a/posthog/clickhouse/schema.py
+++ b/posthog/clickhouse/schema.py
@@ -34,6 +34,8 @@ from posthog.clickhouse.logs import (
LOGS_VOLUME_BUCKETS_TABLE_SQL,
)
from posthog.clickhouse.metrics import (
+ KAFKA_METRICS_AVRO4_MV,
+ KAFKA_METRICS_AVRO4_TABLE_SQL,
METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL,
METRIC_ATTRIBUTES2_TABLE_SQL,
METRIC_ATTRIBUTES3_TABLE_SQL,
@@ -58,7 +60,21 @@ from posthog.clickhouse.metrics import (
METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES3_MV,
METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV,
METRICS2_TABLE_SQL,
+ METRICS4_ATTRIBUTES_TABLE_SQL,
+ METRICS4_INPUT_TABLE_SQL,
+ METRICS4_INPUT_TO_METRICS4_ATTRIBUTES_MV,
+ METRICS4_INPUT_TO_METRICS4_NAMES_MV,
+ METRICS4_INPUT_TO_METRICS4_RESOURCE_ATTRIBUTES_MV,
+ METRICS4_INPUT_TO_METRICS4_SAMPLES_MV,
+ METRICS4_INPUT_TO_METRICS4_SERIES_MV,
+ METRICS4_NAMES_TABLE_SQL,
+ METRICS4_SAMPLES_TABLE_SQL,
+ METRICS4_SERIES_TABLE_SQL,
METRICS_DISTRIBUTED_TABLE_SQL,
+ WRITABLE_METRICS4_ATTRIBUTES_TABLE_SQL,
+ WRITABLE_METRICS4_NAMES_TABLE_SQL,
+ WRITABLE_METRICS4_SAMPLES_TABLE_SQL,
+ WRITABLE_METRICS4_SERIES_TABLE_SQL,
)
from posthog.clickhouse.plugin_log_entries import (
KAFKA_PLUGIN_LOG_ENTRIES_TABLE_SQL,
@@ -469,12 +485,17 @@ CREATE_MERGETREE_TABLE_QUERIES = (
METRIC_SAMPLES_TABLE_SQL,
METRIC_ATTRIBUTES_TABLE_SQL,
METRICS2_INPUT_TABLE_SQL,
+ METRICS4_INPUT_TABLE_SQL,
METRICS2_TABLE_SQL,
METRIC_SERIES2_TABLE_SQL,
METRIC_SERIES3_TABLE_SQL,
METRIC_ATTRIBUTES2_TABLE_SQL,
METRIC_ATTRIBUTES3_TABLE_SQL,
METRIC_NAMES3_TABLE_SQL,
+ METRICS4_SAMPLES_TABLE_SQL,
+ METRICS4_SERIES_TABLE_SQL,
+ METRICS4_NAMES_TABLE_SQL,
+ METRICS4_ATTRIBUTES_TABLE_SQL,
TOPHOG_DATA_TABLE_SQL,
BILLING_USAGE_RECORDS_DATA_TABLE_SQL,
AI_EVENTS_DATA_TABLE_SQL,
@@ -567,6 +588,10 @@ CREATE_DISTRIBUTED_TABLE_QUERIES = (
METRICS2_DISTRIBUTED_TABLE_SQL,
METRIC_SERIES2_DISTRIBUTED_TABLE_SQL,
METRIC_ATTRIBUTES2_DISTRIBUTED_TABLE_SQL,
+ WRITABLE_METRICS4_SAMPLES_TABLE_SQL,
+ WRITABLE_METRICS4_SERIES_TABLE_SQL,
+ WRITABLE_METRICS4_NAMES_TABLE_SQL,
+ WRITABLE_METRICS4_ATTRIBUTES_TABLE_SQL,
WRITABLE_TOPHOG_TABLE_SQL,
DISTRIBUTED_TOPHOG_TABLE_SQL,
DISTRIBUTED_BILLING_USAGE_RECORDS_TABLE_SQL,
@@ -609,6 +634,7 @@ CREATE_KAFKA_TABLE_QUERIES = (
KAFKA_BILLING_USAGE_RECORDS_TABLE_SQL,
KAFKA_AI_EVENTS_TABLE_SQL,
KAFKA_PROPERTY_VALUES_TABLE_SQL_FN,
+ KAFKA_METRICS_AVRO4_TABLE_SQL,
)
CREATE_MV_TABLE_QUERIES = (
PERSON_PROPERTY_MUTATION_LOG_MV_SQL,
@@ -662,6 +688,12 @@ CREATE_MV_TABLE_QUERIES = (
METRICS2_INPUT_TO_METRIC_NAMES3_MV,
METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES_MV,
METRICS2_INPUT_TO_RESOURCE_ATTRIBUTES3_MV,
+ METRICS4_INPUT_TO_METRICS4_SAMPLES_MV,
+ METRICS4_INPUT_TO_METRICS4_SERIES_MV,
+ METRICS4_INPUT_TO_METRICS4_NAMES_MV,
+ METRICS4_INPUT_TO_METRICS4_ATTRIBUTES_MV,
+ METRICS4_INPUT_TO_METRICS4_RESOURCE_ATTRIBUTES_MV,
+ KAFKA_METRICS_AVRO4_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.ambr
index ac78a5e72238..b6daa909a57c 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -501,6 +501,47 @@
'''
# ---
+# name: test_create_kafka_table_with_different_kafka_host[kafka_metrics_avro4]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.kafka_metrics_avro4
+ (
+ `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, kafka_topic_list = 'clickhouse_metrics', kafka_group_name = 'clickhouse-metrics-avro4', kafka_format = 'Avro')
+ SETTINGS
+ kafka_skip_broken_messages = 100,
+ kafka_thread_per_consumer = 1,
+ kafka_num_consumers = 1,
+ kafka_poll_timeout_ms = 3000,
+ kafka_poll_max_batch_size = 1000,
+ kafka_flush_interval_ms = 30000,
+ kafka_max_block_size = 1000000,
+ input_format_avro_allow_missing_fields = 1
+
+ '''
+# ---
# name: test_create_kafka_table_with_different_kafka_host[kafka_performance_events]
'''
@@ -2813,6 +2854,87 @@
'''
# ---
+# name: test_create_table_query[kafka_metrics_avro4]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.kafka_metrics_avro4
+ (
+ `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, kafka_topic_list = 'clickhouse_metrics', kafka_group_name = 'clickhouse-metrics-avro4', kafka_format = 'Avro')
+ SETTINGS
+ kafka_skip_broken_messages = 100,
+ kafka_thread_per_consumer = 1,
+ kafka_num_consumers = 1,
+ kafka_poll_timeout_ms = 3000,
+ kafka_poll_max_batch_size = 1000,
+ kafka_flush_interval_ms = 30000,
+ kafka_max_block_size = 1000000,
+ input_format_avro_allow_missing_fields = 1
+
+ '''
+# ---
+# name: test_create_table_query[kafka_metrics_avro4_mv]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.kafka_metrics_avro4_mv TO posthog_test.metrics4_input
+ 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,
+ timestamp + toIntervalDay(assumeNotNull(if((retention_days IS NOT NULL) AND (retention_days > 0), retention_days, toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(30))))) 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_test.kafka_metrics_avro4
+ WHERE kafka_metrics_avro4.series_fingerprint IS NOT NULL
+ SETTINGS
+ min_insert_block_size_rows = 0,
+ min_insert_block_size_bytes = 0
+
+ '''
+# ---
# name: test_create_table_query[kafka_performance_events]
'''
@@ -4808,6 +4930,358 @@
'''
# ---
+# name: test_create_table_query[metrics4_attributes]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metrics4_attributes
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `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.metrics4_attributes', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_time_bucket)
+ ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket)
+ TTL original_expiry_time_bucket
+ SETTINGS
+ index_granularity = 8192,
+ ttl_only_drop_parts = 1
+
+ '''
+# ---
+# name: test_create_table_query[metrics4_input]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metrics4_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[metrics4_input_to_metrics4_attributes]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics4_input_to_metrics4_attributes TO posthog_test.writable_metrics4_attributes
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `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,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+ FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ filtered_attributes
+ )
+
+ '''
+# ---
+# name: test_create_table_query[metrics4_input_to_metrics4_names]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics4_input_to_metrics4_names TO posthog_test.writable_metrics4_names
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ )
+ AS SELECT
+ team_id,
+ metric_name,
+ toStartOfHour(timestamp) AS time_bucket,
+ toStartOfHour(input.original_expiry_timestamp) AS original_expiry_time_bucket,
+ maxSimpleState(input.original_expiry_timestamp) AS original_expiry_timestamp
+ FROM posthog_test.metrics4_input AS input
+ WHERE has_labels
+ GROUP BY team_id, time_bucket, metric_name, original_expiry_time_bucket
+
+ '''
+# ---
+# name: test_create_table_query[metrics4_input_to_metrics4_resource_attributes]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics4_input_to_metrics4_resource_attributes TO posthog_test.writable_metrics4_attributes
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `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,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+ FROM
+ (
+ SELECT
+ team_id AS team_id,
+ metric_name AS metric_name,
+ 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.metrics4_input
+ WHERE has_labels
+ GROUP BY
+ team_id,
+ metric_name,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ filtered_attributes
+ )
+
+ '''
+# ---
+# name: test_create_table_query[metrics4_input_to_metrics4_samples]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics4_input_to_metrics4_samples TO posthog_test.writable_metrics4_samples
+ AS SELECT
+ team_id,
+ metric_name,
+ toDateTime(toStartOfHour(timestamp)) AS time_bucket,
+ series_fingerprint,
+ toDate32(original_expiry_timestamp) AS original_expiry_date,
+ any(resource_fingerprint) AS resource_fingerprint,
+ any(service_name) AS service_name,
+ any(metric_type) AS metric_type,
+ any(unit) AS unit,
+ any(aggregation_temporality) AS aggregation_temporality,
+ max(toUInt8(is_monotonic)) AS is_monotonic,
+ max(toUInt8(has_labels)) AS has_labels,
+ any(instrumentation_scope) AS instrumentation_scope,
+ anyLast(histogram_bounds) AS histogram_bounds,
+ any(_topic) AS _topic,
+ groupArray(10000)(timestamp) AS timestamp_arr,
+ groupArray(10000)(observed_timestamp) AS observed_timestamp_arr,
+ groupArray(10000)(value) AS value_arr,
+ groupArray(10000)(count) AS count_arr,
+ groupArray(10000)(histogram_counts) AS histogram_counts_arr,
+ groupArray(10000)(trace_id) AS trace_id_arr,
+ groupArray(10000)(span_id) AS span_id_arr,
+ groupArray(10000)(trace_flags) AS trace_flags_arr
+ FROM posthog_test.metrics4_input
+ GROUP BY
+ team_id,
+ metric_name,
+ time_bucket,
+ series_fingerprint,
+ original_expiry_date
+
+ '''
+# ---
+# name: test_create_table_query[metrics4_input_to_metrics4_series]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS posthog_test.metrics4_input_to_metrics4_series TO posthog_test.writable_metrics4_series
+ AS SELECT
+ team_id,
+ metric_name,
+ series_fingerprint,
+ metric_type,
+ unit,
+ aggregation_temporality,
+ is_monotonic,
+ service_name,
+ instrumentation_scope,
+ resource_attributes,
+ attributes,
+ timestamp,
+ original_expiry_timestamp
+ FROM posthog_test.metrics4_input
+ WHERE has_labels
+
+ '''
+# ---
+# name: test_create_table_query[metrics4_names]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metrics4_names
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics4_names', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_time_bucket)
+ ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket)
+ TTL original_expiry_timestamp
+ SETTINGS index_granularity = 8192
+
+ '''
+# ---
+# name: test_create_table_query[metrics4_samples]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metrics4_samples
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime,
+ `series_fingerprint` UInt64 CODEC(Delta(8), Default),
+ `original_expiry_date` Date32,
+ `resource_fingerprint` SimpleAggregateFunction(any, UInt64),
+ `service_name` SimpleAggregateFunction(any, LowCardinality(String)),
+ `metric_type` SimpleAggregateFunction(any, LowCardinality(String)),
+ `unit` SimpleAggregateFunction(any, LowCardinality(String)),
+ `aggregation_temporality` SimpleAggregateFunction(any, LowCardinality(String)),
+ `is_monotonic` SimpleAggregateFunction(max, UInt8),
+ `has_labels` SimpleAggregateFunction(max, UInt8),
+ `instrumentation_scope` SimpleAggregateFunction(any, String),
+ `histogram_bounds` SimpleAggregateFunction(anyLast, Array(Float64)),
+ `_topic` SimpleAggregateFunction(any, LowCardinality(String)),
+ `timestamp_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ `observed_timestamp_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ `value_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)) CODEC(Gorilla, Default),
+ `count_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)) CODEC(T64, Default),
+ `histogram_counts_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))) CODEC(T64, Default),
+ `trace_id_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ `span_id_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ `trace_flags_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(Int32)),
+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1,
+ INDEX idx_trace_id_bf trace_id_arr TYPE bloom_filter(0.01) GRANULARITY 1
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics4_samples', '{replica}-{shard}')
+ PARTITION BY original_expiry_date
+ ORDER BY (team_id, metric_name, time_bucket, series_fingerprint)
+ TTL original_expiry_date
+ SETTINGS
+ index_granularity = 128,
+ ttl_only_drop_parts = 1
+
+ '''
+# ---
+# name: test_create_table_query[metrics4_series]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metrics4_series
+ (
+ `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),
+ `timestamp` DateTime64(6),
+ `time_bucket` DateTime MATERIALIZED toStartOfHour(timestamp),
+ `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,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1
+ )
+ ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics4_series', '{replica}-{shard}', timestamp)
+ PARTITION BY toStartOfWeek(original_expiry_timestamp)
+ ORDER BY (team_id, metric_name, series_fingerprint, time_bucket)
+ TTL original_expiry_timestamp
+ SETTINGS
+ index_granularity = 1024,
+ ttl_only_drop_parts = 1
+
+ '''
+# ---
# name: test_create_table_query[metrics]
'''
@@ -10063,6 +10537,98 @@
'''
# ---
+# name: test_create_table_query[writable_metrics4_attributes]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.writable_metrics4_attributes
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `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_test', 'metrics4_attributes')
+
+ '''
+# ---
+# name: test_create_table_query[writable_metrics4_names]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.writable_metrics4_names
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ )
+ ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'metrics4_names')
+
+ '''
+# ---
+# name: test_create_table_query[writable_metrics4_samples]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.writable_metrics4_samples
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime,
+ `series_fingerprint` UInt64,
+ `original_expiry_date` Date32,
+ `resource_fingerprint` SimpleAggregateFunction(any, UInt64),
+ `service_name` SimpleAggregateFunction(any, LowCardinality(String)),
+ `metric_type` SimpleAggregateFunction(any, LowCardinality(String)),
+ `unit` SimpleAggregateFunction(any, LowCardinality(String)),
+ `aggregation_temporality` SimpleAggregateFunction(any, LowCardinality(String)),
+ `is_monotonic` SimpleAggregateFunction(max, UInt8),
+ `has_labels` SimpleAggregateFunction(max, UInt8),
+ `instrumentation_scope` SimpleAggregateFunction(any, String),
+ `histogram_bounds` SimpleAggregateFunction(anyLast, Array(Float64)),
+ `_topic` SimpleAggregateFunction(any, LowCardinality(String)),
+ `timestamp_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))),
+ `observed_timestamp_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))),
+ `value_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)),
+ `count_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)),
+ `histogram_counts_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))),
+ `trace_id_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ `span_id_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ `trace_flags_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(Int32))
+ )
+ ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'metrics4_samples')
+
+ '''
+# ---
+# name: test_create_table_query[writable_metrics4_series]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.writable_metrics4_series
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `series_fingerprint` UInt64,
+ `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),
+ `timestamp` DateTime64(6),
+ `time_bucket` DateTime MATERIALIZED toStartOfHour(timestamp),
+ `original_expiry_timestamp` DateTime64(6)
+ )
+ ENGINE = Distributed('posthog_single_shard', 'posthog_test', 'metrics4_series')
+
+ '''
+# ---
# name: test_create_table_query[writable_person]
'''
@@ -11884,6 +12450,170 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[metrics4_attributes]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metrics4_attributes
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `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.metrics4_attributes', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_time_bucket)
+ ORDER BY (team_id, metric_name, attribute_type, time_bucket, attribute_key, attribute_value, service_name, original_expiry_time_bucket)
+ TTL original_expiry_time_bucket
+ SETTINGS
+ index_granularity = 8192,
+ ttl_only_drop_parts = 1
+
+ '''
+# ---
+# name: test_create_table_query_replicated_and_storage[metrics4_input]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metrics4_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[metrics4_names]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metrics4_names
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime64(0),
+ `original_expiry_time_bucket` DateTime64(0),
+ `original_expiry_timestamp` SimpleAggregateFunction(max, DateTime64(6))
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics4_names', '{replica}-{shard}')
+ PARTITION BY toDate(original_expiry_time_bucket)
+ ORDER BY (team_id, time_bucket, metric_name, original_expiry_time_bucket)
+ TTL original_expiry_timestamp
+ SETTINGS index_granularity = 8192
+
+ '''
+# ---
+# name: test_create_table_query_replicated_and_storage[metrics4_samples]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metrics4_samples
+ (
+ `team_id` Int32,
+ `metric_name` LowCardinality(String),
+ `time_bucket` DateTime,
+ `series_fingerprint` UInt64 CODEC(Delta(8), Default),
+ `original_expiry_date` Date32,
+ `resource_fingerprint` SimpleAggregateFunction(any, UInt64),
+ `service_name` SimpleAggregateFunction(any, LowCardinality(String)),
+ `metric_type` SimpleAggregateFunction(any, LowCardinality(String)),
+ `unit` SimpleAggregateFunction(any, LowCardinality(String)),
+ `aggregation_temporality` SimpleAggregateFunction(any, LowCardinality(String)),
+ `is_monotonic` SimpleAggregateFunction(max, UInt8),
+ `has_labels` SimpleAggregateFunction(max, UInt8),
+ `instrumentation_scope` SimpleAggregateFunction(any, String),
+ `histogram_bounds` SimpleAggregateFunction(anyLast, Array(Float64)),
+ `_topic` SimpleAggregateFunction(any, LowCardinality(String)),
+ `timestamp_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ `observed_timestamp_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(DateTime64(6))) CODEC(DoubleDelta, Default),
+ `value_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(Float64)) CODEC(Gorilla, Default),
+ `count_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(UInt64)) CODEC(T64, Default),
+ `histogram_counts_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(Array(UInt64))) CODEC(T64, Default),
+ `trace_id_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ `span_id_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(String)),
+ `trace_flags_arr` SimpleAggregateFunction(groupArrayArray(10000), Array(Int32)),
+ INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1,
+ INDEX idx_trace_id_bf trace_id_arr TYPE bloom_filter(0.01) GRANULARITY 1
+ )
+ ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics4_samples', '{replica}-{shard}')
+ PARTITION BY original_expiry_date
+ ORDER BY (team_id, metric_name, time_bucket, series_fingerprint)
+ TTL original_expiry_date
+ SETTINGS
+ index_granularity = 128,
+ ttl_only_drop_parts = 1
+
+ '''
+# ---
+# name: test_create_table_query_replicated_and_storage[metrics4_series]
+ '''
+
+ CREATE TABLE IF NOT EXISTS posthog_test.metrics4_series
+ (
+ `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),
+ `timestamp` DateTime64(6),
+ `time_bucket` DateTime MATERIALIZED toStartOfHour(timestamp),
+ `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,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_time_bucket_minmax time_bucket TYPE minmax GRANULARITY 1
+ )
+ ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metrics4_series', '{replica}-{shard}', timestamp)
+ PARTITION BY toStartOfWeek(original_expiry_timestamp)
+ ORDER BY (team_id, metric_name, series_fingerprint, time_bucket)
+ TTL original_expiry_timestamp
+ SETTINGS
+ index_granularity = 1024,
+ ttl_only_drop_parts = 1
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[partitioned_sharded_posthog_document_embeddings]
'''