Skip to content

Commit 13b7646

Browse files
authored
[mongo] Add tag clustername to mongo metrics (#17876)
* Add tag clustername to mongo metrics * add changelog
1 parent 6c65084 commit 13b7646

8 files changed

+14
-9
lines changed

mongo/changelog.d/17876.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tag `clustername` to mongo metrics. This tag is set only when `cluster_name` is provided in the integration configuration.

mongo/datadog_checks/mongo/config.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ def __init__(self, instance, log):
9797
self.custom_queries = instance.get("custom_queries", [])
9898

9999
self._base_tags = list(set(instance.get('tags', [])))
100-
self.service_check_tags = self._compute_service_check_tags()
101-
self.metric_tags = self._compute_metric_tags()
102100

103101
# DBM config options
104102
self.dbm_enabled = is_affirmative(instance.get('dbm', False))
@@ -112,6 +110,10 @@ def __init__(self, instance, log):
112110
# MongoDB instance hostname override
113111
self.reported_database_hostname = instance.get('reported_database_hostname', None)
114112

113+
# Generate tags for service checks and metrics
114+
self.service_check_tags = self._compute_service_check_tags()
115+
self.metric_tags = self._compute_metric_tags()
116+
115117
def _get_clean_server_name(self):
116118
try:
117119
if not self.server:
@@ -143,7 +145,10 @@ def _compute_service_check_tags(self):
143145
return service_check_tags
144146

145147
def _compute_metric_tags(self):
146-
return self._base_tags + ['server:%s' % self.clean_server_name]
148+
tags = self._base_tags + ['server:%s' % self.clean_server_name]
149+
if self.cluster_name:
150+
tags.append('clustername:%s' % self.cluster_name)
151+
return tags
147152

148153
@property
149154
def operation_samples(self):

mongo/tests/results/opeartion-activities-mongos.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"collection_interval": 10,
88
"ddtags": [
99
"server:mongodb://testUser2:*****@localhost:27017/test",
10+
"clustername:my_cluster",
1011
"sharding_cluster_role:mongos"
1112
],
1213
"timestamp": 1715911398111.2722,

mongo/tests/results/opeartion-activities-standalone.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"ddsource": "mongodb",
66
"dbm_type": "activity",
77
"collection_interval": 10,
8-
"ddtags": ["server:mongodb://testUser2:*****@localhost:27017/test"],
8+
"ddtags": ["server:mongodb://testUser2:*****@localhost:27017/test", "clustername:my_cluster"],
99
"timestamp": 1715911398111.2722,
1010
"mongodb_activity": [
1111
{

mongo/tests/results/operation-samples-mongos.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"dbm_type": "plan",
55
"ddagentversion": "0.0.0",
66
"ddsource": "mongodb",
7-
"ddtags": "server:mongodb://testUser2:*****@localhost:27017/test,sharding_cluster_role:mongos",
7+
"ddtags": "server:mongodb://testUser2:*****@localhost:27017/test,clustername:my_cluster,sharding_cluster_role:mongos",
88
"timestamp": 1715911398111.2722,
99
"network": {
1010
"client": {

mongo/tests/results/operation-samples-standalone.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"dbm_type": "plan",
55
"ddagentversion": "0.0.0",
66
"ddsource": "mongodb",
7-
"ddtags": "server:mongodb://testUser2:*****@localhost:27017/test",
7+
"ddtags": "server:mongodb://testUser2:*****@localhost:27017/test,clustername:my_cluster",
88
"timestamp": 1715911398111.2722,
99
"network": {
1010
"client": {

mongo/tests/test_dbm_operation_samples.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,13 @@ def test_mongo_operation_samples_mongos(aggregator, instance_integration_cluster
6868
expected_samples = json.load(f)
6969
assert len(dbm_samples) == len(expected_samples)
7070
for i, sample in enumerate(dbm_samples):
71-
print(json.dumps(sample))
7271
assert sample == expected_samples[i]
7372

7473
# assert activities
7574
with open(os.path.join(HERE, "results", "opeartion-activities-mongos.json"), 'r') as f:
7675
expected_activities = json.load(f)
7776
assert len(dbm_activities) == len(expected_activities)
7877
for i, activity in enumerate(dbm_activities):
79-
print(json.dumps(activity))
8078
assert activity == expected_activities[i]
8179

8280

mongo/tests/test_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def test_integration_mongos(instance_integration_cluster, aggregator, check, dd_
100100
'jumbo',
101101
'sessions',
102102
],
103-
['sharding_cluster_role:mongos'],
103+
['sharding_cluster_role:mongos', 'clustername:my_cluster'],
104104
)
105105

106106
aggregator.assert_all_metrics_covered()

0 commit comments

Comments
 (0)