class HealthChecker: """ Configures a health checker """ def __init__(self, redis_storage): self._redis_store = redis_storage self._health_check = HealthCheck( failed_status=settings.HEALTH_CHECKER_FAILED_STATUS) self._health_check.add_section( 'commit', os.environ.get('HEROKU_SLUG_COMMIT', None)) self._health_check.add_section( 'release', {"version": os.environ.get('HEROKU_RELEASE_VERSION', None)}) self._health_check.add_check(self._redis_available) def _redis_available(self): try: info = self._redis_store.redis.info() except ConnectionError: return False, "Could not connect to Redis instance" except TimeoutError: return False, "Redis connection timed out" return True, info def run(self): return self._health_check.run()
def test_custom_section_value_failing_check(self): hc = HealthCheck(checkers=[self.check_throws_exception]) hc.add_section("custom_section", "My custom section") message, status, headers = hc.run() self.assertEqual(500, status) jr = json.loads(message) self.assertEqual("My custom section", jr["custom_section"])
def test_custom_section_value_success_check(self): hc = HealthCheck(checkers=[self.check_that_works]) hc.add_section("custom_section", "My custom section") message, status, headers = hc.run() self.assertEqual(200, status) jr = json.loads(message) self.assertEqual("My custom section", jr["custom_section"])
def add_health_check(app): health = HealthCheck() envdump = EnvironmentDump() health.add_section("application", application_data) envdump.add_section("application", application_data) app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run()) app.add_url_rule("/environment", "environment", view_func=lambda: envdump.run())
def test_custom_section_success_check(self): def custom_section(): return "My custom section" hc = HealthCheck(checkers=[self.check_adition_works]) hc.add_section("custom_section", custom_section) message, status, headers = hc.run() self.assertEqual(200, status) jr = json.loads(message) self.assertEqual("My custom section", jr["custom_section"])
@app.errorhandler(400) def bad_request(error: Exception) -> tuple: """Handle bad requests :param Exception error: Exception thrown :return: A http response :rtype: tuple """ return jsonify(_status="ERR", _error={ "code": 400, "message": str(error) }), 400 # Add environment and health check routes envdump.add_section("application", application_data) envdump.add_section("settings", application_settings) health.add_check(mongo_available) health.add_section("version", __version__) app.add_url_rule("/healthcheck", "healthcheck", view_func=health.run) app.add_url_rule("/environment", "environment", view_func=envdump.run) if __name__ != '__main__': # pragma: no cover gunicorn_logger = logging.getLogger('gunicorn.error') app.logger.handlers = gunicorn_logger.handlers logging.basicConfig(level=gunicorn_logger.level) if __name__ == "__main__": # pragma: no cover app.run(host=HOST, debug=DEBUG, port=PORT)
def healthz(): health = HealthCheck() health.add_section("application", application_data) return health.run()