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

Stop query activity collection due to misconfiguration #12343

Merged
merged 7 commits into from
Jun 8, 2022
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
16 changes: 15 additions & 1 deletion mysql/datadog_checks/mysql/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from datadog_checks.base.utils.serialization import json
from datadog_checks.base.utils.tracking import tracked_method

from .util import get_truncation_state
from .util import DatabaseConfigurationError, get_truncation_state, warning_with_tags

try:
import datadog_agent
Expand Down Expand Up @@ -135,6 +135,20 @@ def __init__(self, check, config, connection_args):

def run_job(self):
# type: () -> None
# Detect a database misconfiguration by checking if `events-waits-current` is enabled.
if not self._check.events_wait_current_enabled:
self._check.record_warning(
DatabaseConfigurationError.events_waits_current_not_enabled,
warning_with_tags(
'Query activity and wait event collection is disabled on this host. To enable it, the setup '
'consumer `performance-schema-consumer-events-waits-current` must be enabled on the MySQL server. '
'Please refer to the troubleshooting documentation: '
'https://docs.datadoghq.com/database_monitoring/setup_mysql/troubleshooting/',
code=DatabaseConfigurationError.events_waits_current_not_enabled.value,
host=self._check.resolved_hostname,
),
)
return
self._check_version()
self._collect_activity()

Expand Down
32 changes: 28 additions & 4 deletions mysql/datadog_checks/mysql/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def __init__(self, name, init_config, instances):
self.innodb_stats = InnoDBMetrics()
self.check_initializations.append(self._config.configuration_checks)
self.performance_schema_enabled = None
self.events_wait_current_enabled = None
self._warnings_by_code = {}
self._statement_metrics = MySQLStatementMetrics(self, self._config, self._get_connection_args())
self._statement_samples = MySQLStatementSamples(self, self._config, self._get_connection_args())
Expand Down Expand Up @@ -138,7 +139,11 @@ def agent_hostname(self):
self._agent_hostname = datadog_agent.get_hostname()
return self._agent_hostname

def check_performance_schema_enabled(self, db):
def _check_database_configuration(self, db):
self._check_performance_schema_enabled(db)
self._check_events_wait_current_enabled(db)

def _check_performance_schema_enabled(self, db):
if self.performance_schema_enabled is None:
with closing(db.cursor()) as cursor:
cursor.execute("SHOW VARIABLES LIKE 'performance_schema'")
Expand All @@ -147,6 +152,24 @@ def check_performance_schema_enabled(self, db):

return self.performance_schema_enabled

def _check_events_wait_current_enabled(self, db):
if not self._check_performance_schema_enabled(db):
self.log.debug('`performance_schema` is required to enable `events_waits_current`')
return
if self.events_wait_current_enabled is None:
with closing(db.cursor()) as cursor:
cursor.execute(
"""\
SELECT
NAME,
ENABLED
FROM performance_schema.setup_consumers WHERE NAME = 'events_waits_current'
"""
)
Comment on lines +161 to +168
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a nit that is out of scope for this PR because the pattern already exists, but for the future I think it would be better to make this a boolean function and change the query to:

 cursor.execute(
                    """\
                    SELECT count(*)
                    FROM performance_schema.setup_consumers WHERE NAME = 'events_waits_current'
                    AND ENABLED = 'YES'
                    """
                )
return cursor.fetchone()[0] > 0

This would avoid leaking state and making the monolithic class more complex with additional public variables.

results = dict(cursor.fetchall())
self.events_wait_current_enabled = self._get_variable_enabled(results, 'events_waits_current')
return self.events_wait_current_enabled

def resolve_db_host(self):
return agent_host_resolver(self._config.host)

Expand Down Expand Up @@ -178,7 +201,7 @@ def check(self, _):
if self._get_is_aurora(db):
tags = tags + self._get_runtime_aurora_tags(db)

self.check_performance_schema_enabled(db)
self._check_database_configuration(db)

# Metric collection
if not self._config.only_custom_queries:
Expand Down Expand Up @@ -910,9 +933,10 @@ def _get_replica_status(self, db, above_560, nonblocking):
def _are_values_numeric(cls, array):
return all(v.isdigit() for v in array)

def _get_variable_enabled(self, results, var):
@staticmethod
def _get_variable_enabled(results, var):
alexbarksdale marked this conversation as resolved.
Show resolved Hide resolved
enabled = collect_string(var, results)
return enabled and enabled.lower().strip() == 'on'
return enabled and is_affirmative(enabled.lower().strip())

def _get_query_exec_time_95th_us(self, db):
# Fetches the 95th percentile query execution time and returns the value
Expand Down
1 change: 1 addition & 0 deletions mysql/datadog_checks/mysql/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DatabaseConfigurationError(Enum):
explain_plan_procedure_missing = 'explain-plan-procedure-missing'
explain_plan_fq_procedure_missing = 'explain-plan-fq-procedure-missing'
performance_schema_not_enabled = 'performance-schema-not-enabled'
events_waits_current_not_enabled = 'events-waits-current-not-enabled'


def warning_with_tags(warning_message, *args, **kwargs):
Expand Down