|
7 | 7 | import functools |
8 | 8 | import time |
9 | 9 | from collections import defaultdict |
10 | | -from string import Template |
11 | 10 |
|
12 | 11 | from cachetools import TTLCache |
13 | 12 |
|
@@ -147,7 +146,6 @@ def __init__(self, name, init_config, instances): |
147 | 146 |
|
148 | 147 | self._resolved_hostname = None |
149 | 148 | self._database_hostname = None |
150 | | - self._database_identifier = None |
151 | 149 | self._connection = None |
152 | 150 | self.failed_connections = {} |
153 | 151 | self.instance_metrics = [] |
@@ -358,38 +356,28 @@ def resolved_hostname(self): |
358 | 356 | return self._resolved_hostname |
359 | 357 |
|
360 | 358 | @property |
361 | | - def database_identifier(self): |
362 | | - # type: () -> str |
363 | | - if self._database_identifier is None: |
364 | | - template = Template(self._config.database_identifier.get('template') or '$resolved_hostname') |
365 | | - # Copy self.tag_manager._tags and map values to single values instead of lists |
366 | | - tag_dict = {} |
367 | | - tags = self.tag_manager.get_tags() |
368 | | - # sort tags to ensure consistent ordering |
369 | | - tags.sort() |
370 | | - for t in tags: |
371 | | - if ':' in t: |
372 | | - key, value = t.split(':', 1) |
373 | | - if key in tag_dict: |
374 | | - tag_dict[key] += f",{value}" |
375 | | - else: |
376 | | - tag_dict[key] = value |
377 | | - tag_dict['resolved_hostname'] = self.resolved_hostname |
378 | | - tag_dict['host'] = str(self.host) |
379 | | - tag_dict['port'] = str(self.port) if self.port is not None else None |
380 | | - database = self.instance.get('database', self.connection.DEFAULT_DATABASE if self.connection else None) |
381 | | - if database is not None: |
382 | | - tag_dict['database'] = database |
383 | | - if self.resolved_hostname.endswith(AZURE_SERVER_SUFFIX): |
384 | | - tag_dict['azure_name'] = self.resolved_hostname[: -len(AZURE_SERVER_SUFFIX)] |
385 | | - if self.static_info_cache.get(STATIC_INFO_SERVERNAME) is not None: |
386 | | - tag_dict['server_name'] = self.static_info_cache.get(STATIC_INFO_SERVERNAME) |
387 | | - if self.static_info_cache.get(STATIC_INFO_INSTANCENAME) is not None: |
388 | | - tag_dict['instance_name'] = self.static_info_cache.get(STATIC_INFO_INSTANCENAME) |
389 | | - if self.static_info_cache.get(STATIC_INFO_FULL_SERVERNAME) is not None: |
390 | | - tag_dict['full_server_name'] = self.static_info_cache.get(STATIC_INFO_FULL_SERVERNAME) |
391 | | - self._database_identifier = template.safe_substitute(**tag_dict) |
392 | | - return self._database_identifier |
| 359 | + def database_identifier_template(self) -> str: |
| 360 | + return self._config.database_identifier.get('template') or '$resolved_hostname' |
| 361 | + |
| 362 | + @property |
| 363 | + def database_identifier_params(self) -> dict: |
| 364 | + params = { |
| 365 | + 'resolved_hostname': self.resolved_hostname, |
| 366 | + 'host': str(self.host), |
| 367 | + 'port': str(self.port) if self.port is not None else None, |
| 368 | + } |
| 369 | + database = self.instance.get('database', self.connection.DEFAULT_DATABASE if self.connection else None) |
| 370 | + if database is not None: |
| 371 | + params['database'] = database |
| 372 | + if self.resolved_hostname.endswith(AZURE_SERVER_SUFFIX): |
| 373 | + params['azure_name'] = self.resolved_hostname[: -len(AZURE_SERVER_SUFFIX)] |
| 374 | + if self.static_info_cache.get(STATIC_INFO_SERVERNAME) is not None: |
| 375 | + params['server_name'] = self.static_info_cache.get(STATIC_INFO_SERVERNAME) |
| 376 | + if self.static_info_cache.get(STATIC_INFO_INSTANCENAME) is not None: |
| 377 | + params['instance_name'] = self.static_info_cache.get(STATIC_INFO_INSTANCENAME) |
| 378 | + if self.static_info_cache.get(STATIC_INFO_FULL_SERVERNAME) is not None: |
| 379 | + params['full_server_name'] = self.static_info_cache.get(STATIC_INFO_FULL_SERVERNAME) |
| 380 | + return params |
393 | 381 |
|
394 | 382 | @property |
395 | 383 | def database_hostname(self): |
|
0 commit comments