Esempio n. 1
0
 def collect(self, app):
     """
     Use database connection to collect metric
     :return:
     """
     with app.app_context():
         LOGGER.info("Start collecting metrics")
         try:
             if db_util.is_port_open():
                 with db_util.get_connection() as conn:
                     for metric in self.metrics:
                         LOGGER.debug("collect %s", metric)
                         if hasattr(metric, 'query'):
                             result = db_util.get_query_result(
                                 conn, metric.query)
                             metric.collect(app, result)
                         else:
                             metric.collect(app)
             else:
                 for metric in self.metrics:
                     if hasattr(metric, 'is_up_metric'):
                         metric.collect(app)
         except InterfaceError as e:
             LOGGER.error("Exception when collecting metrics: %s",
                          str(e),
                          exc_info=True)
         LOGGER.info("Finish collecting metrics")
Esempio n. 2
0
    def test_should_raise_nothing_DatabaseError(self):
        cursor_context = MagicMock()
        cursor_context.execute.side_effect = DatabaseError("Test")

        cursor = MagicMock()
        cursor.__enter__.return_value = cursor_context

        connection = MagicMock()
        connection.cursor.return_value = cursor

        for row in get_query_result(connection, 'test'):
            logging.info(row)
Esempio n. 3
0
    def test_should_get_query_result(self):
        test_input = [1, 2, 3]
        cursor_context = MagicMock()
        cursor_context.__iter__.return_value = test_input

        cursor = MagicMock()
        cursor.__enter__.return_value = cursor_context

        connection = MagicMock()
        connection.cursor.return_value = cursor

        for row in get_query_result(connection, 'test'):
            self.assertTrue(row in test_input)
 def collect(self, app):
     """
     Use database connection to collect metric
     :return:
     """
     with app.app_context():
         LOGGER.info("Start collecting metrics")
         try:
             with db_util.get_connection() as conn:
                 for metric in self.metrics:
                     LOGGER.debug("collect %s", metric)
                     result = db_util.get_query_result(conn, metric.query)
                     metric.collect(result)
         except InterfaceError as e:
             LOGGER.error("Exception when collecting metrics: %s",
                          str(e),
                          exc_info=True)
         LOGGER.info("Finish collecting metrics")