diff --git a/mysql/datadog_checks/mysql/mysql.py b/mysql/datadog_checks/mysql/mysql.py index 8d3d3c09b9b8e..e144c877b6eeb 100644 --- a/mysql/datadog_checks/mysql/mysql.py +++ b/mysql/datadog_checks/mysql/mysql.py @@ -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 @@ -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 diff --git a/mysql/tests/tags.py b/mysql/tests/tags.py index 364f9aaa52e77..71f9e91279a36 100644 --- a/mysql/tests/tags.py +++ b/mysql/tests/tags.py @@ -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')] diff --git a/mysql/tests/test_mysql.py b/mysql/tests/test_mysql.py index fdb271b0ad0a6..784bf1e11903a 100644 --- a/mysql/tests/test_mysql.py +++ b/mysql/tests/test_mysql.py @@ -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 @@ -82,18 +84,23 @@ 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, ) @@ -101,7 +108,10 @@ def _assert_complex_config(aggregator, expected_internal_tags, hostname='stubbed 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, ) @@ -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, ) @@ -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 @@ -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 diff --git a/mysql/tests/test_unit.py b/mysql/tests/test_unit.py index b49c201152f17..cdd1b5cbbd9e1 100644 --- a/mysql/tests/test_unit.py +++ b/mysql/tests/test_unit.py @@ -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):