: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(postgres): stop capturing unreachable-source noise on CDC status (#72014)
Tom Owers1f85c22a84b1
diff --git a/products/data_warehouse/backend/presentation/views/external_data_source.py b/products/data_warehouse/backend/presentation/views/external_data_source.pyindex e92f2e0ef3f8..cf500138680a 100644--- a/products/data_warehouse/backend/presentation/views/external_data_source.py+++ b/products/data_warehouse/backend/presentation/views/external_data_source.py@@ -4194,7 +4194,11 @@ class ExternalDataSourceViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixitry:live_status = adapter.get_status(instance)except Exception as e:- capture_exception(e, {"source_id": str(instance.id), "team_id": self.team_id})+ # An unreachable source DB is the degraded state this endpoint exists to report, so+ # don't capture expected connection failures as error-tracking noise. Capture only+ # unexpected errors, which point at a bug in our status read.+ if not adapter.is_connection_error(e):+ capture_exception(e, {"source_id": str(instance.id), "team_id": self.team_id})return Response(status=status.HTTP_400_BAD_REQUEST,data={"message": f"Could not connect to source to read CDC status: {e}"},diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/adapters.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/adapters.pyindex 8e72b5187f0f..21e8dcb0cb08 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/adapters.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/adapters.py@@ -70,6 +70,13 @@ class CDCSourceAdapter(Protocol[CDCConfigT_co]):such that it cannot be resumed and must be recreated."""...+ def is_connection_error(self, exc: BaseException) -> bool:+ """Whether the exception is an expected failure to reach the source DB (unreachable+ host, connect timeout, refused, dropped, or a tunnel that can't be established) rather+ than a bug in PostHog's own code. Callers use it to avoid capturing expected+ customer/upstream connection failures as error-tracking noise."""+ ...+def classify_error(self, exc: BaseException) -> CDCErrorInfo | None:"""Interpret a single engine-specific exception as a CDC error category, or Nonewhen unrecognized (the caller falls back to the engine-agnostic default). Mirrorsdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/adapter.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/adapter.pyindex ef3eae7032e4..28a9af2754f3 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/adapter.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/adapter.py@@ -9,6 +9,7 @@ from typing import TYPE_CHECKING, Any, Literalimport psycopgimport structlog+from sshtunnel import BaseSSHTunnelForwarderErrorfrom products.warehouse_sources.backend.temporal.data_imports.cdc.errors import cdc_error_infofrom products.warehouse_sources.backend.temporal.data_imports.sources.postgres.cdc.config import PostgresCDCConfig@@ -145,6 +146,13 @@ class PostgresCDCAdapter:def is_slot_invalidation_error(self, exc: BaseException) -> bool:return is_slot_invalidation_error(exc)+ def is_connection_error(self, exc: BaseException) -> bool:+ # psycopg raises OperationalError for every failure to reach the source DB+ # (connect timeout, refused, unreachable host, DNS, dropped, auth); sshtunnel+ # raises BaseSSHTunnelForwarderError when the tunnel itself can't be established.+ # Neither points at a bug in our code.+ return isinstance(exc, psycopg.OperationalError | BaseSSHTunnelForwarderError)+def classify_error(self, exc: BaseException) -> CDCErrorInfo | None:category = classify_postgres_cdc_error(exc)return cdc_error_info(category) if category is not None else Nonediff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_adapter.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_adapter.pyindex 2724939ba5d0..11a1950f4ddf 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_adapter.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_adapter.py@@ -4,6 +4,8 @@ import pytestfrom unittest.mock import MagicMock, patchimport psycopg.errors+from parameterized import parameterized+from sshtunnel import BaseSSHTunnelForwarderErrorfrom products.warehouse_sources.backend.temporal.data_imports.sources.postgres.cdc.adapter import (PostgresCDCAdapter,@@ -361,3 +363,18 @@ class TestGetStatus:status = PostgresCDCAdapter().get_status(source)assert status["published_tables"] == []mock_tables.assert_not_called()+++class TestIsConnectionError:+ # A connect-time timeout to an unreachable source DB is the failure `cdc_status` reports as a+ # degraded state; it must classify as a connection error so the endpoint doesn't capture it.+ @parameterized.expand(+ [+ ("connect_timeout", psycopg.errors.ConnectionTimeout("connection timeout expired"), True),+ ("operational", psycopg.OperationalError("connection refused"), True),+ ("ssh_tunnel", BaseSSHTunnelForwarderError("could not open tunnel"), True),+ ("programming_bug", ValueError("unexpected status shape"), False),+ ]+ )+ def test_classifies_connection_errors(self, _name: str, exc: BaseException, expected: bool) -> None:+ assert PostgresCDCAdapter().is_connection_error(exc) is expected