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

engine = create_engine("postgresql://user:pass@localhost/mydb")

# Create tables once at app startup
create_tables(engine)

# Create a client
db = PgHeartbeat(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 = PgHeartbeat(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.

PgHeartbeat(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 PgHeartbeat 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.1.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.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pg_heartbeat-0.1.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.1.0.tar.gz
Algorithm Hash digest
SHA256 6f17cb8a7351332ad3949e5a1c845d7fc6d8ecb662b026e94482d2f364e08c22
MD5 8f12cb6a39e22cecfbcfd2d44fc111d8
BLAKE2b-256 5b509ffe91438c67db7cd2e5db0413b6edfc732d263da158e4f6fab0655d7c07

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pg_heartbeat-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e2a17a38c4ca2075649de521cedda2c31e7e4b64752b065a1ffea5725fd7f48
MD5 d25c20492da9fa72ed8d9ccc7bcaf787
BLAKE2b-256 0541374a9f68762a9689da1d6df7c371b70a66daa247d1f8c69b124affdadbe9

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