Package: healthpy
Health Check for HTTP APIsCreate an health check endpoint on your REST API followingHealth Check RFCdraft version 4.Perform checksOf an external HTTP resourceOf a redis serverReturn health check resultAggregate multiple statusesUse a custom statusHTTP response bodyHTTP response status codeEndpointStarletteFlask-RestXPerform checksIn case you have external dependencies, you should check the health of those dependencies.HTTPIf you have an external HTTP resource, you can check its health, as in the following sample:importhealthpy.httpxstatus,checks=healthpy.httpx.check("petstore","https://petstore3.swagger.io/api/v3/openapi.json")Note:httpxmodule must be installed to perform HTTP health checks.Alternatively, you can userequeststo perform the exact same check:importhealthpy.requestsstatus,checks=healthpy.requests.check("petstore","https://petstore3.swagger.io/api/v3/openapi.json")RedisIf you rely on redis, you should check its health.redismodule must be installed to perform Redis health checks.importhealthpy.redisstatus,checks=healthpy.redis.check("redis://redis_url","redis_key")Return resultOnce all checks have been performed you should return the result to your client.Compute status from multiple statusesIf you performed more than one check, you have to compute an aggregated status from all the checks.importhealthpystatus1=healthpy.pass_statusstatus2=healthpy.warn_statusstatusN=healthpy.fail_statusstatus=healthpy.status(status1,status2,statusN)Using custom statusBy default pass status is "pass", warn status is "warn" and fail status is "fail".It can be tweaked by setting the value of healthpy.*_status as in the following sample:importhealthpyhealthpy.pass_status="ok"healthpy.warn_status="custom"healthpy.fail_status="error"HTTP response bodyHTTP response body can be retrieved as a dictionary to be returned as JSON.importhealthpystatus=healthpy.pass_status# replace with the aggregated statuschecks={}# replace with the computed checksbody=healthpy.response_body(status,checks=checks)Checks results are not mandatory in the response.importhealthpystatus=healthpy.pass_status# replace with the aggregated statusbody=healthpy.response_body(status)HTTP response status codeHTTP response status code can be retrieved as an integer.importhealthpystatus=healthpy.pass_status# replace with the aggregated statusstatus_code=healthpy.response_status_code(status)ConsulHTTP response status code should be a bit different forConsulhealth checks.importhealthpystatus=healthpy.pass_status# replace with the aggregated statusstatus_code=healthpy.consul_response_status_code(status)EndpointStarletteAn helper function is available to create astarletteendpoint forConsulhealth check.fromstarlette.applicationsimportStarletteimporthealthpyimporthealthpy.httpximporthealthpy.redisfromhealthpy.starletteimportadd_consul_health_endpointapp=Starlette()asyncdefhealth_check():# TODO Replace by your own checks.status_1,checks_1=healthpy.httpx.check("my external dependency","http://url_to_check")status_2,checks_2=healthpy.redis.check("redis://redis_url","key_to_check")returnhealthpy.status(status_1,status_2),{**checks_1,**checks_2}# /health endpoint will call the health_check coroutine.add_consul_health_endpoint(app,health_check)Note:starlettemodule must be installed.Flask-RestXAn helper function is available to create aFlask-RestXendpoint for health check.importflaskimportflask_restximporthealthpyimporthealthpy.httpximporthealthpy.redisfromhealthpy.flask_restximportadd_health_endpointapp=flask.Flask(__name__)api=flask_restx.Api(app)asyncdefhealth_check():# TODO Replace by your own checks.status_1,checks_1=healthpy.httpx.check("my external dependency","http://url_to_check")status_2,checks_2=healthpy.redis.check("redis://redis_url","key_to_check")returnhealthpy.status(status_1,status_2),{**checks_1,**checks_2}# /health endpoint will call the health_check coroutine.add_health_endpoint(api,health_check)Note:flask-restxmodule must be installed.Consul Service Health checkAn helper function is available to create aFlask-RestXendpoint forConsulhealth check.importflaskimportflask_restximporthealthpyimporthealthpy.httpximporthealthpy.redisfromhealthpy.flask_restximportadd_consul_health_endpointapp=flask.Flask(__name__)api=flask_restx.Api(app)asyncdefhealth_check():# TODO Replace by your own checks.status_1,checks_1=healthpy.httpx.check("my external dependency","http://url_to_check")status_2,checks_2=healthpy.redis.check("redis://redis_url","key_to_check")returnhealthpy.status(status_1,status_2),{**checks_1,**checks_2}# /health endpoint will call the health_check coroutine.add_consul_health_endpoint(api,health_check)Note:flask-restxmodule must be installed.TestingApytestfixture can be used to mock the datetime returned in http health check.fromhealthpy.testingimportmock_http_health_datetimedeftest_http(mock_http_health_datetime):# Time will be returned as "2018-10-11T15:05:05.663979"pass# Add your test calling healthpy.http.checkHow to installpython 3.7+must be installedUse pip to install module:python-mpipinstallhealthpy