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(clickhouse): stop capturing transient rate-limit noise in partition probe (#71738)

Tom Owers0996b8c8d9e7
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
index 52e3a08421ae..47c9a23c2109 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
@@ -923,6 +923,24 @@ def _get_incremental_row_count(
return int(count) if count is not None else None
+# clickhouse-connect surfaces a non-2xx HTTP status from the server (or a
+# proxy/LB in front of it) as `HTTPDriver for <url> returned response code <N>`.
+# 429 (rate limited) and the transient gateway codes mean the endpoint can't
+# serve us right now, not that anything we sent was wrong — they clear on their
+# own. A real ClickHouse query error carries a `Code: NNN` instead. We match
+# only these transient statuses so genuine failures still surface.
+_TRANSIENT_HTTP_RESPONSE_SUBSTRINGS: tuple[str, ...] = (
+ "returned response code 429",
+ "returned response code 502",
+ "returned response code 503",
+ "returned response code 504",
+)
+
+
+def _is_transient_http_response(message: str) -> bool:
+ return any(substring in message for substring in _TRANSIENT_HTTP_RESPONSE_SUBSTRINGS)
+
+
def _get_partition_settings(
client: ClickHouseClient, database: str, table_name: str, logger: FilteringBoundLogger
) -> PartitionSettings | None:
@@ -944,7 +962,12 @@ def _get_partition_settings(
parameters={"database": database, "table": table_name},
)
except ClickHouseError as e:
- capture_exception(e)
+ # Partitioning is a best-effort optimization; any failure here degrades
+ # to default partitioning. A transient rate-limit/gateway response from
+ # the source isn't actionable on our side, so don't add error-tracking
+ # noise for it — genuine errors are still captured.
+ if not _is_transient_http_response(str(e)):
+ capture_exception(e)
logger.debug(f"_get_partition_settings: failed: {e}")
return None
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
index 9cfd6e57f500..19168cde3aea 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
@@ -18,6 +18,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.clickhouse
_build_query,
_get_client,
_get_incremental_row_count,
+ _get_partition_settings,
_has_duplicate_primary_keys,
_is_transient_connect_drop,
_parse_mv_target,
@@ -1039,6 +1040,40 @@ class TestGetIncrementalRowCount:
assert _get_incremental_row_count(client, "db", "t", "id", 0, self._logger()) is None
+class TestGetPartitionSettings:
+ def _logger(self):
+ return MagicMock()
+
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ "HTTPDriver for https://example.invalid:443 returned response code 429",
+ "HTTPDriver for https://example.invalid:443 returned response code 502",
+ "HTTPDriver for https://example.invalid:443 returned response code 503",
+ "HTTPDriver for https://example.invalid:443 returned response code 504",
+ ],
+ )
+ def test_transient_http_response_not_captured(self, error_msg):
+ client = MagicMock()
+ client.query.side_effect = ClickHouseError(error_msg)
+
+ with patch.object(ch_module, "capture_exception") as mock_capture:
+ # Falls back to default partitioning without adding error-tracking
+ # noise — the source rate-limiting us isn't actionable on our side.
+ assert _get_partition_settings(client, "db", "t", self._logger()) is None
+
+ mock_capture.assert_not_called()
+
+ def test_unexpected_error_is_captured(self):
+ client = MagicMock()
+ client.query.side_effect = ClickHouseError("Code: 62. DB::Exception: Syntax error")
+
+ with patch.object(ch_module, "capture_exception") as mock_capture:
+ assert _get_partition_settings(client, "db", "t", self._logger()) is None
+
+ mock_capture.assert_called_once()
+
+
class TestGetRowsBatching:
"""The source accumulates small Arrow blocks into larger pa.Tables before
yielding so Delta gets fewer, larger commits. Verify the accumulation