Metrics: database timing, pool state, DB-backed gauges, gunicorn
CI / lint (push) Successful in 11s
CI / test (push) Successful in 39s
CI / notify (push) Successful in 5s

- Statement latency by operation and connection-pool gauges via
  SQLAlchemy events
- Users, active users (1h/24h/7d) and now-playing gauges computed at
  scrape time, cached for 60s, from a NullPool engine that is safe in
  the gunicorn master; exports nothing (not an error) if the DB is down
- gunicorn.conf.py: multiprocess metrics aggregated across workers and
  served by the master on METRICS_PORT, stale files cleared at start,
  dead workers marked
- Tests cover labels, in-flight gauge, DB metrics, the collector and a
  real two-worker gunicorn aggregation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
mattandClaude Opus 5.5 committed 2026-09-25 14:15:30 +01:00
1 parent 89f8589424
commit faa74871a5
7 files changed
+319 -7

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
"""SQLAlchemy hooks: statement latency and connection-pool state."""
import time
from sqlalchemy import event
from sqlalchemy.engine import Engine
from scrobbler import metrics
_OPERATIONS = {"select", "insert", "update", "delete"}
def _operation(statement: str) -> str:
word = statement.lstrip().split(None, 1)[0].lower() if statement.strip() else ""
return word if word in _OPERATIONS else "other"
def instrument(engine: Engine) -> None:
@event.listens_for(engine, "before_cursor_execute")
def _before(conn, cursor, statement, parameters, context, executemany):
conn.info.setdefault("_query_start", []).append(time.perf_counter())
@event.listens_for(engine, "after_cursor_execute")
def _after(conn, cursor, statement, parameters, context, executemany):
start = conn.info["_query_start"].pop()
metrics.db_query_duration_seconds.labels(operation=_operation(statement)).observe(
time.perf_counter() - start
)
@event.listens_for(engine, "handle_error")
def _error(context):
stack = context.connection.info.get("_query_start") if context.connection else None
if stack:
stack.pop()
pool = engine.pool
def _report_pool(*_args):
# QueuePool exposes these; other pools (e.g. NullPool in tools) don't.
if not hasattr(pool, "checkedout"):
return
metrics.db_pool_connections.labels(state="checked_out").set(pool.checkedout())
metrics.db_pool_connections.labels(state="idle").set(pool.checkedin())
metrics.db_pool_connections.labels(state="overflow").set(max(0, pool.overflow()))
metrics.db_pool_connections.labels(state="size").set(pool.size())
for name in ("connect", "checkout", "checkin", "close"):
event.listen(pool, name, _report_pool)