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.

chore(clickhouse): declare what the cloud logs and aux nodes already run (#88119)

Paweł Szczurb75f6e2784c9
diff --git a/posthog/clickhouse/hcl/README.md b/posthog/clickhouse/hcl/README.md
index f372f16de97d..9c5b61c49a80 100644
--- a/posthog/clickhouse/hcl/README.md
+++ b/posthog/clickhouse/hcl/README.md
@@ -82,9 +82,9 @@ reconciling a new role.
Every role `manifest.hcl` composes for the gate's env is dumped and gated — for `local-multi` that is
each node the multinode stack runs, one per published port in `dump-live.sh`'s `ROLES`, compared
against its `golden/local-multi/<role>.hcl` (`aux` is filed as `auxiliary`, see `golden_name` in
-`lib.sh`). The local LOGS node runs a partial/newer schema than the cloud logs nodes, so
-`local-multi logs` composes a self-contained `roles/logs/local` (extracted from the live node) rather
-than the shared cloud layers.
+`lib.sh`). Every logs node composes `roles/logs/{base,traces,traces_kafka_metrics}`; the local one
+adds a self-contained `roles/logs/local` (extracted from the live node) for the legacy `logs32`
+family it still runs, and skips the cloud-only metrics ingest in `roles/logs/cloud`.
`node_roles` is **derived**: an object in `roles/shared/` appears in every node's composition →
`node_roles` = every role the manifest declares; an object under `roles/ops/` appears only in the ops
diff --git a/posthog/clickhouse/hcl/golden/dev/ai_events.hcl b/posthog/clickhouse/hcl/golden/dev/ai_events.hcl
index b4ebccf8e54b..a812017224ac 100644
--- a/posthog/clickhouse/hcl/golden/dev/ai_events.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/ai_events.hcl
@@ -1188,6 +1188,82 @@ SQL
}
}
+ view "custom_metrics" {
+ query = <<SQL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ )
+SQL
+
+ }
+
view "custom_metrics_backups" {
query = <<SQL
WITH
diff --git a/posthog/clickhouse/hcl/golden/dev/auxiliary.hcl b/posthog/clickhouse/hcl/golden/dev/auxiliary.hcl
index de920a8457be..49d59398cb6a 100644
--- a/posthog/clickhouse/hcl/golden/dev/auxiliary.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/auxiliary.hcl
@@ -3832,6 +3832,82 @@ SQL
}
}
+ view "custom_metrics" {
+ query = <<SQL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ )
+SQL
+
+ }
+
view "custom_metrics_backups" {
query = <<SQL
WITH
diff --git a/posthog/clickhouse/hcl/golden/dev/batch_exports.hcl b/posthog/clickhouse/hcl/golden/dev/batch_exports.hcl
index 942eb27e2aeb..20fdddfc0c3f 100644
--- a/posthog/clickhouse/hcl/golden/dev/batch_exports.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/batch_exports.hcl
@@ -676,6 +676,82 @@ SQL
}
}
+ view "custom_metrics" {
+ query = <<SQL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ )
+SQL
+
+ }
+
view "custom_metrics_backups" {
query = <<SQL
WITH
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index 9ddb6c28fd68..2b23d54e055e 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -122,6 +122,85 @@ database "posthog" {
group_name = "kafka_group_name = 'clickhouse-metrics-avro-new'"
format = "kafka_format = 'Avro'"
num_consumers = 8
+ max_block_size = 65536
+ skip_broken_messages = 100
+ poll_timeout_ms = 3000
+ poll_max_batch_size = 65536
+ flush_interval_ms = 7500
+ thread_per_consumer = true
+ }
+ }
+
+ table "kafka_trace_spans_avro" {
+ column "uuid" {
+ type = "String"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "String"
+ }
+ column "kind" {
+ type = "Int32"
+ }
+ column "flags" {
+ type = "Int32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "dropped_attributes_count" {
+ type = "Int32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "dropped_events_count" {
+ type = "Int32"
+ }
+ column "links" {
+ type = "Array(String)"
+ }
+ column "dropped_links_count" {
+ type = "Int32"
+ }
+ column "status_code" {
+ type = "Int32"
+ }
+ engine "kafka" {
+ broker_list = "warpstream_traces"
+ topic_list = "kafka_topic_list = 'clickhouse_traces'"
+ group_name = "kafka_group_name = 'clickhouse-traces-avro'"
+ format = "kafka_format = 'Avro'"
+ num_consumers = 8
skip_broken_messages = 100
poll_timeout_ms = 3000
poll_max_batch_size = 1000
@@ -462,6 +541,21 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, severity_text, resource_fingerprint
+SQL
+
+ }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/logs/{shard}/posthog.logs34"
replica_name = "{replica}"
@@ -1242,6 +1336,25 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ metric_name,
+ metric_type,
+ resource_fingerprint,
+ count() AS event_count,
+ sum(value) AS total_value,
+ min(value) AS min_value,
+ max(value) AS max_value
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint
+SQL
+
+ }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/logs/{shard}/posthog.metrics1"
replica_name = "{replica}"
@@ -1629,555 +1742,1540 @@ database "posthog" {
}
}
- table "writable_query_log_archive" {
- column "hostname" {
+ table "trace_attributes" {
+ order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
type = "LowCardinality(String)"
}
- column "user" {
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
+ }
+ column "attribute_key" {
type = "LowCardinality(String)"
}
- column "query_id" {
+ column "attribute_value" {
type = "String"
}
- column "initial_query_id" {
- type = "String"
+ column "attribute_type" {
+ type = "LowCardinality(String)"
}
- column "is_initial_query" {
- type = "UInt8"
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
}
- column "type" {
- type = "Enum8('QueryStart'=1, 'QueryFinish'=2, 'ExceptionBeforeStart'=3, 'ExceptionWhileProcessing'=4)"
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 4
}
- column "event_date" {
- type = "Date"
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 4
}
- column "event_time" {
- type = "DateTime"
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
}
- column "event_time_microseconds" {
- type = "DateTime64(6)"
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
}
- column "query_start_time" {
- type = "DateTime"
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_attributes"
+ replica_name = "{replica}-{shard}"
}
- column "query_start_time_microseconds" {
- type = "DateTime64(6)"
+ }
+
+ table "trace_attributes2" {
+ order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
}
- column "query_duration_ms" {
- type = "UInt64"
+ column "team_id" {
+ type = "Int32"
}
- column "read_rows" {
- type = "UInt64"
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
}
- column "read_bytes" {
- type = "UInt64"
+ column "time_bucket" {
+ type = "DateTime64(0)"
}
- column "written_rows" {
- type = "UInt64"
+ column "service_name" {
+ type = "LowCardinality(String)"
}
- column "written_bytes" {
- type = "UInt64"
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
}
- column "result_rows" {
- type = "UInt64"
+ column "attribute_key" {
+ type = "LowCardinality(String)"
}
- column "result_bytes" {
- type = "UInt64"
+ column "attribute_value" {
+ type = "String"
}
- column "memory_usage" {
- type = "UInt64"
+ column "attribute_type" {
+ type = "LowCardinality(String)"
}
- column "peak_threads_usage" {
- type = "UInt64"
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
}
- column "current_database" {
- type = "LowCardinality(String)"
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 4
}
- column "query" {
- type = "String"
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 4
}
- column "formatted_query" {
- type = "String"
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
}
- column "normalized_query_hash" {
- type = "UInt64"
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
}
- column "query_kind" {
- type = "LowCardinality(String)"
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_attributes2"
+ replica_name = "{replica}-{shard}"
}
- column "exception_code" {
+ }
+
+ table "trace_attributes_distributed" {
+ column "team_id" {
type = "Int32"
}
- column "exception" {
- type = "String"
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
}
- column "stack_trace" {
- type = "String"
+ column "time_bucket" {
+ type = "DateTime64(0)"
}
- column "team_id" {
- type = "Int64"
+ column "service_name" {
+ type = "LowCardinality(String)"
}
- column "log_comment" {
- type = "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)"
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
}
- column "ProfileEvents" {
- type = "Map(String, UInt64)"
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
}
engine "distributed" {
- cluster_name = "ops"
+ cluster_name = "logs"
remote_database = "posthog"
- remote_table = "query_log_archive_buffer"
+ remote_table = "trace_attributes"
}
}
- materialized_view "kafka_logs34_avro_mv" {
- to_table = "posthog.logs34"
- query = <<SQL
-SELECT
- uuid,
- trace_id,
- span_id,
- trace_flags,
- timestamp,
- observed_timestamp,
- body,
- severity_text,
- severity_number,
- service_name,
- instrumentation_scope,
- event_name,
- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
- observed_timestamp
- + toIntervalDay(
- toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
- ) AS original_expiry_timestamp,
- _partition,
- _topic,
- _offset,
- toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,
- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,
- toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed
-FROM posthog.kafka_logs_avro
-SQL
-
+ table "trace_spans" {
+ order_by = ["team_id", "time_bucket", "service_name", "resource_fingerprint", "status_code", "name", "timestamp"]
+ partition_by = "toDate(original_expiry_timestamp)"
+ ttl = "original_expiry_timestamp"
+ settings = {
+ allow_part_offset_column_in_projections = "1"
+ index_granularity = "8192"
+ index_granularity_bytes = "104857600"
+ map_serialization_version = "with_buckets"
+ ttl_only_drop_parts = "1"
+ }
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfInterval(timestamp, toIntervalHour(4))"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
column "uuid" {
type = "String"
}
+ column "team_id" {
+ type = "Int32"
+ }
column "trace_id" {
type = "String"
}
column "span_id" {
type = "String"
}
- column "trace_flags" {
- type = "Int32"
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "is_root_span" {
+ type = "Bool"
+ materialized = "replaceAll(trimRight(parent_span_id, '='), 'A', '') = ''"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "LowCardinality(String)"
+ }
+ column "kind" {
+ type = "Int8"
+ }
+ column "flags" {
+ type = "UInt32"
}
column "timestamp" {
type = "DateTime64(6)"
}
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
column "observed_timestamp" {
type = "DateTime64(6)"
}
- column "body" {
- type = "String"
+ column "created_at" {
+ type = "DateTime64(6)"
+ materialized = "now()"
}
- column "severity_text" {
- type = "String"
+ column "duration_nano" {
+ type = "UInt64"
+ materialized = "toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000"
}
- column "severity_number" {
- type = "Int32"
+ column "status_code" {
+ type = "Int16"
}
column "service_name" {
- type = "String"
+ type = "LowCardinality(String)"
}
- column "instrumentation_scope" {
- type = "String"
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
}
- column "event_name" {
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
+ }
+ column "instrumentation_scope" {
type = "String"
}
column "attributes_map_str" {
- type = "Map(String, String)"
+ type = "Map(LowCardinality(String), String)"
}
- column "resource_attributes" {
- type = "Map(String, String)"
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"
}
- column "team_id" {
- type = "Int32"
+ 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 "original_expiry_timestamp" {
- type = "DateTime64(6)"
+ column "attributes_map_datetime" {
+ type = "Map(LowCardinality(String), DateTime64(6))"
+ materialized = "mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str))"
+ }
+ column "dropped_attributes_count" {
+ type = "UInt32"
+ }
+ column "dropped_events_count" {
+ type = "UInt32"
+ }
+ column "dropped_links_count" {
+ type = "UInt32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "links" {
+ type = "Array(String)"
}
column "_partition" {
- type = "UInt64"
+ type = "UInt32"
}
column "_topic" {
- type = "LowCardinality(String)"
+ type = "String"
}
column "_offset" {
type = "UInt64"
}
- column "_record_count" {
- type = "Int64"
- }
column "_bytes_uncompressed" {
- type = "Nullable(Int64)"
+ type = "UInt64"
}
column "_bytes_compressed" {
- type = "Nullable(Int64)"
+ type = "UInt64"
}
- }
+ column "_record_count" {
+ type = "UInt64"
+ }
+ index "idx_name" {
+ expr = "name"
+ type = "ngrambf_v1(4, 5000, 2, 0)"
+ granularity = 16
+ }
+ index "idx_kind" {
+ expr = "kind"
+ type = "minmax"
+ granularity = 4
+ }
+ index "idx_duration" {
+ expr = "duration_nano"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_status_code" {
+ expr = "status_code"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_timestamp_minmax" {
+ expr = "timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_observed_minmax" {
+ expr = "observed_timestamp"
+ type = "minmax"
+ granularity = 1
+ }
+ index "idx_attributes_str_keys" {
+ expr = "mapKeys(attributes_map_str)"
+ type = "bloom_filter(0.01)"
+ granularity = 16
+ }
+ index "idx_attributes_str_values" {
+ expr = "mapValues(attributes_map_str)"
+ type = "bloom_filter(0.001)"
+ granularity = 16
+ }
+ index "idx_trace_bloom_part" {
+ expr = "trace_id"
+ type = "bloom_filter(0.00001)"
+ granularity = 99999
+ }
+ index "idx_span_id_bloom_part" {
+ expr = "span_id"
+ type = "bloom_filter(0.00001)"
+ granularity = 99999
+ }
+ projection "projection_index_span_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY span_id
+SQL
- materialized_view "kafka_logs_avro_billing_metrics_mv" {
- to_table = "posthog.logs_billing_metrics"
- query = <<SQL
+ }
+ projection "projection_aggregate_counts" {
+ query = <<SQL
SELECT
team_id,
time_bucket,
+ toStartOfMinute(timestamp),
service_name,
- sumSimpleState(floor(_bytes_uncompressed / _record_count)) AS bytes_uncompressed,
- sumSimpleState(floor(_bytes_compressed / _record_count)) AS bytes_compressed,
- sumSimpleState(1) AS record_count
-FROM
- (
- SELECT
- team_id,
- toStartOfInterval(timestamp, toIntervalMinute(1)) AS time_bucket,
- service_name AS service_name,
- _record_count,
- _bytes_uncompressed,
- _bytes_compressed
- FROM posthog.logs34
- )
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
GROUP BY
- team_id, time_bucket, service_name
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
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)"
+ projection "projection_index_trace_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY trace_id
+SQL
+
}
- column "record_count" {
- type = "SimpleAggregateFunction(sum, UInt64)"
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans"
+ replica_name = "{replica}-{shard}"
}
}
- 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"
+ table "trace_spans_distributed" {
+ column "time_bucket" {
+ type = "DateTime"
+ materialized = "toStartOfInterval(timestamp, toIntervalHour(4))"
}
- column "_topic" {
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "uuid" {
type = "String"
}
- column "max_offset" {
- type = "SimpleAggregateFunction(max, UInt64)"
+ column "team_id" {
+ type = "Int32"
}
- column "max_observed_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(6))"
+ column "trace_id" {
+ type = "String"
}
- column "max_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(6))"
+ column "span_id" {
+ type = "String"
}
- column "max_created_at" {
- type = "SimpleAggregateFunction(max, DateTime)"
+ column "parent_span_id" {
+ type = "String"
}
- column "max_lag" {
- type = "SimpleAggregateFunction(max, Decimal(18, 6))"
+ column "is_root_span" {
+ type = "Bool"
+ materialized = "replaceAll(trimRight(parent_span_id, '='), 'A', '') = ''"
}
- }
-
- materialized_view "kafka_metrics_avro_kafka_metrics_mv" {
- to_table = "posthog.metrics_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.kafka_metrics_avro
-GROUP BY
- _partition, _topic
-SQL
-
- column "_partition" {
- type = "UInt64"
+ column "trace_state" {
+ type = "String"
}
- column "_topic" {
+ column "name" {
type = "LowCardinality(String)"
}
- column "max_offset" {
- type = "SimpleAggregateFunction(max, UInt64)"
+ column "kind" {
+ type = "Int8"
}
- column "max_observed_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(6))"
+ column "flags" {
+ type = "UInt32"
}
- column "max_timestamp" {
- type = "SimpleAggregateFunction(max, DateTime64(6))"
+ column "timestamp" {
+ type = "DateTime64(6)"
}
- column "max_created_at" {
- type = "SimpleAggregateFunction(max, DateTime)"
+ column "end_time" {
+ type = "DateTime64(6)"
}
- column "max_lag" {
- type = "SimpleAggregateFunction(max, Decimal(18, 6))"
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
}
- }
-
- materialized_view "kafka_metrics_avro_mv" {
- to_table = "posthog.metrics1"
- query = <<SQL
-SELECT
- uuid,
- trace_id,
- span_id,
- ifNull(trace_flags, 0) AS trace_flags,
- timestamp,
- observed_timestamp,
- ifNull(service_name, '') AS service_name,
- ifNull(metric_name, '') AS metric_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,
- ifNull(unit, '') AS unit,
- ifNull(aggregation_temporality, '') AS aggregation_temporality,
- ifNull(is_monotonic, 0) AS is_monotonic,
- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
- ifNull(instrumentation_scope, '') AS instrumentation_scope,
- mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
- mapSort(
- mapFilter(
- (k, v) -> isNotNull(v),
- mapApply(
- (k, v) -> (concat(k, '__float'), toFloat64OrNull(JSONExtract(v, 'String'))),
- attributes
- )
- )
- ) AS attributes_map_float,
- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id
-FROM posthog.kafka_metrics_avro
-SETTINGS
- min_insert_block_size_rows = 0,
- min_insert_block_size_bytes = 0
-SQL
-
- column "uuid" {
- type = "String"
+ column "created_at" {
+ type = "DateTime64(6)"
+ materialized = "now()"
}
- column "trace_id" {
- type = "String"
+ column "duration_nano" {
+ type = "UInt64"
+ materialized = "toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000"
}
- column "span_id" {
- type = "String"
+ column "status_code" {
+ type = "Int16"
}
- column "trace_flags" {
- type = "Int32"
+ column "service_name" {
+ type = "LowCardinality(String)"
}
- column "timestamp" {
- type = "DateTime64(6)"
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
}
- column "observed_timestamp" {
- type = "DateTime64(6)"
+ column "resource_fingerprint" {
+ type = "UInt64"
+ materialized = "cityHash64(resource_attributes)"
}
- column "service_name" {
+ column "instrumentation_scope" {
type = "String"
}
- column "metric_name" {
- type = "String"
+ column "attributes_map_str" {
+ type = "Map(LowCardinality(String), String)"
}
- column "metric_type" {
- type = "String"
+ column "attributes" {
+ type = "Map(LowCardinality(String), String)"
+ alias = "mapApply((k, v) -> (left(k, -5), v), attributes_map_str)"
}
- column "value" {
- type = "Float64"
+ 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 "count" {
- type = "UInt64"
+ 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 "histogram_bounds" {
- type = "Array(Float64)"
+ column "dropped_attributes_count" {
+ type = "UInt32"
}
- column "histogram_counts" {
- type = "Array(UInt64)"
+ column "dropped_events_count" {
+ type = "UInt32"
}
- column "unit" {
- type = "String"
+ column "dropped_links_count" {
+ type = "UInt32"
}
- column "aggregation_temporality" {
- type = "String"
+ column "events" {
+ type = "Array(String)"
}
- column "is_monotonic" {
- type = "UInt8"
+ column "links" {
+ type = "Array(String)"
}
- column "resource_attributes" {
- type = "Map(String, String)"
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "trace_spans"
}
- column "instrumentation_scope" {
+ }
+
+ table "trace_spans_kafka_metrics" {
+ order_by = ["_topic", "_partition"]
+ settings = {
+ index_granularity = "8192"
+ }
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
type = "String"
}
- column "attributes_map_str" {
- type = "Map(String, String)"
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
}
- column "attributes_map_float" {
- type = "Map(String, Nullable(Float64))"
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
}
- column "team_id" {
- type = "Int32"
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans_kafka_metrics"
+ replica_name = "{replica}-{shard}"
}
}
- materialized_view "kafka_metrics_avro_to_metric_samples" {
- to_table = "posthog.metric_samples1"
- query = <<SQL
-SELECT
- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
- ifNull(metric_name, '') AS metric_name,
- reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,
- timestamp,
- 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
-FROM posthog.kafka_metrics_avro
-WHERE kafka_metrics_avro.series_fingerprint IS NOT NULL
-SETTINGS
- min_insert_block_size_rows = 0,
- min_insert_block_size_bytes = 0
-SQL
-
- column "team_id" {
- type = "Int32"
+ table "writable_query_log_archive" {
+ column "hostname" {
+ type = "LowCardinality(String)"
}
- column "metric_name" {
+ column "user" {
+ type = "LowCardinality(String)"
+ }
+ column "query_id" {
type = "String"
}
- column "series_fingerprint" {
- type = "UInt64"
+ column "initial_query_id" {
+ type = "String"
}
- column "timestamp" {
+ 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 "value" {
- type = "Float64"
+ column "query_start_time" {
+ type = "DateTime"
}
- column "count" {
+ column "query_start_time_microseconds" {
+ type = "DateTime64(6)"
+ }
+ column "query_duration_ms" {
type = "UInt64"
}
- column "histogram_bounds" {
- type = "Array(Float64)"
+ column "read_rows" {
+ type = "UInt64"
}
- column "histogram_counts" {
- type = "Array(UInt64)"
+ column "read_bytes" {
+ type = "UInt64"
}
- column "trace_id" {
+ 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"
}
- column "span_id" {
+ column "formatted_query" {
type = "String"
}
- column "trace_flags" {
+ column "normalized_query_hash" {
+ type = "UInt64"
+ }
+ column "query_kind" {
+ type = "LowCardinality(String)"
+ }
+ column "exception_code" {
type = "Int32"
}
+ column "exception" {
+ type = "String"
+ }
+ column "stack_trace" {
+ type = "String"
+ }
+ column "team_id" {
+ type = "Int64"
+ }
+ column "log_comment" {
+ type = "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)"
+ }
+ column "ProfileEvents" {
+ type = "Map(String, UInt64)"
+ }
+ engine "distributed" {
+ cluster_name = "ops"
+ remote_database = "posthog"
+ remote_table = "query_log_archive_buffer"
+ }
}
- materialized_view "kafka_metrics_avro_to_metric_series" {
- to_table = "posthog.metric_series1"
+ materialized_view "kafka_logs34_avro_mv" {
+ to_table = "posthog.logs34"
+ query = <<SQL
+SELECT
+ uuid,
+ trace_id,
+ span_id,
+ trace_flags,
+ timestamp,
+ observed_timestamp,
+ body,
+ severity_text,
+ severity_number,
+ service_name,
+ instrumentation_scope,
+ event_name,
+ mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
+ observed_timestamp
+ + toIntervalDay(
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ ) AS original_expiry_timestamp,
+ _partition,
+ _topic,
+ _offset,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,
+ toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_uncompressed')]) / _record_count AS _bytes_uncompressed,
+ toInt64OrNull(_headers.value[indexOf(_headers.name, 'bytes_compressed')]) / _record_count AS _bytes_compressed
+FROM posthog.kafka_logs_avro
+SQL
+
+ column "uuid" {
+ type = "String"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "trace_flags" {
+ type = "Int32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "body" {
+ type = "String"
+ }
+ column "severity_text" {
+ type = "String"
+ }
+ column "severity_number" {
+ type = "Int32"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "event_name" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(String, String)"
+ }
+ column "resource_attributes" {
+ type = "Map(String, String)"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "_partition" {
+ type = "UInt64"
+ }
+ column "_topic" {
+ type = "LowCardinality(String)"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_record_count" {
+ type = "Int64"
+ }
+ column "_bytes_uncompressed" {
+ type = "Nullable(Float64)"
+ }
+ column "_bytes_compressed" {
+ type = "Nullable(Float64)"
+ }
+ }
+
+ materialized_view "kafka_logs_avro_billing_metrics_mv" {
+ to_table = "posthog.logs_billing_metrics"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ sumSimpleState(floor(_bytes_uncompressed / _record_count)) AS bytes_uncompressed,
+ sumSimpleState(floor(_bytes_compressed / _record_count)) AS bytes_compressed,
+ sumSimpleState(1) AS record_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalMinute(1)) AS time_bucket,
+ service_name AS service_name,
+ _record_count,
+ _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_avro_kafka_metrics_mv" {
+ to_table = "posthog.metrics_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.kafka_metrics_avro
+GROUP BY
+ _partition, _topic
+SQL
+
+ column "_partition" {
+ type = "UInt64"
+ }
+ column "_topic" {
+ type = "LowCardinality(String)"
+ }
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime)"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, Decimal(18, 6))"
+ }
+ }
+
+ materialized_view "kafka_metrics_avro_mv" {
+ to_table = "posthog.metrics1"
+ query = <<SQL
+SELECT
+ uuid,
+ trace_id,
+ span_id,
+ ifNull(trace_flags, 0) AS trace_flags,
+ timestamp,
+ observed_timestamp,
+ ifNull(service_name, '') AS service_name,
+ ifNull(metric_name, '') AS metric_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,
+ ifNull(unit, '') AS unit,
+ ifNull(aggregation_temporality, '') AS aggregation_temporality,
+ ifNull(is_monotonic, 0) AS is_monotonic,
+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
+ ifNull(instrumentation_scope, '') AS instrumentation_scope,
+ mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
+ mapSort(
+ mapFilter(
+ (k, v) -> isNotNull(v),
+ mapApply(
+ (k, v) -> (concat(k, '__float'), toFloat64OrNull(JSONExtract(v, 'String'))),
+ attributes
+ )
+ )
+ ) AS attributes_map_float,
+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id
+FROM posthog.kafka_metrics_avro
+SETTINGS
+ min_insert_block_size_rows = 0,
+ min_insert_block_size_bytes = 0
+SQL
+
+ column "uuid" {
+ type = "String"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "trace_flags" {
+ type = "Int32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "metric_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 "unit" {
+ type = "String"
+ }
+ column "aggregation_temporality" {
+ type = "String"
+ }
+ column "is_monotonic" {
+ type = "UInt8"
+ }
+ column "resource_attributes" {
+ type = "Map(String, String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(String, String)"
+ }
+ column "attributes_map_float" {
+ type = "Map(String, Nullable(Float64))"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ }
+
+ materialized_view "kafka_metrics_avro_to_metric_samples" {
+ to_table = "posthog.metric_samples1"
+ query = <<SQL
+SELECT
+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
+ ifNull(metric_name, '') AS metric_name,
+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,
+ timestamp,
+ 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
+FROM posthog.kafka_metrics_avro
+WHERE kafka_metrics_avro.series_fingerprint IS NOT NULL
+SETTINGS
+ min_insert_block_size_rows = 0,
+ min_insert_block_size_bytes = 0
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "String"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ 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"
+ }
+ }
+
+ materialized_view "kafka_metrics_avro_to_metric_series" {
+ to_table = "posthog.metric_series1"
+ query = <<SQL
+SELECT
+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
+ ifNull(metric_name, '') AS metric_name,
+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,
+ ifNull(metric_type, '') AS metric_type,
+ ifNull(unit, '') AS unit,
+ ifNull(aggregation_temporality, '') AS aggregation_temporality,
+ ifNull(is_monotonic, 0) AS is_monotonic,
+ ifNull(service_name, '') AS service_name,
+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)) AS attributes,
+ timestamp AS last_seen
+FROM posthog.kafka_metrics_avro
+WHERE kafka_metrics_avro.series_fingerprint IS NOT NULL
+SETTINGS
+ min_insert_block_size_rows = 0,
+ min_insert_block_size_bytes = 0
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "metric_name" {
+ type = "String"
+ }
+ column "series_fingerprint" {
+ type = "UInt64"
+ }
+ column "metric_type" {
+ type = "String"
+ }
+ column "unit" {
+ type = "String"
+ }
+ column "aggregation_temporality" {
+ type = "String"
+ }
+ column "is_monotonic" {
+ type = "UInt8"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(String, String)"
+ }
+ column "attributes" {
+ type = "Map(String, String)"
+ }
+ column "last_seen" {
+ type = "DateTime64(6)"
+ }
+ }
+
+ materialized_view "kafka_trace_spans_avro_mv" {
+ to_table = "posthog.trace_spans"
+ query = <<SQL
+SELECT
+ * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ toInt8(kind) AS kind,
+ toUInt32(flags) AS flags,
+ toUInt32(dropped_attributes_count) AS dropped_attributes_count,
+ toUInt32(dropped_events_count) AS dropped_events_count,
+ toUInt32(dropped_links_count) AS dropped_links_count,
+ toInt16(status_code) AS status_code,
+ mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
+ observed_timestamp
+ + toIntervalDay(
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ ) AS original_expiry_timestamp,
+ _partition,
+ _topic,
+ _offset,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,
+ 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
+SQL
+
+ column "uuid" {
+ type = "String"
+ }
+ column "trace_id" {
+ type = "String"
+ }
+ column "span_id" {
+ type = "String"
+ }
+ column "parent_span_id" {
+ type = "String"
+ }
+ column "trace_state" {
+ type = "String"
+ }
+ column "name" {
+ type = "String"
+ }
+ column "kind" {
+ type = "Int8"
+ }
+ column "flags" {
+ type = "UInt32"
+ }
+ column "timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "end_time" {
+ type = "DateTime64(6)"
+ }
+ column "observed_timestamp" {
+ type = "DateTime64(6)"
+ }
+ column "service_name" {
+ type = "String"
+ }
+ column "resource_attributes" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "instrumentation_scope" {
+ type = "String"
+ }
+ column "attributes_map_str" {
+ type = "Map(LowCardinality(String), String)"
+ }
+ column "dropped_attributes_count" {
+ type = "UInt32"
+ }
+ column "events" {
+ type = "Array(String)"
+ }
+ column "dropped_events_count" {
+ type = "UInt32"
+ }
+ column "links" {
+ type = "Array(String)"
+ }
+ column "dropped_links_count" {
+ type = "UInt32"
+ }
+ column "status_code" {
+ type = "Int16"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
+ }
+
+ materialized_view "logs34_to_log_attributes" {
+ to_table = "posthog.log_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS attributes,
+ arrayJoin(attributes) AS attribute,
+ 'log' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.logs34
+ GROUP BY
+ team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "logs34_to_log_attributes3" {
+ to_table = "posthog.log_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ severity_text,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ severity_text AS severity_text,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS attributes,
+ arrayJoin(attributes) AS attribute,
+ 'log' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.logs34
+ GROUP BY
+ team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "severity_text" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "logs34_to_resource_attributes" {
+ to_table = "posthog.log_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.logs34
+ GROUP BY
+ team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, resource_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "logs34_to_resource_attributes3" {
+ to_table = "posthog.log_attributes3"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ original_expiry_time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ severity_text,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ severity_text AS severity_text,
+ arrayJoin(resource_attributes) AS attribute,
+ 'resource' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.logs34
+ GROUP BY
+ team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, resource_attributes
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "severity_text" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "logs34_to_volume_buckets" {
+ to_table = "posthog.logs_volume_buckets"
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ service_name,
+ namespace,
+ environment,
+ severity_text,
+ sumSimpleState(1) AS log_count
+FROM
+ (
+ SELECT
+ team_id,
+ toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
+ service_name,
+ if(
+ (resource_attributes['k8s.namespace.name']) != '',
+ resource_attributes['k8s.namespace.name'],
+ resource_attributes['service.namespace']
+ ) AS namespace,
+ if(
+ (resource_attributes['deployment.environment.name']) != '',
+ resource_attributes['deployment.environment.name'],
+ if(
+ (resource_attributes['deployment.environment']) != '',
+ resource_attributes['deployment.environment'],
+ resource_attributes['env']
+ )
+ ) AS environment,
+ lower(severity_text) AS severity_text
+ FROM posthog.logs34
+ )
+GROUP BY
+ team_id, time_bucket, service_name, namespace, environment, severity_text
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "time_bucket" {
+ type = "DateTime('UTC')"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "namespace" {
+ type = "LowCardinality(String)"
+ }
+ column "environment" {
+ type = "LowCardinality(String)"
+ }
+ column "severity_text" {
+ type = "LowCardinality(String)"
+ }
+ column "log_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "metrics1_to_metric_attributes" {
+ to_table = "posthog.metric_attributes"
query = <<SQL
SELECT
- toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
- ifNull(metric_name, '') AS metric_name,
- reinterpretAsUInt64(assumeNotNull(series_fingerprint)) AS series_fingerprint,
- ifNull(metric_type, '') AS metric_type,
- ifNull(unit, '') AS unit,
- ifNull(aggregation_temporality, '') AS aggregation_temporality,
- ifNull(is_monotonic, 0) AS is_monotonic,
- ifNull(service_name, '') AS service_name,
- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes)) AS attributes,
- timestamp AS last_seen
-FROM posthog.kafka_metrics_avro
-WHERE kafka_metrics_avro.series_fingerprint IS NOT NULL
-SETTINGS
- min_insert_block_size_rows = 0,
- min_insert_block_size_bytes = 0
+ team_id,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS attributes,
+ arrayJoin(attributes) AS attribute,
+ 'metric' AS attribute_type,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.metrics1
+ GROUP BY
+ team_id, time_bucket, service_name, resource_fingerprint, attributes
+ )
SQL
column "team_id" {
type = "Int32"
}
- column "metric_name" {
- type = "String"
- }
- column "series_fingerprint" {
- type = "UInt64"
- }
- column "metric_type" {
- type = "String"
+ column "time_bucket" {
+ type = "DateTime64(0)"
}
- column "unit" {
- type = "String"
+ column "service_name" {
+ type = "LowCardinality(String)"
}
- column "aggregation_temporality" {
- type = "String"
+ column "resource_fingerprint" {
+ type = "UInt64"
}
- column "is_monotonic" {
- type = "UInt8"
+ column "attribute_key" {
+ type = "LowCardinality(String)"
}
- column "service_name" {
+ column "attribute_value" {
type = "String"
}
- column "resource_attributes" {
- type = "Map(String, String)"
- }
- column "attributes" {
- type = "Map(String, String)"
+ column "attribute_type" {
+ type = "LowCardinality(String)"
}
- column "last_seen" {
- type = "DateTime64(6)"
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
}
}
- materialized_view "logs34_to_log_attributes" {
- to_table = "posthog.log_attributes2"
+ materialized_view "metrics1_to_resource_attributes" {
+ to_table = "posthog.metric_attributes"
query = <<SQL
SELECT
team_id,
time_bucket,
- original_expiry_time_bucket,
service_name,
resource_fingerprint,
attribute_key,
@@ -2189,18 +3287,16 @@ FROM
SELECT
team_id AS team_id,
toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
- toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
service_name AS service_name,
resource_fingerprint,
- mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS attributes,
- arrayJoin(attributes) AS attribute,
- 'log' AS attribute_type,
+ arrayJoin(resource_attributes) AS attribute,
+ 'resource' AS attribute_type,
attribute.1 AS attribute_key,
attribute.2 AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.logs34
+ FROM posthog.metrics1
GROUP BY
- team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, attributes
+ team_id, time_bucket, service_name, resource_fingerprint, resource_attributes
)
SQL
@@ -2210,9 +3306,6 @@ SQL
column "time_bucket" {
type = "DateTime64(0)"
}
- column "original_expiry_time_bucket" {
- type = "DateTime64(0)"
- }
column "service_name" {
type = "LowCardinality(String)"
}
@@ -2233,48 +3326,178 @@ SQL
}
}
- materialized_view "logs34_to_log_attributes3" {
- to_table = "posthog.log_attributes3"
+ materialized_view "ops_query_log_archive_mv" {
+ to_table = "posthog.writable_query_log_archive"
+ query = <<SQL
+SELECT
+ hostname,
+ user,
+ query_id,
+ initial_query_id,
+ is_initial_query,
+ type,
+ event_date,
+ event_time,
+ event_time_microseconds,
+ query_start_time,
+ query_start_time_microseconds,
+ query_duration_ms,
+ read_rows,
+ read_bytes,
+ written_rows,
+ written_bytes,
+ result_rows,
+ result_bytes,
+ memory_usage,
+ peak_threads_usage,
+ current_database,
+ query,
+ formatted_query,
+ normalized_query_hash,
+ query_kind,
+ exception_code,
+ exception,
+ stack_trace,
+ JSONExtractInt(log_comment, 'team_id') AS team_id,
+ if(isValidJSON(log_comment), log_comment, '{}') AS log_comment,
+ ProfileEvents
+FROM system.query_log
+WHERE type != 'QueryStart'
+SQL
+
+ 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"
+ }
+ column "formatted_query" {
+ type = "String"
+ }
+ column "normalized_query_hash" {
+ type = "UInt64"
+ }
+ column "query_kind" {
+ type = "LowCardinality(String)"
+ }
+ column "exception_code" {
+ type = "Int32"
+ }
+ column "exception" {
+ type = "String"
+ }
+ column "stack_trace" {
+ type = "String"
+ }
+ column "team_id" {
+ type = "Int64"
+ }
+ column "log_comment" {
+ type = "String"
+ }
+ column "ProfileEvents" {
+ type = "Map(LowCardinality(String), UInt64)"
+ }
+ }
+
+ materialized_view "trace_span_to_attributes" {
+ to_table = "posthog.trace_attributes"
query = <<SQL
SELECT
team_id,
- time_bucket,
original_expiry_time_bucket,
+ time_bucket,
service_name,
resource_fingerprint,
attribute_key,
attribute_value,
- attribute_type,
- severity_text,
+ 'span_attribute' AS attribute_type,
attribute_count
FROM
(
SELECT
team_id AS team_id,
- toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
service_name AS service_name,
resource_fingerprint,
- severity_text AS severity_text,
- mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS attributes,
- arrayJoin(attributes) AS attribute,
- 'log' AS attribute_type,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
attribute.1 AS attribute_key,
attribute.2 AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.logs34
+ FROM posthog.trace_spans
GROUP BY
- team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, attributes
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
)
SQL
column "team_id" {
type = "Int32"
}
- column "time_bucket" {
+ column "original_expiry_time_bucket" {
type = "DateTime64(0)"
}
- column "original_expiry_time_bucket" {
+ column "time_bucket" {
type = "DateTime64(0)"
}
column "service_name" {
@@ -2292,53 +3515,49 @@ SQL
column "attribute_type" {
type = "LowCardinality(String)"
}
- column "severity_text" {
- type = "LowCardinality(String)"
- }
column "attribute_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
}
- materialized_view "logs34_to_resource_attributes" {
- to_table = "posthog.log_attributes2"
+ materialized_view "trace_span_to_attributes2" {
+ to_table = "posthog.trace_attributes2"
query = <<SQL
SELECT
team_id,
- time_bucket,
original_expiry_time_bucket,
+ time_bucket,
service_name,
resource_fingerprint,
attribute_key,
attribute_value,
- attribute_type,
+ 'span_attribute' AS attribute_type,
attribute_count
FROM
(
SELECT
team_id AS team_id,
- toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
service_name AS service_name,
resource_fingerprint,
- arrayJoin(resource_attributes) AS attribute,
- 'resource' AS attribute_type,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
attribute.1 AS attribute_key,
attribute.2 AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.logs34
+ FROM posthog.trace_spans
GROUP BY
- team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, resource_attributes
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
)
SQL
column "team_id" {
type = "Int32"
}
- column "time_bucket" {
+ column "original_expiry_time_bucket" {
type = "DateTime64(0)"
}
- column "original_expiry_time_bucket" {
+ column "time_bucket" {
type = "DateTime64(0)"
}
column "service_name" {
@@ -2361,47 +3580,44 @@ SQL
}
}
- materialized_view "logs34_to_resource_attributes3" {
- to_table = "posthog.log_attributes3"
+ materialized_view "trace_span_to_resource_attributes" {
+ to_table = "posthog.trace_attributes"
query = <<SQL
SELECT
team_id,
- time_bucket,
original_expiry_time_bucket,
+ time_bucket,
service_name,
resource_fingerprint,
attribute_key,
attribute_value,
- attribute_type,
- severity_text,
+ 'span_resource_attribute' AS attribute_type,
attribute_count
FROM
(
SELECT
team_id AS team_id,
- toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
service_name AS service_name,
resource_fingerprint,
- severity_text AS severity_text,
arrayJoin(resource_attributes) AS attribute,
- 'resource' AS attribute_type,
attribute.1 AS attribute_key,
attribute.2 AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.logs34
+ FROM posthog.trace_spans
GROUP BY
- team_id, time_bucket, original_expiry_time_bucket, service_name, resource_fingerprint, severity_text, resource_attributes
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
)
SQL
column "team_id" {
type = "Int32"
}
- column "time_bucket" {
+ column "original_expiry_time_bucket" {
type = "DateTime64(0)"
}
- column "original_expiry_time_bucket" {
+ column "time_bucket" {
type = "DateTime64(0)"
}
column "service_name" {
@@ -2419,109 +3635,107 @@ SQL
column "attribute_type" {
type = "LowCardinality(String)"
}
- column "severity_text" {
- type = "LowCardinality(String)"
- }
column "attribute_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
}
- materialized_view "logs34_to_volume_buckets" {
- to_table = "posthog.logs_volume_buckets"
+ materialized_view "trace_span_to_resource_attributes2" {
+ to_table = "posthog.trace_attributes2"
query = <<SQL
SELECT
team_id,
+ original_expiry_time_bucket,
time_bucket,
service_name,
- namespace,
- environment,
- severity_text,
- sumSimpleState(1) AS log_count
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_resource_attribute' AS attribute_type,
+ attribute_count
FROM
(
SELECT
- team_id,
- toStartOfInterval(timestamp, toIntervalSecond(300), 'UTC') AS time_bucket,
- service_name,
- if(
- (resource_attributes['k8s.namespace.name']) != '',
- resource_attributes['k8s.namespace.name'],
- resource_attributes['service.namespace']
- ) AS namespace,
- if(
- (resource_attributes['deployment.environment.name']) != '',
- resource_attributes['deployment.environment.name'],
- if(
- (resource_attributes['deployment.environment']) != '',
- resource_attributes['deployment.environment'],
- resource_attributes['env']
- )
- ) AS environment,
- lower(severity_text) AS severity_text
- FROM posthog.logs34
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
)
-GROUP BY
- team_id, time_bucket, service_name, namespace, environment, severity_text
SQL
column "team_id" {
type = "Int32"
}
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
column "time_bucket" {
- type = "DateTime('UTC')"
+ type = "DateTime64(0)"
}
column "service_name" {
type = "LowCardinality(String)"
}
- column "namespace" {
- type = "LowCardinality(String)"
+ column "resource_fingerprint" {
+ type = "UInt64"
}
- column "environment" {
+ column "attribute_key" {
type = "LowCardinality(String)"
}
- column "severity_text" {
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
type = "LowCardinality(String)"
}
- column "log_count" {
+ column "attribute_count" {
type = "SimpleAggregateFunction(sum, UInt64)"
}
}
- materialized_view "metrics1_to_metric_attributes" {
- to_table = "posthog.metric_attributes"
+ materialized_view "trace_span_to_span_attributes" {
+ to_table = "posthog.trace_attributes"
query = <<SQL
SELECT
team_id,
+ original_expiry_time_bucket,
time_bucket,
service_name,
resource_fingerprint,
attribute_key,
attribute_value,
- attribute_type,
+ 'span' AS attribute_type,
attribute_count
FROM
(
SELECT
team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
service_name AS service_name,
resource_fingerprint,
- mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes) AS attributes,
- arrayJoin(attributes) AS attribute,
- 'metric' AS attribute_type,
- attribute.1 AS attribute_key,
- attribute.2 AS attribute_value,
+ 'name' AS attribute_key,
+ name AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.metrics1
+ FROM posthog.trace_spans
GROUP BY
- team_id, time_bucket, service_name, resource_fingerprint, attributes
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
)
SQL
column "team_id" {
type = "Int32"
}
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
column "time_bucket" {
type = "DateTime64(0)"
}
@@ -2545,39 +3759,42 @@ SQL
}
}
- materialized_view "metrics1_to_resource_attributes" {
- to_table = "posthog.metric_attributes"
+ materialized_view "trace_span_to_span_attributes2" {
+ to_table = "posthog.trace_attributes2"
query = <<SQL
SELECT
team_id,
+ original_expiry_time_bucket,
time_bucket,
service_name,
resource_fingerprint,
attribute_key,
attribute_value,
- attribute_type,
+ 'span' AS attribute_type,
attribute_count
FROM
(
SELECT
team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
service_name AS service_name,
resource_fingerprint,
- arrayJoin(resource_attributes) AS attribute,
- 'resource' AS attribute_type,
- attribute.1 AS attribute_key,
- attribute.2 AS attribute_value,
+ 'name' AS attribute_key,
+ name AS attribute_value,
sumSimpleState(1) AS attribute_count
- FROM posthog.metrics1
+ FROM posthog.trace_spans
GROUP BY
- team_id, time_bucket, service_name, resource_fingerprint, resource_attributes
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
)
SQL
column "team_id" {
type = "Int32"
}
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
column "time_bucket" {
type = "DateTime64(0)"
}
@@ -2601,137 +3818,42 @@ SQL
}
}
- materialized_view "ops_query_log_archive_mv" {
- to_table = "posthog.writable_query_log_archive"
+ materialized_view "trace_spans_to_kafka_metrics_mv" {
+ to_table = "posthog.trace_spans_kafka_metrics"
query = <<SQL
SELECT
- hostname,
- user,
- query_id,
- initial_query_id,
- is_initial_query,
- type,
- event_date,
- event_time,
- event_time_microseconds,
- query_start_time,
- query_start_time_microseconds,
- query_duration_ms,
- read_rows,
- read_bytes,
- written_rows,
- written_bytes,
- result_rows,
- result_bytes,
- memory_usage,
- peak_threads_usage,
- current_database,
- query,
- formatted_query,
- normalized_query_hash,
- query_kind,
- exception_code,
- exception,
- stack_trace,
- JSONExtractInt(log_comment, 'team_id') AS team_id,
- if(isValidJSON(log_comment), log_comment, '{}') AS log_comment,
- ProfileEvents
-FROM system.query_log
-WHERE type != 'QueryStart'
+ _partition,
+ _topic,
+ maxSimpleState(_offset) AS max_offset,
+ maxSimpleState(observed_timestamp) AS max_observed_timestamp,
+ maxSimpleState(timestamp) AS max_timestamp,
+ maxSimpleState(now()) AS max_created_at,
+ maxSimpleState(now() - observed_timestamp) AS max_lag
+FROM posthog.trace_spans
+GROUP BY
+ _partition, _topic
SQL
- column "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"
- }
- column "formatted_query" {
- type = "String"
- }
- column "normalized_query_hash" {
+ column "_partition" {
type = "UInt64"
}
- column "query_kind" {
+ column "_topic" {
type = "LowCardinality(String)"
}
- column "exception_code" {
- type = "Int32"
- }
- column "exception" {
- type = "String"
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
}
- column "stack_trace" {
- type = "String"
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
}
- column "team_id" {
- type = "Int64"
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
}
- column "log_comment" {
- type = "String"
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime)"
}
- column "ProfileEvents" {
- type = "Map(LowCardinality(String), UInt64)"
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, Decimal(18, 6))"
}
}
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index c11b66560dec..225bb2be799e 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -2080,6 +2080,13 @@ SQL
type = "bloom_filter(0.00001)"
granularity = 99999
}
+ projection "projection_index_span_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY span_id
+SQL
+
+ }
projection "projection_aggregate_counts" {
query = <<SQL
SELECT
@@ -2094,13 +2101,6 @@ GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
SQL
- }
- projection "projection_index_span_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY span_id
-SQL
-
}
projection "projection_index_trace_id" {
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index 812e7b8f5fa4..7f57240f1f46 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -13609,6 +13609,13 @@ SQL
type = "bloom_filter(0.00001)"
granularity = 99999
}
+ projection "projection_index_span_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY span_id
+SQL
+
+ }
projection "projection_aggregate_counts" {
query = <<SQL
SELECT
@@ -13623,13 +13630,6 @@ GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
SQL
- }
- projection "projection_index_span_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY span_id
-SQL
-
}
projection "projection_index_trace_id" {
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/ai_events.hcl b/posthog/clickhouse/hcl/golden/prod-eu/ai_events.hcl
index 0973b776f2fd..96281ceca5a0 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/ai_events.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/ai_events.hcl
@@ -1188,6 +1188,82 @@ SQL
}
}
+ view "custom_metrics" {
+ query = <<SQL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ )
+SQL
+
+ }
+
view "custom_metrics_backups" {
query = <<SQL
WITH
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hcl b/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hcl
index 3e9aa594d640..08e551859a56 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hcl
@@ -1814,6 +1814,42 @@ database "posthog" {
}
}
+ table "sharded_ingestion_warnings" {
+ order_by = ["team_id", "toHour(timestamp)", "type", "source", "timestamp"]
+ partition_by = "toYear(timestamp)"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int64"
+ }
+ column "source" {
+ type = "LowCardinality(String)"
+ }
+ column "type" {
+ type = "String"
+ }
+ column "details" {
+ type = "String"
+ }
+ column "timestamp" {
+ type = "DateTime64(6, 'UTC')"
+ }
+ column "_timestamp" {
+ type = "DateTime"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_partition" {
+ type = "UInt64"
+ }
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/reshard/{shard}/posthog.sharded_ingestion_warnings"
+ replica_name = "{replica}"
+ }
+ }
+
table "sharded_marketing_conversions_preaggregated" {
order_by = ["team_id", "job_id", "person_id", "conversion_timestamp"]
partition_by = "toYYYYMMDD(expires_at)"
@@ -3796,6 +3832,82 @@ SQL
}
}
+ view "custom_metrics" {
+ query = <<SQL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ )
+SQL
+
+ }
+
view "custom_metrics_backups" {
query = <<SQL
WITH
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/batch_exports.hcl b/posthog/clickhouse/hcl/golden/prod-eu/batch_exports.hcl
index eb66a83a48a9..ea5001e24229 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/batch_exports.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/batch_exports.hcl
@@ -684,6 +684,82 @@ SQL
}
}
+ view "custom_metrics" {
+ query = <<SQL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ )
+SQL
+
+ }
+
view "custom_metrics_backups" {
query = <<SQL
WITH
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index 1ec6e93e24cc..113b7a788021 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -216,6 +216,7 @@ database "posthog" {
settings = {
deduplicate_merge_projection_mode = "drop"
index_granularity = "8192"
+ ttl_only_drop_parts = "1"
}
column "team_id" {
type = "Int32"
@@ -281,6 +282,7 @@ database "posthog" {
settings = {
deduplicate_merge_projection_mode = "drop"
index_granularity = "8192"
+ ttl_only_drop_parts = "1"
}
column "team_id" {
type = "Int32"
@@ -540,6 +542,21 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, severity_text, resource_fingerprint
+SQL
+
+ }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/logs/{shard}/posthog.logs34"
replica_name = "{replica}"
@@ -1324,6 +1341,25 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ metric_name,
+ metric_type,
+ resource_fingerprint,
+ count() AS event_count,
+ sum(value) AS total_value,
+ min(value) AS min_value,
+ max(value) AS max_value
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint
+SQL
+
+ }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/logs/{shard}/posthog.metrics1"
replica_name = "{replica}"
@@ -1772,6 +1808,67 @@ database "posthog" {
}
}
+ table "trace_attributes2" {
+ order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_attributes2"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "trace_attributes_distributed" {
column "team_id" {
type = "Int32"
@@ -1988,6 +2085,52 @@ database "posthog" {
type = "bloom_filter(0.00001)"
granularity = 99999
}
+ projection "projection_index_span_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY span_id
+SQL
+
+ }
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint
+SQL
+
+ }
+ projection "projection_index_trace_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY trace_id
+SQL
+
+ settings = {
+ index_granularity = "512"
+ }
+ }
+ projection "projection_aggregate_counts2" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, is_root_span, resource_fingerprint
+SQL
+
+ }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/logs/{shard}/posthog.trace_spans"
replica_name = "{replica}"
@@ -2121,6 +2264,38 @@ database "posthog" {
}
}
+ table "trace_spans_kafka_metrics" {
+ order_by = ["_topic", "_partition"]
+ settings = {
+ index_granularity = "8192"
+ }
+ column "_partition" {
+ type = "UInt32"
+ }
+ column "_topic" {
+ type = "String"
+ }
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime64(9))"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans_kafka_metrics"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "writable_query_log_archive" {
column "hostname" {
type = "LowCardinality(String)"
@@ -2677,6 +2852,10 @@ SELECT
mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
+ observed_timestamp
+ + toIntervalDay(
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ ) AS original_expiry_timestamp,
_partition,
_topic,
_offset,
@@ -2752,6 +2931,9 @@ SQL
column "team_id" {
type = "Int32"
}
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
}
materialized_view "logs34_to_log_attributes" {
@@ -3378,6 +3560,66 @@ SQL
}
}
+ materialized_view "trace_span_to_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "trace_span_to_resource_attributes" {
to_table = "posthog.trace_attributes"
query = <<SQL
@@ -3438,6 +3680,66 @@ SQL
}
}
+ materialized_view "trace_span_to_resource_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_resource_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "trace_span_to_span_attributes" {
to_table = "posthog.trace_attributes"
query = <<SQL
@@ -3497,6 +3799,104 @@ SQL
}
}
+ materialized_view "trace_span_to_span_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
+ materialized_view "trace_spans_to_kafka_metrics_mv" {
+ to_table = "posthog.trace_spans_kafka_metrics"
+ query = <<SQL
+SELECT
+ _partition,
+ _topic,
+ maxSimpleState(_offset) AS max_offset,
+ maxSimpleState(observed_timestamp) AS max_observed_timestamp,
+ maxSimpleState(timestamp) AS max_timestamp,
+ maxSimpleState(now()) AS max_created_at,
+ maxSimpleState(now() - observed_timestamp) AS max_lag
+FROM posthog.trace_spans
+GROUP BY
+ _partition, _topic
+SQL
+
+ column "_partition" {
+ type = "UInt64"
+ }
+ column "_topic" {
+ type = "LowCardinality(String)"
+ }
+ column "max_offset" {
+ type = "SimpleAggregateFunction(max, UInt64)"
+ }
+ column "max_observed_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_timestamp" {
+ type = "SimpleAggregateFunction(max, DateTime64(6))"
+ }
+ column "max_created_at" {
+ type = "SimpleAggregateFunction(max, DateTime)"
+ }
+ column "max_lag" {
+ type = "SimpleAggregateFunction(max, Decimal(18, 6))"
+ }
+ }
+
view "custom_metrics" {
query = <<SQL
SELECT * REPLACE(toFloat64(value) AS value)
diff --git a/posthog/clickhouse/hcl/golden/prod-us/ai_events.hcl b/posthog/clickhouse/hcl/golden/prod-us/ai_events.hcl
index 0973b776f2fd..96281ceca5a0 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/ai_events.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/ai_events.hcl
@@ -1188,6 +1188,82 @@ SQL
}
}
+ view "custom_metrics" {
+ query = <<SQL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ )
+SQL
+
+ }
+
view "custom_metrics_backups" {
query = <<SQL
WITH
diff --git a/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hcl b/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hcl
index 78143edf66b6..cd3c6c862c07 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hcl
@@ -3870,6 +3870,82 @@ SQL
}
}
+ view "custom_metrics" {
+ query = <<SQL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ )
+SQL
+
+ }
+
view "custom_metrics_backups" {
query = <<SQL
WITH
diff --git a/posthog/clickhouse/hcl/golden/prod-us/batch_exports.hcl b/posthog/clickhouse/hcl/golden/prod-us/batch_exports.hcl
index 942eb27e2aeb..20fdddfc0c3f 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/batch_exports.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/batch_exports.hcl
@@ -676,6 +676,82 @@ SQL
}
}
+ view "custom_metrics" {
+ query = <<SQL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ )
+SQL
+
+ }
+
view "custom_metrics_backups" {
query = <<SQL
WITH
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index a2f5e77af7b5..445ec16c4c6a 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -539,6 +539,21 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, severity_text, resource_fingerprint
+SQL
+
+ }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/logs/{shard}/posthog.logs34"
replica_name = "{replica}"
@@ -1323,6 +1338,25 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ metric_name,
+ metric_type,
+ resource_fingerprint,
+ count() AS event_count,
+ sum(value) AS total_value,
+ min(value) AS min_value,
+ max(value) AS max_value
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint
+SQL
+
+ }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/logs/{shard}/posthog.metrics1"
replica_name = "{replica}"
@@ -1771,6 +1805,67 @@ database "posthog" {
}
}
+ table "trace_attributes2" {
+ order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_attributes2"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "trace_attributes_distributed" {
column "team_id" {
type = "Int32"
@@ -1987,6 +2082,52 @@ database "posthog" {
type = "bloom_filter(0.00001)"
granularity = 99999
}
+ projection "projection_index_span_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY span_id
+SQL
+
+ }
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint
+SQL
+
+ }
+ projection "projection_index_trace_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY trace_id
+SQL
+
+ settings = {
+ index_granularity = "512"
+ }
+ }
+ projection "projection_aggregate_counts2" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, is_root_span, resource_fingerprint
+SQL
+
+ }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/logs/{shard}/posthog.trace_spans"
replica_name = "{replica}"
@@ -2690,6 +2831,10 @@ SELECT
mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
+ observed_timestamp
+ + toIntervalDay(
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ ) AS original_expiry_timestamp,
_partition,
_topic,
_offset,
@@ -2765,6 +2910,9 @@ SQL
column "team_id" {
type = "Int32"
}
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
}
materialized_view "logs34_to_log_attributes" {
@@ -3391,6 +3539,66 @@ SQL
}
}
+ materialized_view "trace_span_to_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "trace_span_to_resource_attributes" {
to_table = "posthog.trace_attributes"
query = <<SQL
@@ -3451,6 +3659,66 @@ SQL
}
}
+ materialized_view "trace_span_to_resource_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_resource_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "trace_span_to_span_attributes" {
to_table = "posthog.trace_attributes"
query = <<SQL
@@ -3510,6 +3778,65 @@ SQL
}
}
+ materialized_view "trace_span_to_span_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = <<SQL
+SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ )
+SQL
+
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "trace_spans_to_kafka_metrics_mv" {
to_table = "posthog.trace_spans_kafka_metrics"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/manifest.hcl b/posthog/clickhouse/hcl/manifest.hcl
index 1618cdfdef8a..f298d2d0b1ff 100644
--- a/posthog/clickhouse/hcl/manifest.hcl
+++ b/posthog/clickhouse/hcl/manifest.hcl
@@ -29,14 +29,14 @@ role "ops" {
env "local-multi" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/ops/shared", "roles/ops/local"] }
}
-# The local LOGS node runs a partial/newer schema than the cloud logs nodes, so it
-# composes a self-contained roles/logs/local (extracted from the live node) rather
-# than the shared cloud layers.
+# Every logs node composes the trace suite; the local node adds a self-contained
+# roles/logs/local (extracted from the live node) for the legacy logs32 family it
+# still runs, and skips the cloud-only metrics ingest.
role "logs" {
env "local-multi" { layers = ["roles/shared/qla.hcl", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/local"] }
- env "dev" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/logs/base", "roles/logs/shared", "roles/logs/prod/tables.hcl", "roles/logs/dev"] }
- env "prod-us" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/shared", "roles/logs/prod", "roles/logs/prod-us"] }
- env "prod-eu" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/logs/base", "roles/logs/traces", "roles/logs/shared", "roles/logs/prod", "roles/logs/prod-eu"] }
+ env "dev" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/shared", "roles/logs/cloud", "roles/logs/dev"] }
+ env "prod-us" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/shared", "roles/logs/cloud", "roles/logs/prod", "roles/logs/prod-us"] }
+ env "prod-eu" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/logs/base", "roles/logs/traces", "roles/logs/traces_kafka_metrics", "roles/logs/shared", "roles/logs/cloud", "roles/logs/prod", "roles/logs/prod-eu"] }
}
# AI_EVENTS satellite (LLM analytics). local/hobby run the MSK variant
@@ -49,21 +49,22 @@ role "logs" {
# Kafka consumer for dev volume.
role "ai_events" {
env "local-multi" { layers = ["roles/shared", "roles/coshared/ai_events_data", "roles/ai_events/shared", "roles/ai_events/local"] }
- env "dev" { layers = ["roles/shared", "roles/coshared/ai_events_data", "roles/ai_events/shared", "roles/ai_events/prod", "roles/ai_events/dev"] }
- env "prod-us" { layers = ["roles/shared", "roles/coshared/ai_events_data", "roles/ai_events/shared", "roles/ai_events/prod"] }
- env "prod-eu" { layers = ["roles/shared", "roles/coshared/ai_events_data", "roles/ai_events/shared", "roles/ai_events/prod"] }
+ env "dev" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/coshared/ai_events_data", "roles/ai_events/shared", "roles/ai_events/prod", "roles/ai_events/dev"] }
+ env "prod-us" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/coshared/ai_events_data", "roles/ai_events/shared", "roles/ai_events/prod"] }
+ env "prod-eu" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/coshared/ai_events_data", "roles/ai_events/shared", "roles/ai_events/prod"] }
}
# AUX satellite: auxiliary tables (error tracking, hog invocations, message assets,
# property values, web/marketing preaggregated). roles/auxiliary/shared holds the env-uniform
# objects; local carries the MSK ingest variant (kafka_error_tracking + its MV, MSK
-# kafka_hog_invocation_results); prod carries the WarpStream variant. prod-us adds the
-# ingestion_warnings tables. prod goldens are dump-baselined (not live-verifiable here).
+# kafka_hog_invocation_results); prod carries the WarpStream variant. Every cloud env
+# hosts the ingestion_warnings store; prod-us adds the Distributed proxy onto the data
+# cluster. prod goldens are dump-baselined (not live-verifiable here).
role "aux" {
env "local-multi" { layers = ["roles/shared", "roles/coshared/aux_data", "roles/auxiliary/shared", "roles/auxiliary/local"] }
- env "dev" { layers = ["roles/shared", "roles/coshared/aux_data", "roles/coshared/ingestion_warnings_store", "roles/auxiliary/shared", "roles/auxiliary/prod", "roles/auxiliary/dev"] }
- env "prod-us" { layers = ["roles/shared", "roles/coshared/aux_data", "roles/coshared/ingestion_warnings_store", "roles/auxiliary/shared", "roles/auxiliary/prod", "roles/auxiliary/prod-us"] }
- env "prod-eu" { layers = ["roles/shared", "roles/coshared/aux_data", "roles/auxiliary/shared", "roles/auxiliary/prod", "roles/auxiliary/prod-eu"] }
+ env "dev" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/coshared/aux_data", "roles/coshared/ingestion_warnings_store", "roles/auxiliary/shared", "roles/auxiliary/prod", "roles/auxiliary/dev"] }
+ env "prod-us" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/coshared/aux_data", "roles/coshared/ingestion_warnings_store", "roles/auxiliary/shared", "roles/auxiliary/prod", "roles/auxiliary/prod-us"] }
+ env "prod-eu" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/coshared/aux_data", "roles/coshared/ingestion_warnings_store", "roles/auxiliary/shared", "roles/auxiliary/prod", "roles/auxiliary/prod-eu"] }
}
# SESSIONS satellite: the local node runs only the shared query_log_archive path
@@ -94,9 +95,9 @@ role "sessionsv3" {
# column. Dump-baselined (no local batch-exports node).
role "batch_exports" {
# dev composes the prod-us stack verbatim (verified zero drift via hclexp diff).
- env "dev" { layers = ["roles/shared", "roles/coshared/batch_exports_data", "roles/batch_exports/prod-us"] }
- env "prod-us" { layers = ["roles/shared", "roles/coshared/batch_exports_data", "roles/batch_exports/prod-us"] }
- env "prod-eu" { layers = ["roles/shared", "roles/coshared/batch_exports_data", "roles/batch_exports/prod-eu"] }
+ env "dev" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/coshared/batch_exports_data", "roles/batch_exports/prod-us"] }
+ env "prod-us" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/coshared/batch_exports_data", "roles/batch_exports/prod-us"] }
+ env "prod-eu" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/coshared/batch_exports_data", "roles/batch_exports/prod-eu"] }
}
# DATA cluster: the main sharded cluster (events family, persons/groups, sessions,
diff --git a/posthog/clickhouse/hcl/roles/coshared/custom_metrics/views.hcl b/posthog/clickhouse/hcl/roles/coshared/custom_metrics/views.hcl
index 4294ded10a1c..a7d38283c529 100644
--- a/posthog/clickhouse/hcl/roles/coshared/custom_metrics/views.hcl
+++ b/posthog/clickhouse/hcl/roles/coshared/custom_metrics/views.hcl
@@ -1,6 +1,7 @@
-# The custom_metrics aggregator view, identical on the roles that host it (ops all
-# envs + cloud logs). The invariant custom_metrics_* sub-views it reads live in
-# roles/shared/custom_metrics.hcl; the local logs node does not run the aggregator.
+# The custom_metrics aggregator view, identical on the roles that host it: ops, and
+# the cloud logs / ai_events / aux / batch_exports nodes. The invariant
+# custom_metrics_* sub-views it reads live in roles/shared/custom_metrics.hcl; the
+# local satellite nodes do not run the aggregator.
database "posthog" {
view "custom_metrics" {
query = file("sql/custom_metrics.sql")
diff --git a/posthog/clickhouse/hcl/roles/coshared/ingestion_warnings_store/tables.hcl b/posthog/clickhouse/hcl/roles/coshared/ingestion_warnings_store/tables.hcl
index 3b12b7d1b4c9..d86630e4c6e5 100644
--- a/posthog/clickhouse/hcl/roles/coshared/ingestion_warnings_store/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/coshared/ingestion_warnings_store/tables.hcl
@@ -1,6 +1,6 @@
-# The sharded_ingestion_warnings store, hosted by prod-us AUX and the local
-# data node. Base is the AUX form; the local node's partitioning, details codec
-# and replication path are patches in roles/data/local.
+# The sharded_ingestion_warnings store, hosted by every cloud AUX node and the
+# local data node. Base is the AUX form; the local node's partitioning, details
+# codec and replication path are patches in roles/data/local.
database "posthog" {
table "sharded_ingestion_warnings" {
order_by = ["team_id", "toHour(timestamp)", "type", "source", "timestamp"]
diff --git a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
index 42a28af31316..8e0276a191b1 100644
--- a/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/base/tables.hcl
@@ -1084,6 +1084,21 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, severity_text, resource_fingerprint
+SQL
+
+ }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/logs/{shard}/posthog.logs34"
replica_name = "{replica}"
diff --git a/posthog/clickhouse/hcl/roles/logs/prod/sql/kafka_metrics_avro_to_metric_samples.sql b/posthog/clickhouse/hcl/roles/logs/cloud/sql/kafka_metrics_avro_to_metric_samples.sql
similarity index 100%
rename from posthog/clickhouse/hcl/roles/logs/prod/sql/kafka_metrics_avro_to_metric_samples.sql
rename to posthog/clickhouse/hcl/roles/logs/cloud/sql/kafka_metrics_avro_to_metric_samples.sql
diff --git a/posthog/clickhouse/hcl/roles/logs/prod/sql/kafka_metrics_avro_to_metric_series.sql b/posthog/clickhouse/hcl/roles/logs/cloud/sql/kafka_metrics_avro_to_metric_series.sql
similarity index 100%
rename from posthog/clickhouse/hcl/roles/logs/prod/sql/kafka_metrics_avro_to_metric_series.sql
rename to posthog/clickhouse/hcl/roles/logs/cloud/sql/kafka_metrics_avro_to_metric_series.sql
diff --git a/posthog/clickhouse/hcl/roles/logs/prod/tables.hcl b/posthog/clickhouse/hcl/roles/logs/cloud/tables.hcl
similarity index 83%
rename from posthog/clickhouse/hcl/roles/logs/prod/tables.hcl
rename to posthog/clickhouse/hcl/roles/logs/cloud/tables.hcl
index c441e3db4f3e..32d7306d1801 100644
--- a/posthog/clickhouse/hcl/roles/logs/prod/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/cloud/tables.hcl
@@ -1,7 +1,7 @@
-# LOGS role, prod only — the Kafka metrics ingest MVs. Env-identical across
-# prod-us / prod-eu (both read the WarpStream metrics_avro topic straight into the
-# metric_samples1 / metric_series1 tables). Not present in dev/local, which have no
-# metrics ingest path.
+# LOGS role, cloud only — the Kafka metrics ingest MVs. Env-identical across dev /
+# prod-us / prod-eu (all read the WarpStream metrics_avro topic straight into the
+# metric_samples1 / metric_series1 tables). The local node has no metrics ingest.
+# The ZK-path and codec deltas the two prods share sit in roles/logs/prod.
database "posthog" {
materialized_view "kafka_metrics_avro_to_metric_samples" {
diff --git a/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl b/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl
index 20cf3f9442fe..b106d1467036 100644
--- a/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl
@@ -1,6 +1,10 @@
# dev deltas to base logs objects. logs34 carries the prod storage/settings shape;
-# the metrics/metrics1 codec and trace-family ZK-path deltas in roles/logs/prod are
-# deliberately not composed here (intentional dev divergence).
+# the metrics/metrics1 codec deltas in roles/logs/prod are deliberately not composed
+# here (intentional dev divergence).
+#
+# The trace family sits between the two shapes: dev keeps the single-shard ZK path
+# and the three-projection trace_spans the local node runs, but reads through the
+# `logs` cluster like both prods.
database "posthog" {
patch_table "logs34" {
settings = {
@@ -9,4 +13,67 @@ database "posthog" {
max_buckets_in_map = "32"
}
}
+
+ # dev batches metrics ingest harder than the prods do.
+ patch_table "kafka_metrics_avro" {
+ engine "kafka" {
+ broker_list = "warpstream_metrics"
+ topic_list = "kafka_topic_list = 'clickhouse_metrics'"
+ group_name = "kafka_group_name = 'clickhouse-metrics-avro-new'"
+ format = "kafka_format = 'Avro'"
+ num_consumers = 8
+ max_block_size = 65536
+ skip_broken_messages = 100
+ poll_timeout_ms = 3000
+ poll_max_batch_size = 65536
+ flush_interval_ms = 7500
+ thread_per_consumer = true
+ }
+ }
+
+ patch_table "trace_attributes_distributed" {
+ engine "distributed" {
+ cluster_name = "logs"
+ remote_database = "posthog"
+ remote_table = "trace_attributes"
+ }
+ }
+
+ patch_table "trace_spans" {
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
+SQL
+
+ }
+ projection "projection_index_trace_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY trace_id
+SQL
+
+ }
+ engine "replicated_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
+ patch_materialized_view "kafka_logs34_avro_mv" {
+ modify_column "_bytes_uncompressed" {
+ type = "Nullable(Float64)"
+ }
+ modify_column "_bytes_compressed" {
+ type = "Nullable(Float64)"
+ }
+ }
}
diff --git a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
index 69845b1a4f35..9801170e107d 100644
--- a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
@@ -403,21 +403,6 @@ SQL
}
}
patch_table "logs34" {
- projection "projection_aggregate_counts" {
- query = <<SQL
-SELECT
- team_id,
- time_bucket,
- toStartOfMinute(timestamp),
- service_name,
- severity_text,
- resource_fingerprint,
- count() AS event_count
-GROUP BY
- team_id, time_bucket, toStartOfMinute(timestamp), service_name, severity_text, resource_fingerprint
-SQL
-
- }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.logs34"
replica_name = "{replica}-{shard}"
@@ -584,13 +569,6 @@ GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
SQL
- }
- projection "projection_index_span_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY span_id
-SQL
-
}
projection "projection_index_trace_id" {
query = <<SQL
@@ -604,242 +582,6 @@ SQL
replica_name = "{replica}-{shard}"
}
}
- table "trace_attributes2" {
- order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
- partition_by = "toDate(original_expiry_time_bucket)"
- ttl = "original_expiry_time_bucket"
- settings = {
- index_granularity = "8192"
- }
- column "team_id" {
- type = "Int32"
- }
- column "original_expiry_time_bucket" {
- type = "DateTime64(0)"
- }
- column "time_bucket" {
- type = "DateTime64(0)"
- }
- column "service_name" {
- type = "LowCardinality(String)"
- }
- column "resource_fingerprint" {
- type = "UInt64"
- default = "0"
- }
- column "attribute_key" {
- type = "LowCardinality(String)"
- }
- column "attribute_value" {
- type = "String"
- }
- column "attribute_type" {
- type = "LowCardinality(String)"
- }
- column "attribute_count" {
- type = "SimpleAggregateFunction(sum, UInt64)"
- }
- index "idx_attribute_key" {
- expr = "attribute_key"
- type = "bloom_filter(0.01)"
- granularity = 4
- }
- index "idx_attribute_value" {
- expr = "attribute_value"
- type = "bloom_filter(0.01)"
- granularity = 4
- }
- index "idx_attribute_key_n3" {
- expr = "attribute_key"
- type = "ngrambf_v1(3, 32768, 3, 0)"
- granularity = 4
- }
- index "idx_attribute_value_n3" {
- expr = "attribute_value"
- type = "ngrambf_v1(3, 32768, 3, 0)"
- granularity = 4
- }
- engine "replicated_aggregating_merge_tree" {
- zoo_path = "/clickhouse/tables/noshard/posthog.trace_attributes2"
- replica_name = "{replica}-{shard}"
- }
- }
- materialized_view "trace_span_to_attributes2" {
- to_table = "posthog.trace_attributes2"
- query = <<SQL
-SELECT
- team_id,
- original_expiry_time_bucket,
- time_bucket,
- service_name,
- resource_fingerprint,
- attribute_key,
- attribute_value,
- 'span_attribute' AS attribute_type,
- attribute_count
-FROM
- (
- SELECT
- team_id AS team_id,
- toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
- toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
- service_name AS service_name,
- resource_fingerprint,
- arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
- attribute.1 AS attribute_key,
- attribute.2 AS attribute_value,
- sumSimpleState(1) AS attribute_count
- FROM posthog.trace_spans
- GROUP BY
- team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
- )
-SQL
-
- column "team_id" {
- type = "Int32"
- }
- column "original_expiry_time_bucket" {
- type = "DateTime64(0)"
- }
- column "time_bucket" {
- type = "DateTime64(0)"
- }
- column "service_name" {
- type = "LowCardinality(String)"
- }
- column "resource_fingerprint" {
- type = "UInt64"
- }
- column "attribute_key" {
- type = "LowCardinality(String)"
- }
- column "attribute_value" {
- type = "String"
- }
- column "attribute_type" {
- type = "LowCardinality(String)"
- }
- column "attribute_count" {
- type = "SimpleAggregateFunction(sum, UInt64)"
- }
- }
- materialized_view "trace_span_to_resource_attributes2" {
- to_table = "posthog.trace_attributes2"
- query = <<SQL
-SELECT
- team_id,
- original_expiry_time_bucket,
- time_bucket,
- service_name,
- resource_fingerprint,
- attribute_key,
- attribute_value,
- 'span_resource_attribute' AS attribute_type,
- attribute_count
-FROM
- (
- SELECT
- team_id AS team_id,
- toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
- toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
- service_name AS service_name,
- resource_fingerprint,
- arrayJoin(resource_attributes) AS attribute,
- attribute.1 AS attribute_key,
- attribute.2 AS attribute_value,
- sumSimpleState(1) AS attribute_count
- FROM posthog.trace_spans
- GROUP BY
- team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
- )
-SQL
-
- column "team_id" {
- type = "Int32"
- }
- column "original_expiry_time_bucket" {
- type = "DateTime64(0)"
- }
- column "time_bucket" {
- type = "DateTime64(0)"
- }
- column "service_name" {
- type = "LowCardinality(String)"
- }
- column "resource_fingerprint" {
- type = "UInt64"
- }
- column "attribute_key" {
- type = "LowCardinality(String)"
- }
- column "attribute_value" {
- type = "String"
- }
- column "attribute_type" {
- type = "LowCardinality(String)"
- }
- column "attribute_count" {
- type = "SimpleAggregateFunction(sum, UInt64)"
- }
- }
- materialized_view "trace_span_to_span_attributes2" {
- to_table = "posthog.trace_attributes2"
- query = <<SQL
-SELECT
- team_id,
- original_expiry_time_bucket,
- time_bucket,
- service_name,
- resource_fingerprint,
- attribute_key,
- attribute_value,
- 'span' AS attribute_type,
- attribute_count
-FROM
- (
- SELECT
- team_id AS team_id,
- toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
- toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
- service_name AS service_name,
- resource_fingerprint,
- 'name' AS attribute_key,
- name AS attribute_value,
- sumSimpleState(1) AS attribute_count
- FROM posthog.trace_spans
- GROUP BY
- team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
- )
-SQL
-
- column "team_id" {
- type = "Int32"
- }
- column "original_expiry_time_bucket" {
- type = "DateTime64(0)"
- }
- column "time_bucket" {
- type = "DateTime64(0)"
- }
- column "service_name" {
- type = "LowCardinality(String)"
- }
- column "resource_fingerprint" {
- type = "UInt64"
- }
- column "attribute_key" {
- type = "LowCardinality(String)"
- }
- column "attribute_value" {
- type = "String"
- }
- column "attribute_type" {
- type = "LowCardinality(String)"
- }
- column "attribute_count" {
- type = "SimpleAggregateFunction(sum, UInt64)"
- }
- }
patch_materialized_view "kafka_trace_spans_avro_mv" {
query = <<SQL
SELECT
@@ -866,9 +608,5 @@ SELECT
FROM posthog.kafka_trace_spans_avro
SQL
- column "original_expiry_timestamp" {
- type = "DateTime64(6)"
- after = "team_id"
- }
}
}
diff --git a/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hcl b/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hcl
index 95bcc1245e69..b8e22dc6f454 100644
--- a/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/prod-eu/patches.hcl
@@ -1,5 +1,17 @@
# prod-eu deltas to base logs objects.
database "posthog" {
+ # EU drops whole parts on TTL for the attribute tables; dev and prod-us still
+ # rewrite parts to expire rows.
+ patch_table "log_attributes2" {
+ settings = {
+ ttl_only_drop_parts = "1"
+ }
+ }
+ patch_table "log_attributes3" {
+ settings = {
+ ttl_only_drop_parts = "1"
+ }
+ }
patch_table "kafka_logs_avro" {
column "bytes_uncompressed" {
type = "Int64"
diff --git a/posthog/clickhouse/hcl/roles/logs/prod/patches.hcl b/posthog/clickhouse/hcl/roles/logs/prod/patches.hcl
index 10308470eff5..894ff95923db 100644
--- a/posthog/clickhouse/hcl/roles/logs/prod/patches.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/prod/patches.hcl
@@ -33,6 +33,50 @@ database "posthog" {
replica_name = "{replica}"
}
}
+ # Both prods kept the original span-count projection and added the is_root_span
+ # cut alongside it; local and dev carry the is_root_span cut under the original
+ # name instead.
+ patch_table "trace_spans" {
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint
+SQL
+
+ }
+ projection "projection_index_trace_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY trace_id
+SQL
+
+ settings = {
+ index_granularity = "512"
+ }
+ }
+ projection "projection_aggregate_counts2" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, is_root_span, resource_fingerprint
+SQL
+
+ }
+ }
patch_table "trace_attributes_distributed" {
engine "distributed" {
cluster_name = "logs"
diff --git a/posthog/clickhouse/hcl/roles/logs/shared/tables.hcl b/posthog/clickhouse/hcl/roles/logs/shared/tables.hcl
index fca4259aeae1..7795206aa052 100644
--- a/posthog/clickhouse/hcl/roles/logs/shared/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/shared/tables.hcl
@@ -521,6 +521,25 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ projection "projection_aggregate_counts" {
+ query = <<SQL
+SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ metric_name,
+ metric_type,
+ resource_fingerprint,
+ count() AS event_count,
+ sum(value) AS total_value,
+ min(value) AS min_value,
+ max(value) AS max_value
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint
+SQL
+
+ }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/logs/{shard}/posthog.metrics1"
replica_name = "{replica}"
diff --git a/posthog/clickhouse/hcl/roles/logs/traces/sql/kafka_trace_spans_avro_mv.sql b/posthog/clickhouse/hcl/roles/logs/traces/sql/kafka_trace_spans_avro_mv.sql
index 0ca8e7ee842d..88af352654c4 100644
--- a/posthog/clickhouse/hcl/roles/logs/traces/sql/kafka_trace_spans_avro_mv.sql
+++ b/posthog/clickhouse/hcl/roles/logs/traces/sql/kafka_trace_spans_avro_mv.sql
@@ -9,6 +9,10 @@ SELECT
mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
+ observed_timestamp
+ + toIntervalDay(
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ ) AS original_expiry_timestamp,
_partition,
_topic,
_offset,
diff --git a/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl b/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl
index 02bb867cfbe8..bdbd78b27925 100644
--- a/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl
@@ -1,7 +1,7 @@
-# The trace-spans suite, hosted by the local logs node and both prod logs envs.
-# The dev trace suite predates this shape and is not composed here. Env deltas are
-# patch_table / patch_materialized_view blocks in the env layers, never second
-# definitions.
+# The trace-spans suite, hosted by every logs node. Env deltas are patch_table /
+# patch_materialized_view blocks in the env layers, never second definitions: the
+# prods shard trace_attributes and trace_spans onto the logs cluster and carry a
+# second span-count projection, dev keeps the single-shard ZK paths.
database "posthog" {
table "trace_attributes" {
order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
@@ -64,6 +64,67 @@ database "posthog" {
}
}
+ table "trace_attributes2" {
+ order_by = ["team_id", "attribute_type", "time_bucket", "resource_fingerprint", "attribute_key", "attribute_value"]
+ partition_by = "toDate(original_expiry_time_bucket)"
+ ttl = "original_expiry_time_bucket"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ default = "0"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ index "idx_attribute_key" {
+ expr = "attribute_key"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_value" {
+ expr = "attribute_value"
+ type = "bloom_filter(0.01)"
+ granularity = 4
+ }
+ index "idx_attribute_key_n3" {
+ expr = "attribute_key"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ index "idx_attribute_value_n3" {
+ expr = "attribute_value"
+ type = "ngrambf_v1(3, 32768, 3, 0)"
+ granularity = 4
+ }
+ engine "replicated_aggregating_merge_tree" {
+ zoo_path = "/clickhouse/tables/noshard/posthog.trace_attributes2"
+ replica_name = "{replica}-{shard}"
+ }
+ }
+
table "trace_attributes_distributed" {
column "team_id" {
type = "Int32"
@@ -357,6 +418,13 @@ database "posthog" {
type = "bloom_filter(0.00001)"
granularity = 99999
}
+ projection "projection_index_span_id" {
+ query = <<SQL
+SELECT _part_offset
+ORDER BY span_id
+SQL
+
+ }
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/logs/{shard}/posthog.trace_spans"
replica_name = "{replica}"
@@ -504,6 +572,38 @@ database "posthog" {
}
}
+ materialized_view "trace_span_to_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = file("sql/trace_span_to_attributes.sql")
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "trace_span_to_resource_attributes" {
to_table = "posthog.trace_attributes"
query = file("sql/trace_span_to_resource_attributes.sql")
@@ -536,6 +636,38 @@ database "posthog" {
}
}
+ materialized_view "trace_span_to_resource_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = file("sql/trace_span_to_resource_attributes.sql")
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "trace_span_to_span_attributes" {
to_table = "posthog.trace_attributes"
query = file("sql/trace_span_to_span_attributes.sql")
@@ -568,6 +700,38 @@ database "posthog" {
}
}
+ materialized_view "trace_span_to_span_attributes2" {
+ to_table = "posthog.trace_attributes2"
+ query = file("sql/trace_span_to_span_attributes.sql")
+ column "team_id" {
+ type = "Int32"
+ }
+ column "original_expiry_time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "time_bucket" {
+ type = "DateTime64(0)"
+ }
+ column "service_name" {
+ type = "LowCardinality(String)"
+ }
+ column "resource_fingerprint" {
+ type = "UInt64"
+ }
+ column "attribute_key" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_value" {
+ type = "String"
+ }
+ column "attribute_type" {
+ type = "LowCardinality(String)"
+ }
+ column "attribute_count" {
+ type = "SimpleAggregateFunction(sum, UInt64)"
+ }
+ }
+
materialized_view "kafka_trace_spans_avro_mv" {
to_table = "posthog.trace_spans"
query = file("sql/kafka_trace_spans_avro_mv.sql")
@@ -637,5 +801,8 @@ database "posthog" {
column "team_id" {
type = "Int32"
}
+ column "original_expiry_timestamp" {
+ type = "DateTime64(6)"
+ }
}
}
diff --git a/posthog/clickhouse/hcl/roles/logs/traces_kafka_metrics/tables.hcl b/posthog/clickhouse/hcl/roles/logs/traces_kafka_metrics/tables.hcl
index 0bfa5f221dc6..578153b2fab1 100644
--- a/posthog/clickhouse/hcl/roles/logs/traces_kafka_metrics/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/traces_kafka_metrics/tables.hcl
@@ -1,5 +1,6 @@
-# Kafka-consumer lag metrics for the trace-spans ingest, present on the local
-# logs node and prod-us only.
+# Kafka-consumer lag metrics for the trace-spans ingest, present on every logs
+# node. prod-us shards the storage table onto the logs cluster; the others keep
+# the single-shard ZK path declared here.
database "posthog" {
table "trace_spans_kafka_metrics" {
order_by = ["_topic", "_partition"]
diff --git a/posthog/clickhouse/hcl/sql/dev/ai_events.sql b/posthog/clickhouse/hcl/sql/dev/ai_events.sql
index 6ed24d0cfc67..6eac0f441576 100644
--- a/posthog/clickhouse/hcl/sql/dev/ai_events.sql
+++ b/posthog/clickhouse/hcl/sql/dev/ai_events.sql
@@ -441,3 +441,73 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT
1 AS value,
'Test to check that the metric endpoint is working' AS help,
'gauge' AS type;
+CREATE VIEW posthog.custom_metrics AS SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ );
diff --git a/posthog/clickhouse/hcl/sql/dev/auxiliary.sql b/posthog/clickhouse/hcl/sql/dev/auxiliary.sql
index 418b4a1c104b..fc9984f06f90 100644
--- a/posthog/clickhouse/hcl/sql/dev/auxiliary.sql
+++ b/posthog/clickhouse/hcl/sql/dev/auxiliary.sql
@@ -1210,3 +1210,73 @@ CREATE TABLE posthog.session_replay_features (
selection_copy_count SimpleAggregateFunction(sum, Int64),
is_deleted SimpleAggregateFunction(max, UInt8) DEFAULT 0
) ENGINE = Distributed('aux', 'posthog', 'sharded_session_replay_features', sipHash64(session_id));
+CREATE VIEW posthog.custom_metrics AS SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ );
diff --git a/posthog/clickhouse/hcl/sql/dev/batch_exports.sql b/posthog/clickhouse/hcl/sql/dev/batch_exports.sql
index 46009f1734cb..9c44756e091d 100644
--- a/posthog/clickhouse/hcl/sql/dev/batch_exports.sql
+++ b/posthog/clickhouse/hcl/sql/dev/batch_exports.sql
@@ -292,3 +292,73 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT
1 AS value,
'Test to check that the metric endpoint is working' AS help,
'gauge' AS type;
+CREATE VIEW posthog.custom_metrics AS SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ );
diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index d187de06a98f..917deeecc67e 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -38,7 +38,30 @@ CREATE TABLE posthog.kafka_metrics_avro (
instrumentation_scope Nullable(String),
attributes Map(String, String),
series_fingerprint Nullable(Int64)
-) ENGINE = Kafka() SETTINGS kafka_broker_list = 'warpstream_metrics', kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = 'kafka_group_name = \'clickhouse-metrics-avro-new\'', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'kafka_topic_list = \'clickhouse_metrics\'';
+) ENGINE = Kafka() SETTINGS kafka_broker_list = 'warpstream_metrics', kafka_flush_interval_ms = 7500, kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = 'kafka_group_name = \'clickhouse-metrics-avro-new\'', kafka_max_block_size = 65536, kafka_num_consumers = 8, kafka_poll_max_batch_size = 65536, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'kafka_topic_list = \'clickhouse_metrics\'';
+CREATE TABLE posthog.kafka_trace_spans_avro (
+ uuid String,
+ trace_id String,
+ span_id String,
+ parent_span_id String,
+ trace_state String,
+ name String,
+ kind Int32,
+ flags Int32,
+ timestamp DateTime64(6),
+ end_time DateTime64(6),
+ observed_timestamp DateTime64(6),
+ service_name String,
+ resource_attributes Map(LowCardinality(String), String),
+ instrumentation_scope String,
+ attributes Map(LowCardinality(String), String),
+ dropped_attributes_count Int32,
+ events Array(String),
+ dropped_events_count Int32,
+ links Array(String),
+ dropped_links_count Int32,
+ status_code Int32
+) ENGINE = Kafka() SETTINGS kafka_broker_list = 'warpstream_traces', kafka_format = 'kafka_format = \'Avro\'', kafka_group_name = 'kafka_group_name = \'clickhouse-traces-avro\'', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'kafka_topic_list = \'clickhouse_traces\'';
CREATE TABLE posthog.log_attributes2 (
team_id Int32,
time_bucket DateTime64(0),
@@ -121,7 +144,17 @@ CREATE TABLE posthog.logs34 (
INDEX idx_body_ngram3 lower(body) TYPE ngrambf_v1(3, 25000, 2, 0) GRANULARITY 1,
INDEX idx_uuid_bloom uuid TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
- INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ PROJECTION projection_aggregate_counts (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, severity_text, resource_fingerprint)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.logs34', '{replica}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, severity_text, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS add_minmax_index_for_numeric_columns = 1, allow_experimental_reverse_key = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_buckets_strategy = 'constant', map_serialization_version = 'with_buckets', max_buckets_in_map = 32, storage_policy = 's3_tiered', ttl_only_drop_parts = 1;
CREATE TABLE posthog.logs_billing_metrics (
team_id Int32,
@@ -283,7 +316,21 @@ CREATE TABLE posthog.metrics1 (
INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,
INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1
+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ PROJECTION projection_aggregate_counts (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ metric_name,
+ metric_type,
+ resource_fingerprint,
+ count() AS event_count,
+ sum(value) AS total_value,
+ min(value) AS min_value,
+ max(value) AS max_value
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.metrics1', '{replica}') ORDER BY (team_id, time_bucket, service_name, metric_name, resource_fingerprint, timestamp) PARTITION BY toDate(timestamp) SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metrics_kafka_metrics (
_partition UInt32,
@@ -389,6 +436,152 @@ CREATE TABLE posthog.query_log_archive (
lc_dagster__owner String ALIAS CAST(log_comment.`dagster.tags.owner`, 'String'),
lc_modifiers String ALIAS if(is_initial_query, JSONExtractRaw(toString(log_comment), 'modifiers'), '')
) ENGINE = Distributed('ops', 'posthog', 'sharded_query_log_archive');
+CREATE TABLE posthog.trace_attributes (
+ team_id Int32,
+ original_expiry_time_bucket DateTime64(0),
+ time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ resource_fingerprint UInt64 DEFAULT 0,
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_attributes', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.trace_attributes2 (
+ team_id Int32,
+ original_expiry_time_bucket DateTime64(0),
+ time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ resource_fingerprint UInt64 DEFAULT 0,
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.trace_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.trace_attributes_distributed (
+ team_id Int32,
+ original_expiry_time_bucket DateTime64(0),
+ time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ resource_fingerprint UInt64 DEFAULT 0,
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64)
+) ENGINE = Distributed('logs', 'posthog', 'trace_attributes');
+CREATE TABLE posthog.trace_spans (
+ time_bucket DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
+ original_expiry_timestamp DateTime64(6),
+ uuid String,
+ team_id Int32,
+ trace_id String,
+ span_id String,
+ parent_span_id String,
+ is_root_span Bool MATERIALIZED replaceAll(trimRight(parent_span_id, '='), 'A', '') = '',
+ trace_state String,
+ name LowCardinality(String),
+ kind Int8,
+ flags UInt32,
+ timestamp DateTime64(6),
+ end_time DateTime64(6),
+ observed_timestamp DateTime64(6),
+ created_at DateTime64(6) MATERIALIZED now(),
+ duration_nano UInt64 MATERIALIZED toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000,
+ status_code Int16,
+ service_name LowCardinality(String),
+ resource_attributes Map(LowCardinality(String), String),
+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),
+ instrumentation_scope String,
+ attributes_map_str Map(LowCardinality(String), String),
+ attributes Map(LowCardinality(String), String) ALIAS mapApply((k, v) -> (left(k, -5), v), attributes_map_str),
+ attributes_map_float Map(LowCardinality(String), Float64) MATERIALIZED mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str)),
+ attributes_map_datetime Map(LowCardinality(String), DateTime64(6)) MATERIALIZED mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str)),
+ dropped_attributes_count UInt32,
+ dropped_events_count UInt32,
+ dropped_links_count UInt32,
+ events Array(String),
+ links Array(String),
+ _partition UInt32,
+ _topic String,
+ _offset UInt64,
+ _bytes_uncompressed UInt64,
+ _bytes_compressed UInt64,
+ _record_count UInt64,
+ INDEX idx_name name TYPE ngrambf_v1(4, 5000, 2, 0) GRANULARITY 16,
+ INDEX idx_kind kind TYPE minmax GRANULARITY 4,
+ INDEX idx_duration duration_nano TYPE minmax GRANULARITY 1,
+ INDEX idx_status_code status_code TYPE minmax GRANULARITY 1,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 16,
+ INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
+ INDEX idx_trace_bloom_part trace_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+ INDEX idx_span_id_bloom_part span_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+ PROJECTION projection_index_span_id (SELECT _part_offset
+ORDER BY span_id),
+ PROJECTION projection_aggregate_counts (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span),
+ PROJECTION projection_index_trace_id (SELECT _part_offset
+ORDER BY trace_id)
+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_spans', '{replica}-{shard}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS allow_part_offset_column_in_projections = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_serialization_version = 'with_buckets', ttl_only_drop_parts = 1;
+CREATE TABLE posthog.trace_spans_distributed (
+ time_bucket DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
+ original_expiry_timestamp DateTime64(6),
+ uuid String,
+ team_id Int32,
+ trace_id String,
+ span_id String,
+ parent_span_id String,
+ is_root_span Bool MATERIALIZED replaceAll(trimRight(parent_span_id, '='), 'A', '') = '',
+ trace_state String,
+ name LowCardinality(String),
+ kind Int8,
+ flags UInt32,
+ timestamp DateTime64(6),
+ end_time DateTime64(6),
+ observed_timestamp DateTime64(6),
+ created_at DateTime64(6) MATERIALIZED now(),
+ duration_nano UInt64 MATERIALIZED toUInt64(dateDiff('microsecond', timestamp, end_time)) * 1000,
+ status_code Int16,
+ service_name LowCardinality(String),
+ resource_attributes Map(LowCardinality(String), String),
+ resource_fingerprint UInt64 MATERIALIZED cityHash64(resource_attributes),
+ instrumentation_scope String,
+ attributes_map_str Map(LowCardinality(String), String),
+ attributes Map(LowCardinality(String), String) ALIAS mapApply((k, v) -> (left(k, -5), v), attributes_map_str),
+ attributes_map_float Map(LowCardinality(String), Float64) MATERIALIZED mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__float'), toFloat64OrNull(v)), attributes_map_str)),
+ attributes_map_datetime Map(LowCardinality(String), DateTime64(6)) MATERIALIZED mapFilter((k, v) -> (v IS NOT NULL), mapApply((k, v) -> (concat(left(k, -5), '__datetime'), parseDateTimeBestEffortOrNull(v, 6)), attributes_map_str)),
+ dropped_attributes_count UInt32,
+ dropped_events_count UInt32,
+ dropped_links_count UInt32,
+ events Array(String),
+ links Array(String)
+) ENGINE = Distributed('logs', 'posthog', 'trace_spans');
+CREATE TABLE posthog.trace_spans_kafka_metrics (
+ _partition UInt32,
+ _topic String,
+ max_offset SimpleAggregateFunction(max, UInt64),
+ max_observed_timestamp SimpleAggregateFunction(max, DateTime64(9)),
+ max_timestamp SimpleAggregateFunction(max, DateTime64(9)),
+ max_created_at SimpleAggregateFunction(max, DateTime64(9)),
+ max_lag SimpleAggregateFunction(max, UInt64)
+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_spans_kafka_metrics', '{replica}-{shard}') ORDER BY (_topic, _partition) SETTINGS index_granularity = 8192;
CREATE TABLE posthog.writable_query_log_archive (
hostname LowCardinality(String),
user LowCardinality(String),
@@ -422,7 +615,7 @@ 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_logs34_avro_mv TO posthog.logs34 (uuid String, trace_id String, span_id String, trace_flags Int32, timestamp DateTime64(6), observed_timestamp DateTime64(6), body String, severity_text String, severity_number Int32, service_name String, instrumentation_scope String, event_name String, attributes_map_str Map(String, String), resource_attributes Map(String, String), team_id Int32, original_expiry_timestamp DateTime64(6), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Int64), _bytes_compressed Nullable(Int64)) AS SELECT
+CREATE MATERIALIZED VIEW posthog.kafka_logs34_avro_mv TO posthog.logs34 (uuid String, trace_id String, span_id String, trace_flags Int32, timestamp DateTime64(6), observed_timestamp DateTime64(6), body String, severity_text String, severity_number Int32, service_name String, instrumentation_scope String, event_name String, attributes_map_str Map(String, String), resource_attributes Map(String, String), team_id Int32, original_expiry_timestamp DateTime64(6), _partition UInt64, _topic LowCardinality(String), _offset UInt64, _record_count Int64, _bytes_uncompressed Nullable(Float64), _bytes_compressed Nullable(Float64)) AS SELECT
uuid,
trace_id,
span_id,
@@ -559,6 +752,28 @@ WHERE kafka_metrics_avro.series_fingerprint IS NOT NULL
SETTINGS
min_insert_block_size_rows = 0,
min_insert_block_size_bytes = 0;
+CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spans (uuid String, trace_id String, span_id String, parent_span_id String, trace_state String, name String, kind Int8, flags UInt32, timestamp DateTime64(6), end_time DateTime64(6), observed_timestamp DateTime64(6), service_name String, resource_attributes Map(LowCardinality(String), String), instrumentation_scope String, attributes_map_str Map(LowCardinality(String), String), dropped_attributes_count UInt32, events Array(String), dropped_events_count UInt32, links Array(String), dropped_links_count UInt32, status_code Int16, team_id Int32, original_expiry_timestamp DateTime64(6)) AS SELECT
+ * EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
+ toInt8(kind) AS kind,
+ toUInt32(flags) AS flags,
+ toUInt32(dropped_attributes_count) AS dropped_attributes_count,
+ toUInt32(dropped_events_count) AS dropped_events_count,
+ toUInt32(dropped_links_count) AS dropped_links_count,
+ toInt16(status_code) AS status_code,
+ mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
+ mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
+ toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
+ observed_timestamp
+ + toIntervalDay(
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ ) AS original_expiry_timestamp,
+ _partition,
+ _topic,
+ _offset,
+ toInt64OrDefault(_headers.value[indexOf(_headers.name, 'record_count')], toInt64(1)) AS _record_count,
+ 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.logs34_to_log_attributes TO posthog.log_attributes2 (team_id Int32, time_bucket DateTime64(0), original_expiry_time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
time_bucket,
@@ -791,6 +1006,171 @@ CREATE MATERIALIZED VIEW posthog.ops_query_log_archive_mv TO posthog.writable_qu
ProfileEvents
FROM system.query_log
WHERE type != 'QueryStart';
+CREATE MATERIALIZED VIEW posthog.trace_span_to_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
+CREATE MATERIALIZED VIEW posthog.trace_span_to_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
+CREATE MATERIALIZED VIEW posthog.trace_span_to_resource_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_resource_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
+CREATE MATERIALIZED VIEW posthog.trace_span_to_resource_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_resource_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
+CREATE MATERIALIZED VIEW posthog.trace_span_to_span_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ );
+CREATE MATERIALIZED VIEW posthog.trace_span_to_span_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ );
+CREATE MATERIALIZED VIEW posthog.trace_spans_to_kafka_metrics_mv TO posthog.trace_spans_kafka_metrics (_partition UInt64, _topic LowCardinality(String), max_offset SimpleAggregateFunction(max, UInt64), max_observed_timestamp SimpleAggregateFunction(max, DateTime64(6)), max_timestamp SimpleAggregateFunction(max, DateTime64(6)), max_created_at SimpleAggregateFunction(max, DateTime), max_lag SimpleAggregateFunction(max, Decimal(18, 6))) AS SELECT
+ _partition,
+ _topic,
+ maxSimpleState(_offset) AS max_offset,
+ maxSimpleState(observed_timestamp) AS max_observed_timestamp,
+ maxSimpleState(timestamp) AS max_timestamp,
+ maxSimpleState(now()) AS max_created_at,
+ maxSimpleState(now() - observed_timestamp) AS max_lag
+FROM posthog.trace_spans
+GROUP BY
+ _partition, _topic;
CREATE VIEW posthog.custom_metrics_backups AS WITH
['ClickHouseCustomMetric_BackupFailed', 'ClickHouseCustomMetric_BackupSuccess', 'ClickHouseCustomMetric_BackupCancelled', 'ClickHouseCustomMetric_BackupAttempts'] AS names,
[toInt64(countIf(status = 'BACKUP_FAILED')), toInt64(countIf(status = 'BACKUP_CREATED')), toInt64(countIf(status = 'BACKUP_CANCELLED')), toInt64(countIf(status = 'CREATING_BACKUP'))] AS values,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index 10fa915802fd..15ecdaa3eefb 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -499,6 +499,8 @@ CREATE TABLE posthog.trace_spans (
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
INDEX idx_trace_bloom_part trace_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
INDEX idx_span_id_bloom_part span_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+ PROJECTION projection_index_span_id (SELECT _part_offset
+ORDER BY span_id),
PROJECTION projection_aggregate_counts (SELECT
team_id,
time_bucket,
@@ -509,8 +511,6 @@ CREATE TABLE posthog.trace_spans (
count() AS event_count
GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span),
- PROJECTION projection_index_span_id (SELECT _part_offset
-ORDER BY span_id),
PROJECTION projection_index_trace_id (SELECT _part_offset
ORDER BY trace_id)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_spans', '{replica}-{shard}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS allow_part_offset_column_in_projections = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_serialization_version = 'with_buckets', ttl_only_drop_parts = 1;
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 0657258e9e17..e8a4d5d58079 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -2816,6 +2816,8 @@ CREATE TABLE posthog.trace_spans (
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
INDEX idx_trace_bloom_part trace_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
INDEX idx_span_id_bloom_part span_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+ PROJECTION projection_index_span_id (SELECT _part_offset
+ORDER BY span_id),
PROJECTION projection_aggregate_counts (SELECT
team_id,
time_bucket,
@@ -2826,8 +2828,6 @@ CREATE TABLE posthog.trace_spans (
count() AS event_count
GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span),
- PROJECTION projection_index_span_id (SELECT _part_offset
-ORDER BY span_id),
PROJECTION projection_index_trace_id (SELECT _part_offset
ORDER BY trace_id)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_spans', '{replica}-{shard}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS allow_part_offset_column_in_projections = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_serialization_version = 'with_buckets', ttl_only_drop_parts = 1;
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/ai_events.sql b/posthog/clickhouse/hcl/sql/prod-eu/ai_events.sql
index 9f419a68eac2..dfa2f3cbfe39 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/ai_events.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/ai_events.sql
@@ -441,3 +441,73 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT
1 AS value,
'Test to check that the metric endpoint is working' AS help,
'gauge' AS type;
+CREATE VIEW posthog.custom_metrics AS SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ );
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sql b/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sql
index a769b4c0fb2f..ac7937f00dbc 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sql
@@ -307,6 +307,16 @@ CREATE TABLE posthog.sharded_experiment_metric_events_preaggregated (
computed_at DateTime64(6, 'UTC') DEFAULT now(),
expires_at Date DEFAULT today() + toIntervalDay(7)
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.experiment_metric_events_preaggregated', '{replica}-{shard}', computed_at) ORDER BY (team_id, job_id, entity_id, timestamp, event_uuid) PARTITION BY toYYYYMMDD(expires_at) TTL expires_at SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
+CREATE TABLE posthog.sharded_ingestion_warnings (
+ team_id Int64,
+ source LowCardinality(String),
+ type String,
+ details String,
+ timestamp DateTime64(6, 'UTC'),
+ _timestamp DateTime,
+ _offset UInt64,
+ _partition UInt64
+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/reshard/{shard}/posthog.sharded_ingestion_warnings', '{replica}') ORDER BY (team_id, toHour(timestamp), type, source, timestamp) PARTITION BY toYear(timestamp) SETTINGS index_granularity = 8192;
CREATE TABLE posthog.sharded_marketing_conversions_preaggregated (
team_id Int64,
job_id UUID,
@@ -1200,3 +1210,73 @@ CREATE TABLE posthog.session_replay_features (
selection_copy_count SimpleAggregateFunction(sum, Int64),
is_deleted SimpleAggregateFunction(max, UInt8) DEFAULT 0
) ENGINE = Distributed('aux', 'posthog', 'sharded_session_replay_features', sipHash64(session_id));
+CREATE VIEW posthog.custom_metrics AS SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ );
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/batch_exports.sql b/posthog/clickhouse/hcl/sql/prod-eu/batch_exports.sql
index 339845a4f488..620bee688e63 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/batch_exports.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/batch_exports.sql
@@ -293,3 +293,73 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT
1 AS value,
'Test to check that the metric endpoint is working' AS help,
'gauge' AS type;
+CREATE VIEW posthog.custom_metrics AS SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ );
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index 0ff54a43712f..de52a08214f6 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -77,7 +77,7 @@ CREATE TABLE posthog.log_attributes2 (
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/logs/{shard}/log_attributes34', '{replica}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(time_bucket) TTL time_bucket + toIntervalDay(15) SETTINGS deduplicate_merge_projection_mode = 'drop', index_granularity = 8192;
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/logs/{shard}/log_attributes34', '{replica}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(time_bucket) TTL time_bucket + toIntervalDay(15) SETTINGS deduplicate_merge_projection_mode = 'drop', index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.log_attributes3 (
team_id Int32,
time_bucket DateTime64(0),
@@ -93,7 +93,7 @@ CREATE TABLE posthog.log_attributes3 (
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.log_attributes3', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value, severity_text) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS deduplicate_merge_projection_mode = 'drop', index_granularity = 8192;
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.log_attributes3', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value, severity_text) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS deduplicate_merge_projection_mode = 'drop', index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.log_attributes_distributed (
team_id Int32,
time_bucket DateTime64(0),
@@ -145,7 +145,17 @@ CREATE TABLE posthog.logs34 (
INDEX idx_body_ngram3 lower(body) TYPE ngrambf_v1(3, 25000, 2, 0) GRANULARITY 1,
INDEX idx_uuid_bloom uuid TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
- INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ PROJECTION projection_aggregate_counts (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, severity_text, resource_fingerprint)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.logs34', '{replica}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, severity_text, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS add_minmax_index_for_numeric_columns = 1, allow_experimental_reverse_key = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_serialization_version = 'with_buckets', storage_policy = 's3_tiered', ttl_only_drop_parts = 1;
CREATE TABLE posthog.logs_billing_metrics (
team_id Int32,
@@ -307,7 +317,21 @@ CREATE TABLE posthog.metrics1 (
INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,
INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1
+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ PROJECTION projection_aggregate_counts (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ metric_name,
+ metric_type,
+ resource_fingerprint,
+ count() AS event_count,
+ sum(value) AS total_value,
+ min(value) AS min_value,
+ max(value) AS max_value
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.metrics1', '{replica}') ORDER BY (team_id, time_bucket, service_name, metric_name, resource_fingerprint, timestamp) PARTITION BY toDate(timestamp) SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metrics_kafka_metrics (
_partition UInt32,
@@ -428,6 +452,21 @@ CREATE TABLE posthog.trace_attributes (
INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.trace_attributes', '{replica}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.trace_attributes2 (
+ team_id Int32,
+ original_expiry_time_bucket DateTime64(0),
+ time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ resource_fingerprint UInt64 DEFAULT 0,
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.trace_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192;
CREATE TABLE posthog.trace_attributes_distributed (
team_id Int32,
original_expiry_time_bucket DateTime64(0),
@@ -486,7 +525,30 @@ CREATE TABLE posthog.trace_spans (
INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 16,
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
INDEX idx_trace_bloom_part trace_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
- INDEX idx_span_id_bloom_part span_id TYPE bloom_filter(0.00001) GRANULARITY 99999
+ INDEX idx_span_id_bloom_part span_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+ PROJECTION projection_index_span_id (SELECT _part_offset
+ORDER BY span_id),
+ PROJECTION projection_aggregate_counts (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint),
+ PROJECTION projection_index_trace_id (SELECT _part_offset
+ORDER BY trace_id) WITH SETTINGS (index_granularity = 512),
+ PROJECTION projection_aggregate_counts2 (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, is_root_span, resource_fingerprint)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.trace_spans', '{replica}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS allow_part_offset_column_in_projections = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_serialization_version = 'with_buckets', ttl_only_drop_parts = 1;
CREATE TABLE posthog.trace_spans_distributed (
time_bucket DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
@@ -527,6 +589,15 @@ CREATE TABLE posthog.trace_spans_distributed (
_bytes_compressed UInt64,
_record_count UInt64
) ENGINE = Distributed('logs', 'posthog', 'trace_spans');
+CREATE TABLE posthog.trace_spans_kafka_metrics (
+ _partition UInt32,
+ _topic String,
+ max_offset SimpleAggregateFunction(max, UInt64),
+ max_observed_timestamp SimpleAggregateFunction(max, DateTime64(9)),
+ max_timestamp SimpleAggregateFunction(max, DateTime64(9)),
+ max_created_at SimpleAggregateFunction(max, DateTime64(9)),
+ max_lag SimpleAggregateFunction(max, UInt64)
+) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_spans_kafka_metrics', '{replica}-{shard}') ORDER BY (_topic, _partition) SETTINGS index_granularity = 8192;
CREATE TABLE posthog.writable_query_log_archive (
hostname LowCardinality(String),
user LowCardinality(String),
@@ -697,7 +768,7 @@ WHERE kafka_metrics_avro.series_fingerprint IS NOT NULL
SETTINGS
min_insert_block_size_rows = 0,
min_insert_block_size_bytes = 0;
-CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spans (uuid String, trace_id String, span_id String, parent_span_id String, trace_state String, name String, kind Int8, flags UInt32, timestamp DateTime64(6), end_time DateTime64(6), observed_timestamp DateTime64(6), service_name String, resource_attributes Map(LowCardinality(String), String), instrumentation_scope String, attributes_map_str Map(LowCardinality(String), String), dropped_attributes_count UInt32, events Array(String), dropped_events_count UInt32, links Array(String), dropped_links_count UInt32, status_code Int16, team_id Int32) AS SELECT
+CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spans (uuid String, trace_id String, span_id String, parent_span_id String, trace_state String, name String, kind Int8, flags UInt32, timestamp DateTime64(6), end_time DateTime64(6), observed_timestamp DateTime64(6), service_name String, resource_attributes Map(LowCardinality(String), String), instrumentation_scope String, attributes_map_str Map(LowCardinality(String), String), dropped_attributes_count UInt32, events Array(String), dropped_events_count UInt32, links Array(String), dropped_links_count UInt32, status_code Int16, team_id Int32, original_expiry_timestamp DateTime64(6)) AS SELECT
* EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
@@ -708,6 +779,10 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_span
mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
+ observed_timestamp
+ + toIntervalDay(
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ ) AS original_expiry_timestamp,
_partition,
_topic,
_offset,
@@ -973,6 +1048,32 @@ FROM
GROUP BY
team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
);
+CREATE MATERIALIZED VIEW posthog.trace_span_to_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
CREATE MATERIALIZED VIEW posthog.trace_span_to_resource_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
original_expiry_time_bucket,
@@ -999,6 +1100,32 @@ FROM
GROUP BY
team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
);
+CREATE MATERIALIZED VIEW posthog.trace_span_to_resource_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_resource_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
CREATE MATERIALIZED VIEW posthog.trace_span_to_span_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
original_expiry_time_bucket,
@@ -1024,6 +1151,42 @@ FROM
GROUP BY
team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
);
+CREATE MATERIALIZED VIEW posthog.trace_span_to_span_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ );
+CREATE MATERIALIZED VIEW posthog.trace_spans_to_kafka_metrics_mv TO posthog.trace_spans_kafka_metrics (_partition UInt64, _topic LowCardinality(String), max_offset SimpleAggregateFunction(max, UInt64), max_observed_timestamp SimpleAggregateFunction(max, DateTime64(6)), max_timestamp SimpleAggregateFunction(max, DateTime64(6)), max_created_at SimpleAggregateFunction(max, DateTime), max_lag SimpleAggregateFunction(max, Decimal(18, 6))) AS SELECT
+ _partition,
+ _topic,
+ maxSimpleState(_offset) AS max_offset,
+ maxSimpleState(observed_timestamp) AS max_observed_timestamp,
+ maxSimpleState(timestamp) AS max_timestamp,
+ maxSimpleState(now()) AS max_created_at,
+ maxSimpleState(now() - observed_timestamp) AS max_lag
+FROM posthog.trace_spans
+GROUP BY
+ _partition, _topic;
CREATE VIEW posthog.custom_metrics_backups AS WITH
['ClickHouseCustomMetric_BackupFailed', 'ClickHouseCustomMetric_BackupSuccess', 'ClickHouseCustomMetric_BackupCancelled', 'ClickHouseCustomMetric_BackupAttempts'] AS names,
[toInt64(countIf(status = 'BACKUP_FAILED')), toInt64(countIf(status = 'BACKUP_CREATED')), toInt64(countIf(status = 'BACKUP_CANCELLED')), toInt64(countIf(status = 'CREATING_BACKUP'))] AS values,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/ai_events.sql b/posthog/clickhouse/hcl/sql/prod-us/ai_events.sql
index b20f1776ab1e..5dc5fec6b489 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/ai_events.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/ai_events.sql
@@ -441,3 +441,73 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT
1 AS value,
'Test to check that the metric endpoint is working' AS help,
'gauge' AS type;
+CREATE VIEW posthog.custom_metrics AS SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ );
diff --git a/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sql b/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sql
index db874a4025b5..8e9d357609d7 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sql
@@ -1220,3 +1220,73 @@ CREATE TABLE posthog.session_replay_features (
selection_copy_count SimpleAggregateFunction(sum, Int64),
is_deleted SimpleAggregateFunction(max, UInt8) DEFAULT 0
) ENGINE = Distributed('aux', 'posthog', 'sharded_session_replay_features', sipHash64(session_id));
+CREATE VIEW posthog.custom_metrics AS SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ );
diff --git a/posthog/clickhouse/hcl/sql/prod-us/batch_exports.sql b/posthog/clickhouse/hcl/sql/prod-us/batch_exports.sql
index 29de9cf6be9b..122e81842535 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/batch_exports.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/batch_exports.sql
@@ -292,3 +292,73 @@ CREATE VIEW posthog.custom_metrics_test AS SELECT
1 AS value,
'Test to check that the metric endpoint is working' AS help,
'gauge' AS type;
+CREATE VIEW posthog.custom_metrics AS SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_test
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_replication_queue
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_server_crash
+UNION ALL
+SELECT *
+FROM posthog.custom_metrics_table_sizes
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_part_counts
+UNION ALL
+SELECT * REPLACE(toFloat64(value) AS value)
+FROM posthog.custom_metrics_dictionaries
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_S3DiskBytesUsed' AS name,
+ map('instance', hostname(), 'disk', disk_name) AS labels,
+ toFloat64(sum(bytes_on_disk)) AS value,
+ 'Bytes currently used by ClickHouse parts on S3-backed disks on this node' AS help,
+ 'gauge' AS type
+FROM system.parts
+WHERE disk_name IN ('s3disk', 'cache')
+GROUP BY
+ disk_name
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeFailures15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(count()) AS value,
+ 'Number of failed merge operations in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM system.part_log
+WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+AND
+ (event_type = 'MergeParts')
+AND
+ (error > 0)
+AND
+ (merge_reason != 'NotAMerge')
+AND
+ (error != 40)
+UNION ALL
+SELECT
+ 'ClickHouseCustomMetric_MergeRetriesMaxPerTable15m' AS name,
+ map('instance', hostname()) AS labels,
+ toFloat64(max(cnt)) AS value,
+ 'Max failed merge retries for any single table in the last 15 minutes' AS help,
+ 'gauge' AS type
+FROM
+ (
+ SELECT count() AS cnt
+ FROM system.part_log
+ WHERE
+ (event_time >= (now() - toIntervalMinute(15)))
+ AND
+ (event_type = 'MergeParts')
+ AND
+ (error > 0)
+ AND
+ (merge_reason != 'NotAMerge')
+ AND
+ (error != 40)
+ GROUP BY
+ database, `table`, partition_id
+ );
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index 2058f8769db0..0597a0cb4f8d 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -144,7 +144,17 @@ CREATE TABLE posthog.logs34 (
INDEX idx_body_ngram3 lower(body) TYPE ngrambf_v1(3, 25000, 2, 0) GRANULARITY 1,
INDEX idx_uuid_bloom uuid TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
- INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
+ PROJECTION projection_aggregate_counts (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ severity_text,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, severity_text, resource_fingerprint)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.logs34', '{replica}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, severity_text, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS add_minmax_index_for_numeric_columns = 1, allow_experimental_reverse_key = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_buckets_strategy = 'constant', map_serialization_version = 'with_buckets', max_buckets_in_map = 32, storage_policy = 's3_tiered', ttl_only_drop_parts = 1;
CREATE TABLE posthog.logs_billing_metrics (
team_id Int32,
@@ -306,7 +316,21 @@ CREATE TABLE posthog.metrics1 (
INDEX idx_metric_type_set metric_type TYPE set(10) GRANULARITY 1,
INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1
+ INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ PROJECTION projection_aggregate_counts (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ metric_name,
+ metric_type,
+ resource_fingerprint,
+ count() AS event_count,
+ sum(value) AS total_value,
+ min(value) AS min_value,
+ max(value) AS max_value
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, metric_name, metric_type, resource_fingerprint)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.metrics1', '{replica}') ORDER BY (team_id, time_bucket, service_name, metric_name, resource_fingerprint, timestamp) PARTITION BY toDate(timestamp) SETTINGS index_granularity = 8192, index_granularity_bytes = 104857600, ttl_only_drop_parts = 1;
CREATE TABLE posthog.metrics_kafka_metrics (
_partition UInt32,
@@ -427,6 +451,21 @@ CREATE TABLE posthog.trace_attributes (
INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.trace_attributes', '{replica}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192;
+CREATE TABLE posthog.trace_attributes2 (
+ team_id Int32,
+ original_expiry_time_bucket DateTime64(0),
+ time_bucket DateTime64(0),
+ service_name LowCardinality(String),
+ resource_fingerprint UInt64 DEFAULT 0,
+ attribute_key LowCardinality(String),
+ attribute_value String,
+ attribute_type LowCardinality(String),
+ attribute_count SimpleAggregateFunction(sum, UInt64),
+ INDEX idx_attribute_key attribute_key TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_value attribute_value TYPE bloom_filter(0.01) GRANULARITY 4,
+ INDEX idx_attribute_key_n3 attribute_key TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4,
+ INDEX idx_attribute_value_n3 attribute_value TYPE ngrambf_v1(3, 32768, 3, 0) GRANULARITY 4
+) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/noshard/posthog.trace_attributes2', '{replica}-{shard}') ORDER BY (team_id, attribute_type, time_bucket, resource_fingerprint, attribute_key, attribute_value) PARTITION BY toDate(original_expiry_time_bucket) TTL original_expiry_time_bucket SETTINGS index_granularity = 8192;
CREATE TABLE posthog.trace_attributes_distributed (
team_id Int32,
original_expiry_time_bucket DateTime64(0),
@@ -485,7 +524,30 @@ CREATE TABLE posthog.trace_spans (
INDEX idx_attributes_str_keys mapKeys(attributes_map_str) TYPE bloom_filter(0.01) GRANULARITY 16,
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
INDEX idx_trace_bloom_part trace_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
- INDEX idx_span_id_bloom_part span_id TYPE bloom_filter(0.00001) GRANULARITY 99999
+ INDEX idx_span_id_bloom_part span_id TYPE bloom_filter(0.00001) GRANULARITY 99999,
+ PROJECTION projection_index_span_id (SELECT _part_offset
+ORDER BY span_id),
+ PROJECTION projection_aggregate_counts (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint),
+ PROJECTION projection_index_trace_id (SELECT _part_offset
+ORDER BY trace_id) WITH SETTINGS (index_granularity = 512),
+ PROJECTION projection_aggregate_counts2 (SELECT
+ team_id,
+ time_bucket,
+ toStartOfMinute(timestamp),
+ service_name,
+ resource_fingerprint,
+ is_root_span,
+ count() AS event_count
+GROUP BY
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, is_root_span, resource_fingerprint)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/logs/{shard}/posthog.trace_spans', '{replica}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS allow_part_offset_column_in_projections = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_serialization_version = 'with_buckets', ttl_only_drop_parts = 1;
CREATE TABLE posthog.trace_spans_distributed (
time_bucket DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
@@ -699,7 +761,7 @@ WHERE kafka_metrics_avro.series_fingerprint IS NOT NULL
SETTINGS
min_insert_block_size_rows = 0,
min_insert_block_size_bytes = 0;
-CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spans (uuid String, trace_id String, span_id String, parent_span_id String, trace_state String, name String, kind Int8, flags UInt32, timestamp DateTime64(6), end_time DateTime64(6), observed_timestamp DateTime64(6), service_name String, resource_attributes Map(LowCardinality(String), String), instrumentation_scope String, attributes_map_str Map(LowCardinality(String), String), dropped_attributes_count UInt32, events Array(String), dropped_events_count UInt32, links Array(String), dropped_links_count UInt32, status_code Int16, team_id Int32) AS SELECT
+CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_spans (uuid String, trace_id String, span_id String, parent_span_id String, trace_state String, name String, kind Int8, flags UInt32, timestamp DateTime64(6), end_time DateTime64(6), observed_timestamp DateTime64(6), service_name String, resource_attributes Map(LowCardinality(String), String), instrumentation_scope String, attributes_map_str Map(LowCardinality(String), String), dropped_attributes_count UInt32, events Array(String), dropped_events_count UInt32, links Array(String), dropped_links_count UInt32, status_code Int16, team_id Int32, original_expiry_timestamp DateTime64(6)) AS SELECT
* EXCEPT(attributes, resource_attributes, kind, flags, dropped_attributes_count, dropped_events_count, dropped_links_count, status_code),
toInt8(kind) AS kind,
toUInt32(flags) AS flags,
@@ -710,6 +772,10 @@ CREATE MATERIALIZED VIEW posthog.kafka_trace_spans_avro_mv TO posthog.trace_span
mapSort(mapApply((k, v) -> (concat(k, '__str'), JSONExtractString(v)), attributes)) AS attributes_map_str,
mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)) AS resource_attributes,
toInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) AS team_id,
+ observed_timestamp
+ + toIntervalDay(
+ toInt32OrDefault(_headers.value[indexOf(_headers.name, 'retention-days')], toInt32(15))
+ ) AS original_expiry_timestamp,
_partition,
_topic,
_offset,
@@ -975,6 +1041,32 @@ FROM
GROUP BY
team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
);
+CREATE MATERIALIZED VIEW posthog.trace_span_to_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(mapFilter((k, v) -> ((length(k) < 256) AND (length(v) < 256)), attributes)) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
CREATE MATERIALIZED VIEW posthog.trace_span_to_resource_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
original_expiry_time_bucket,
@@ -1001,6 +1093,32 @@ FROM
GROUP BY
team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
);
+CREATE MATERIALIZED VIEW posthog.trace_span_to_resource_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span_resource_attribute' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ arrayJoin(resource_attributes) AS attribute,
+ attribute.1 AS attribute_key,
+ attribute.2 AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, attribute
+ );
CREATE MATERIALIZED VIEW posthog.trace_span_to_span_attributes TO posthog.trace_attributes (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
team_id,
original_expiry_time_bucket,
@@ -1026,6 +1144,31 @@ FROM
GROUP BY
team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
);
+CREATE MATERIALIZED VIEW posthog.trace_span_to_span_attributes2 TO posthog.trace_attributes2 (team_id Int32, original_expiry_time_bucket DateTime64(0), time_bucket DateTime64(0), service_name LowCardinality(String), resource_fingerprint UInt64, attribute_key LowCardinality(String), attribute_value String, attribute_type LowCardinality(String), attribute_count SimpleAggregateFunction(sum, UInt64)) AS SELECT
+ team_id,
+ original_expiry_time_bucket,
+ time_bucket,
+ service_name,
+ resource_fingerprint,
+ attribute_key,
+ attribute_value,
+ 'span' AS attribute_type,
+ attribute_count
+FROM
+ (
+ SELECT
+ team_id AS team_id,
+ toStartOfInterval(original_expiry_timestamp, toIntervalMinute(10)) AS original_expiry_time_bucket,
+ toStartOfInterval(timestamp, toIntervalMinute(10)) AS time_bucket,
+ service_name AS service_name,
+ resource_fingerprint,
+ 'name' AS attribute_key,
+ name AS attribute_value,
+ sumSimpleState(1) AS attribute_count
+ FROM posthog.trace_spans
+ GROUP BY
+ team_id, original_expiry_time_bucket, time_bucket, service_name, resource_fingerprint, name
+ );
CREATE MATERIALIZED VIEW posthog.trace_spans_to_kafka_metrics_mv TO posthog.trace_spans_kafka_metrics (_partition UInt64, _topic LowCardinality(String), max_offset SimpleAggregateFunction(max, UInt64), max_observed_timestamp SimpleAggregateFunction(max, DateTime64(6)), max_timestamp SimpleAggregateFunction(max, DateTime64(6)), max_created_at SimpleAggregateFunction(max, DateTime), max_lag SimpleAggregateFunction(max, Decimal(18, 6))) AS SELECT
_partition,
_topic,