Skip to content

Commit

Permalink
[mongo] Continue coll/index stats collection if user is not authorize…
Browse files Browse the repository at this point in the history
…d to perform aggregation (DataDog#18044)

* Continue collection of coll stats if user is not authorized to perform collStats aggregation

* add changelog

* fix lint

* continue on index stats collection if user is not authorized

* update changelog
  • Loading branch information
lu-zhengda authored and ravindrasojitra-crest committed Aug 5, 2024
1 parent 2bbf815 commit c3b5719
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions mongo/changelog.d/18044.fixed
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 9 additions & 1 deletion mongo/datadog_checks/mongo/collectors/coll_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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', {})
Expand Down
5 changes: 5 additions & 0 deletions mongo/datadog_checks/mongo/collectors/index_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

0 comments on commit c3b5719

Please sign in to comment.