|
8 | 8 | from typing import TYPE_CHECKING, TypedDict |
9 | 9 |
|
10 | 10 | from datadog_checks.base.utils.serialization import json |
11 | | -from datadog_checks.sqlserver.utils import construct_use_statement, execute_query |
| 11 | +from datadog_checks.sqlserver.utils import construct_use_statement, execute_query, is_azure_database |
12 | 12 |
|
13 | 13 | if TYPE_CHECKING: |
14 | 14 | from datadog_checks.sqlserver import SQLServer |
15 | 15 |
|
16 | 16 | from datadog_checks.base.utils.db.schemas import SchemaCollector, SchemaCollectorConfig |
17 | 17 | from datadog_checks.sqlserver.const import ( |
18 | 18 | DEFAULT_SCHEMAS_COLLECTION_INTERVAL, |
| 19 | + STATIC_INFO_ENGINE_EDITION, |
19 | 20 | STATIC_INFO_MAJOR_VERSION, |
20 | 21 | ) |
21 | 22 | from datadog_checks.sqlserver.queries import ( |
|
32 | 33 |
|
33 | 34 | KEY_PREFIX = "dbm-schemas-" |
34 | 35 | KEY_PREFIX_PRE_2017 = "dbm-schemas-pre-2017" |
| 36 | +# The modern schema query uses database-scoped JSON output, which requires SQL Server 2016 compatibility. |
| 37 | +MINIMUM_JSON_COMPATIBILITY_LEVEL = 130 |
35 | 38 |
|
36 | 39 |
|
37 | 40 | class DatabaseInfo(TypedDict): |
38 | 41 | name: str |
39 | 42 | id: str |
40 | 43 | collation: str |
41 | 44 | owner: str |
| 45 | + compatibility_level: str |
42 | 46 |
|
43 | 47 |
|
44 | 48 | # The schema collector sends lists of DatabaseObjects to the agent |
@@ -81,41 +85,73 @@ def __init__(self, check: SQLServer): |
81 | 85 | ) |
82 | 86 | config.max_tables = check._config.schema_config.get('max_tables', 300) |
83 | 87 | self._is_2016_or_earlier = None |
| 88 | + self._database_compatibility_levels: dict[str, int] = {} |
84 | 89 | super().__init__(check, config) |
85 | 90 |
|
86 | | - def collect_schemas(self): |
87 | | - # We wait until collect is called to check for static information |
88 | | - major_version = int(self._check.static_info_cache.get(STATIC_INFO_MAJOR_VERSION) or 0) |
89 | | - if major_version == 0: |
90 | | - self._check.log.debug("major_version is not available yet, defaulting to 2016 or earlier") |
91 | | - self._is_2016_or_earlier = major_version <= 13 |
92 | | - |
93 | | - super().collect_schemas() |
94 | | - |
95 | 91 | @property |
96 | 92 | def kind(self): |
97 | 93 | return "sqlserver_databases" |
98 | 94 |
|
99 | | - def _get_databases(self): |
| 95 | + def _get_databases(self) -> list[DatabaseInfo]: |
100 | 96 | database_names = self._check.get_databases() |
101 | 97 | with self._check.connection.open_managed_default_connection(KEY_PREFIX): |
102 | 98 | with self._check.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
103 | 99 | if not database_names: |
104 | 100 | return [] |
105 | 101 | placeholders = ",".join(["?"] * len(database_names)) |
106 | 102 | query = DB_QUERY.format(placeholders) |
107 | | - return execute_query(query, cursor, convert_results_to_str=True, parameters=tuple(database_names)) |
| 103 | + databases = execute_query(query, cursor, convert_results_to_str=True, parameters=tuple(database_names)) |
| 104 | + self._record_database_compatibility_levels(databases) |
| 105 | + return databases |
108 | 106 |
|
109 | 107 | @contextlib.contextmanager |
110 | 108 | def _get_cursor(self, database_name): |
111 | 109 | with self._check.connection.open_managed_default_connection(KEY_PREFIX): |
112 | 110 | with self._check.connection.get_managed_cursor(KEY_PREFIX) as cursor: |
113 | 111 | switch_db_statement = construct_use_statement(database_name) |
114 | 112 | cursor.execute(switch_db_statement) |
| 113 | + self._is_2016_or_earlier = self._should_use_legacy_schema_query(database_name) |
115 | 114 | query = self._get_tables_query() |
116 | 115 | cursor.execute(query) |
117 | 116 | yield cursor |
118 | 117 |
|
| 118 | + def _record_database_compatibility_levels(self, databases: list[DatabaseInfo]) -> None: |
| 119 | + self._database_compatibility_levels = {} |
| 120 | + for database in databases: |
| 121 | + self._database_compatibility_levels[database["name"]] = int(database["compatibility_level"]) |
| 122 | + |
| 123 | + def _should_use_legacy_schema_query(self, database_name: str) -> bool: |
| 124 | + """ |
| 125 | + Return whether the current database needs the legacy schema query. |
| 126 | +
|
| 127 | + The modern schema query depends on two SQL Server features: |
| 128 | + - STRING_AGG, used to build index column lists. On self-managed SQL Server, this requires SQL Server 2017 |
| 129 | + or later. Azure SQL Database and Azure SQL Managed Instance report ProductMajorVersion 12 while still |
| 130 | + supporting STRING_AGG, so only self-managed SQL Server uses this version gate. |
| 131 | + - JSON output, used for column, index, and foreign key metadata. This is controlled by each database's |
| 132 | + compatibility_level and requires level 130 or higher. |
| 133 | +
|
| 134 | + If ProductMajorVersion is missing, use the legacy query because we cannot confirm that STRING_AGG is |
| 135 | + available. |
| 136 | + """ |
| 137 | + engine_edition = self._check.static_info_cache.get(STATIC_INFO_ENGINE_EDITION) |
| 138 | + if not is_azure_database(engine_edition): |
| 139 | + major_version = int(self._check.static_info_cache.get(STATIC_INFO_MAJOR_VERSION) or 0) |
| 140 | + if major_version == 0: |
| 141 | + self._check.log.debug("major_version is not available yet, using legacy schema query") |
| 142 | + return True |
| 143 | + if major_version <= 13: |
| 144 | + return True |
| 145 | + |
| 146 | + compatibility_level = self._database_compatibility_levels.get(database_name) |
| 147 | + if compatibility_level is None: |
| 148 | + self._check.log.debug( |
| 149 | + "compatibility_level is not available for SQL Server database %s, using pre-2017 schema query", |
| 150 | + database_name, |
| 151 | + ) |
| 152 | + return True |
| 153 | + return compatibility_level < MINIMUM_JSON_COMPATIBILITY_LEVEL |
| 154 | + |
119 | 155 | def _get_tables_query(self): |
120 | 156 | limit = int(self._config.max_tables or 1_000_000) |
121 | 157 |
|
|
0 commit comments