Skip to content

Commit 58abe3d

Browse files
authored
[mongo] Add cluster_name to metrics tags (#17576)
1 parent 474d918 commit 58abe3d

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

mongo/changelog.d/17576.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adds tag `cluster_name` to metrics

mongo/datadog_checks/mongo/config.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ def __init__(self, instance, log):
9595
self.coll_names = instance.get('collections', [])
9696
self.custom_queries = instance.get("custom_queries", [])
9797

98-
self._base_tags = list(set(instance.get('tags', [])))
99-
self.service_check_tags = self._compute_service_check_tags()
100-
self.metric_tags = self._compute_metric_tags()
101-
10298
# DBM config options
10399
self.dbm_enabled = is_affirmative(instance.get('dbm', False))
104100
self.database_instance_collection_interval = instance.get('database_instance_collection_interval', 1800)
@@ -107,6 +103,10 @@ def __init__(self, instance, log):
107103
if self.dbm_enabled and not self.cluster_name:
108104
raise ConfigurationError('`cluster_name` must be set when `dbm` is enabled')
109105

106+
self._base_tags = list(set(instance.get('tags', [])))
107+
self.service_check_tags = self._compute_service_check_tags()
108+
self.metric_tags = self._compute_metric_tags()
109+
110110
def _get_clean_server_name(self):
111111
try:
112112
if not self.server:
@@ -138,4 +138,7 @@ def _compute_service_check_tags(self):
138138
return service_check_tags
139139

140140
def _compute_metric_tags(self):
141-
return self._base_tags + ['server:%s' % self.clean_server_name]
141+
tags = self._base_tags + ['server:%s' % self.clean_server_name]
142+
if self.cluster_name:
143+
tags.append('cluster_name:%s' % self.cluster_name)
144+
return tags

mongo/tests/test_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_integration_mongos(instance_integration_cluster, aggregator, check, dd_
9696
'jumbo',
9797
'sessions',
9898
],
99-
['sharding_cluster_role:mongos'],
99+
['sharding_cluster_role:mongos', 'cluster_name:my_cluster'],
100100
)
101101

102102
aggregator.assert_all_metrics_covered()
@@ -112,7 +112,7 @@ def test_integration_mongos(instance_integration_cluster, aggregator, check, dd_
112112
)
113113
assert len(aggregator._events) == 0
114114

115-
expected_tags = ['server:mongodb://localhost:27017/', 'sharding_cluster_role:mongos']
115+
expected_tags = ['server:mongodb://localhost:27017/', 'sharding_cluster_role:mongos', 'cluster_name:my_cluster']
116116
_assert_mongodb_instance_event(
117117
aggregator,
118118
mongos_check,

0 commit comments

Comments
 (0)