App factory, data model, initial migration and HTTP metrics
- Flask app factory with env-driven config (.env loaded at import) - SQLAlchemy models: users, API apps, UI tokens, Last.fm auth tokens, sessions, scrobbles (deduplicated) and now-playing - Initial Alembic migration; CI checks migrations match the models - Prometheus metric definitions, per-endpoint RED hooks with bounded labels, and a standalone metrics server - docker compose Postgres with a test database; CI test job runs against a Postgres service container Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
606348800b
commit
98c8f2e824
21 files changed
+1777
-6
No files matched your search
@@ -0,0 +1,36 @@
|
||||
import time
|
||||
|
||||
from flask import Flask, g, request
|
||||
|
||||
from scrobbler import metrics
|
||||
|
||||
|
||||
def _endpoint() -> str:
|
||||
return request.url_rule.endpoint if request.url_rule else "unmatched"
|
||||
|
||||
|
||||
def init_app(app: Flask) -> None:
|
||||
@app.before_request
|
||||
def _start_timer():
|
||||
g._metrics_endpoint = _endpoint()
|
||||
g._metrics_start = time.perf_counter()
|
||||
metrics.http_requests_in_progress.labels(endpoint=g._metrics_endpoint).inc()
|
||||
|
||||
@app.after_request
|
||||
def _record(response):
|
||||
endpoint = g.get("_metrics_endpoint", _endpoint())
|
||||
start = g.get("_metrics_start")
|
||||
if start is not None:
|
||||
metrics.http_request_duration_seconds.labels(
|
||||
endpoint=endpoint, http_method=request.method
|
||||
).observe(time.perf_counter() - start)
|
||||
metrics.http_requests_total.labels(
|
||||
endpoint=endpoint, http_method=request.method, status=str(response.status_code)
|
||||
).inc()
|
||||
return response
|
||||
|
||||
@app.teardown_request
|
||||
def _finish(_exc):
|
||||
endpoint = g.pop("_metrics_endpoint", None)
|
||||
if endpoint is not None:
|
||||
metrics.http_requests_in_progress.labels(endpoint=endpoint).dec()
|
||||
Reference in new issue
Block a user