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(postgres): stop capturing expected connection errors from metadata probe (#66894)

Tom Owers7422a3991ee0
diff --git a/products/data_warehouse/backend/presentation/views/external_data_source.py b/products/data_warehouse/backend/presentation/views/external_data_source.py
index 70b463bcfa39..623766d14325 100644
--- a/products/data_warehouse/backend/presentation/views/external_data_source.py
+++ b/products/data_warehouse/backend/presentation/views/external_data_source.py
@@ -395,7 +395,13 @@ def get_direct_connection_metadata(
try:
metadata = metadata_fetcher(source_config, team_id, require_ssl=require_ssl)
except Exception as error:
- capture_exception(error)
+ # Connection metadata is best-effort — we fall back below regardless. An expected
+ # user/upstream connection failure (unreachable or misconfigured host, refused connection,
+ # bad credentials) is the customer's to fix and is already surfaced by credential
+ # validation, so don't capture it as error-tracking noise. Mirrors `refresh_schemas`.
+ _, is_expected_source_error = _classify_refresh_schemas_error(source_impl, error)
+ if not is_expected_source_error:
+ capture_exception(error)
return fallback or {}
return metadata if isinstance(metadata, dict) else (fallback or {})
diff --git a/products/data_warehouse/backend/tests/api/test_external_data_source.py b/products/data_warehouse/backend/tests/api/test_external_data_source.py
index 449f9843c349..4ac16067a3dd 100644
--- a/products/data_warehouse/backend/tests/api/test_external_data_source.py
+++ b/products/data_warehouse/backend/tests/api/test_external_data_source.py
@@ -10,7 +10,7 @@ from unittest.mock import MagicMock, Mock, PropertyMock, patch
from django.conf import settings
from django.db import connection
-from django.test import override_settings
+from django.test import SimpleTestCase, override_settings
from django.utils import timezone
import psycopg
@@ -38,6 +38,7 @@ from products.data_warehouse.backend.direct_postgres import DIRECT_POSTGRES_URL_
from products.data_warehouse.backend.models.revenue_analytics_config import ExternalDataSourceRevenueAnalyticsConfig
from products.data_warehouse.backend.presentation.views.external_data_schema import ExternalDataSchemaSerializer
from products.data_warehouse.backend.presentation.views.external_data_source import (
+ get_direct_connection_metadata,
get_nonsensitive_and_sensitive_field_names,
strip_sensitive_from_dict,
)
@@ -10142,3 +10143,35 @@ class TestExternalDataSourcePreviewAndCustomPayload(APIBaseTest):
source = ExternalDataSource.objects.get(id=response.json()["id"])
assert source.source_type == "Custom"
assert source.created_via == ExternalDataSource.CreatedVia.MCP
+
+
+class TestGetDirectConnectionMetadata(SimpleTestCase):
+ def _source_impl(self, error: Exception, non_retryable: dict | None = None) -> Mock:
+ impl = Mock()
+ impl.get_connection_metadata.side_effect = error
+ impl.get_non_retryable_errors.return_value = non_retryable or {}
+ return impl
+
+ @patch("products.data_warehouse.backend.presentation.views.external_data_source.capture_exception")
+ def test_expected_connection_error_is_not_captured(self, mock_capture):
+ # An unreachable customer host fails the best-effort metadata probe — already surfaced by
+ # credential validation, so it must degrade to the fallback without flooding error tracking.
+ impl = self._source_impl(
+ psycopg.OperationalError('connection to server at "192.0.2.1", port 5432 failed: Network is unreachable')
+ )
+ fallback = {"database": "existing"}
+
+ result = get_direct_connection_metadata(source_impl=impl, source_config=Mock(), team_id=1, fallback=fallback)
+
+ self.assertEqual(result, fallback)
+ mock_capture.assert_not_called()
+
+ @patch("products.data_warehouse.backend.presentation.views.external_data_source.capture_exception")
+ def test_unexpected_error_is_still_captured(self, mock_capture):
+ error = ValueError("unexpected bug in metadata probe")
+ impl = self._source_impl(error)
+
+ result = get_direct_connection_metadata(source_impl=impl, source_config=Mock(), team_id=1, fallback=None)
+
+ self.assertEqual(result, {})
+ mock_capture.assert_called_once_with(error)