Monitoring stack, containers and dashboards
- API image (uv multi-stage, non-root, gunicorn, migrations on start, health check) and UI image (nginx, CSP, config.js rendered from API_BASE_URL); /healthz documented in OpenAPI - docker compose runs db, api and ui; the "monitoring" profile adds Prometheus, postgres-exporter and Grafana, all provisioned from files - Grafana dashboards: API overview (RED), Last.fm API, Usage, Database - Prometheus alert rules: API down, error rate, latency, auth failure spike, ignored scrobbles, DB pool saturation, Postgres down - Pool checkout gauge counts events (the pool's own counters lag the checkin event); error ratios show 0 instead of "No data" - Tests check dashboards and alerts only use metrics that exist - scripts/generate_traffic.py drives realistic traffic for demos Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
1 parent
faa74871a5
commit
ea705589f1
28 files changed
+3508
-14
No files matched your search
@@ -43,13 +43,14 @@ def init_app(app: Flask) -> None:
|
||||
},
|
||||
)
|
||||
|
||||
from scrobbler.api import apps, auth, sessions, stats, tokens
|
||||
from scrobbler.api import apps, auth, health, sessions, stats, tokens
|
||||
|
||||
api.register_blueprint(auth.blp, url_prefix="/api/v1/auth")
|
||||
api.register_blueprint(apps.blp, url_prefix="/api/v1/apps")
|
||||
api.register_blueprint(sessions.blp, url_prefix="/api/v1/sessions")
|
||||
api.register_blueprint(tokens.blp, url_prefix="/api/v1/tokens")
|
||||
api.register_blueprint(stats.blp, url_prefix="/api/v1/me")
|
||||
api.register_blueprint(health.blp)
|
||||
|
||||
from scrobbler.openapi import lastfm
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
from flask_smorest import Blueprint, abort
|
||||
from marshmallow import Schema, fields
|
||||
from sqlalchemy import text
|
||||
|
||||
from scrobbler.extensions import db
|
||||
from scrobbler.schemas import ErrorSchema
|
||||
|
||||
blp = Blueprint("Health", __name__, description="Liveness for load balancers and containers")
|
||||
|
||||
|
||||
class HealthSchema(Schema):
|
||||
status = fields.String(metadata={"example": "ok"})
|
||||
|
||||
|
||||
@blp.route("/healthz", methods=["GET"])
|
||||
@blp.response(200, HealthSchema)
|
||||
@blp.alt_response(503, schema=ErrorSchema, description="The database is unreachable")
|
||||
def healthz():
|
||||
"""Health check
|
||||
|
||||
Returns 200 when the API can reach its database.
|
||||
"""
|
||||
try:
|
||||
db.session.execute(text("SELECT 1"))
|
||||
except Exception:
|
||||
db.session.rollback()
|
||||
abort(503, code="database_unavailable", message="The database is unreachable")
|
||||
return {"status": "ok"}
|
||||
+21
-10
@@ -34,15 +34,26 @@ def instrument(engine: Engine) -> None:
|
||||
stack.pop()
|
||||
|
||||
pool = engine.pool
|
||||
if not hasattr(pool, "size"): # e.g. NullPool in tools: nothing worth reporting
|
||||
return
|
||||
checked_out = metrics.db_pool_connections.labels(state="checked_out")
|
||||
overflow = metrics.db_pool_connections.labels(state="overflow")
|
||||
metrics.db_pool_connections.labels(state="size").set(pool.size())
|
||||
|
||||
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())
|
||||
# Pool events fire before the pool updates its own counters, so reading
|
||||
# pool.checkedout() inside them is off by one; count the events instead.
|
||||
@event.listens_for(pool, "checkout")
|
||||
def _checkout(*_args):
|
||||
checked_out.inc()
|
||||
|
||||
for name in ("connect", "checkout", "checkin", "close"):
|
||||
event.listen(pool, name, _report_pool)
|
||||
@event.listens_for(pool, "checkin")
|
||||
def _checkin(*_args):
|
||||
checked_out.dec()
|
||||
|
||||
@event.listens_for(pool, "connect")
|
||||
def _connect(*_args):
|
||||
overflow.set(max(0, pool.overflow()))
|
||||
|
||||
@event.listens_for(pool, "close")
|
||||
def _close(*_args):
|
||||
overflow.set(max(0, pool.overflow()))
|
||||
Reference in new issue
Block a user