harden deploy reports and admin alerts
This commit is contained in:
26
app/main.py
26
app/main.py
@@ -25,7 +25,8 @@ from app.api import (
|
||||
work_orders,
|
||||
)
|
||||
from app.core.config import settings
|
||||
from app.db.session import get_session
|
||||
from app.db.session import async_session_factory, get_session
|
||||
from app.services.admin_notifications import create_admin_notification
|
||||
from app.services.rate_limit import get_redis_client
|
||||
|
||||
|
||||
@@ -49,8 +50,29 @@ async def production_headers_and_metrics(request: Request, call_next):
|
||||
start = monotonic()
|
||||
try:
|
||||
response = await call_next(request)
|
||||
except Exception:
|
||||
except Exception as exc:
|
||||
REQUEST_ERRORS += 1
|
||||
try:
|
||||
async with async_session_factory() as session:
|
||||
await create_admin_notification(
|
||||
session,
|
||||
event_type="system_error",
|
||||
title="Unhandled API error",
|
||||
body=f"{request.method} {request.url.path}\nError: {type(exc).__name__}",
|
||||
entity_type="system",
|
||||
entity_id=request.url.path,
|
||||
severity="error",
|
||||
idempotency_key=f"system_error:{request.url.path}:{type(exc).__name__}:{int(start // 60)}",
|
||||
metadata={
|
||||
"path": request.url.path,
|
||||
"method": request.method,
|
||||
"request_id": request_id,
|
||||
"error_type": type(exc).__name__,
|
||||
},
|
||||
)
|
||||
await session.commit()
|
||||
except Exception:
|
||||
pass
|
||||
raise
|
||||
duration = monotonic() - start
|
||||
REQUEST_COUNT += 1
|
||||
|
||||
Reference in New Issue
Block a user