Skip to main content

A library for fastapi health checks

Project description

sag_py_fastapi_health

Maintainability Coverage Status Known Vulnerabilities

Add health check endpoints to fastapi (similar to the ones dotnet core has)

What it does

  • Adds one or multiple health endpoint
  • Configurable output format (json or prtg)
  • Possibility to add checks (own and pre shipped)
  • Pre-Shipped tests for http get requests including basic auth and directory existence/readability/writability

Installation

pip install sag-py-fastapi-health

How to use

Sample usage with existing checks

from sag_py_fastapi_health.checks.http import HttpCheck
from sag_py_fastapi_health.checks.storage import StorageExistsCheck, StorageReadableCheck
from sag_py_fastapi_health.formatter import DefaultResponseFormatter, PrtgResponseFormatter
from sag_py_fastapi_health.models import Probe
from sag_py_fastapi_health.router import HealthcheckRouter

from config import config

router = HealthcheckRouter(
    Probe(
        name="health",
        response_formatter=DefaultResponseFormatter(),
        checks=[
            StorageExistsCheck("/opt/app/data", name="my_dir_exists"),
            StorageReadableCheck("/opt/app/data", name="my_dir_is_readable"),
            HttpCheck("https://localhost/auth", name="auth_available", timeout=5),
        ],
    ),
    Probe(
        name="health-prtg",
        response_formatter=PrtgResponseFormatter(),
        checks=[
            StorageExistsCheck("/opt/app/data", name="my_dir_exists"),
            StorageReadableCheck("/opt/app/data", name="my_dir_is_readable"),
            HttpCheck("https://localhost/auth", name="auth_available", timeout=5),
        ],
    ),
)

Write your own check

from sag_py_fastapi_health.models import CheckResult

class TestCheck(Check):
    def __init__(self, name: str = "check") -> None:
        self._name: str = name

    async def __call__(self) -> CheckResult:
        is_healthy: bool = a_custom_check()
        description: str = "A description of the status or a error message"

        return CheckResult(
            name=self._name,
            status="Healthy" if is_healthy else "Unhealthy",
            description=description,
        )

The description contains something like "Directory ... was accessable" or "Service is running" if everything is ok. If there was an error, you can add the error message/exception message there.

How to configure in prtg

use the sensor "HTTP data advanced" (https://www.paessler.com/manuals/prtg/http_data_advanced_sensor) and configure to your prtg health endpoint (like in the example above ([URL_TO_YOUR_SERVICE]/health/health-prtg)

How to start developing

With vscode

Just install vscode with dev containers extension. All required extensions and configurations are prepared automatically.

With pycharm

  • Install latest pycharm
  • Install pycharm plugin BlackConnect
  • Install pycharm plugin Mypy
  • Configure the python interpreter/venv
  • pip install requirements-dev.txt
  • pip install black[d]
  • Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed files
  • Ctl+Alt+S => Check Tools => BlackConnect => Trigger on code reformat
  • Ctl+Alt+S => Click Tools => BlackConnect => "Load from pyproject.yaml" (ensure line length is 120)
  • Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the "local instance" config (e.g. C:\Python310\Scripts\blackd.exe)
  • Ctl+Alt+S => Click Tools => Actions on save => Reformat code
  • Restart pycharm

How to publish

  • Update the version in setup.py and commit your change
  • Create a tag with the same version number
  • Let github do the rest

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

sag_py_fastapi_health-0.3.2.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

sag_py_fastapi_health-0.3.2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file sag_py_fastapi_health-0.3.2.tar.gz.

File metadata

  • Download URL: sag_py_fastapi_health-0.3.2.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for sag_py_fastapi_health-0.3.2.tar.gz
Algorithm Hash digest
SHA256 88e949b6ac682e5bc9902483207fead4eb388baba484cbc8bf40a76ca3729ffa
MD5 5336ae751a22695e6623f3cc94297292
BLAKE2b-256 e98f4890ddda6f253c04eb815621e3e7f315aef10f25855e6fb4c94181421008

See more details on using hashes here.

File details

Details for the file sag_py_fastapi_health-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sag_py_fastapi_health-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 615f64cf8bb9c276fb971d9f1979ccbcc732ea398348d759b219a0be17392f27
MD5 5ab80abad16aaa0c3a84d76953e9e1e9
BLAKE2b-256 6c910b3c63daf563594a67551a7657ede3950a7dd7a31b575ff27424d84411a2

See more details on using hashes here.

Supported by

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