Skip to main content

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() and history() 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 HeartbeatHandle instance 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

Metadata

Release files for pg-heartbeat 0.4.0

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

Source distribution (sdist)

Source distribution for pg-heartbeat 0.4.0
File Size Uploaded Provenance Actions
pg_heartbeat-0.4.0.tar.gz 29.2 kB Without provenance Details for pg_heartbeat-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pg-heartbeat 0.4.0
File Interpreter ABI Platform Actions
pg_heartbeat-0.4.0-py3-none-any.whl Python 3 none any Details for pg_heartbeat-0.4.0-py3-none-any.whl

Total release size: 34.4 kB

Release files / pg_heartbeat-0.4.0.tar.gz

Download URL pg_heartbeat-0.4.0.tar.gz
Size 29.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8764ed3dad0c4b8839d48d6c4b596d962f8fd999b84581f7a363e7faba88e183
BLAKE2b-256 checksum
How to use checksums
584519edeba606216c6e2a1c1df0cd06bf66a5d35d0cb7bb3a910e6a6be256e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.13

Release files / pg_heartbeat-0.4.0-py3-none-any.whl

Download URL pg_heartbeat-0.4.0-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b4e559534260c0a5b544eba06ebeeafc60d25f288a4b0f6294364e9e8c1bd150
BLAKE2b-256 checksum
How to use checksums
51a16760c7ae87c90e8f3ed7f5e2913865460b52b22f7a684e3bb9445e616bcd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.13

Security for pg-heartbeat 0.4.0

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