diff --git a/api/chatbot/main.py b/api/chatbot/main.py index b1194cb..66c7ec2 100644 --- a/api/chatbot/main.py +++ b/api/chatbot/main.py @@ -1,17 +1,19 @@ """Main entrypoint for the app.""" +import re from contextlib import asynccontextmanager from fastapi import FastAPI, status from fastapi.encoders import jsonable_encoder from fastapi.requests import Request -from fastapi.responses import JSONResponse, RedirectResponse +from fastapi.responses import JSONResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver from loguru import logger from prometheus_client import make_asgi_app from sqlalchemy.exc import NoResultFound +from starlette.routing import Mount from chatbot.config import settings from chatbot.dependencies import EmailHeaderDep, UserIdHeaderDep, UsernameHeaderDep @@ -47,17 +49,11 @@ async def lifespan(app: FastAPI): ) # Add prometheus asgi middleware to route /metrics requests -metrics_app = make_asgi_app() -app.mount("/metrics/", metrics_app) - - -# NOTE: Even I mounted it to `/metrics`, I must append the trailing slash to access the metrics. i.e. `/metrics/` -# I think it's a bug in fastapi. -# My current solution is to add a redirect endpoint for this specific request. -@app.get("/metrics") -async def redirect_to_metrics_with_slash(): - return RedirectResponse(url="/metrics/") - +metrics_route = Mount("/metrics", make_asgi_app()) +# @See https://github.com/prometheus/client_python/issues/1016#issuecomment-2088243791 +# @See https://github.com/vllm-project/vllm/pull/4511#issuecomment-2088375895 +metrics_route.path_regex = re.compile("^/metrics(?P.*)$") +app.routes.append(metrics_route) app.include_router(chat_router) app.include_router(conversation_router)