Package: powerflex-monitoring
PowerFlex Python Monitoring library (powerflex-monitoring)Tools to assist in monitoring a Python service.InstallationYou can install fromPyPidirectly:pip install powerflex-monitoringDemoSee./demo.pyfor a usage example.
Also see the type hints and the docstrings.To run the demo:Install docker and docker-composeRunmake upRundocker container lsto see the open ports and try hitting the monitoring URLs listed below$ docker logs powerflex_monitoring_main
[INFO] Added route at port 8000: /monitoring/health/
[INFO] Added route at port 8000: /monitoring/ready/
[INFO] Added route at port 8000: /monitoring/metrics/
[INFO] 🚀 Starting monitoring server at port 8000
[INFO] Connected to NATS_ADDRESS nats:4222
[INFO] Configuration for NATS health check
[INFO] Service has been marked as ready. Cause: Service is initializedFeature: monitoring serverfrom powerflex_monitoring.monitoring import MonitoringServerReadiness and healthiness endpoints are at/monitoring/ready/and/monitoring/health/.
These are useful in environments like Kubernetes:https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/Prometheus metrics are at/monitoring/metrics/.
Use theprometheus_clientlibrary to expose more metrics.Feature: NATS health checkerfrom powerflex_monitoring.nats_health_check import NatsHealthCheckerThis class checks if NATS is healthy and marks the service as unhealthy if NATS is ever down for a certain period of time.Feature: Redis health checkerThis feature checks if Redis service is healthy and marks the service as unhealthy if Redis is ever down for a certain period of time.
How to use it?Import theRedisHealthCheckerfrom powerflex_monitoring.redis_health_check import RedisHealthCheckerInstantiate a RedisHealtChecher object with the Redis connection that you want to ckeck and the monitoring server to which you will inform the health statusredis_health_checker = RedisHealthChecker(redis_conn, monitoring_server)Run the async functionperiodically_check_redis_healthlike this:await redis_health_checker.periodically_check_redis_health()Feature: wait for service to be readyThis is useful when starting your integration tests.
If you use the readiness check, then you can delay an action until your service is ready.python -m powerflex_monitoring.wait_for_service_ready \
  --ready-check-url http://localhost:8000/monitoring/ready/Make sure this library is installed in your virtualenv or globally,
otherwise you won't be able to run this script.See our Makefile for an example of using this script to wait before running integration tests.Test resultsThis library is unit tested with 100% coverage in Python versions 3.8 to 3.11 and in pypy 3.9.DevelopingInstall the dependencies withmake setup.
This will usepipenvto manage a virtualenv.Run the commands in theMakefileto test your code.
Runpipenv shellbefore running the commands or runpipenv run make ...to run the Makefile commands withpipenv.make commitreadymake test-unitormake test-unit-all-python-versionsmake format-fixmake lintmake type-check-strictPlease keep the unit test coverage at 100%.Releasing toPyPi.orgMake sure all code checks have passed withmake commitready.Make sure you commit all code you wish to release withgit commit.Set the version in./src/powerflex_monitoring/VERSIONPlease attempt to followsemantic versioning.Runmake bump-versionto commit the change to theVERSIONfile.Runmake releaseto upload the package to pypi.org and to push a new git tag