Skip to content

Commit

Permalink
fix: mount metrics to /metrics and its subpaths
Browse files Browse the repository at this point in the history
  • Loading branch information
vegetablest authored and edwardzjl committed Jan 3, 2025
1 parent ee847c9 commit b15bdde
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions api/chatbot/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<path>.*)$")
app.routes.append(metrics_route)

app.include_router(chat_router)
app.include_router(conversation_router)
Expand Down

0 comments on commit b15bdde

Please sign in to comment.