Skip to main content

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

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)

Create a client. Accepts a SQLAlchemy Engine.

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, 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

pg_heartbeat-0.2.0.tar.gz (28.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pg_heartbeat-0.2.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file pg_heartbeat-0.2.0.tar.gz.

File metadata

  • Download URL: pg_heartbeat-0.2.0.tar.gz
  • Upload date:
  • Size: 28.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for pg_heartbeat-0.2.0.tar.gz
Algorithm Hash digest
SHA256 94c96f5d9c52d4768f66768f599656686a814e3e507a04f2638466c9e9017234
MD5 0ae43244888c6b42b498e1baf739f3c5
BLAKE2b-256 9315f5e2ffba77299c097c271cbe6b1ca292623a530cbad048b68fcb48654ceb

See more details on using hashes here.

File details

Details for the file pg_heartbeat-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pg_heartbeat-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e558e5e39fb9899cef2ef2220ddbb6535053cbf7317c925f3ce43f7507de366
MD5 da0963a1a6e230a42716b7204649fd2d
BLAKE2b-256 3f4f8a849b932c9be8916444424cdd38fc49585511a2b0dffe486106bb6c79e1

See more details on using hashes here.

Supported by

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