(self, custom_metrics)
Store the list of metrics to collect by instance_key.
        Will also create and cache cursors to query the db.
Store the list of metrics to collect by instance_key.
        Will also create and cache cursors to query the db.
Store the list of metrics to collect by instance_key . Will also create and cache cursors to query the db .
def _make_metric_list_to_collect(self, custom_metrics):
        """
        Store the list of metrics to collect by instance_key.
        Will also create and cache cursors to query the db.
        """

        metrics_to_collect = []
        tags = self.instance.get('tags', [])

        # Load instance-level (previously Performance) metrics)
        # If several check instances are querying the same server host, it can be wise to turn these off
        # to avoid sending duplicate metrics
        if is_affirmative(self.instance.get('include_instance_metrics', True)):
            common_metrics = INSTANCE_METRICS
            if not self.dbm_enabled:
                common_metrics = common_metrics + DBM_MIGRATED_METRICS

            self._add_performance_counters(
                chain(common_metrics, INSTANCE_METRICS_TOTAL), metrics_to_collect, tags, db=None
            )

        # populated through autodiscovery
        if self.databases:
            for db in self.databases:
                self._add_performance_counters(INSTANCE_METRICS_TOTAL, metrics_to_collect, tags, db=db)

        # Load database statistics
        for name, table, column in DATABASE_METRICS:
            # include database as a filter option
            db_names = self.databases or [self.instance.get('database', self.connection.DEFAULT_DATABASE)]
            for db_name in db_names:
                cfg = {'name': name, 'table': table, 'column': column, 'instance_name': db_name, 'tags': tags}
                metrics_to_collect.append(self.typed_metric(cfg_inst=cfg, table=table, column=column))

        # Load AlwaysOn metrics
        if is_affirmative(self.instance.get('include_ao_metrics', False)):
            for name, table, column in AO_METRICS + AO_METRICS_PRIMARY + AO_METRICS_SECONDARY:
                db_name = 'master'
                cfg = {
                    'name': name,
                    'table': table,
                    'column': column,
                    'instance_name': db_name,
                    'tags': tags,
                    'ao_database': self.instance.get('ao_database', None),
                    'availability_group': self.instance.get('availability_group', None),
                    'only_emit_local': is_affirmative(self.instance.get('only_emit_local', False)),
                }
                metrics_to_collect.append(self.typed_metric(cfg_inst=cfg, table=table, column=column))

        # Load FCI metrics
        if is_affirmative(self.instance.get('include_fci_metrics', False)):
            for name, table, column in FCI_METRICS:
                cfg = {
                    'name': name,
                    'table': table,
                    'column': column,
                    'tags': tags,
                }
                metrics_to_collect.append(self.typed_metric(cfg_inst=cfg, table=table, column=column))

        # Load metrics from scheduler and task tables, if enabled
        if is_affirmative(self.instance.get('include_task_scheduler_metrics', False)):
            for name, table, column in TASK_SCHEDULER_METRICS:
                cfg = {'name': name, 'table': table, 'column': column, 'tags': tags}
                metrics_to_collect.append(self.typed_metric(cfg_inst=cfg, table=table, column=column))

        # Load sys.master_files metrics
        if is_affirmative(self.instance.get('include_master_files_metrics', False)):
            for name, table, column in DATABASE_MASTER_FILES:
                cfg = {'name': name, 'table': table, 'column': column, 'tags': tags}
                metrics_to_collect.append(self.typed_metric(cfg_inst=cfg, table=table, column=column))

        # Load DB Fragmentation metrics
        if is_affirmative(self.instance.get('include_db_fragmentation_metrics', False)):
            db_fragmentation_object_names = self.instance.get('db_fragmentation_object_names', [])
            db_names = self.databases or [self.instance.get('database', self.connection.DEFAULT_DATABASE)]

            if not db_fragmentation_object_names:
                self.log.debug(
                    "No fragmentation object names specified, will return fragmentation metrics for all "
                    "object_ids of current database(s): %s",
                    db_names,
                )

            for db_name in db_names:
                for name, table, column in DATABASE_FRAGMENTATION_METRICS:
                    cfg = {
                        'name': name,
                        'table': table,
                        'column': column,
                        'instance_name': db_name,
                        'tags': tags,
                        'db_fragmentation_object_names': db_fragmentation_object_names,
                    }
                    metrics_to_collect.append(self.typed_metric(cfg_inst=cfg, table=table, column=column))

        # Load any custom metrics from conf.d/sqlserver.yaml
        for cfg in custom_metrics:
            sql_type = None
            base_name = None

            custom_tags = tags + cfg.get('tags', [])
            cfg['tags'] = custom_tags

            db_table = cfg.get('table', DEFAULT_PERFORMANCE_TABLE)
            if db_table not in VALID_TABLES:
                self.log.error('%s has an invalid table name: %s', cfg['name'], db_table)
                continue

            if cfg.get('database', None) and cfg.get('database') != self.instance.get('database'):
                self.log.debug(
                    'Skipping custom metric %s for database %s, check instance configured for database %s',
                    cfg['name'],
                    cfg.get('database'),
                    self.instance.get('database'),
                )
                continue

            if db_table == DEFAULT_PERFORMANCE_TABLE:
                user_type = cfg.get('type')
                if user_type is not None and user_type not in VALID_METRIC_TYPES:
                    self.log.error('%s has an invalid metric type: %s', cfg['name'], user_type)
                sql_type = None
                try:
                    if user_type is None:
                        sql_type, base_name = self.get_sql_type(cfg['counter_name'])
                except Exception:
                    self.log.warning("Can't load the metric %s, ignoring", cfg['name'], exc_info=True)
                    continue

                metrics_to_collect.append(
                    self.typed_metric(
                        cfg_inst=cfg, table=db_table, base_name=base_name, user_type=user_type, sql_type=sql_type
                    )
                )

            else:
                for column in cfg['columns']:
                    metrics_to_collect.append(
                        self.typed_metric(
                            cfg_inst=cfg, table=db_table, base_name=base_name, sql_type=sql_type, column=column
                        )
                    )

        self.instance_metrics = metrics_to_collect
        self.log.debug("metrics to collect %s", metrics_to_collect)

        # create an organized grouping of metric names to their metric classes
        for m in metrics_to_collect:
            cls = m.__class__.__name__
            name = m.sql_name or m.column
            self.log.debug("Adding metric class %s named %s", cls, name)

            self.instance_per_type_metrics[cls].add(name)
            if m.base_name:
                self.instance_per_type_metrics[cls].add(m.base_name)
def _make_metric_list_to_collect ( self , custom_metrics ) : metrics_to_collect = [ ] tags = self . instance . get ( 'tags' , [ ] ) # Load instance-level (previously Performance) metrics) # If several check instances are querying the same server host, it can be wise to turn these off # to avoid sending duplicate metrics if is_affirmative ( self . instance . get ( 'include_instance_metrics' , True ) ) : common_metrics = INSTANCE_METRICS if not self . dbm_enabled : common_metrics = common_metrics + DBM_MIGRATED_METRICS self . _add_performance_counters ( chain ( common_metrics , INSTANCE_METRICS_TOTAL ) , metrics_to_collect , tags , db = None ) # populated through autodiscovery if self . databases : for db in self . databases : self . _add_performance_counters ( INSTANCE_METRICS_TOTAL , metrics_to_collect , tags , db = db ) # Load database statistics for name , table , column in DATABASE_METRICS : # include database as a filter option db_names = self . databases or [ self . instance . get ( 'database' , self . connection . DEFAULT_DATABASE ) ] for db_name in db_names : cfg = { 'name' : name , 'table' : table , 'column' : column , 'instance_name' : db_name , 'tags' : tags } metrics_to_collect . append ( self . typed_metric ( cfg_inst = cfg , table = table , column = column ) ) # Load AlwaysOn metrics if is_affirmative ( self . instance . get ( 'include_ao_metrics' , False ) ) : for name , table , column in AO_METRICS + AO_METRICS_PRIMARY + AO_METRICS_SECONDARY : db_name = 'master' cfg = { 'name' : name , 'table' : table , 'column' : column , 'instance_name' : db_name , 'tags' : tags , 'ao_database' : self . instance . get ( 'ao_database' , None ) , 'availability_group' : self . instance . get ( 'availability_group' , None ) , 'only_emit_local' : is_affirmative ( self . instance . get ( 'only_emit_local' , False ) ) , } metrics_to_collect . append ( self . typed_metric ( cfg_inst = cfg , table = table , column = column ) ) # Load FCI metrics if is_affirmative ( self . instance . get ( 'include_fci_metrics' , False ) ) : for name , table , column in FCI_METRICS : cfg = { 'name' : name , 'table' : table , 'column' : column , 'tags' : tags , } metrics_to_collect . append ( self . typed_metric ( cfg_inst = cfg , table = table , column = column ) ) # Load metrics from scheduler and task tables, if enabled if is_affirmative ( self . instance . get ( 'include_task_scheduler_metrics' , False ) ) : for name , table , column in TASK_SCHEDULER_METRICS : cfg = { 'name' : name , 'table' : table , 'column' : column , 'tags' : tags } metrics_to_collect . append ( self . typed_metric ( cfg_inst = cfg , table = table , column = column ) ) # Load sys.master_files metrics if is_affirmative ( self . instance . get ( 'include_master_files_metrics' , False ) ) : for name , table , column in DATABASE_MASTER_FILES : cfg = { 'name' : name , 'table' : table , 'column' : column , 'tags' : tags } metrics_to_collect . append ( self . typed_metric ( cfg_inst = cfg , table = table , column = column ) ) # Load DB Fragmentation metrics if is_affirmative ( self . instance . get ( 'include_db_fragmentation_metrics' , False ) ) : db_fragmentation_object_names = self . instance . get ( 'db_fragmentation_object_names' , [ ] ) db_names = self . databases or [ self . instance . get ( 'database' , self . connection . DEFAULT_DATABASE ) ] if not db_fragmentation_object_names : self . log . debug ( "No fragmentation object names specified, will return fragmentation metrics for all " "object_ids of current database(s): %s" , db_names , ) for db_name in db_names : for name , table , column in DATABASE_FRAGMENTATION_METRICS : cfg = { 'name' : name , 'table' : table , 'column' : column , 'instance_name' : db_name , 'tags' : tags , 'db_fragmentation_object_names' : db_fragmentation_object_names , } metrics_to_collect . append ( self . typed_metric ( cfg_inst = cfg , table = table , column = column ) ) # Load any custom metrics from conf.d/sqlserver.yaml for cfg in custom_metrics : sql_type = None base_name = None custom_tags = tags + cfg . get ( 'tags' , [ ] ) cfg [ 'tags' ] = custom_tags db_table = cfg . get ( 'table' , DEFAULT_PERFORMANCE_TABLE ) if db_table not in VALID_TABLES : self . log . error ( '%s has an invalid table name: %s' , cfg [ 'name' ] , db_table ) continue if cfg . get ( 'database' , None ) and cfg . get ( 'database' ) != self . instance . get ( 'database' ) : self . log . debug ( 'Skipping custom metric %s for database %s, check instance configured for database %s' , cfg [ 'name' ] , cfg . get ( 'database' ) , self . instance . get ( 'database' ) , ) continue if db_table == DEFAULT_PERFORMANCE_TABLE : user_type = cfg . get ( 'type' ) if user_type is not None and user_type not in VALID_METRIC_TYPES : self . log . error ( '%s has an invalid metric type: %s' , cfg [ 'name' ] , user_type ) sql_type = None try : if user_type is None : sql_type , base_name = self . get_sql_type ( cfg [ 'counter_name' ] ) except Exception : self . log . warning ( "Can't load the metric %s, ignoring" , cfg [ 'name' ] , exc_info = True ) continue metrics_to_collect . append ( self . typed_metric ( cfg_inst = cfg , table = db_table , base_name = base_name , user_type = user_type , sql_type = sql_type ) ) else : for column in cfg [ 'columns' ] : metrics_to_collect . append ( self . typed_metric ( cfg_inst = cfg , table = db_table , base_name = base_name , sql_type = sql_type , column = column ) ) self . instance_metrics = metrics_to_collect self . log . debug ( "metrics to collect %s" , metrics_to_collect ) # create an organized grouping of metric names to their metric classes for m in metrics_to_collect : cls = m . __class__ . __name__ name = m . sql_name or m . column self . log . debug ( "Adding metric class %s named %s" , cls , name ) self . instance_per_type_metrics [ cls ] . add ( name ) if m . base_name : self . instance_per_type_metrics [ cls ] . add ( m . base_name )