1111from cachetools import TTLCache
1212
1313from datadog_checks .base import AgentCheck
14+ from datadog_checks .base .checks .db import DatabaseCheck
1415from datadog_checks .base .config import is_affirmative
1516from datadog_checks .base .utils .db import QueryExecutor , QueryManager
17+ from datadog_checks .base .utils .db .health import HealthEvent , HealthStatus
1618from datadog_checks .base .utils .db .utils import (
1719 TagManager ,
1820 default_json_event_encoding ,
4951 SQLServerXESessionMetrics ,
5052)
5153from datadog_checks .sqlserver .deadlocks import Deadlocks
54+ from datadog_checks .sqlserver .health import SqlServerHealth
5255from datadog_checks .sqlserver .metadata import SqlserverMetadata
5356from datadog_checks .sqlserver .schemas import Schemas
5457from datadog_checks .sqlserver .statements import SqlserverStatementMetrics
5558from datadog_checks .sqlserver .stored_procedures import SqlserverProcedureMetrics
5659from datadog_checks .sqlserver .utils import Database , construct_use_statement , parse_sqlserver_major_version
5760from datadog_checks .sqlserver .xe_collection .registry import get_xe_session_handlers
5861
62+ from .config import sanitize
63+
5964try :
60- import datadog_agent
65+ import datadog_agent # type: ignore
6166except ImportError :
6267 from datadog_checks .base .stubs import datadog_agent
6368
116121set_default_driver_conf ()
117122
118123
119- class SQLServer (AgentCheck ):
124+ class SQLServer (DatabaseCheck ):
120125 __NAMESPACE__ = "sqlserver"
121126
122127 HA_SUPPORTED = True
123128
124129 def __init__ (self , name , init_config , instances ):
125130 super (SQLServer , self ).__init__ (name , init_config , instances )
126131
132+ self .health = SqlServerHealth (self )
133+
127134 self .static_info_cache = TTLCache (
128135 maxsize = 100 ,
129136 # cache these for a full day
@@ -141,6 +148,8 @@ def __init__(self, name, init_config, instances):
141148 self .do_check = True
142149
143150 self ._config = SQLServerConfig (self .init_config , self .instance , self .log )
151+ self ._initialized_at = int (time .time () * 1000 )
152+
144153 self .cloud_metadata = self ._config .cloud_metadata
145154 self .tag_manager = TagManager (normalizer = lambda tag : self .normalize_tag (tag ).lower ())
146155 self .tag_manager .set_tags_from_list (self ._config .tags , replace = True ) # Initialize from static config tags
@@ -183,6 +192,8 @@ def __init__(self, name, init_config, instances):
183192
184193 self ._schemas = Schemas (self , self ._config )
185194
195+ self ._submit_initialization_health_event ()
196+
186197 def initialize_xe_session_handlers (self ):
187198 """Initialize the XE session handlers without starting them"""
188199 # Initialize XE session handlers if not already initialized
@@ -218,6 +229,22 @@ def config_checks(self):
218229 "Autodiscovery is disabled, autodiscovery_include and autodiscovery_exclude will be ignored"
219230 )
220231
232+ def _submit_initialization_health_event (self ):
233+ try :
234+ # Handle the config validation result after we've set tags so those tags are included in the health event
235+ # TODO: Validate the config once it has been refactored
236+ self .health .submit_health_event (
237+ name = HealthEvent .INITIALIZATION ,
238+ status = HealthStatus .OK ,
239+ cooldown_time = 60 * 60 * 6 , # 6 hours
240+ data = {
241+ "initialized_at" : self ._initialized_at ,
242+ "instance" : sanitize (self .instance ),
243+ },
244+ )
245+ except Exception as e :
246+ self .log .error ("Error submitting health event for initialization: %s" , e )
247+
221248 def _new_query_executor (self , queries , executor , extra_tags = None , track_operation_time = False ):
222249 tags = self .tag_manager .get_tags () + (extra_tags or [])
223250 return QueryExecutor (
@@ -299,6 +326,10 @@ def port(self):
299326 def resolve_db_host (self ):
300327 return agent_host_resolver (self .host )
301328
329+ @property
330+ def tags (self ):
331+ return self .tag_manager .get_tags ()
332+
302333 @property
303334 def reported_hostname (self ):
304335 # type: () -> str
@@ -821,6 +852,8 @@ def _check_database_conns(self):
821852 self ._check_connections_by_use_db ()
822853
823854 def check (self , _ ):
855+ self ._submit_initialization_health_event ()
856+
824857 if self .do_check :
825858 self .load_static_information ()
826859 # configure custom queries for the check
0 commit comments