M22: implement operational observability
This commit is contained in:
+56
-8
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
@@ -19,6 +20,7 @@ from app.api.routers import (
|
||||
integrations,
|
||||
knowledge,
|
||||
mcp_integrations,
|
||||
observability,
|
||||
search,
|
||||
users,
|
||||
vehicles,
|
||||
@@ -28,9 +30,19 @@ from app.api.routers.auth import bootstrap_initial_admin
|
||||
from app.core.config import PRODUCT_NAME, get_settings
|
||||
from app.core.db import SessionLocal
|
||||
from app.core.errors import AppError, error_body
|
||||
from app.core.observability import (
|
||||
DATABASE_READY,
|
||||
configure_logging,
|
||||
correlation_id_context,
|
||||
correlation_id_for,
|
||||
request_finished,
|
||||
request_started,
|
||||
)
|
||||
from app.services.dispatcher import start_background_dispatcher, stop_background_dispatcher
|
||||
|
||||
settings = get_settings()
|
||||
configure_logging(settings.log_level)
|
||||
request_logger = logging.getLogger("mobilityops.request")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -53,6 +65,34 @@ app.add_middleware(
|
||||
https_only=settings.session_cookie_secure,
|
||||
)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def request_observability(request: Request, call_next):
|
||||
correlation_id = correlation_id_for(request)
|
||||
request.state.correlation_id = correlation_id
|
||||
token = correlation_id_context.set(correlation_id)
|
||||
started_at = request_started()
|
||||
status_code = 500
|
||||
try:
|
||||
response = await call_next(request)
|
||||
status_code = response.status_code
|
||||
response.headers["X-Correlation-Id"] = correlation_id
|
||||
return response
|
||||
finally:
|
||||
duration = request_finished(request, status_code, started_at)
|
||||
request_logger.info(
|
||||
"request_completed",
|
||||
extra={
|
||||
"method": request.method,
|
||||
"path": request.url.path,
|
||||
"status_code": status_code,
|
||||
"duration_ms": round(duration * 1000, 2),
|
||||
"client_ip": request.client.host if request.client else None,
|
||||
},
|
||||
)
|
||||
correlation_id_context.reset(token)
|
||||
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=[o.strip() for o in settings.cors_allow_origins.split(",")],
|
||||
@@ -63,21 +103,26 @@ app.add_middleware(
|
||||
|
||||
|
||||
@app.exception_handler(AppError)
|
||||
def handle_app_error(_request: Request, exc: AppError) -> JSONResponse:
|
||||
def handle_app_error(request: Request, exc: AppError) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content=error_body(exc.code, exc.message, exc.correlation_id, exc.details),
|
||||
content=error_body(
|
||||
exc.code,
|
||||
exc.message,
|
||||
request.state.correlation_id,
|
||||
exc.details,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(HTTPException)
|
||||
def handle_http_exception(_request: Request, exc: HTTPException) -> JSONResponse:
|
||||
def handle_http_exception(request: Request, exc: HTTPException) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content=error_body(
|
||||
code=str(exc.status_code),
|
||||
message=str(exc.detail),
|
||||
correlation_id=str(uuid.uuid4()),
|
||||
correlation_id=getattr(request.state, "correlation_id", str(uuid.uuid4())),
|
||||
details={},
|
||||
),
|
||||
)
|
||||
@@ -101,10 +146,12 @@ def readiness() -> JSONResponse:
|
||||
with SessionLocal() as db:
|
||||
db.execute(text("SELECT 1"))
|
||||
except Exception: # noqa: BLE001 -- readiness must convert infrastructure errors to 503
|
||||
return JSONResponse(
|
||||
status_code=503,
|
||||
content={"status": "not_ready", "service": "mobilityops-api", "database": "down"},
|
||||
)
|
||||
DATABASE_READY.set(0)
|
||||
DATABASE_READY.set(1)
|
||||
return JSONResponse(
|
||||
status_code=503,
|
||||
content={"status": "not_ready", "service": "mobilityops-api", "database": "down"},
|
||||
)
|
||||
return JSONResponse(content={"status": "ready", "service": "mobilityops-api", "database": "up"})
|
||||
|
||||
|
||||
@@ -135,3 +182,4 @@ app.include_router(mcp_integrations.router)
|
||||
app.include_router(search.router)
|
||||
app.include_router(integration_status.router)
|
||||
app.include_router(users.router)
|
||||
app.include_router(observability.router)
|
||||
|
||||
Reference in New Issue
Block a user