Skip to content

Commit

Permalink
applied linter
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovb committed Mar 27, 2024
1 parent 6f8310f commit 5b57eba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
21 changes: 12 additions & 9 deletions sqlserver/datadog_checks/sqlserver/sqlserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,12 @@ def make_metric_list_to_collect(self):
if not db_exists:
self.do_check = False
self.log.warning("Database %s does not exist. Disabling checks for this instance.", context)
return
return
if self.instance.get('stored_procedure') is None:
with self.connection.open_managed_default_connection():
with self.connection.get_managed_cursor() as cursor:
self.autodiscover_databases(cursor)
self._make_metric_list_to_collect(self._config.custom_metric)
with self.connection.open_managed_default_connection():
with self.connection.get_managed_cursor() as cursor:
self.autodiscover_databases(cursor)
self._make_metric_list_to_collect(self._config.custom_metric)
except SQLConnectionError as e:
raise e
except Exception as e:
Expand Down Expand Up @@ -963,19 +963,22 @@ def do_stored_procedure_check(self):
# Reference: https://github.com/mkleehammer/pyodbc/wiki/Calling-Stored-Procedures
call_proc = '{{CALL {}}}'.format(proc)
cursor.execute(call_proc)

rows = cursor.fetchall()
self.log.debug("Row count (%s) : %s", proc, cursor.rowcount)

for row in rows:
tags = [] if row.tags is None or row.tags == '' else row.tags.split(',')
tags.extend(custom_tags)

if row.type.lower() in self.proc_type_mapping:
self.proc_type_mapping[row.type](row.metric, row.value, tags, raw=True)
else:
self.log.warning(
'%s is not a recognised type from procedure %s, metric %s', row.type, proc, row.metric
'%s is not a recognised type from procedure %s, metric %s',
row.type,
proc,
row.metric,
)
except Exception as e:
self.log.warning("Could not call procedure %s: %s", proc, e)
Expand Down
11 changes: 8 additions & 3 deletions sqlserver/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def test_missing_db(instance_docker, dd_run_check):
instance['ignore_missing_database'] = False
import mock

with mock.patch('datadog_checks.sqlserver.connection.Connection.open_managed_default_connection', side_effect=SQLConnectionError(Exception("couldnt connect"))):
with mock.patch(
'datadog_checks.sqlserver.connection.Connection.open_managed_default_connection',
side_effect=SQLConnectionError(Exception("couldnt connect")),
):
with pytest.raises(SQLConnectionError):
check = SQLServer(CHECK_NAME, {}, [instance])
check.initialize_connection()
Expand All @@ -59,6 +62,7 @@ def test_missing_db(instance_docker, dd_run_check):
dd_run_check(check)
assert check.do_check is False


@mock.patch('datadog_checks.sqlserver.connection.Connection.open_managed_default_database')
@mock.patch('datadog_checks.sqlserver.connection.Connection.get_cursor')
def test_db_exists(get_cursor, mock_connect, instance_docker_defaults, dd_run_check):
Expand Down Expand Up @@ -107,10 +111,10 @@ def test_db_exists(get_cursor, mock_connect, instance_docker_defaults, dd_run_ch
check.initialize_connection()
check.make_metric_list_to_collect()
assert check.do_check is True

# check case sensitive but mismatched db
instance['database'] = 'cASEsENSITIVE2018'
check = SQLServer(CHECK_NAME, {}, [instance])
check = SQLServer(CHECK_NAME, {}, [instance])
check.initialize_connection()
check.make_metric_list_to_collect()
assert check.do_check is False
Expand All @@ -122,6 +126,7 @@ def test_db_exists(get_cursor, mock_connect, instance_docker_defaults, dd_run_ch
check.make_metric_list_to_collect()
assert check.do_check is True


@mock.patch('datadog_checks.sqlserver.connection.Connection.open_managed_default_database')
@mock.patch('datadog_checks.sqlserver.connection.Connection.get_cursor')
def test_azure_cross_database_queries_excluded(get_cursor, mock_connect, instance_docker_defaults, dd_run_check):
Expand Down

0 comments on commit 5b57eba

Please sign in to comment.