91
91
).strip ()
92
92
93
93
# Turns out sys.dm_exec_requests does not contain idle sessions.
94
- # Inner joining dm_exec_sessions with dm_exec_requests will not return any idle sessions.
95
- # This prevent us reusing the same ACTIVITY_QUERY for regular activities and idle sessions.
94
+ # Inner joining dm_exec_sessions with dm_exec_requests will not return any idle blocking sessions.
95
+ # This prevent us reusing the same ACTIVITY_QUERY for regular activities and idle blocking sessions.
96
96
# The query below is used for idle sessions and does not join with dm_exec_requests.
97
97
# The last query execution on the connection is fetched from dm_exec_connections.most_recent_sql_handle.
98
- IDLE_SESSIONS_QUERY = re .sub (
98
+ IDLE_BLOCKING_SESSIONS_QUERY = re .sub (
99
99
r'\s+' ,
100
100
' ' ,
101
101
"""\
114
114
c.client_net_address as client_address,
115
115
sess.host_name as host_name,
116
116
sess.program_name as program_name,
117
- sess.is_user_process as is_user_process,
118
- sess.context_info as context_info
117
+ sess.is_user_process as is_user_process
119
118
FROM sys.dm_exec_sessions sess
120
119
INNER JOIN sys.dm_exec_connections c
121
120
ON sess.session_id = c.session_id
122
121
CROSS APPLY sys.dm_exec_sql_text(c.most_recent_sql_handle) lqt
123
- WHERE
122
+ WHERE sess.status = 'sleeping'
123
+ AND sess.session_id IN ({blocking_session_ids})
124
+ AND c.session_id IN ({blocking_session_ids})
124
125
""" ,
125
126
).strip ()
126
127
127
- IDLE_BLOCKERS_FILTER = """(sess.status = 'sleeping'
128
- AND sess.session_id IN ({blocking_session_ids})
129
- AND c.session_id IN ({blocking_session_ids}))"""
130
-
131
- IDLE_RECENTLY_ACTIVE_FILTER = (
132
- "(sess.status = 'sleeping' AND sess.last_request_start_time > DATEADD(SECOND, - ?, GETDATE()))"
133
- )
134
-
135
-
136
128
# enumeration of the columns we collect
137
129
# from sys.dm_exec_requests
138
130
DM_EXEC_REQUESTS_COLS = [
@@ -203,10 +195,6 @@ def __init__(self, check, config: SQLServerConfig):
203
195
self ._conn_key_prefix = "dbm-activity-"
204
196
self ._activity_payload_max_bytes = MAX_PAYLOAD_BYTES
205
197
self ._exec_requests_cols_cached = None
206
- self ._sample_recently_active_idle_sessions = is_affirmative (
207
- self ._config .activity_config .get ('sample_recently_active_idle_sessions' , False )
208
- )
209
- self ._time_since_last_activity_event = 0
210
198
211
199
self ._collect_raw_query_statement = self ._config .collect_raw_query_statement .get ("enabled" , False )
212
200
self ._raw_statement_text_cache = RateLimitingTTLCache (
@@ -231,34 +219,15 @@ def _get_active_connections(self, cursor):
231
219
self .log .debug ("loaded sql server current connections len(rows)=%s" , len (rows ))
232
220
return rows
233
221
234
- def _is_sample_idle_recently_active_sessions (self ) -> bool :
235
- return self ._sample_recently_active_idle_sessions and self ._time_since_last_activity_event
236
-
237
222
@tracked_method (agent_check_getter = agent_check_getter , track_result_length = True )
238
- def _get_idle_sessions (self , cursor , blocking_session_ids ):
239
- query = IDLE_SESSIONS_QUERY .format (
223
+ def _get_idle_blocking_sessions (self , cursor , blocking_session_ids ):
224
+ # The IDLE_BLOCKING_SESSIONS_QUERY contains minimum information on idle blocker
225
+ query = IDLE_BLOCKING_SESSIONS_QUERY .format (
226
+ blocking_session_ids = "," .join (map (str , blocking_session_ids )),
240
227
proc_char_limit = self ._config .stored_procedure_characters_limit ,
241
228
)
242
- idle_filter = None
243
-
244
- # a stateful closure that appends filters to the query
245
- def _append_filter (filter : str ) -> str :
246
- if idle_filter :
247
- return f"{ idle_filter } OR { filter } "
248
- return filter
249
-
250
- if blocking_session_ids :
251
- idle_filter = _append_filter (
252
- IDLE_BLOCKERS_FILTER .format (blocking_session_ids = "," .join (map (str , blocking_session_ids )))
253
- )
254
- if self ._is_sample_idle_recently_active_sessions ():
255
- idle_filter = _append_filter (IDLE_RECENTLY_ACTIVE_FILTER )
256
- query += idle_filter
257
229
self .log .debug ("Running query [%s]" , query )
258
- if self ._is_sample_idle_recently_active_sessions ():
259
- cursor .execute (query , (int (time .time () - self ._time_since_last_activity_event ),))
260
- else :
261
- cursor .execute (query )
230
+ cursor .execute (query )
262
231
columns = [i [0 ] for i in cursor .description ]
263
232
rows = [dict (zip (columns , row )) for row in cursor .fetchall ()]
264
233
return rows
@@ -284,9 +253,9 @@ def _get_activity(self, cursor, exec_request_columns, input_buffer_columns, inpu
284
253
blocking_session_ids = {r ['blocking_session_id' ] for r in rows if r ['blocking_session_id' ]}
285
254
# if there are blocking sessions and some of the session(s) are not captured in the activity query
286
255
idle_blocking_session_ids = blocking_session_ids - session_ids
287
- if idle_blocking_session_ids or self . _is_sample_idle_recently_active_sessions () :
288
- idle_sessions = self ._get_idle_sessions (cursor , idle_blocking_session_ids )
289
- rows .extend (idle_sessions )
256
+ if idle_blocking_session_ids :
257
+ idle_blocking_sessions = self ._get_idle_blocking_sessions (cursor , idle_blocking_session_ids )
258
+ rows .extend (idle_blocking_sessions )
290
259
return rows
291
260
292
261
def _normalize_queries_and_filter_rows (self , rows , max_bytes_limit ):
@@ -464,7 +433,6 @@ def _get_estimated_row_size_bytes(row):
464
433
return len (str (row ))
465
434
466
435
def _create_activity_event (self , active_sessions , active_connections ):
467
- self ._time_since_last_activity_event = time .time ()
468
436
event = {
469
437
"host" : self ._check .resolved_hostname ,
470
438
"ddagentversion" : datadog_agent .get_version (),
@@ -485,7 +453,7 @@ def _create_activity_event(self, active_sessions, active_connections):
485
453
@tracked_method (agent_check_getter = agent_check_getter )
486
454
def collect_activity (self ):
487
455
"""
488
- Collects all current activity for the SQLServer instance .
456
+ Collects all current activity for the SQLServer intance .
489
457
:return:
490
458
"""
491
459
0 commit comments