A simple way for Python applications to push heartbeat info to a SQL database
Project description
pg-heartbeat
A simple way for Python applications to push heartbeat info to a SQL database.
Installation
pip install pg-heartbeat
Quick Start
from sqlmodel import create_engine
from pg_heartbeat import HeartbeatHandle, create_tables
engine = create_engine("postgresql://user:pass@localhost/mydb")
# Create tables once at app startup
create_tables(engine)
# Create a client
db = HeartbeatHandle(engine, service="my-service", version="1.0.0")
# Send a heartbeat
db.beat()
# Send with details
db.beat(status="ok", message="Processed 42 items", metadata={"queue_depth": 12})
# Get the latest heartbeat
latest = db.latest()
print(f"{latest.service} at {latest.timestamp}: {latest.status}")
# Get history
for hb in db.history(limit=10):
print(f"{hb.timestamp}: {hb.status} - {hb.message}")
Override service per call
You can query or record heartbeats for a different service by passing service to any method:
db = HeartbeatHandle(engine, service="api-server")
db.beat() # records for "api-server"
db.latest(service="worker") # queries "worker" instead
Multiple instances of the same service
Use instance_id to distinguish replicas or containers:
db = HeartbeatHandle(engine, service="my-api", instance_id="replica-1")
db.beat() # tagged with instance_id="replica-1"
db.latest() # returns latest for "my-api" + "replica-1" only
API
create_tables(engine_or_url, echo=False)
Create the heartbeat tables. Call once at app startup. Accepts a database URL string or an existing SQLAlchemy Engine.
HeartbeatHandle(engine, service, version=None, instance_id=None)
Create a client. Accepts a SQLAlchemy Engine.
- instance_id — optional identifier to distinguish multiple instances of the same service (e.g. replica ID, container ID). When set,
latest()andhistory()are scoped to that instance.
The following fields are auto-populated on every beat() call:
- hostname — looked up via
socket.gethostname()at init - version — set from the constructor argument
- uptime_seconds — seconds since the
HeartbeatHandleinstance was created
db.beat(service=None, status="ok", message=None, hostname=None, version=None, instance_id=None, uptime_seconds=None, metadata=None)
Record a heartbeat. Returns the Heartbeat row. Auto-populated fields (hostname, version, uptime_seconds) can be overridden per call.
metadata accepts a dict that is stored as JSON.
db.latest(service=None)
Get the most recent heartbeat for a service, or None.
db.history(service=None, limit=100, since=None)
Get recent heartbeats, newest first. Optionally filter by a since datetime.
Database Support
Works with any database supported by SQLAlchemy — SQLite, PostgreSQL, MySQL, etc. Install the appropriate driver:
pip install pg-heartbeat psycopg2-binary # PostgreSQL
pip install pg-heartbeat pymysql # MySQL
License
MIT
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pg_heartbeat-0.4.0.tar.gz.
File metadata
- Download URL: pg_heartbeat-0.4.0.tar.gz
- Upload date:
- Size: 29.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8764ed3dad0c4b8839d48d6c4b596d962f8fd999b84581f7a363e7faba88e183
|
|
| MD5 |
f20f071cc6366e1f09c03568f5ebf030
|
|
| BLAKE2b-256 |
584519edeba606216c6e2a1c1df0cd06bf66a5d35d0cb7bb3a910e6a6be256e7
|
File details
Details for the file pg_heartbeat-0.4.0-py3-none-any.whl.
File metadata
- Download URL: pg_heartbeat-0.4.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4e559534260c0a5b544eba06ebeeafc60d25f288a4b0f6294364e9e8c1bd150
|
|
| MD5 |
c8a312849ba80d34bd121595a9852419
|
|
| BLAKE2b-256 |
51a16760c7ae87c90e8f3ed7f5e2913865460b52b22f7a684e3bb9445e616bcd
|