diff --git a/Makefile b/Makefile index 36a17f6..2deeeb4 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ test: pytest dev: - fastapi dev app.py --host 0.0.0.0 + MUSICLIBRARY_DB=musiclibrary.db fastapi dev app.py --host 0.0.0.0 prod: fastapi run app.py diff --git a/app.py b/app.py index e23b816..027550e 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ import logging +import time from typing import Annotated from urllib.parse import quote_plus @@ -18,6 +19,7 @@ console_handler = logging.StreamHandler() console_handler.setFormatter(Logfmter(keys=["at", "name", "asctime"])) logging.basicConfig(level=logging.INFO, handlers=[console_handler]) +SLOW_REQUEST_THRESHOLD = 500 logger = logging.getLogger("beets-statistics") @@ -61,6 +63,17 @@ async def get_beets_statistics(): templates.env.filters["quote_plus"] = lambda u: quote_plus(u) +@app.middleware("http") +async def add_slow_request_log(request: Request, call_next): + start_time = time.time() + response = await call_next(request) + process_time = (time.time() - start_time) * 1000 + logger.debug(f"Completed request in {process_time:.2f} ms") + if process_time > SLOW_REQUEST_THRESHOLD: + logger.warning("Slow log: {process_time:.2f} ms") + return response + + @app.get("/", response_class=HTMLResponse) async def get_general_stats( request: Request,