Skip to content

Commit

Permalink
DBM-2344 Fix resources not sent on service check metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-normand committed Apr 19, 2023
1 parent 85ab590 commit 1455b75
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
4 changes: 2 additions & 2 deletions mysql/datadog_checks/mysql/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(self, name, init_config, instances):
self._agent_hostname = None
self._is_aurora = None
self._config = MySQLConfig(self.instance)
self.tags = copy.copy(self._config.tags)
self.tags = self._config.tags
self.cloud_metadata = self._config.cloud_metadata

# Create a new connection on every check run
Expand Down Expand Up @@ -374,7 +374,7 @@ def _service_check_tags(self, server=None):
server = self._config.mysql_sock if self._config.mysql_sock != '' else self._config.host
service_check_tags = [
'port:{}'.format(self._config.port if self._config.port else 'unix_socket'),
] + self._config.tags
] + self.tags
if not self.disable_generic_tags:
service_check_tags.append('server:{0}'.format(server))
return service_check_tags
Expand Down
5 changes: 3 additions & 2 deletions mysql/tests/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
'tag1:value1',
'tag2:value2',
]
SC_TAGS_MIN = ['port:' + str(common.PORT)]
SC_TAGS_MIN = ['port:' + str(common.PORT), DATABASE_INSTANCE_RESOURCE_TAG.format(hostname='stubbed.hostname')]
SC_TAGS_REPLICA = [
'port:' + str(common.SLAVE_PORT),
'tag1:value1',
'tag2:value2',
'dd.internal.resource:database_instance:stubbed.hostname',
]
SC_FAILURE_TAGS = ['port:unix_socket']
SC_FAILURE_TAGS = ['port:unix_socket', DATABASE_INSTANCE_RESOURCE_TAG.format(hostname='stubbed.hostname')]
46 changes: 24 additions & 22 deletions mysql/tests/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ def test_minimal_config(aggregator, dd_run_check, instance_basic):
def test_complex_config(aggregator, dd_run_check, instance_complex):
mysql_check = MySql(common.CHECK_NAME, {}, [instance_complex])
dd_run_check(mysql_check)

_assert_complex_config(
aggregator,
[tags.DATABASE_INSTANCE_RESOURCE_TAG.format(hostname='stubbed.hostname')],
tags.SC_TAGS + [tags.DATABASE_INSTANCE_RESOURCE_TAG.format(hostname='stubbed.hostname')],
tags.METRIC_TAGS_WITH_RESOURCE,
)
aggregator.assert_metrics_using_metadata(
get_metadata_metrics(), check_submission_type=True, exclude=['alice.age', 'bob.age'] + variables.STATEMENT_VARS
Expand All @@ -82,26 +84,34 @@ def test_complex_config(aggregator, dd_run_check, instance_complex):
@pytest.mark.e2e
def test_e2e(dd_agent_check, dd_default_hostname, instance_complex):
aggregator = dd_agent_check(instance_complex)
_assert_complex_config(aggregator, [], hostname=dd_default_hostname)
_assert_complex_config(
aggregator,
tags.SC_TAGS + [tags.DATABASE_INSTANCE_RESOURCE_TAG.format(hostname=dd_default_hostname)],
tags.METRIC_TAGS,
hostname=dd_default_hostname,
)
aggregator.assert_metrics_using_metadata(
get_metadata_metrics(), exclude=['alice.age', 'bob.age'] + variables.STATEMENT_VARS
)


def _assert_complex_config(aggregator, expected_internal_tags, hostname='stubbed.hostname'):
def _assert_complex_config(aggregator, service_check_tags, metric_tags, hostname='stubbed.hostname'):
# Test service check
aggregator.assert_service_check(
'mysql.can_connect',
status=MySql.OK,
tags=tags.METRIC_TAGS + ['port:' + str(common.PORT)],
tags=service_check_tags,
hostname=hostname,
count=1,
)
if MYSQL_REPLICATION == 'classic':
aggregator.assert_service_check(
'mysql.replication.slave_running',
status=MySql.OK,
tags=tags.METRIC_TAGS + ['port:' + str(common.PORT), 'replication_mode:source'],
tags=service_check_tags
+ [
'replication_mode:source',
],
hostname=hostname,
at_least=1,
)
Expand All @@ -125,13 +135,8 @@ def _assert_complex_config(aggregator, expected_internal_tags, hostname='stubbed
aggregator.assert_service_check(
'mysql.replication.group.status',
status=MySql.OK,
tags=tags.METRIC_TAGS
+ [
'port:' + str(common.PORT),
'channel_name:group_replication_applier',
'member_role:PRIMARY',
'member_state:ONLINE',
],
tags=service_check_tags
+ ['channel_name:group_replication_applier', 'member_role:PRIMARY', 'member_state:ONLINE'],
count=1,
)

Expand All @@ -150,18 +155,14 @@ def _assert_complex_config(aggregator, expected_internal_tags, hostname='stubbed
continue

if mname == 'mysql.performance.query_run_time.avg':
aggregator.assert_metric(mname, tags=tags.METRIC_TAGS + expected_internal_tags + ['schema:testdb'], count=1)
aggregator.assert_metric(mname, tags=tags.METRIC_TAGS + expected_internal_tags + ['schema:mysql'], count=1)
aggregator.assert_metric(mname, tags=metric_tags + ['schema:testdb'], count=1)
aggregator.assert_metric(mname, tags=metric_tags + ['schema:mysql'], count=1)
elif mname == 'mysql.info.schema.size':
aggregator.assert_metric(mname, tags=tags.METRIC_TAGS + expected_internal_tags + ['schema:testdb'], count=1)
aggregator.assert_metric(
mname, tags=tags.METRIC_TAGS + expected_internal_tags + ['schema:information_schema'], count=1
)
aggregator.assert_metric(
mname, tags=tags.METRIC_TAGS + expected_internal_tags + ['schema:performance_schema'], count=1
)
aggregator.assert_metric(mname, tags=metric_tags + ['schema:testdb'], count=1)
aggregator.assert_metric(mname, tags=metric_tags + ['schema:information_schema'], count=1)
aggregator.assert_metric(mname, tags=metric_tags + ['schema:performance_schema'], count=1)
else:
aggregator.assert_metric(mname, tags=tags.METRIC_TAGS + expected_internal_tags, at_least=0)
aggregator.assert_metric(mname, tags=metric_tags, at_least=0)

# TODO: test this if it is implemented
# Assert service metadata
Expand Down Expand Up @@ -318,6 +319,7 @@ def test_correct_hostname(dbm_enabled, reported_hostname, expected_hostname, agg
expected_tags = [
'server:{}'.format(HOST),
'port:{}'.format(PORT),
'dd.internal.resource:database_instance:{}'.format(expected_hostname),
]
aggregator.assert_service_check(
'mysql.can_connect', status=MySql.OK, tags=expected_tags, count=1, hostname=expected_hostname
Expand Down
8 changes: 4 additions & 4 deletions mysql/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,14 @@ def cursor(self):
@pytest.mark.parametrize(
'disable_generic_tags, hostname, expected_tags',
[
(True, None, {'port:unix_socket'}),
(True, None, {'port:unix_socket', 'dd.internal.resource:database_instance:stubbed.hostname'}),
(
False,
None,
{'port:unix_socket', 'server:localhost'},
{'port:unix_socket', 'server:localhost', 'dd.internal.resource:database_instance:stubbed.hostname'},
),
(True, 'foo', {'port:unix_socket'}),
(False, 'foo', {'port:unix_socket', 'server:foo'}),
(True, 'foo', {'port:unix_socket', 'dd.internal.resource:database_instance:stubbed.hostname'}),
(False, 'foo', {'port:unix_socket', 'server:foo', 'dd.internal.resource:database_instance:stubbed.hostname'}),
],
)
def test_service_check(disable_generic_tags, expected_tags, hostname):
Expand Down

0 comments on commit 1455b75

Please sign in to comment.