|
27 | 27 | logger_name,
|
28 | 28 | make_logger,
|
29 | 29 | )
|
| 30 | +from starlette.middleware import Middleware |
| 31 | +from starlette.middleware.base import BaseHTTPMiddleware |
30 | 32 |
|
31 | 33 | logger = make_logger(logger_name())
|
32 | 34 |
|
33 |
| -app = FastAPI(title="launch", version="1.0.0", redoc_url="/api") |
| 35 | + |
| 36 | +class CustomMiddleware(BaseHTTPMiddleware): |
| 37 | + async def dispatch(self, request: Request, call_next): |
| 38 | + try: |
| 39 | + LoggerTagManager.set(LoggerTagKey.REQUEST_ID, str(uuid.uuid4())) |
| 40 | + return await call_next(request) |
| 41 | + except Exception as e: |
| 42 | + tb_str = traceback.format_exception(etype=type(e), value=e, tb=e.__traceback__) |
| 43 | + request_id = LoggerTagManager.get(LoggerTagKey.REQUEST_ID) |
| 44 | + timestamp = datetime.now(pytz.timezone("US/Pacific")).strftime("%Y-%m-%d %H:%M:%S %Z") |
| 45 | + structured_log = { |
| 46 | + "error": str(e), |
| 47 | + "request_id": str(request_id), |
| 48 | + "traceback": "".join(tb_str), |
| 49 | + } |
| 50 | + logger.error("Unhandled exception: %s", structured_log) |
| 51 | + return JSONResponse( |
| 52 | + { |
| 53 | + "status_code": 500, |
| 54 | + "content": { |
| 55 | + "error": "Internal error occurred. Our team has been notified.", |
| 56 | + "timestamp": timestamp, |
| 57 | + "request_id": request_id, |
| 58 | + }, |
| 59 | + } |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +app = FastAPI( |
| 64 | + title="launch", version="1.0.0", redoc_url="/api", middleware=[Middleware(CustomMiddleware)] |
| 65 | +) |
34 | 66 |
|
35 | 67 | app.include_router(batch_job_router_v1)
|
36 | 68 | app.include_router(inference_task_router_v1)
|
|
44 | 76 | app.include_router(trigger_router_v1)
|
45 | 77 |
|
46 | 78 |
|
47 |
| -@app.middleware("http") |
48 |
| -async def dispatch(request: Request, call_next): |
49 |
| - try: |
50 |
| - LoggerTagManager.set(LoggerTagKey.REQUEST_ID, str(uuid.uuid4())) |
51 |
| - return await call_next(request) |
52 |
| - except Exception as e: |
53 |
| - tb_str = traceback.format_exception(etype=type(e), value=e, tb=e.__traceback__) |
54 |
| - request_id = LoggerTagManager.get(LoggerTagKey.REQUEST_ID) |
55 |
| - timestamp = datetime.now(pytz.timezone("US/Pacific")).strftime("%Y-%m-%d %H:%M:%S %Z") |
56 |
| - structured_log = { |
57 |
| - "error": str(e), |
58 |
| - "request_id": str(request_id), |
59 |
| - "traceback": "".join(tb_str), |
60 |
| - } |
61 |
| - logger.error("Unhandled exception: %s", structured_log) |
62 |
| - return JSONResponse( |
63 |
| - { |
64 |
| - "status_code": 500, |
65 |
| - "content": { |
66 |
| - "error": "Internal error occurred. Our team has been notified.", |
67 |
| - "timestamp": timestamp, |
68 |
| - "request_id": request_id, |
69 |
| - }, |
70 |
| - } |
71 |
| - ) |
72 |
| - |
73 |
| - |
74 | 79 | # TODO: Remove this once we have a better way to serve internal docs
|
75 | 80 | INTERNAL_DOCS_PATH = str(Path(__file__).parents[3] / "launch_internal/site")
|
76 | 81 | if os.path.exists(INTERNAL_DOCS_PATH):
|
|
0 commit comments