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

+34
View File
@@ -0,0 +1,34 @@
"""Gunicorn settings for the API. Metrics from every worker are aggregated through
prometheus_client's multiprocess mode and served by the master on METRICS_PORT."""
import os
import shutil
# Must be set before prometheus_client is imported by anything.
MULTIPROC_DIR = os.environ.setdefault("PROMETHEUS_MULTIPROC_DIR", "/tmp/scrobbler-metrics")
wsgi_app = "scrobbler:create_app()"
bind = os.environ.get("BIND", "0.0.0.0:8000")
workers = int(os.environ.get("WEB_CONCURRENCY", "2"))
accesslog = "-"
forwarded_allow_ips = os.environ.get("FORWARDED_ALLOW_IPS", "127.0.0.1")
def on_starting(server):
# Stale files from a previous run would double-count.
shutil.rmtree(MULTIPROC_DIR, ignore_errors=True)
os.makedirs(MULTIPROC_DIR, exist_ok=True)
def when_ready(server):
from scrobbler.metrics.server import start_metrics_server
start_metrics_server(
int(os.environ.get("METRICS_PORT", "9100")), os.environ.get("DATABASE_URL")
)
def child_exit(server, worker):
from prometheus_client import multiprocess
multiprocess.mark_process_dead(worker.pid)