Skip to content

Commit 48a7ac4

Browse files
jasonmp85claude
andauthored
[mongo] Include database_instance tag in mongodb.can_connect service check (#23476)
- Both service_check() calls in check() now use _get_service_check_tags() instead of self._config.service_check_tags directly, adding the database_instance:<resolved_hostname> tag used by the DBM frontend - Stop clearing _resolved_hostname in _unset_metadata() so the tag remains stable across consecutive CRITICAL checks during an outage - Update tag assertions in test_integration_standalone.py and test_integration_shard.py to include the new tag Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 7af3387 commit 48a7ac4

4 files changed

Lines changed: 11 additions & 5 deletions

File tree

mongo/changelog.d/23476.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Include `database_instance` tag in `mongodb.can_connect` service check.

mongo/datadog_checks/mongo/mongo.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ def check(self, _):
296296
self._schemas.run_job_loop(tags=self._get_tags(include_internal_resource_tags=True))
297297
self._query_metrics.run_job_loop(tags=self._get_tags(include_internal_resource_tags=True))
298298
except CRITICAL_FAILURE as e:
299-
self.service_check(SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=self._config.service_check_tags)
299+
self.service_check(SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=self._get_service_check_tags())
300300
self._unset_metadata()
301301
raise e # Let exception bubble up to global handler and show full error in the logs.
302302
else:
303-
self.service_check(SERVICE_CHECK_NAME, AgentCheck.OK, tags=self._config.service_check_tags)
303+
self.service_check(SERVICE_CHECK_NAME, AgentCheck.OK, tags=self._get_service_check_tags())
304304

305305
def _refresh_metadata(self):
306306
if self._mongo_version is None or self._mongo_modules is None:
@@ -323,7 +323,6 @@ def _refresh_metadata(self):
323323
def _unset_metadata(self):
324324
self.log.debug('Due to connection failure we will need to reset the metadata.')
325325
self._mongo_version = None
326-
self._resolved_hostname = None
327326

328327
def _collect_metrics(self):
329328
deployment = self.deployment_type

mongo/tests/test_integration_shard.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ def test_mongo_arbiter(aggregator, check, instance_arbiter, dd_run_check):
1717
check = check(instance_arbiter)
1818
dd_run_check(check)
1919

20-
tags = [f'host:{common.HOST}', f'port:{common.PORT_ARBITER}', 'db:admin']
20+
tags = [
21+
f'host:{common.HOST}',
22+
f'port:{common.PORT_ARBITER}',
23+
'db:admin',
24+
f'database_instance:{check._resolved_hostname}',
25+
]
2126
aggregator.assert_service_check('mongodb.can_connect', status=MongoDb.OK, tags=tags)
2227

2328
metric_names = aggregator.metric_names

mongo/tests/test_integration_standalone.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_mongo_db_test(aggregator, check, instance_user, dd_run_check):
5656
check = check(instance_user)
5757
dd_run_check(check)
5858

59-
tags = [f'host:{common.HOST}', f'port:{common.PORT1}', 'db:test']
59+
tags = [f'host:{common.HOST}', f'port:{common.PORT1}', 'db:test', f'database_instance:{check._resolved_hostname}']
6060
aggregator.assert_service_check('mongodb.can_connect', status=MongoDb.OK, tags=tags)
6161

6262
metric_names = aggregator.metric_names
@@ -289,5 +289,6 @@ def test_propagate_agent_tags(
289289
f'host:{common.HOST}',
290290
f'port:{common.PORT1}',
291291
'db:test',
292+
f'database_instance:{check._resolved_hostname}',
292293
] + agent_tags
293294
aggregator.assert_service_check('mongodb.can_connect', status=MongoDb.OK, tags=expected_tags)

0 commit comments

Comments
 (0)