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(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.py
index 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, ObjectId
from bson.codec_options import DatetimeConversion
from pymongo import MongoClient
from pymongo.collection import Collection
+from pymongo.errors import PyMongoError
from pymongo.server_description import ServerDescription
from structlog.types import FilteringBoundLogger
@@ -470,7 +471,16 @@ def _get_rows_to_sync(collection: Collection, query: dict[str, Any], logger: Fil
rows_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 0
except 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 0
diff --git a/posthog/temporal/data_imports/sources/mongodb/test_mongo.py b/posthog/temporal/data_imports/sources/mongodb/test_mongo.py
index 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_settings
from bson import Binary, DatetimeMS, ObjectId
from bson.binary import UUID_SUBTYPE
from parameterized import parameterized
+from pymongo.errors import ServerSelectionTimeoutError
from pymongo.server_description import ServerDescription
from 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 None
assert 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()