Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resources not sent on service check metrics #14422

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like this is asserting that the dd.resource.database_instance:* would be in this array twice? I am assuming the test case normalizes the tag set somewhere and not that the tags would show up twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, the array only includes the internal resource tag once. tags.SC_TAGS omits the internal tag because it had to be a formatted string depending on the hostname.

Copy link
Contributor

@jmeunier28 jmeunier28 Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tags.METRIC_TAGS_WITH_RESOURCE includes it, though. Am i just reading this diff incorrectly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does but one of those parameters is the service check tags and the other is the metric tags. They had to be separated because service check tags will include the internal resource tag even in the end to end case while metrics won't have the internal tags (because it gets transformed into actual resources on metrics).

)
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