Skip to main content

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

Metadata

Release files for sag-py-fastapi-health 0.3.6

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

Source distribution (sdist)

Source distribution for sag-py-fastapi-health 0.3.6
File Size Uploaded Provenance Actions
sag_py_fastapi_health-0.3.6.tar.gz 11.4 kB Without provenance Details for sag_py_fastapi_health-0.3.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sag-py-fastapi-health 0.3.6
File Interpreter ABI Platform Actions
sag_py_fastapi_health-0.3.6-py3-none-any.whl Python 3 none any Details for sag_py_fastapi_health-0.3.6-py3-none-any.whl

Total release size: 20.3 kB

Release files / sag_py_fastapi_health-0.3.6.tar.gz

Download URL sag_py_fastapi_health-0.3.6.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fe928ed7bea3cd6503f047ed73f3961cb96676f5dd156e0c4f85055051ff0536
BLAKE2b-256 checksum
How to use checksums
0188516289fc6da269e08aa726d466fdbabadc01f9bd0f4b04e727e38c1be98b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / sag_py_fastapi_health-0.3.6-py3-none-any.whl

Download URL sag_py_fastapi_health-0.3.6-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f9f73a92a2153ba0e3191a5c75c5ba7903ebd22b4622673654d045c61326fe1e
BLAKE2b-256 checksum
How to use checksums
bc52a5019fe858f2df3b1a9935511719dc151583b7954ed8801b7612528203d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for sag-py-fastapi-health 0.3.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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