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.

fix(tracing): point the dev trace spans MV at writable_trace_spans (#105140)

Frank Hamand3ae407f3f83c
diff --git a/posthog/clickhouse/migrations/0330_trace_spans_retention_days.py b/posthog/clickhouse/migrations/0330_trace_spans_retention_days.py
index 2ba31626b34e..d5d3f5456332 100644
--- a/posthog/clickhouse/migrations/0330_trace_spans_retention_days.py
+++ b/posthog/clickhouse/migrations/0330_trace_spans_retention_days.py
@@ -20,11 +20,17 @@ DB = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
# create a second, unfed one on its logs nodes. `run_mode()` is resolved here (not a raw
# CLOUD_DEPLOYMENT check) so a test re-import under a patched deployment picks up the right branch.
# Local single-node is unaffected either way: migration_tools collapses it to NodeRole.ALL.
-_role = NodeRole.APM if run_mode() is RunMode.CLOUD_DEV else NodeRole.LOGS
+#
+# The MV's destination differs with it. Dev's apm nodes host `writable_trace_spans`, the
+# Distributed proxy into the logs cluster, rather than `trace_spans` itself, so the MV there has
+# to write through the proxy or it resolves to nothing.
+_is_dev = run_mode() is RunMode.CLOUD_DEV
+_role = NodeRole.APM if _is_dev else NodeRole.LOGS
+_mv = KAFKA_TRACE_SPANS_AVRO_MV(to_table="writable_trace_spans") if _is_dev else KAFKA_TRACE_SPANS_AVRO_MV()
operations = [
run_sql_with_exceptions(f"DROP TABLE IF EXISTS {DB}.kafka_trace_spans_avro_mv", node_roles=[_role]),
run_sql_with_exceptions(f"DROP TABLE IF EXISTS {DB}.kafka_trace_spans_avro", node_roles=[_role]),
run_sql_with_exceptions(KAFKA_TRACE_SPANS_AVRO_TABLE_SQL(), node_roles=[_role]),
- run_sql_with_exceptions(KAFKA_TRACE_SPANS_AVRO_MV(), node_roles=[_role]),
+ run_sql_with_exceptions(_mv, node_roles=[_role]),
]
diff --git a/posthog/clickhouse/traces/spans.py b/posthog/clickhouse/traces/spans.py
index f0c47d04434c..b8ef8226c5d8 100644
--- a/posthog/clickhouse/traces/spans.py
+++ b/posthog/clickhouse/traces/spans.py
@@ -333,10 +333,13 @@ SETTINGS
"""
-def KAFKA_TRACE_SPANS_AVRO_MV():
+def KAFKA_TRACE_SPANS_AVRO_MV(to_table: str = TABLE_NAME):
db = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+ # `to_table` defaults to `trace_spans` for the environments that keep this MV on the logs
+ # nodes, where that table is local. Dev keeps it on the apm nodes instead, which host only
+ # `writable_trace_spans` — the Distributed proxy — so migration 0330 passes that name there.
return f"""
-CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{KAFKA_TABLE_NAME}_mv TO {db}.{TABLE_NAME}
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db}.{KAFKA_TABLE_NAME}_mv TO {db}.{to_table}
(
`uuid` String,
`trace_id` String,