: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(clickhouse): drop IP columns from instance status by type (#89125)
Alessandro Pogliaghi77c0e7a87d4a
diff --git a/posthog/clickhouse/client/execute.py b/posthog/clickhouse/client/execute.pyindex f26e0b11d64d..45928e4b3b67 100644--- a/posthog/clickhouse/client/execute.py+++ b/posthog/clickhouse/client/execute.py@@ -582,6 +582,7 @@ def query_with_columns(columns_to_remove: Optional[Sequence[str]] = None,columns_to_rename: Optional[dict[str, str]] = None,*,+ column_types_to_remove: Optional[Sequence[str]] = None,workload: Workload = Workload.DEFAULT,team_id: Optional[int] = None,settings: Optional[dict[str, Any]] = None,@@ -590,6 +591,8 @@ def query_with_columns(columns_to_remove = []if columns_to_rename is None:columns_to_rename = {}+ if column_types_to_remove is None:+ column_types_to_remove = []metrics, types = sync_execute(query,args,@@ -598,14 +601,19 @@ def query_with_columns(workload=workload,team_id=team_id,)- type_names = [key for key, _type in types]+ column_names = [key for key, _type in types]+ # A `SELECT *` over a system table gains columns as ClickHouse versions land, so a caller+ # that must exclude a whole class of column matches on the type instead of naming each one.+ dropped = set(columns_to_remove) | {+ name for name, type_name in types if any(unwanted in str(type_name) for unwanted in column_types_to_remove)+ }rows = []for row in metrics:result = {}- for type_name, value in zip(type_names, row):- if type_name not in columns_to_remove:- result[columns_to_rename.get(type_name, type_name)] = value+ for column_name, value in zip(column_names, row):+ if column_name not in dropped:+ result[columns_to_rename.get(column_name, column_name)] = valuerows.append(result)diff --git a/posthog/clickhouse/client/test/test_execute.py b/posthog/clickhouse/client/test/test_execute.pyindex 9ccbe63abd50..e1f488c4cded 100644--- a/posthog/clickhouse/client/test/test_execute.py+++ b/posthog/clickhouse/client/test/test_execute.py@@ -1,8 +1,10 @@+from ipaddress import IPv4Address, IPv6Address+import pytestfrom unittest.mock import MagicMock, patchfrom posthog.clickhouse.client.connection import ClickHouseUser, Workload-from posthog.clickhouse.client.execute import sync_execute+from posthog.clickhouse.client.execute import query_with_columns, sync_executefrom posthog.clickhouse.client.limit import ConcurrencySlot, RateLimit, get_llm_analytics_rate_limiterfrom posthog.clickhouse.query_tagging import AccessMethod, Product, tags_context@@ -105,3 +107,32 @@ def test_llm_analytics_queries_take_a_concurrency_slot(client_from_pool, llm_anasync_execute("SELECT 1", flush=False)assert llm_analytics_slots.call_count == expected_slots+++ "types,row,expected",+ [+ # `system.query_log` grew `connection_address` on top of the two IP columns the+ # callers used to name, and one unserializable value fails the whole API response.+ (+ [("duration", "Float64"), ("connection_address", "IPv6"), ("address", "IPv6")],+ (1.0, IPv6Address("::1"), IPv6Address("::1")),+ {"duration": 1.0},+ ),+ (+ [("duration", "Float64"), ("client", "Nullable(IPv4)")],+ (1.0, IPv4Address("127.0.0.1")),+ {"duration": 1.0},+ ),+ (+ [("duration", "Float64"), ("query", "String")],+ (1.0, "SELECT 1"),+ {"duration": 1.0, "query": "SELECT 1"},+ ),+ ],+)+def test_query_with_columns_removes_columns_by_type(types, row, expected):+ with patch("posthog.clickhouse.client.execute.sync_execute", return_value=([row], types)):+ rows = query_with_columns("SELECT *", column_types_to_remove=("IPv4", "IPv6"))++ assert rows == [expected]diff --git a/posthog/clickhouse/system_status.py b/posthog/clickhouse/system_status.pyindex 565d6b1c70c4..d711c46cb1a6 100644--- a/posthog/clickhouse/system_status.py+++ b/posthog/clickhouse/system_status.py@@ -17,6 +17,10 @@ from posthog.session_recordings.models.system_status_queries import get_recordinSLOW_THRESHOLD_MS = 10000SLOW_AFTER = relativedelta(hours=6)+# ClickHouse hands IP columns back as `ipaddress` objects, which the API renderer cannot+# serialize, so the whole response fails on a single such value.+IP_COLUMN_TYPES = ("IPv4", "IPv6")+CLICKHOUSE_FLAMEGRAPH_EXECUTABLE = abspath(join(dirname(__file__), "bin", "clickhouse-flamegraph"))FLAMEGRAPH_PL = abspath(join(dirname(__file__), "bin", "flamegraph.pl"))@@ -200,7 +204,8 @@ def dead_letter_queue_ratio_ok_cached() -> bool:def get_clickhouse_running_queries() -> list[dict]:return query_with_columns("SELECT elapsed as duration, query, * FROM system.processes ORDER BY duration DESC",- columns_to_remove=["address", "initial_address", "elapsed"],+ columns_to_remove=["elapsed"],+ column_types_to_remove=IP_COLUMN_TYPES,)@@ -217,9 +222,8 @@ def get_clickhouse_slow_log() -> list[dict]:LIMIT 200""",{"after": timezone.now() - SLOW_AFTER},+ column_types_to_remove=IP_COLUMN_TYPES,columns_to_remove=[- "address",- "initial_address","query_duration_ms","event_time","event_date",