Skip to content

Commit

Permalink
[mongo] Add warnings when agent user is not authorized to run collSta…
Browse files Browse the repository at this point in the history
…ts and indexStats on collections (#18506)

* raise unauthorized exception when running collStats

* log different error with unauthorized op

* more error handling

* add changelog
  • Loading branch information
lu-zhengda authored Sep 5, 2024
1 parent 19df64a commit 1b5e51f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions mongo/changelog.d/18506.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add warning logs when agent user is not authorized to run $collStats and $indexStats on collections.
15 changes: 13 additions & 2 deletions mongo/datadog_checks/mongo/collectors/coll_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ def _get_collection_stats(self, api, coll_name):
try:
return api.coll_stats(self.db_name, coll_name)
except OperationFailure as e:
if e.code == 13:
# Unauthorized to run $collStats, do not try use the compatible mode, raise the exception
raise e
# Failed to get collection stats using $collStats aggregation
self.log.debug(
"Failed not collect stats for collection %s with $collStats, fallback to collStats command",
coll_name,
e,
e.details,
)
self.coll_stats_pipeline_supported = False
return [api.coll_stats_compatable(self.db_name, coll_name)]
Expand All @@ -61,7 +64,15 @@ def collect(self, api):
collection_stats = self._get_collection_stats(api, 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)
if e.code == 13:
self.log.warning("Unauthorized to run $collStats on collection %s.%s", self.db_name, coll_name)
else:
self.log.warning(
"Could not collect stats for collection %s.%s: %s", self.db_name, coll_name, e.details
)
continue
except Exception as e:
self.log.error("Unexpected error when fetch stats for collection %s.%s: %s", self.db_name, coll_name, e)
continue

for coll_stats in collection_stats:
Expand Down
11 changes: 9 additions & 2 deletions mongo/datadog_checks/mongo/collectors/index_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ def collect(self, api):
self._submit_payload({"indexes": stats}, additional_tags, INDEX_METRICS, "collection")
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)
if e.code == 13:
self.log.warning("Unauthorized to run $indexStats on collection %s.%s", self.db_name, coll_name)
else:
self.log.warning(
"Could not collect index stats for collection %s.%s: %s", self.db_name, coll_name, e.details
)
except Exception as e:
self.log.error("Could not fetch indexes stats for collection %s: %s", coll_name, e)
self.log.error(
"Unexpected error when fetch indexes stats for collection %s.%s: %s", self.db_name, coll_name, e
)
raise e

0 comments on commit 1b5e51f

Please sign in to comment.