コード例 #1
0
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
コード例 #2
0
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
コード例 #3
0
 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"])
コード例 #4
0
 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"])
コード例 #5
0
 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"])
コード例 #6
0
 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"])
コード例 #7
0
    def __init__(self):
        args = dict(db=db)

        handlers = [(r"/", MainHandler, args),
                    (r"/health", TornadoHandler, dict(checker=HealthCheck()))]

        tornado.web.Application.__init__(self, handlers)
コード例 #8
0
 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")
コード例 #9
0
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
コード例 #10
0
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)
コード例 #11
0
ファイル: HealthCheck.py プロジェクト: mataprasad/flask-seed
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])
コード例 #12
0
ファイル: __init__.py プロジェクト: sujaypatil96/prov-service
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
コード例 #13
0
ファイル: __init__.py プロジェクト: tej99/eq-survey-runner
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)
コード例 #14
0
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())
コード例 #15
0
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
コード例 #16
0
    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)
コード例 #17
0
    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)
コード例 #18
0
    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"])
コード例 #19
0
ファイル: app.py プロジェクト: arunkrk/Flask_Repo
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
コード例 #20
0
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)
コード例 #21
0
    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)
コード例 #22
0
ファイル: server.py プロジェクト: Botifier/adnymics_test
 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()
コード例 #23
0
    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)
コード例 #24
0
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)
コード例 #25
0
    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"])
コード例 #26
0
    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"])
コード例 #27
0
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
コード例 #28
0
ファイル: app.py プロジェクト: ivan-c/truenth-portal
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,
    )
コード例 #29
0
    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)
コード例 #30
0
ファイル: __init__.py プロジェクト: coderbydesign/turnpike
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