def add_check_api(): app = flask.Flask('check_api') health = HealthCheck(app, "/health") envdump = EnvironmentDump(app, "/environment") def health_check(): # TODO check that the datasource backend is working # TODO check that the active resource backends are available db_engines_ok = True maintenance = False if 1 + 1 == 2: return {'status', 'up'} elif db_engines_ok: return {'status', 'up'} elif not maintenance: return {'status', 'out_of_service'} else: return {'status', 'down'} def application_data(): return { 'maintainer': 'ElasTest', 'git_repo': 'https://github.com/elastest/elastest-service-manager' } health.add_check(health_check) envdump.add_section("application", application_data) return app
def create_app() -> Flask: app = Flask(__name__) # Always use dev config app.config.from_object('config_dev') # *Should* load prod config when deployed into Docker container if os.getenv('LEARN_FLASK_CONFIG') is not None: app.config.from_envvar('LEARN_FLASK_CONFIG') # Set logging log_file = app.config['LOG_LOC'] + app.config['LOG_FILE'] logging.basicConfig(level=app.config['LOG_LEVEL'], format=('%(levelname)s %(asctime)s %(name)s ' 'LrnFlsk %(threadName)s: %(message)s'), datefmt='%Y-%m-%d %H:%M:%S', filename=log_file) # Register blueprints app.register_blueprint(bye, url_prefix='/bye') app.register_blueprint(hello, url_prefix='/hello') app.register_blueprint(sqs, url_prefix='/sqs') # Setup system check endpoints health = HealthCheck() envdump = EnvironmentDump() app.add_url_rule('/healthcheck', 'healthcheck', view_func=lambda: health.run()) app.add_url_rule('/environment', 'environment', view_func=lambda: envdump.run()) return app
def test_custom_section_function_failing_check(self): hc = HealthCheck(checkers=[self.check_throws_exception]) hc.add_section("custom_section", lambda: "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_signature_value_failing_check(self): hc = HealthCheck(checkers=[self.check_throws_exception], 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_function_success_check(self): hc = HealthCheck(checkers=[self.check_that_works]) hc.add_section("custom_section", lambda: "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 test_custom_section_signature_value_success_check(self): hc = HealthCheck(checkers=[self.check_that_works], 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 __init__(self): args = dict(db=db) handlers = [(r"/", MainHandler, args), (r"/health", TornadoHandler, dict(checker=HealthCheck()))] tornado.web.Application.__init__(self, handlers)
def test_custom_section_prevent_duplication(self): hc = HealthCheck(checkers=[self.check_that_works], custom_section="My custom section") self.assertRaises(Exception, 'The name "custom_section" is already taken.', hc.add_section, "custom_section", "My custom section")
def create_app(configuration): logger = get_logger() logger.info("Initialising app") app = Flask(__name__) logger.info("Load config") app.config.from_object(config[configuration]) logger.info("Initialising db") db.init_app(app) logger.info("Initialise migration") migrate.init_app( app, db ) logger.info("Initialise metrics endpoint") monitor(app, path="/metrics", http_server=True, port=8080, addr="127.0.0.1") logger.info("Initialise health endpoint") health = HealthCheck(app, "/healthz") logger.info("App running") return app
def add_check_api(): app = flask.Flask('check_api') health = HealthCheck(app, "/health") envdump = EnvironmentDump(app, "/environment") def health_check(): db_engines_ok = STORE.is_ok() # calls specific DB check resource_engine_ok = RM.is_ok() # calls specific RM check this_ok = (1 + 1 == 2) # basic local check # maintenance = False if this_ok and db_engines_ok and resource_engine_ok: return {'status', 'up'} # elif not maintenance: # return {'status', 'out_of_service'} else: return {'status', 'down'} def application_data(): return { 'maintainer': 'ElasTest', 'git_repo': 'https://github.com/elastest/elastest-service-manager' } health.add_check(health_check) envdump.add_section("application", application_data) return add_mware(app)
def init_health_check(app): # wrap the flask app and give a heathcheck url health = HealthCheck(app, "/admin/healthcheck") envdump = EnvironmentDump(app, "/admin/environment") for check in checks: health.add_check(check) for dump in dumps: envdump.add_section(dump, dumps[dump])
def create_app(): app = connex_app app.add_api('openapi.yaml', arguments={'title': 'Provenance Service'}) # wrap the flask app and give a heathcheck url health = HealthCheck(app, "/healthcheck") health.add_check(neo4j_available) return app
def add_health_check(application, headers): from healthcheck import HealthCheck application.healthcheck = HealthCheck(application, '/healthcheck', success_headers=headers, failed_headers=headers) application.healthcheck.add_check(rabbitmq_available) application.healthcheck.add_check(git_revision)
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 add_check_api(app): from healthcheck import HealthCheck, EnvironmentDump health = HealthCheck(app, API_PREFIX + API_HEALTHCHECK, log_on_failure=False, success_ttl=None, failed_ttl=None, failed_status=200) envdump = EnvironmentDump(app, API_PREFIX + API_ENVIRONMENT) def health_check(): try: r = requests.get(app.config['SPARK_MASTER_URL']) soup = BeautifulSoup(r.content, 'html.parser') listItems = soup.find_all('li') statusItems = [ item.text for item in listItems if 'Status:' in str(item) ] alive = True if sum(1 for item in statusItems if 'ALIVE' in str(item)) > 0 else False statusString = str(statusItems[0]) except requests.exceptions.RequestException as e: # This is the correct syntax alive = False statusString = str(e) return alive, statusString def application_data(): try: r = requests.get(app.config['SPARK_MASTER_URL']) soup = BeautifulSoup(r.content, 'html.parser') listItems = soup.find_all('li') spark = { '_'.join(k.lower().split()): " ".join(v.split()) for k, v in (item.text.replace('\n', '').split(':', 1) for item in listItems) } except requests.exceptions.RequestException as e: # This is the correct syntax spark = {'error': str(e)} return { 'spark': spark, 'maintainer': 'Savas Gioldasis ([email protected])', 'git_repo': 'https://github.com/elastest/elastest-bigdata-service', } health.add_check(health_check) envdump.add_section("application", application_data) return app
def __init__(self): # args = dict(db=db) handlers = [ (r"/detect", AnomalyHandler), (r"/forecast", ForecastHandler), (r"/health", TornadoHandler, dict(checker=HealthCheck())) ] tornado.web.Application.__init__(self, handlers)
def test_custom_section_prevent_duplication(self): def broke_section(): raise Exception("My broke section") hc = HealthCheck(checkers=[self.check_adition_works], custom_section=broke_section) message, status, headers = hc.run() self.assertEqual(200, status) jr = json.loads(message) self.assertNotIn("custom_section", jr)
def test_custom_section_signature_success_check(self): def custom_section(): return "My custom section" hc = HealthCheck(checkers=[self.check_adition_works], 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"])
def get_ping(): response = {} health = HealthCheck() response['message'] = constants.hc_success (system_data, status, c_t) = health.run() response['code'] = status if status != 200: response['code'] = constants.failure_code response['message'] = constants.hc_failure return response
def attach_monitor(app): health = HealthCheck(app, '/status') envdump = EnvironmentDump(app, '/env', include_os=False, include_config=False, include_process=False, include_python=False) health.add_check(db_available) envdump.add_section("application", app_data)
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 __init__(self, host=config.HOST, port=config.PORT): self._host = host self._port = port self._app = flask.Flask(__name__) self._add_routes() self._fib = Fibonacci() self._init_env() self._init_logging() self._health = HealthCheck(self._app, "/health") self._add_mongo_check() self._add_fib_service_check()
def test_should_run_only_filtered_checks(self): check = 'check_that_works' hc = HealthCheck( checkers=[self.check_that_works, self.check_throws_exception]) message, status, headers = hc.run(check) self.assertEqual(200, status) jr = json.loads(message) self.assertEqual("success", jr["status"]) self.assertEqual(len(jr["results"]), 1)
def attach_monitor(app): """ Attach status and environment endpoints for monitoring site health """ health = HealthCheck(app, '/status') envdump = EnvironmentDump(app, '/env', include_os=False, include_config=False, include_process=False, include_python=False) health.add_check(db_available)
def test_default_timeout_should_success_check(self): def timeout_check(): import time time.sleep(10) return True, "Waited for 10 seconds" hc = HealthCheck(checkers=[timeout_check]) message, status, headers = hc.run() self.assertEqual(200, status) jr = json.loads(message) self.assertEqual("success", jr["status"])
def test_error_timeout_function_should_failing_check(self): def timeout_check(): import time time.sleep(5) return True, "Waited for 10 seconds" hc = HealthCheck(checkers=[timeout_check], error_timeout=2) message, status, headers = hc.run() self.assertEqual(500, status) jr = json.loads(message) self.assertEqual("failure", jr["status"])
def create_app(): """ Creates the Flask app :return: Flask app """ app = Flask(__name__) FlaskDynaconf(app) HealthCheck(app, "/healthcheck") EnvironmentDump(app, "/environment") register_blueprints(app) return app
def configure_healthcheck(app): """Configure the API used to check the health of our dependencies""" # Initializes the /healthcheck API that returns # the health of the service's dependencies based # on the results of the given checks app.healthcheck = HealthCheck( app=app, path='/healthcheck', checkers=HEALTH_CHECKS, failed_status=HEALTHCHECK_FAILURE_STATUS_CODE, log_on_failure=False, )
def __initialize_healthcheck(self, app): healthcheck_endpoint = self.config["endpoint_prefixes"]["healthcheck"] LOGGER.debug( f"Setting up healthcheck endpoint at {healthcheck_endpoint}") health = HealthCheck(app, healthcheck_endpoint) def mongo_okay(): db = get_db() stats = db.command("dbstats") return bool(stats["ok"]), "Mongo Database is UP" if stats[ "ok"] else "ERROR: Mongo Database is DOWN" health.add_check(mongo_okay)
def create_app(test_config=None): dictConfig( { "version": 1, "formatters": { "default": {"format": "{levelname} {asctime} {module} {process:d} {thread:d} {message}", "style": "{"} }, "handlers": { "wsgi": { "class": "logging.StreamHandler", "formatter": "default", "level": "DEBUG", "stream": "ext://flask.logging.wsgi_errors_stream", } }, "root": {"level": "DEBUG", "handlers": ["wsgi"]}, } ) app = Flask(__name__) app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_port=1) if test_config: app.config.from_mapping(test_config) else: app.config.from_object("turnpike.config") app.config.from_envvar("TURNPIKE_CONFIG", silent=True) session_obj = Session() session_obj.init_app(app) health = HealthCheck() app.add_url_rule("/_healthcheck/", view_func=health.run) app.add_url_rule("/_nginx_config/", view_func=views.nginx_config_data) app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {"/metrics": make_wsgi_app()}) chain_objs = [] for plugin_name in app.config["PLUGIN_CHAIN"]: mod_name, cls_name = plugin_name.rsplit(".", 1) mod = importlib.import_module(mod_name) cls = getattr(mod, cls_name) if not issubclass(cls, plugin.TurnpikePlugin): raise ValueError(f"Plugin {plugin_name} does not resolve to a TurnpikePlugin.") app.logger.info(f"Registering plugin: {plugin_name}") instance = cls(app) instance.register_blueprint() chain_objs.append(instance) app.config["PLUGIN_CHAIN_OBJS"] = chain_objs app.add_url_rule("/auth/", view_func=views.policy_view) app.add_url_rule("/api/turnpike/identity/", view_func=views.identity) return app