: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(mongodb): stop reporting swallowed count errors to error tracking (#63740)
Tom Owers4e9b9b4fe010
diff --git a/posthog/temporal/data_imports/sources/mongodb/mongo.py b/posthog/temporal/data_imports/sources/mongodb/mongo.pyindex 9b493d66b83e..ea7a04d9c3ca 100644--- a/posthog/temporal/data_imports/sources/mongodb/mongo.py+++ b/posthog/temporal/data_imports/sources/mongodb/mongo.py@@ -15,6 +15,7 @@ from bson import Binary, DatetimeMS, ObjectIdfrom bson.codec_options import DatetimeConversionfrom pymongo import MongoClientfrom pymongo.collection import Collection+from pymongo.errors import PyMongoErrorfrom pymongo.server_description import ServerDescriptionfrom structlog.types import FilteringBoundLogger@@ -470,7 +471,16 @@ def _get_rows_to_sync(collection: Collection, query: dict[str, Any], logger: Filrows_to_sync = collection.count_documents(query)logger.debug(f"_get_rows_to_sync: rows_to_sync={rows_to_sync}")return rows_to_sync+ except PyMongoError as e:+ # rows_to_sync is only a progress estimate, so a failed count degrades to 0+ # rather than failing the sync. Connectivity/auth failures here are expected+ # operational errors — the real data read fails and is classified by+ # get_non_retryable_errors — so log and move on instead of flooding error+ # tracking with non-actionable noise.+ logger.warning(f"_get_rows_to_sync: could not count documents ({e}). Using 0 as rows to sync")+ return 0except Exception as e:+ # Unexpected, non-PyMongo error — surface it so genuine bugs aren't hidden.logger.debug(f"_get_rows_to_sync: Error: {e}. Using 0 as rows to sync", exc_info=e)capture_exception(e)return 0diff --git a/posthog/temporal/data_imports/sources/mongodb/test_mongo.py b/posthog/temporal/data_imports/sources/mongodb/test_mongo.pyindex 0545071ab09b..204add38de26 100644--- a/posthog/temporal/data_imports/sources/mongodb/test_mongo.py+++ b/posthog/temporal/data_imports/sources/mongodb/test_mongo.py@@ -9,9 +9,11 @@ from django.test import SimpleTestCase, override_settingsfrom bson import Binary, DatetimeMS, ObjectIdfrom bson.binary import UUID_SUBTYPEfrom parameterized import parameterized+from pymongo.errors import ServerSelectionTimeoutErrorfrom pymongo.server_description import ServerDescriptionfrom posthog.temporal.data_imports.sources.mongodb.mongo import (+ _get_rows_to_sync,_make_safe_server_selector,_process_doc_with_field_logging,_process_nested_value,@@ -395,3 +397,30 @@ class TestMongoDBNonRetryableErrors(SimpleTestCase):message = self.non_retryable[pattern]assert message is not Noneassert expected_substring in message.lower()+++class TestGetRowsToSync(SimpleTestCase):+ """rows_to_sync is a best-effort progress estimate; a failed count must degrade to+ 0 without failing the sync, and expected pymongo errors must not be reported to+ error tracking (they are transient/operational and classified by the real data read)."""++ def test_returns_count_on_success(self):+ coll = MagicMock()+ coll.count_documents.return_value = 42+ assert _get_rows_to_sync(coll, {}, MagicMock()) == 42++ def test_pymongo_error_returns_zero_without_capture(self):+ coll = MagicMock()+ coll.count_documents.side_effect = ServerSelectionTimeoutError(+ "atlas-sql.query.mongodb.net:27017: connection closed, Timeout: 10.0s"+ )+ with patch("posthog.temporal.data_imports.sources.mongodb.mongo.capture_exception") as capture:+ assert _get_rows_to_sync(coll, {}, MagicMock()) == 0+ capture.assert_not_called()++ def test_unexpected_error_returns_zero_and_captures(self):+ coll = MagicMock()+ coll.count_documents.side_effect = ValueError("unexpected bug")+ with patch("posthog.temporal.data_imports.sources.mongodb.mongo.capture_exception") as capture:+ assert _get_rows_to_sync(coll, {}, MagicMock()) == 0+ capture.assert_called_once()