:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(metrics): read ingest series_fingerprint, drop the MV hashing (#67196)
Daniel Visca1d502e554cb4
diff --git a/bin/clickhouse-metrics.sql b/bin/clickhouse-metrics.sqlindex c2cede8b08d0..cdfa59749c13 100644--- a/bin/clickhouse-metrics.sql+++ b/bin/clickhouse-metrics.sql@@ -240,7 +240,8 @@ CREATE OR REPLACE TABLE kafka_metrics_avro`is_monotonic` Nullable(UInt8),`resource_attributes` Map(String, String),`instrumentation_scope` Nullable(String),- `attributes` Map(String, String)+ `attributes` Map(String, String),+ `series_fingerprint` Nullable(Int64))ENGINE = Kafka('kafka:9092', 'clickhouse_metrics', 'clickhouse-metrics-avro', 'Avro')SETTINGS@@ -280,41 +281,47 @@ AS SELECTtoInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) as team_idFROM kafka_metrics_avro settings min_insert_block_size_rows=0, min_insert_block_size_bytes=0;--- Two MVs on the existing kafka_metrics_avro: every clickhouse_metrics point--- fans into metric_series (labels, deduped) and metric_samples (the tiny row),--- alongside kafka_metrics_avro_mv -> metrics1. The series_fingerprint is computed--- identically in both (over the JSON-decoded label maps) so the query-time join--- matches; keep it in sync with SERIES_FINGERPRINT_SQL in metric_events.py.+-- Two MVs on the existing kafka_metrics_avro: every clickhouse_metrics point fans+-- into metric_series (labels, deduped) and metric_samples (the tiny row), alongside+-- kafka_metrics_avro_mv -> metrics1. series_fingerprint is assigned ONCE at ingest+-- (capture-logs) and shipped in the Avro payload; both MVs read it verbatim — they do+-- NOT recompute it. ClickHouse never computes the identity (no cityHash64 over the+-- maps), so the two tables cannot disagree and the hash cannot collapse. The Avro+-- `long` carries the u64 bits as signed; reinterpretAsUInt64 restores them.+-- Rows with a NULL series_fingerprint (a producer that predates the ingest change, or+-- a rollback) are dropped, not coerced to 0: coalescing to a shared id would collapse+-- every such series onto one ReplacingMergeTree row with arbitrary labels — silent join+-- corruption. Dropped rows are recoverable by replaying the topic once ingest is live.drop table if exists kafka_metrics_avro_to_metric_series;CREATE MATERIALIZED VIEW kafka_metrics_avro_to_metric_series TO metric_series1AS SELECTtoInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) as team_id,ifNull(metric_name, '') as metric_name,- cityHash64(ifNull(metric_name, ''), ifNull(service_name, ''),- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes))) as series_fingerprint,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) as series_fingerprint,ifNull(metric_type, '') as metric_type,ifNull(unit, '') as unit,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 kafka_metrics_avro settings min_insert_block_size_rows=0, min_insert_block_size_bytes=0;+FROM kafka_metrics_avro+WHERE series_fingerprint IS NOT NULL+settings min_insert_block_size_rows=0, min_insert_block_size_bytes=0;drop table if exists kafka_metrics_avro_to_metric_samples;CREATE MATERIALIZED VIEW kafka_metrics_avro_to_metric_samples TO metric_samples1AS SELECTtoInt32OrZero(_headers.value[indexOf(_headers.name, 'team_id')]) as team_id,ifNull(metric_name, '') as metric_name,- cityHash64(ifNull(metric_name, ''), ifNull(service_name, ''),- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), resource_attributes)),- mapSort(mapApply((k, v) -> (k, JSONExtractString(v)), attributes))) as series_fingerprint,+ reinterpretAsUInt64(assumeNotNull(series_fingerprint)) as series_fingerprint,timestamp,ifNull(value, 0) as value,trace_id,span_id,ifNull(trace_flags, 0) as trace_flags-FROM kafka_metrics_avro settings min_insert_block_size_rows=0, min_insert_block_size_bytes=0;+FROM kafka_metrics_avro+WHERE series_fingerprint IS NOT NULL+settings min_insert_block_size_rows=0, min_insert_block_size_bytes=0;-- Kafka consumer lag trackingcreate or replace table metrics_kafka_metricsdiff --git a/posthog/clickhouse/metrics/metric_events.py b/posthog/clickhouse/metrics/metric_events.pyindex d6ec2589e6f7..e7ba2bd49dcd 100644--- a/posthog/clickhouse/metrics/metric_events.py+++ b/posthog/clickhouse/metrics/metric_events.py@@ -7,19 +7,21 @@ from posthog.clickhouse.table_engines import Distributed, MergeTreeEngine, Repla# number, so inlining the label maps on every row would dwarf the data ~100x).## - metric_series: one row per unique (metric, label-set), labels stored ONCE,-# keyed by a fingerprint. Deduped via ReplacingMergeTree.-# - metric_samples: the hot table — tiny rows (fingerprint + timestamp + value),-# plus an inline trace_id so a spike can still pivot to its trace (empty for-# most points, so it compresses to ~nothing). Joined to metric_series on the-# fingerprint at query time.+# keyed by series_fingerprint. Deduped via ReplacingMergeTree.+# - metric_samples: the hot table — tiny rows (series_fingerprint + timestamp ++# value), plus an inline trace_id so a spike can still pivot to its trace (empty+# for most points, so it compresses to ~nothing). Joined to metric_series on+# series_fingerprint at query time.+#+# series_fingerprint is assigned ONCE at ingest (rust/capture-logs computes it over+# the canonical label set and ships it in the Avro payload); the ingest MVs store it+# verbatim and never recompute it. This is the TSDB / Snuffle-default approach — the+# storage engine must not compute identity, or two independent MVs diverge (and the+# stored-map MV context corrupts the hash). See docs/internal/metrics for the history.## Distinct from `metrics1` (pre-aggregated rollups for dashboards/alerts): this# keeps every raw sample, for exact quantiles, per-emission drill-down, and the# metric->trace pivot. Lives on the logs ClickHouse cluster next to logs/traces.-#-# The fingerprint must be computed identically wherever samples/series are-# written (see the ingest MV); keep this expression and that MV in sync.-SERIES_FINGERPRINT_SQL = "cityHash64(metric_name, service_name, mapSort(resource_attributes), mapSort(attributes))"SAMPLES_TABLE_NAME = "metric_samples1"SAMPLES_DISTRIBUTED_TABLE_NAME = "metric_samples"