|
124 | 124 |
|
125 | 125 | set_default_driver_conf() |
126 | 126 |
|
| 127 | +KEY_PREFIX = "dbm-sqlserver-" |
| 128 | + |
127 | 129 |
|
128 | 130 | class SQLServer(DatabaseCheck): |
129 | 131 | __NAMESPACE__ = "sqlserver" |
@@ -411,8 +413,8 @@ def load_static_information(self): |
411 | 413 | } |
412 | 414 | missing_keys = expected_keys - set(self.static_info_cache.keys()) |
413 | 415 | if missing_keys: |
414 | | - with self.connection.open_managed_default_connection(): |
415 | | - with self.connection.get_managed_cursor() as cursor: |
| 416 | + with self.connection.open_managed_default_connection(KEY_PREFIX): |
| 417 | + with self.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
416 | 418 | if STATIC_INFO_VERSION not in self.static_info_cache: |
417 | 419 | cursor.execute("select @@version") |
418 | 420 | results = cursor.fetchall() |
@@ -533,8 +535,8 @@ def make_metric_list_to_collect(self): |
533 | 535 | self.log.warning("Database %s does not exist. Disabling checks for this instance.", context) |
534 | 536 | return |
535 | 537 | if self.instance.get("stored_procedure") is None: |
536 | | - with self.connection.open_managed_default_connection(): |
537 | | - with self.connection.get_managed_cursor() as cursor: |
| 538 | + with self.connection.open_managed_default_connection(KEY_PREFIX): |
| 539 | + with self.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
538 | 540 | self.autodiscover_databases(cursor) |
539 | 541 | self._make_metric_list_to_collect(self._config.custom_metrics) |
540 | 542 | except SQLConnectionError: |
@@ -754,7 +756,7 @@ def get_sql_counter_type(self, counter_name): |
754 | 756 | cached = self._sql_counter_types.get(counter_name) |
755 | 757 | if cached: |
756 | 758 | return cached |
757 | | - with self.connection.get_managed_cursor() as cursor: |
| 759 | + with self.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
758 | 760 | cursor.execute(COUNTER_TYPE_QUERY, (counter_name,)) |
759 | 761 | (sql_counter_type,) = cursor.fetchone() |
760 | 762 | if sql_counter_type == PERF_LARGE_RAW_BASE: |
@@ -832,8 +834,8 @@ def _check_connections_by_connecting_to_db(self): |
832 | 834 | self.log.warning("failed service check for auto discovered database: %s", e) |
833 | 835 |
|
834 | 836 | def _check_connections_by_use_db(self): |
835 | | - with self.connection.open_managed_default_connection(): |
836 | | - with self.connection.get_managed_cursor() as cursor: |
| 837 | + with self.connection.open_managed_default_connection(KEY_PREFIX): |
| 838 | + with self.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
837 | 839 | for db in self.databases: |
838 | 840 | check_err_message = "Database {} connection service check failed: {}" |
839 | 841 | try: |
@@ -1030,34 +1032,34 @@ def load_basic_metrics(self, cursor): |
1030 | 1032 |
|
1031 | 1033 | def collect_metrics(self): |
1032 | 1034 | """Fetch the metrics from all the associated database tables.""" |
1033 | | - with self.connection.open_managed_default_connection(): |
| 1035 | + with self.connection.open_managed_default_connection(KEY_PREFIX): |
1034 | 1036 | if not self._config.only_custom_queries: |
1035 | | - with self.connection.get_managed_cursor() as cursor: |
| 1037 | + with self.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
1036 | 1038 | self.load_basic_metrics(cursor) |
1037 | 1039 |
|
1038 | 1040 | # Neither pyodbc nor adodbapi are able to read results of a query if the number of rows affected |
1039 | 1041 | # statement are returned as part of the result set, so we disable for the entire connection |
1040 | 1042 | # this is important mostly for custom_queries or the stored_procedure feature |
1041 | 1043 | # https://docs.microsoft.com/en-us/sql/t-sql/statements/set-nocount-transact-sql |
1042 | | - with self.connection.get_managed_cursor() as cursor: |
| 1044 | + with self.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
1043 | 1045 | cursor.execute("SET NOCOUNT ON") |
1044 | 1046 | try: |
1045 | 1047 | if not self._config.only_custom_queries: |
1046 | 1048 | # restore the current database after executing dynamic queries |
1047 | 1049 | # this is to ensure the current database context is not changed |
1048 | | - with self.connection.restore_current_database_context(): |
| 1050 | + with self.connection.restore_current_database_context(KEY_PREFIX): |
1049 | 1051 | if self.database_metrics: |
1050 | 1052 | for database_metric in self.database_metrics: |
1051 | 1053 | database_metric.execute() |
1052 | 1054 |
|
1053 | 1055 | # reuse the connection for custom queries |
1054 | 1056 | self._query_manager.execute() |
1055 | 1057 | finally: |
1056 | | - with self.connection.get_managed_cursor() as cursor: |
| 1058 | + with self.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
1057 | 1059 | cursor.execute("SET NOCOUNT OFF") |
1058 | 1060 |
|
1059 | 1061 | def execute_query_raw(self, query, db=None): |
1060 | | - with self.connection.get_managed_cursor() as cursor: |
| 1062 | + with self.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
1061 | 1063 | if db: |
1062 | 1064 | ctx = construct_use_statement(db) |
1063 | 1065 | self.log.debug("changing cursor context via use statement: %s", ctx) |
|
0 commit comments