(self, db)
Query pgbouncer for various metrics
Query pgbouncer for various metrics
Query pgbouncer for various metrics
def _collect_stats(self, db):
        """Query pgbouncer for various metrics"""

        metric_scope = [STATS_METRICS, POOLS_METRICS, DATABASES_METRICS, CONFIG_METRICS]

        try:
            with db.cursor(cursor_factory=pgextras.DictCursor) as cursor:
                for scope in metric_scope:
                    descriptors = scope['descriptors']
                    metrics = scope['metrics']
                    query = scope['query']

                    try:
                        self.log.debug("Running query: %s", query)
                        cursor.execute(query)
                        rows = cursor.fetchall()

                    except Exception as e:
                        self.log.exception("Not all metrics may be available: %s", str(e))

                    else:
                        for row in rows:
                            self.log.debug("Processing row: %r", row)

                            if 'key' in row:  # We are processing "config metrics"
                                # Make a copy of the row to allow mutation
                                # (a `psycopg2.lib.extras.DictRow` object doesn't accept a new key)
                                row = row.copy()
                                # We flip/rotate the row: row value becomes the column name
                                row[row['key']] = row['value']
                            # Skip the "pgbouncer" database
                            elif row.get('database') == self.DB_NAME:
                                continue

                            tags = list(self.tags)
                            tags += ["%s:%s" % (tag, row[column]) for (column, tag) in descriptors if column in row]
                            for (column, (name, reporter)) in metrics:
                                if column in row:
                                    reporter(self, name, row[column], tags)

                        if not rows:
                            self.log.warning("No results were found for query: %s", query)

        except pg.Error:
            self.log.exception("Connection error")

            raise ShouldRestartException
def _collect_stats ( self , db ) : metric_scope = [ STATS_METRICS , POOLS_METRICS , DATABASES_METRICS , CONFIG_METRICS ] try : with db . cursor ( cursor_factory = pgextras . DictCursor ) as cursor : for scope in metric_scope : descriptors = scope [ 'descriptors' ] metrics = scope [ 'metrics' ] query = scope [ 'query' ] try : self . log . debug ( "Running query: %s" , query ) cursor . execute ( query ) rows = cursor . fetchall ( ) except Exception as e : self . log . exception ( "Not all metrics may be available: %s" , str ( e ) ) else : for row in rows : self . log . debug ( "Processing row: %r" , row ) if 'key' in row : # We are processing "config metrics" # Make a copy of the row to allow mutation # (a `psycopg2.lib.extras.DictRow` object doesn't accept a new key) row = row . copy ( ) # We flip/rotate the row: row value becomes the column name row [ row [ 'key' ] ] = row [ 'value' ] # Skip the "pgbouncer" database elif row . get ( 'database' ) == self . DB_NAME : continue tags = list ( self . tags ) tags += [ "%s:%s" % ( tag , row [ column ] ) for ( column , tag ) in descriptors if column in row ] for ( column , ( name , reporter ) ) in metrics : if column in row : reporter ( self , name , row [ column ] , tags ) if not rows : self . log . warning ( "No results were found for query: %s" , query ) except pg . Error : self . log . exception ( "Connection error" ) raise ShouldRestartException