Skip to content

Commit 6fe482f

Browse files
takaidohigasiclaude
andcommitted
Re-implement wait event derivation for TiDB activity
- Added _derive_tidb_wait_event() method back - Maps TiDB states to wait events: - 'autocommit' -> CPU - 'wait', 'lock' -> Lock - 'sync', 'syncing' -> Synch - Default -> CPU - Set wait_timer_start/end to match event timers - Provides required wait event fields for GUI compatibility 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2563943 commit 6fe482f

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

mysql/datadog_checks/mysql/activity.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,28 @@ def _get_tidb_activity(self, cursor):
265265
cursor.execute(TIDB_ACTIVITY_QUERY)
266266
return cursor.fetchall()
267267

268+
def _derive_tidb_wait_event(self, state):
269+
# type: (str) -> Tuple[str, str]
270+
"""
271+
Derive wait event and wait event group from TiDB processlist state.
272+
Returns (wait_event, wait_event_group)
273+
"""
274+
if not state:
275+
return 'CPU', 'CPU'
276+
277+
state_lower = state.lower()
278+
279+
# Map TiDB states to wait events
280+
if 'autocommit' in state_lower:
281+
return 'CPU', 'CPU'
282+
elif 'wait' in state_lower or 'lock' in state_lower:
283+
return 'Lock', 'Lock'
284+
elif 'syncing' in state_lower or 'sync' in state_lower:
285+
return 'Synch', 'Synch'
286+
else:
287+
# Default to CPU for active queries
288+
return 'CPU', 'CPU'
289+
268290

269291
def _normalize_tidb_rows(self, rows):
270292
# type: (List[Dict[str]]) -> List[Dict[str]]
@@ -276,6 +298,10 @@ def _normalize_tidb_rows(self, rows):
276298
# Generate unique identifiers for TiDB
277299
thread_id = row.get('processlist_id', 0)
278300

301+
# Derive wait event from state
302+
state = row.get('processlist_state', '')
303+
wait_event, wait_event_group = self._derive_tidb_wait_event(state)
304+
279305
# Convert TiDB fields to match MySQL activity format
280306
normalized_row = {
281307
'thread_id': thread_id,
@@ -289,9 +315,9 @@ def _normalize_tidb_rows(self, rows):
289315
'query_time': row.get('query_time', 0),
290316
'memory_usage': row.get('memory_usage', 0),
291317
'txn_start_time': row.get('txn_start_time'),
292-
# TiDB doesn't have wait events - leave as None
293-
'wait_event': None,
294-
'wait_event_type': None,
318+
# Derived wait events
319+
'wait_event': wait_event,
320+
'wait_event_type': wait_event_group,
295321
}
296322

297323
# Add query truncation state
@@ -349,10 +375,10 @@ def _create_tidb_activity_event(self, active_sessions, tags):
349375
'event_timer_start': event_start_ms * 1000000, # Convert to nanoseconds
350376
'event_timer_end': current_time_ms * 1000000, # Convert to nanoseconds
351377
'lock_time': 0, # TiDB doesn't provide lock time in CLUSTER_PROCESSLIST
352-
# Wait event info - TiDB doesn't have this information
353-
'wait_event': row.get('wait_event'), # Will be None
354-
'wait_timer_start': None,
355-
'wait_timer_end': None,
378+
# Wait event info
379+
'wait_event': row.get('wait_event', 'CPU'),
380+
'wait_timer_start': event_start_ms * 1000000, # Same as event timer
381+
'wait_timer_end': current_time_ms * 1000000,
356382
# Additional MySQL compatibility fields
357383
'object_name': None, # TiDB doesn't track file operations
358384
'object_type': None,

0 commit comments

Comments
 (0)