Skip to main content

No project description provided

Project description

Heartbeat module for Flask applications and OAREPO Invenio repository

A heartbeat module for flask and OAREPO Invenio. It provides 3 endpoints:

  • .well-known/heartbeat/readiness

  • .well-known/heartbeat/liveliness

  • .well-known/heartbeat/environ

.well-known/heartbeat/readiness

This endpoint returns HTTP status 200 if the server is ready for user requests or 500 if the server is not yet ready.

At the same time, it returns a payload explaining what is not yet ready/what went wrong:

{
    "status": false,
    "checks": {
        "Database": {
            "status": false,
            "message": "Error accessing database"
        }
    }
}

This endpoint should be called as Kubernetes readiness probe

Note: the result is extensible, ignore unknown keys

Signals:

A oarepo_heartbeat.readiness_probe signal (with name oarepo.probe.readiness) is called during the readiness processing. Signal handler should return a response in the form of a tuple (name, status, data). The status is the logical and of returned statuses and data are passed inside the element. The following section will be added to the response:

"checks": {
    "returned_name": {
        "status": "returned_status",
        **returned_data
    }
}

Initial implementation:

When no signals are attached, the probe always returns HTTP 200, thus checking if the server is running.

.well-known/heartbeat/liveliness

This endpoint returns HTTP status 200 if the server is functioning correctly or 500 if the server has a problem.

At the same time, it returns a payload explaining what went wrong in the same format as in readiness probe:

{
    "status": false,
    "checks": {
        "Database": {
            "status": false,
            "message": "Error accessing database"
        }
    }
}

This endpoint should be called as Kubernetes liveliness probe

Note: the result is extensible, ignore unknown keys

Signals:

A oarepo_heartbeat.liveliness_probe signal (with name oarepo.probe.liveliness) is called during the readiness processing. Signal handler should return a response in the form of a tuple (name, status, data). The status is the logical and of returned statuses and data are passed inside the element.

Initial implementation:

When no signals are attached, the probe always returns HTTP 200, thus checking if the server is running.

.well-known/heartbeat/environ

Endpoint returning the runtime environment of the server. The result contains at least a set of libraries present in the virtualenv and their versions.

{
    "status": true,
    "libraries": {
        "oarepo": {
            "conflicts": null,
            "version": "3.1.1"
        }
    },
    "python": [3, 6, 1]
}

Note: the result is extensible, ignore unknown keys

Signals:

A oarepo_heartbeat.environ_probe signal (with name oarepo.probe.environ) is called during the readiness processing. Signal handler should return a response as a tuple (status, {data}). The status is the logical and of returned statuses and the data are merged into one dictionary.

Initial implementation:

When no signals are attached, the probe always returns HTTP 200 with json containing libraries and python elements as shown above.

Invenio usage:

To use this library on invenio, do not forget to add it to setup’s blueprints and define your own readiness & liveliness signal handlers as needed (for example, checking database, ES connectivity):

setup.py:

'invenio_base.blueprints': [
    'oarepo-heartbeat = oarepo_heartbeat.views:blueprint',
],

ext.py:

from invenio_search import current_search_client
from oarepo_heartbeat import liveliness_probe, readiness_probe
from invenio_db import db

@liveliness_probe.connect
@readiness_probe.connect
def database_check(*args, **kwargs):
    try:
        t1 = time.time()
        db.session.execute('select id from records_metadata limit 1').fetchall()
        t2 = time.time()
        return ('database', True, {'time': t2-t1})
    except Exception as e:
        return ('database', False, {'error': str(e)})


@liveliness_probe.connect
@readiness_probe.connect
def elasticsearch_check(*args, **kwargs):
    try:
        t1 = time.time()
        current_search_client.indices.get_alias("*", request_timeout=10)
        t2 = time.time()
        return ('elasticsearch', True, {'time': t2-t1})
    except Exception as e:
        return ('elasticsearch', False, {'error': str(e)})

Flask usage:

Register the oarepo_heartbeat.views:blueprint blueprint to your flask application and write your own readiness and liveliness signals as needed.

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

oarepo-heartbeat-1.0.3.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

oarepo_heartbeat-1.0.3-py2.py3-none-any.whl (4.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file oarepo-heartbeat-1.0.3.tar.gz.

File metadata

  • Download URL: oarepo-heartbeat-1.0.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for oarepo-heartbeat-1.0.3.tar.gz
Algorithm Hash digest
SHA256 9f2c525e18a94b50d11020b529675a168d150de1294a85fc9f3fd19ac19123e9
MD5 1ebbe97d2e11bdb203c9d695ce36b0ba
BLAKE2b-256 95a29efbaecdcb9a6fedf23690b784895590560ecfdf7a21441f1b93f0308c4b

See more details on using hashes here.

File details

Details for the file oarepo_heartbeat-1.0.3-py2.py3-none-any.whl.

File metadata

  • Download URL: oarepo_heartbeat-1.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for oarepo_heartbeat-1.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 423895a51c9cdadcd977b5b3ed143256db4b1e139896be74b90559d00f77ee1b
MD5 18f1f0aef3c637ad013b246ce5a1d50e
BLAKE2b-256 3fdc555698ee149e61a49c1136e4b0bd8ef6d095fd1f3c03e2721d71f2ac82ee

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