-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathtest_stored_procedures.py
More file actions
380 lines (339 loc) · 14.1 KB
/
Copy pathtest_stored_procedures.py
File metadata and controls
380 lines (339 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# (C) Datadog, Inc. 2023-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from __future__ import unicode_literals
import logging
from concurrent.futures.thread import ThreadPoolExecutor
from copy import copy
import pytest
from datadog_checks.base.utils.db.utils import DBMAsyncJob
from datadog_checks.sqlserver import SQLServer
from datadog_checks.sqlserver.const import (
ENGINE_EDITION_ENTERPRISE,
ENGINE_EDITION_EXPRESS,
ENGINE_EDITION_PERSONAL,
ENGINE_EDITION_STANDARD,
STATIC_INFO_SERVERNAME,
)
from datadog_checks.sqlserver.stored_procedures import SQL_SERVER_PROCEDURE_METRICS_COLUMNS
from .common import CHECK_NAME, OPERATION_TIME_METRIC_NAME
from .utils import CLOSE_TO_ZERO_INTERVAL
try:
import pyodbc
except ImportError:
pyodbc = None
SELF_HOSTED_ENGINE_EDITIONS = {
ENGINE_EDITION_PERSONAL,
ENGINE_EDITION_STANDARD,
ENGINE_EDITION_ENTERPRISE,
ENGINE_EDITION_EXPRESS,
}
logger = logging.getLogger(__name__)
def _expected_dbm_instance_tags(check):
return check._config.tags + [
"database_hostname:{}".format("stubbed.hostname"),
"database_instance:{}".format("stubbed.hostname"),
"ddagenthostname:{}".format("stubbed.hostname"),
"dd.internal.resource:database_instance:{}".format("stubbed.hostname"),
"sqlserver_servername:{}".format(check.static_info_cache.get(STATIC_INFO_SERVERNAME)),
]
@pytest.fixture(autouse=True)
def stop_orphaned_threads():
# make sure we shut down any orphaned threads and create a new Executor for each test
DBMAsyncJob.executor.shutdown(wait=True)
DBMAsyncJob.executor = ThreadPoolExecutor()
@pytest.fixture
def dbm_instance(instance_docker):
instance_docker['dbm'] = True
instance_docker['min_collection_interval'] = 1
instance_docker['query_metrics'] = {'enabled': False}
instance_docker['query_activity'] = {'enabled': False}
instance_docker['collect_settings'] = {'enabled': False}
# Set collection_interval close to 0. This is needed if the test runs the check multiple times.
# This prevents DBMAsync from skipping job executions, as it is designed
# to not execute jobs more frequently than their collection period.
instance_docker['procedure_metrics'] = {
'enabled': True,
'run_sync': True,
'collection_interval': CLOSE_TO_ZERO_INTERVAL,
}
return copy(instance_docker)
@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
@pytest.mark.parametrize(
"expected_columns,available_columns",
[
[
["execution_count", "total_worker_time"],
["execution_count", "total_worker_time"],
],
[
["execution_count", "total_worker_time", "some_missing_column"],
["execution_count", "total_worker_time"],
],
],
)
def test_get_available_procedure_metrics_columns(dbm_instance, expected_columns, available_columns):
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
check.initialize_connection()
_conn_key_prefix = "dbm-test-procedures"
with check.connection.open_managed_default_connection(key_prefix=_conn_key_prefix):
with check.connection.get_managed_cursor(key_prefix=_conn_key_prefix) as cursor:
result_available_columns = check.procedure_metrics._get_available_procedure_metrics_columns(
cursor, expected_columns
)
assert result_available_columns == available_columns
@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
def test_get_procedure_metrics_query_cached(aggregator, dbm_instance, caplog):
caplog.set_level(logging.DEBUG)
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
check.initialize_connection()
_conn_key_prefix = "dbm-test-procedures"
with check.connection.open_managed_default_connection(key_prefix=_conn_key_prefix):
with check.connection.get_managed_cursor(key_prefix=_conn_key_prefix) as cursor:
for _ in range(3):
query = check.procedure_metrics._get_procedure_metrics_query_cached(cursor)
assert query, "query should be non-empty"
times_columns_loaded = 0
for r in caplog.records:
if r.message.startswith("found available sys.dm_exec_procedure_stats columns"):
times_columns_loaded += 1
assert times_columns_loaded == 1, "columns should have been loaded only once"
test_procedure_metrics_parametrized = (
"database,query,param_groups,execution_count,expected_objects",
[
[
"master", # database
"EXEC multiQueryProc", # query
((),),
1,
[
{
'schema_name': 'dbo',
'procedure_name': 'multiQueryProc',
'database_name': 'master',
'execution_count': 1,
}
],
],
[
"master", # database
"EXEC multiQueryProc", # query
((),),
5,
[
{
'schema_name': 'dbo',
'procedure_name': 'multiQueryProc',
'database_name': 'master',
'execution_count': 5,
}
],
],
[
"master", # database
"EXEC encryptedProc", # query
((),),
3,
[
{
'schema_name': 'dbo',
'procedure_name': 'encryptedProc',
'database_name': 'master',
'execution_count': 3,
}
],
],
[
"datadog_test-1", # database
"EXEC bobProc", # query
((),),
1,
[
{
'schema_name': 'dbo',
'procedure_name': 'bobProc',
'database_name': 'datadog_test-1',
'execution_count': 1,
}
],
],
[
"datadog_test-1", # database
"EXEC bobProc", # query
((),),
10,
[
{
'schema_name': 'dbo',
'procedure_name': 'bobProc',
'database_name': 'datadog_test-1',
'execution_count': 10,
}
],
],
[
"datadog_test-1", # database
"EXEC bobProcParams @P1 = ?, @P2 = ?", # query
(
(1, "foo"),
(2, "bar"),
),
1, # This will execute each param set once, for a total of 2 executions
[
{
'schema_name': 'dbo',
'procedure_name': 'bobProcParams',
'database_name': 'datadog_test-1',
'execution_count': 2,
}
],
],
],
)
@pytest.mark.flaky
@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
@pytest.mark.parametrize(*test_procedure_metrics_parametrized)
def test_procedure_metrics(
aggregator,
dd_run_check,
dbm_instance,
bob_conn,
database,
query,
param_groups,
execution_count,
expected_objects,
caplog,
datadog_agent,
):
caplog.set_level(logging.INFO)
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
# the check must be run three times:
# 1) set _last_stats_query_time (this needs to happen before the 1st test queries to ensure the query time
# interval is correct)
# 2) load the test queries into the StatementMetrics state
# 3) emit the procedure metrics based on the diff of current and last state
logger.warning('dd_run_check')
dd_run_check(check)
for _ in range(0, execution_count):
for params in param_groups:
bob_conn.execute_with_retries(query, params, database=database)
logger.warning('dd_run_check')
dd_run_check(check)
aggregator.reset()
for _ in range(0, execution_count):
for params in param_groups:
bob_conn.execute_with_retries(query, params, database=database)
logger.warning('dd_run_check')
dd_run_check(check)
_conn_key_prefix = "dbm-test-procedures"
with check.connection.open_managed_default_connection(key_prefix=_conn_key_prefix):
with check.connection.get_managed_cursor(key_prefix=_conn_key_prefix) as cursor:
available_procedure_metrics_columns = check.procedure_metrics._get_available_procedure_metrics_columns(
cursor, SQL_SERVER_PROCEDURE_METRICS_COLUMNS
)
instance_tags = dbm_instance.get('tags', [])
expected_instance_tags = {t for t in instance_tags if not t.startswith('dd.internal')}
expected_instance_tags.add("database_hostname:stubbed.hostname")
expected_instance_tags.add("database_instance:stubbed.hostname")
expected_instance_tags.add("ddagenthostname:{}".format("stubbed.hostname"))
expected_instance_tags.add("dd.internal.resource:database_instance:stubbed.hostname")
expected_instance_tags.add("sqlserver_servername:{}".format(check.static_info_cache.get(STATIC_INFO_SERVERNAME)))
# dbm-metrics
dbm_metrics = aggregator.get_event_platform_events("dbm-metrics")
assert len(dbm_metrics) == 1, "should have collected exactly one dbm-metrics payload"
payload = next((n for n in dbm_metrics if n.get('kind') == 'procedure_metrics'), None)
for expected_object in expected_objects:
matched = False
for row in payload['sqlserver_rows']:
# verify that each key is present and that the correct values are found
is_match = all(key in row and row[key] == expected_object[key] for key in expected_object.keys())
if is_match:
matched = True
break
assert matched, "could not find expected_object in sqlserver_rows. expected={} seen={}".format(
expected_objects, payload['sqlserver_rows']
)
assert set(payload['tags']) == expected_instance_tags
assert payload['ddagenthostname'] == datadog_agent.get_hostname()
for row in payload['sqlserver_rows']:
for column in available_procedure_metrics_columns:
assert column in row, "missing required procedure metric column {}".format(column)
# internal debug metrics
aggregator.assert_metric(
OPERATION_TIME_METRIC_NAME,
tags=['agent_hostname:stubbed.hostname', 'operation:collect_procedure_metrics']
+ _expected_dbm_instance_tags(check),
)
@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
def test_procedure_metrics_limit(aggregator, dd_run_check, dbm_instance, bob_conn):
dbm_instance['procedure_metrics']['max_procedures'] = 2
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
# the check must be run three times:
# 1) set _last_stats_query_time (this needs to happen before the 1st test queries to ensure the query time
# interval is correct)
# 2) load the test queries into the StatementMetrics state
# 3) emit the procedure metrics based on the diff of current and last state
dd_run_check(check)
bob_conn.execute_with_retries('EXEC multiQueryProc', (), database='master')
bob_conn.execute_with_retries('EXEC encryptedProc', (), database='master')
bob_conn.execute_with_retries('EXEC bobProc', (), database='datadog_test-1')
dd_run_check(check)
aggregator.reset()
bob_conn.execute_with_retries('EXEC multiQueryProc', (), database='master')
bob_conn.execute_with_retries('EXEC encryptedProc', (), database='master')
bob_conn.execute_with_retries('EXEC bobProc', (), database='datadog_test-1')
dd_run_check(check)
# dbm-metrics
dbm_metrics = aggregator.get_event_platform_events("dbm-metrics")
assert len(dbm_metrics) == 1, "should have collected exactly one dbm-metrics payload"
payload = next((n for n in dbm_metrics if n.get('kind') == 'procedure_metrics'), None)
# metrics rows
sqlserver_rows = payload.get('sqlserver_rows', [])
assert sqlserver_rows, "should have collected some sqlserver query metrics rows"
assert len(sqlserver_rows) == dbm_instance['procedure_metrics']['max_procedures']
# check that it's sorted
assert sqlserver_rows == sorted(sqlserver_rows, key=lambda i: i['total_elapsed_time'], reverse=True)
@pytest.mark.parametrize("procedure_metrics_enabled", [True, False])
def test_async_job_enabled(dd_run_check, dbm_instance, procedure_metrics_enabled):
dbm_instance['procedure_metrics'] = {'enabled': procedure_metrics_enabled, 'run_sync': False}
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
dd_run_check(check)
check.cancel()
if procedure_metrics_enabled:
assert check.procedure_metrics._job_loop_future is not None
check.procedure_metrics._job_loop_future.result()
else:
assert check.procedure_metrics._job_loop_future is None
@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
def test_async_job_inactive_stop(aggregator, dd_run_check, dbm_instance):
dbm_instance['procedure_metrics']['run_sync'] = False
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
dd_run_check(check)
check.procedure_metrics._job_loop_future.result()
aggregator.assert_metric(
"dd.sqlserver.async_job.inactive_stop",
tags=['job:procedure-metrics'] + _expected_dbm_instance_tags(check),
hostname='',
)
@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
def test_async_job_cancel_cancel(aggregator, dd_run_check, dbm_instance):
dbm_instance['procedure_metrics']['run_sync'] = False
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
dd_run_check(check)
check.cancel()
# wait for it to stop and make sure it doesn't throw any exceptions
check.procedure_metrics._job_loop_future.result()
assert not check.procedure_metrics._job_loop_future.running(), "metrics thread should be stopped"
# if the thread doesn't start until after the cancel signal is set then the db connection will never
# be created in the first place
aggregator.assert_metric(
"dd.sqlserver.async_job.cancel",
tags=_expected_dbm_instance_tags(check) + ['job:procedure-metrics'],
)