Skip to content

Commit

Permalink
[mongo] add index usage per second metric (#18405)
Browse files Browse the repository at this point in the history
* add index usage per second metric

* add changelog
  • Loading branch information
lu-zhengda authored Aug 23, 2024
1 parent 9f0e9cf commit 961bed2
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 5 deletions.
1 change: 1 addition & 0 deletions mongo/changelog.d/18405.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add metric `mongodb.collection.indexes.accesses.opsps` to measure number of times the index was used per second.
4 changes: 2 additions & 2 deletions mongo/datadog_checks/mongo/collectors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@ def _submit_payload(self, payload, additional_tags=None, metrics_to_collect=None

metric_name_alias = self._normalize(metric_name_alias, submit_method, prefix)
submit_method(self.check, metric_name_alias, value, tags=tags)
if metric_name_alias.endswith("countps"):
# Keep old incorrect metric name (only 'top' metrics are affected)
if metric_name_alias.endswith("countps") or metric_name_alias.endswith("accesses.opsps"):
# Keep old incorrect metric name (only 'top' and 'index' metrics are affected)
self.gauge(metric_name_alias[:-2], value, tags=tags)
8 changes: 5 additions & 3 deletions mongo/datadog_checks/mongo/collectors/index_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pymongo.errors import OperationFailure

from datadog_checks.mongo.collectors.base import MongoCollector
from datadog_checks.mongo.metrics import INDEX_METRICS


class IndexStatsCollector(MongoCollector):
Expand All @@ -29,13 +30,14 @@ def collect(self, api):
for coll_name in coll_names:
try:
for stats in api.index_stats(self.db_name, coll_name):
idx_tags = self.base_tags + [
additional_tags = [
"name:{0}".format(stats.get('name', 'unknown')),
"collection:{0}".format(coll_name),
"db:{0}".format(self.db_name),
]
val = int(stats.get('accesses', {}).get('ops', 0))
self.gauge('mongodb.collection.indexes.accesses.ops', val, idx_tags)
if stats.get('shard'):
additional_tags.append("shard:{0}".format(stats['shard']))
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)
Expand Down
4 changes: 4 additions & 0 deletions mongo/datadog_checks/mongo/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@
'collection.collectionScans.nonTailable': GAUGE,
}

INDEX_METRICS = {
'indexes.accesses.ops': RATE,
}

"""
Mapping for case-sensitive metric name suffixes.
Expand Down
1 change: 1 addition & 0 deletions mongo/metadata.csv
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mongodb.collection.commands.latency.avg,gauge,,microsecond,,Average latency for
mongodb.collection.commands.opsps,gauge,,operation,second,Number of command operations per second on the collection.,0,mongodb,command operations per second,,
mongodb.collection.count,gauge,,item,,Total number of objects in the collection.,0,mongodb,number of objects in the collection,,
mongodb.collection.indexes.accesses.ops,gauge,,event,,Number of time the index was used.,0,mongodb,index usage,,
mongodb.collection.indexes.accesses.opsps,gauge,,event,,Number of time the index was used per second.,0,mongodb,index usage per second,,
mongodb.collection.indexsizes,gauge,,byte,,Size of index in bytes.,0,mongodb,index size,,
mongodb.collection.max,gauge,,document,,Maximum number of documents in a capped collection.,0,mongodb,max documents in capped collection,,
mongodb.collection.maxsize,gauge,,byte,,Maximum size of a capped collection in bytes.,0,mongodb,max size of capped collection,,
Expand Down
88 changes: 88 additions & 0 deletions mongo/tests/results/metrics-indexes-stats-autodiscover.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,93 @@
"collection:bar",
"db:integration"
]
},
{
"name": "mongodb.collection.indexes.accesses.opsps",
"type": 1,
"value": 1243.0,
"tags": [
"server:mongodb://testUser2:*****@localhost:27017/test",
"name:_id_",
"collection:foo",
"db:test"
]
},
{
"name": "mongodb.collection.indexes.accesses.opsps",
"type": 1,
"value": 12.0,
"tags": [
"server:mongodb://testUser2:*****@localhost:27017/test",
"name:_id_",
"collection:bar",
"db:test"
]
},
{
"name": "mongodb.collection.indexes.accesses.opsps",
"type": 1,
"value": 1243.0,
"tags": [
"server:mongodb://testUser2:*****@localhost:27017/test",
"name:_id_",
"collection:foo",
"db:admin"
]
},
{
"name": "mongodb.collection.indexes.accesses.opsps",
"type": 1,
"value": 12.0,
"tags": [
"server:mongodb://testUser2:*****@localhost:27017/test",
"name:_id_",
"collection:bar",
"db:admin"
]
},
{
"name": "mongodb.collection.indexes.accesses.opsps",
"type": 1,
"value": 1243.0,
"tags": [
"server:mongodb://testUser2:*****@localhost:27017/test",
"name:_id_",
"collection:foo",
"db:config"
]
},
{
"name": "mongodb.collection.indexes.accesses.opsps",
"type": 1,
"value": 12.0,
"tags": [
"server:mongodb://testUser2:*****@localhost:27017/test",
"name:_id_",
"collection:bar",
"db:config"
]
},
{
"name": "mongodb.collection.indexes.accesses.opsps",
"type": 1,
"value": 1243.0,
"tags": [
"server:mongodb://testUser2:*****@localhost:27017/test",
"name:_id_",
"collection:foo",
"db:integration"
]
},
{
"name": "mongodb.collection.indexes.accesses.opsps",
"type": 1,
"value": 12.0,
"tags": [
"server:mongodb://testUser2:*****@localhost:27017/test",
"name:_id_",
"collection:bar",
"db:integration"
]
}
]
22 changes: 22 additions & 0 deletions mongo/tests/results/metrics-indexes-stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,27 @@
"collection:bar",
"db:test"
]
},
{
"name": "mongodb.collection.indexes.accesses.opsps",
"type": 1,
"value": 1243.0,
"tags": [
"server:mongodb://testUser2:*****@localhost:27017/test",
"name:_id_",
"collection:foo",
"db:test"
]
},
{
"name": "mongodb.collection.indexes.accesses.opsps",
"type": 1,
"value": 12.0,
"tags": [
"server:mongodb://testUser2:*****@localhost:27017/test",
"name:_id_",
"collection:bar",
"db:test"
]
}
]

0 comments on commit 961bed2

Please sign in to comment.