diff --git a/mongo/changelog.d/18044.fixed b/mongo/changelog.d/18044.fixed new file mode 100644 index 0000000000000..2fd210c9fbf3d --- /dev/null +++ b/mongo/changelog.d/18044.fixed @@ -0,0 +1 @@ +Fix coll or index stats metrics failure when the agent user is not authorized to perform $collStats or $indexStats aggregation on a collection. This fix prevents check to fail when an OperationFailure is raised to run $collStats or $indexStats on system collections such as system.replset on local database. diff --git a/mongo/datadog_checks/mongo/collectors/coll_stats.py b/mongo/datadog_checks/mongo/collectors/coll_stats.py index d8f5e8e8b9e00..6dbf68a6f9354 100644 --- a/mongo/datadog_checks/mongo/collectors/coll_stats.py +++ b/mongo/datadog_checks/mongo/collectors/coll_stats.py @@ -2,6 +2,7 @@ # All rights reserved # Licensed under a 3-clause BSD style license (see LICENSE) +from pymongo.errors import OperationFailure from six import iteritems from datadog_checks.base import AgentCheck @@ -32,7 +33,14 @@ def collect(self, api): coll_names = self._get_collections(api) for coll_name in coll_names: # Grab the stats from the collection - for coll_stats in api.coll_stats(self.db_name, coll_name): + try: + collection_stats = api.coll_stats(self.db_name, coll_name) + except OperationFailure as e: + # Atlas restricts $collStats on system collections + self.log.warning("Could not collect stats for collection %s: %s", coll_name, e) + continue + + for coll_stats in collection_stats: # Submit the metrics storage_stats = coll_stats.get('storageStats', {}) latency_stats = coll_stats.get('latencyStats', {}) diff --git a/mongo/datadog_checks/mongo/collectors/index_stats.py b/mongo/datadog_checks/mongo/collectors/index_stats.py index 425087b1d0c07..b0d0f4976e35f 100644 --- a/mongo/datadog_checks/mongo/collectors/index_stats.py +++ b/mongo/datadog_checks/mongo/collectors/index_stats.py @@ -2,6 +2,8 @@ # All rights reserved # Licensed under a 3-clause BSD style license (see LICENSE) +from pymongo.errors import OperationFailure + from datadog_checks.mongo.collectors.base import MongoCollector @@ -34,6 +36,9 @@ def collect(self, api): ] val = int(stats.get('accesses', {}).get('ops', 0)) self.gauge('mongodb.collection.indexes.accesses.ops', val, idx_tags) + except OperationFailure as e: + # Atlas restricts $indexStats on system collections + self.log.warning("Could not collect index stats for collection %s: %s", coll_name, e) except Exception as e: self.log.error("Could not fetch indexes stats for collection %s: %s", coll_name, e) raise e