Skip to main content

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.

Metadata

Release files for flask-management-endpoints 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-management-endpoints 1.1.0
File Size Uploaded
flask-management-endpoints-1.1.0.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-management-endpoints 1.1.0
File Interpreter ABI Platform
flask_management_endpoints-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.3 kB

Release files / flask-management-endpoints-1.1.0.tar.gz

Download URL flask-management-endpoints-1.1.0.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
65e6d330c035d6413baadaa8751e4ccfd81d3673a34d5f6b47e4809825a662ce
BLAKE2b-256 checksum
How to use checksums
1e1bf5e9386ee59fddbc9dcbb4e17748d9e99d101fa65e0f319eecd5ab868e6a
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / flask_management_endpoints-1.1.0-py3-none-any.whl

Download URL flask_management_endpoints-1.1.0-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd2a30b663678c93daf42cb593cffda3e56554a82b3b77a6c00985cb921c98e6
BLAKE2b-256 checksum
How to use checksums
6072642cedb074ac1becbcff9b2b7d2f02f50b56dffddf306fe7d6dee0cd6fcb
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page