: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 missing-read-privilege errors as noise (#67785)
Tom Owersaa6ef6dc598e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.pyindex bd594272267f..e91fb02f4e90 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py@@ -215,10 +215,19 @@ class MongoDBSource(SimpleSource[MongoDBSourceConfig], ValidateDatabaseHostMixin# signature hashes, BSON ids — so str(e) must never be surfaced. Map the stable error# markers to a clean message; an authorization failure ("not authorized") means the# credentials are valid but lack read access, which is distinct from a bad password.- capture_exception(e)- if "not authorized" in str(e):+ # Both are user-side credential/permission problems we already surface an actionable+ # message for and classify as non-retryable — never a PostHog bug — so don't report+ # them to error tracking as non-actionable noise.+ message = str(e)+ if "not authorized" in message:return False, _MONGO_NOT_AUTHORIZED_MESSAGE- return False, _MONGO_AUTHENTICATION_FAILED_MESSAGE+ if any(marker in message for marker in ("AuthenticationFailed", "Authentication failed", "bad auth")):+ return False, _MONGO_AUTHENTICATION_FAILED_MESSAGE+ # Any other OperationFailure on listCollections is unexpected (server bug, unsupported+ # option, ...) — capture it so the signal isn't lost, and surface a generic message+ # rather than mislabelling it as an authentication problem.+ capture_exception(e)+ return False, _MONGO_CONNECT_FAILED_MESSAGEexcept ServerSelectionTimeoutError as e:# pymongo dumps a verbose topology description into str(e); surface a concise,# actionable message instead. A DNS failure means the host doesn't resolve at all,diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.pyindex 13e9ee8213c6..1422ce03fb68 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.py@@ -177,3 +177,60 @@ class TestMongoValidateCredentialsErrorTrackingNoise:assert ok is Falseassert err == _MONGO_CONNECT_FAILED_MESSAGEmock_capture.assert_called_once()++ # A recognized OperationFailure on listCollections is a user-side credential/permission problem+ # — a missing read role ("not authorized") or bad credentials — that we already surface an+ # actionable message for and classify as non-retryable, so it must not be captured as noise. An+ # unrecognized OperationFailure is unexpected and must still be captured so the signal isn't lost.+ @pytest.mark.parametrize(+ "exc, expected_message, should_capture",+ [+ (+ OperationFailure(+ "not authorized on demo_db to execute command { listCollections: 1 }",+ 13,+ {"ok": 0.0, "errmsg": "not authorized on demo_db", "code": 13, "codeName": "Unauthorized"},+ ),+ _MONGO_NOT_AUTHORIZED_MESSAGE,+ False,+ ),+ (+ OperationFailure(+ "Authentication failed.", 18, {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18}+ ),+ _MONGO_AUTHENTICATION_FAILED_MESSAGE,+ False,+ ),+ (+ OperationFailure(+ "bad auth : authentication failed",+ 8000,+ {"ok": 0.0, "errmsg": "bad auth : authentication failed", "code": 8000, "codeName": "AtlasError"},+ ),+ _MONGO_AUTHENTICATION_FAILED_MESSAGE,+ False,+ ),+ (+ OperationFailure(+ "listCollections: unrecognized field 'authorizedCollections'",+ 40415,+ {"ok": 0.0, "errmsg": "unrecognized field", "code": 40415, "codeName": "Location40415"},+ ),+ _MONGO_CONNECT_FAILED_MESSAGE,+ True,+ ),+ ],+ )+ @patch("products.warehouse_sources.backend.temporal.data_imports.sources.mongodb.source.capture_exception")+ @patch("products.warehouse_sources.backend.temporal.data_imports.sources.mongodb.source.get_collection_names")+ def test_operation_failure_capture_behavior(+ self, mock_get_collections, mock_capture, exc, expected_message, should_capture+ ):+ mock_get_collections.side_effect = exc+ config = MongoDBSourceConfig.from_dict({"connection_string": _SRV_WITH_DB})++ ok, err = MongoDBSource().validate_credentials(config, team_id=1)++ assert ok is False+ assert err == expected_message+ assert mock_capture.called is should_capture