prometheus.py 1009 B

123456789101112131415161718192021222324252627282930313233343536
  1. from fastapi import APIRouter, HTTPException, Request, status
  2. from gpustack.config.config import get_global_config
  3. from gpustack.routes.proxy import proxy_to
  4. router = APIRouter()
  5. @router.api_route(
  6. "/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]
  7. )
  8. async def prometheus_proxy(
  9. path: str,
  10. request: Request,
  11. ):
  12. """
  13. Prometheus proxy endpoint.
  14. Only admin users can access.
  15. """
  16. cfg = get_global_config()
  17. prometheus_base_url = cfg.get_builtin_prometheus_url()
  18. if not prometheus_base_url:
  19. raise HTTPException(
  20. status_code=status.HTTP_404_NOT_FOUND,
  21. detail="Not found",
  22. )
  23. target_path = path.lstrip("/")
  24. if target_path:
  25. target_url = f"{prometheus_base_url}/prometheus/{target_path}"
  26. else:
  27. target_url = f"{prometheus_base_url}/prometheus"
  28. if request.url.query:
  29. target_url = f"{target_url}?{request.url.query}"
  30. return await proxy_to(request, target_url)