1515import pymysql
1616from cachetools import TTLCache
1717
18- from datadog_checks .base import AgentCheck , is_affirmative
18+ from datadog_checks .base import AgentCheck , DatabaseCheck , is_affirmative
1919from datadog_checks .base .utils .db import QueryExecutor , QueryManager
20+ from datadog_checks .base .utils .db .health import HealthEvent , HealthStatus
2021from datadog_checks .base .utils .db .utils import (
2122 TagManager ,
2223 default_json_event_encoding ,
2829from datadog_checks .base .utils .serialization import json
2930from datadog_checks .mysql import aws
3031from datadog_checks .mysql .cursor import CommenterCursor , CommenterDictCursor , CommenterSSCursor
32+ from datadog_checks .mysql .health import MySqlHealth
3133
3234from .__about__ import __version__
3335from .activity import MySQLActivity
3436from .collection_utils import collect_all_scalars , collect_scalar , collect_string , collect_type
35- from .config import MySQLConfig
37+ from .config import MySQLConfig , sanitize
3638from .const import (
3739 AWS_RDS_HOSTNAME_SUFFIX ,
3840 AZURE_DEPLOYMENT_TYPE_TO_RESOURCE_TYPE ,
9597 PSUTIL_AVAILABLE = False
9698
9799try :
98- import datadog_agent
100+ import datadog_agent # type: ignore
99101except ImportError :
100102 from datadog_checks .base .stubs import datadog_agent
101103
102104
103- class MySql (AgentCheck ):
105+ class MySql (DatabaseCheck ):
104106 SERVICE_CHECK_NAME = 'mysql.can_connect'
105107 SLAVE_SERVICE_CHECK_NAME = 'mysql.replication.slave_running'
106108 REPLICA_SERVICE_CHECK_NAME = 'mysql.replication.replica_running'
@@ -110,6 +112,7 @@ class MySql(AgentCheck):
110112
111113 def __init__ (self , name , init_config , instances ):
112114 super (MySql , self ).__init__ (name , init_config , instances )
115+ self .health = MySqlHealth (self )
113116 self .qcache_stats = {}
114117 self .version = None
115118 self .is_mariadb = None
@@ -122,6 +125,7 @@ def __init__(self, name, init_config, instances):
122125 self ._events_wait_current_enabled = None
123126 self ._group_replication_active = None
124127 self ._replication_role = None
128+ self ._initialized_at = int (time .time () * 1000 )
125129 self ._config = MySQLConfig (self .instance , init_config )
126130 self .tag_manager = TagManager ()
127131 self .tag_manager .set_tags_from_list (self ._config .tags , replace = True ) # Initialize from static config tags
@@ -167,6 +171,21 @@ def __init__(self, name, init_config, instances):
167171 self .set_resource_tags ()
168172 self ._is_innodb_engine_enabled_cached = None
169173
174+ self ._submit_initialization_health_event ()
175+
176+ def _submit_initialization_health_event (self ):
177+ try :
178+ # Handle the config validation result after we've set tags so those tags are included in the health event
179+ # TODO: validate the config once it is refactored similar to Postgres, and then send the computed config
180+ self .health .submit_health_event (
181+ name = HealthEvent .INITIALIZATION ,
182+ status = HealthStatus .OK ,
183+ cooldown_time = 60 * 60 * 6 , # 6 hours
184+ data = {"initialized_at" : self ._initialized_at , "instance" : sanitize (self .instance )},
185+ )
186+ except Exception as e :
187+ self .log .error ("Error submitting health event for initialization: %s" , e )
188+
170189 def execute_query_raw (self , query ):
171190 with closing (self ._conn .cursor (CommenterSSCursor )) as cursor :
172191 cursor .execute (query )
@@ -179,6 +198,10 @@ def _send_metadata(self):
179198 self .set_metadata ('flavor' , self .version .flavor )
180199 self .set_metadata ('resolved_hostname' , self .resolved_hostname )
181200
201+ @property
202+ def tags (self ):
203+ return self .tag_manager .get_tags ()
204+
182205 @property
183206 def reported_hostname (self ):
184207 # type: () -> str
@@ -352,6 +375,8 @@ def get_library_versions(cls):
352375 return {'pymysql' : pymysql .__version__ }
353376
354377 def check (self , _ ):
378+ self ._submit_initialization_health_event ()
379+
355380 if self .instance .get ('user' ):
356381 self ._log_deprecation ('_config_renamed' , 'user' , 'username' )
357382
0 commit comments