def collect(self): """ execute db queries defined by metrics_config/api file, and store them in a prometheus metric_object, which will be served over http for scraping e.g gauge, etc. """ # Try to reconnect if the connection is lost. It will raise an exception is case of error self.reconnect() for query in self._metrics_config.queries: if not query.enabled: self._logger.info('Query %s is disabled', query.query) elif not utils.check_hana_range(self._hana_version, query.hana_version_range): self._logger.info('Query %s out of the provided hana version range: %s', query.query, query.hana_version_range) else: try: query_result = self._hdb_connector.query(query.query) except hdb_connector.connectors.base_connector.QueryError as err: self._logger.error('Failure in query: %s, skipping...', query.query) self._logger.error(str(err)) continue # Moving to the next iteration (query) formatted_query_result = utils.format_query_result(query_result) for metric in query.metrics: if metric.type == "gauge": try: metric_obj = self._manage_gauge(metric, formatted_query_result) except ValueError as err: self._logger.error(str(err)) # If an a ValueError exception is caught, skip the metric and go on to # complete the rest of the loop continue else: raise NotImplementedError('{} type not implemented'.format(metric.type)) yield metric_obj
def retrieve_metadata(self): """ Retrieve database metadata: sid, instance number, database name and hana version """ query = \ """SELECT (SELECT value FROM M_SYSTEM_OVERVIEW WHERE section = 'System' AND name = 'Instance ID') SID, (SELECT value FROM M_SYSTEM_OVERVIEW WHERE section = 'System' AND name = 'Instance Number') INSNR, m.database_name, m.version FROM m_database m;""" self._logger.info('Querying database metadata...') query_result = self._hdb_connector.query(query) formatted_result = utils.format_query_result(query_result)[0] self._hana_version = formatted_result['VERSION'] self._sid = formatted_result['SID'] self._insnr = formatted_result['INSNR'] self._database_name = formatted_result['DATABASE_NAME'] self._logger.info( 'Metadata retrieved. version: %s, sid: %s, insnr: %s, database: %s', self._hana_version, self._sid, self._insnr, self._database_name)
def _get_tenants_port(self): """ Get tenants port """ data = self._system_db_connector.query(self.TENANT_DATA_QUERY) formatted_data = utils.format_query_result(data) for tenant_data in formatted_data: if tenant_data['DATABASE_NAME'] != 'SYSTEMDB': yield tenant_data['DATABASE_NAME'], int( tenant_data['SQL_PORT'])
def get_hana_version(connector): """ Query the SAP HANA database version Args: connector: HANA database api connector """ query = 'SELECT * FROM sys.m_database;' query_result = connector.query(query) return utils.format_query_result(query_result)[0]['VERSION']
def test_format_query_result(self): query_results = mock.Mock() query_results.metadata = [( 'column1', 'other_data', ), ('column2', 'other_data'), ('column3', 'other_data')] query_results.records = [('data1', 'data2', 'data3'), ('data4', 'data5', 'data6'), ('data7', 'data8', 'data9')] formatted_result = utils.format_query_result(query_results) assert formatted_result == [{ 'column1': 'data1', 'column2': 'data2', 'column3': 'data3' }, { 'column1': 'data4', 'column2': 'data5', 'column3': 'data6' }, { 'column1': 'data7', 'column2': 'data8', 'column3': 'data9' }]