Skip to main content

A module for adding Spring Actuator style health endpoints to your Flask application

Project description

Flask Management Endpoints

Flask Management Endpoints allows for the definition of endpoints in your Flask application such that Kubernetes can use them for liveness and readiness probes. Additionally, it provides verbose health and informational endpoints. The API is designed in the style of Spring Actuator management endpoints.

Endpoint Method / Return Type Description
/info GET / json Provides information about the application and host system.
/health GET / json Runs all health checks and outputs the status of each health check.
/health/liveness GET / json Returns successfully if endpoint is running with terse output.
/health/ping GET / json Returns successfully if endpoint is running with terse output (same as liveness).
/health/readiness GET / json Readiness probe endpoint that can run dependent checks, but is the same as liveness by default.
/version GET / plaintext By default returns the contents of the environment variable VERSION, but can be configured to return any value via a closure.

Configuration

To register the Flask Blueprint in your application:

from flask import Flask
from flask_management_endpoints import z_blueprint

app = Flask(__name__)
app.register_blueprint(z_blueprint)

If you would like to mount the endpoint at a different URL prefix than the default (/z), then it can be specified when registering the blueprint:

app.register_blueprint(z_blueprint, url_prefix="/admin")

Next, define the URL service health checks that you would like to register. The service dependencies can be defined as a fixed URL in which checks will be appended to the end. Alternatively, dependencies can be defined with simply a hostname and an optional port, then the URL scheme and paths will be filled in by defaults.

app.config.update(
    Z_ENDPOINTS={        
        "service_dependencies": {
            # key is an identifier for the service name
            # value is a base URL pointing to a Spring Actuator style health endpoint or just a hostname with
            # an optional port.
            'users_api': 'https://user-service:9922/admin', # readiness check: https://user-service:9922/admin/readiness
            'widget_api': 'widget-service', # readiness check: {PREFERRED_URL_SCHEME}://widget-service/{url_prefix}/health/readiness
        }
    }
)

If you would like to have custom functions that will execute on the on a given check (health, readiness, version, etc), you can define them as follows.

def db_check():
    try:
        engine = users_db.engine
        result = engine.execute('SELECT 1')
        return result.first()[0] == 1
    except Exception as err:
        app.logger.error(f'DB health check failed: {err}')
        return False

app.config.update(
    Z_ENDPOINTS={
        'check_functions': {
            'health': {
                'db': db_check
            }
        }
    }

Extension

This project can also be used via the provided Flask extension. With the extension the blueprint is registered using the ManagementEndpoints class.

from flask import Flask
from flask_management_endpoints import ManagementEndpoints

app = Flask(__name__)
ManagementEndpoints(app)

The rest of the configuration is identical.

The extension has an additional option, no_log, that can disable logging of the HTTP requests handled by your healthz endpoints, to avoid cluttering your web log files with automated requests. At the moment, only the gunicorn web server is supported.

ManagementEndpoints(app, no_log=True)

Other Resources

If you need a less opinionated Flask health check blueprint, check out flask-healthz.

License

Copyright 2021 Elijah Zupancic

The Flask Management Endpoints project is licensed under the same license as Flask itself: BSD 3-clause.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-management-endpoints-1.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file flask-management-endpoints-1.1.0.tar.gz.

File metadata

  • Download URL: flask-management-endpoints-1.1.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for flask-management-endpoints-1.1.0.tar.gz
Algorithm Hash digest
SHA256 65e6d330c035d6413baadaa8751e4ccfd81d3673a34d5f6b47e4809825a662ce
MD5 266f2e79b96bf4ec3f8cdbfd02335d97
BLAKE2b-256 1e1bf5e9386ee59fddbc9dcbb4e17748d9e99d101fa65e0f319eecd5ab868e6a

See more details on using hashes here.

File details

Details for the file flask_management_endpoints-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: flask_management_endpoints-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for flask_management_endpoints-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd2a30b663678c93daf42cb593cffda3e56554a82b3b77a6c00985cb921c98e6
MD5 2853db71548d72f182d509e8a500115d
BLAKE2b-256 6072642cedb074ac1becbcff9b2b7d2f02f50b56dffddf306fe7d6dee0cd6fcb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page