From 1b5e51f0377540666a4c99eda55ab003b7f0f878 Mon Sep 17 00:00:00 2001 From: Zhengda Lu Date: Thu, 5 Sep 2024 12:23:54 -0400 Subject: [PATCH] [mongo] Add warnings when agent user is not authorized to run collStats and indexStats on collections (#18506) * raise unauthorized exception when running collStats * log different error with unauthorized op * more error handling * add changelog --- mongo/changelog.d/18506.added | 1 + .../datadog_checks/mongo/collectors/coll_stats.py | 15 +++++++++++++-- .../mongo/collectors/index_stats.py | 11 +++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 mongo/changelog.d/18506.added diff --git a/mongo/changelog.d/18506.added b/mongo/changelog.d/18506.added new file mode 100644 index 0000000000000..1b39055d84c9b --- /dev/null +++ b/mongo/changelog.d/18506.added @@ -0,0 +1 @@ +Add warning logs when agent user is not authorized to run $collStats and $indexStats on collections. diff --git a/mongo/datadog_checks/mongo/collectors/coll_stats.py b/mongo/datadog_checks/mongo/collectors/coll_stats.py index e165012eae9ea..68778a5546e16 100644 --- a/mongo/datadog_checks/mongo/collectors/coll_stats.py +++ b/mongo/datadog_checks/mongo/collectors/coll_stats.py @@ -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)] @@ -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: diff --git a/mongo/datadog_checks/mongo/collectors/index_stats.py b/mongo/datadog_checks/mongo/collectors/index_stats.py index e58a5f5cdfc54..da164c93d27e2 100644 --- a/mongo/datadog_checks/mongo/collectors/index_stats.py +++ b/mongo/datadog_checks/mongo/collectors/index_stats.py @@ -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