diff --git a/packages/api/chart/templates/istio-admin.yaml b/packages/api/chart/templates/istio-admin.yaml new file mode 100644 index 000000000..c369e8786 --- /dev/null +++ b/packages/api/chart/templates/istio-admin.yaml @@ -0,0 +1,24 @@ +{{- if .Capabilities.APIVersions.Has "security.istio.io/v1beta1" }} +apiVersion: security.istio.io/v1beta1 +kind: AuthorizationPolicy +metadata: + name: api-block-metrics-access-from-public-gateway + namespace: {{ .Release.Namespace }} +spec: + selector: + matchLabels: + {{- include "chart.selectorLabels" . | nindent 6 }} + action: DENY + rules: + - to: + - operation: + ports: + - "8080" + paths: + - /metrics* + from: + - source: + notNamespaces: + - istio-admin-gateway + - monitoring +{{- end }} diff --git a/packages/api/chart/templates/uds-package.yaml b/packages/api/chart/templates/uds-package.yaml index a6a83dea8..17220788d 100644 --- a/packages/api/chart/templates/uds-package.yaml +++ b/packages/api/chart/templates/uds-package.yaml @@ -7,6 +7,11 @@ metadata: labels: {{- include "chart.labels" . | nindent 4 }} spec: + monitor: + - portName: http + targetPort: {{ .Values.api.service.port }} + selector: + {{- include "chart.selectorLabels" . | nindent 8 }} network: expose: - service: {{ include "chart.fullname" . }} diff --git a/src/leapfrogai_api/main.py b/src/leapfrogai_api/main.py index f9b3682d4..108ccd51e 100644 --- a/src/leapfrogai_api/main.py +++ b/src/leapfrogai_api/main.py @@ -8,7 +8,7 @@ from fastapi import FastAPI from fastapi.exception_handlers import request_validation_exception_handler from fastapi.exceptions import RequestValidationError - +from fastapi.responses import RedirectResponse from leapfrogai_api.routers.base import router as base_router from leapfrogai_api.routers.leapfrogai import auth from leapfrogai_api.routers.leapfrogai import models as lfai_models @@ -30,6 +30,7 @@ vector_stores, ) from leapfrogai_api.utils import get_model_config +from prometheus_fastapi_instrumentator import Instrumentator logging.basicConfig( level=os.getenv("LFAI_LOG_LEVEL", logging.INFO), @@ -62,6 +63,21 @@ async def lifespan(app: FastAPI): app = FastAPI(lifespan=lifespan) +@app.get("/", include_in_schema=False) +async def root(): + """Intercepts the root path and redirects to the API documentation.""" + return RedirectResponse(url="/docs") + + +Instrumentator( + excluded_handlers=["/healthz", "/metrics"], + should_group_status_codes=False, +).instrument(app).expose( + app, + include_in_schema=False, +) + + @app.exception_handler(RequestValidationError) async def validation_exception_handler(request, exc): logger.error(f"The client sent invalid data!: {exc}") diff --git a/src/leapfrogai_api/pyproject.toml b/src/leapfrogai_api/pyproject.toml index ea9b8f7e4..4542f7922 100644 --- a/src/leapfrogai_api/pyproject.toml +++ b/src/leapfrogai_api/pyproject.toml @@ -26,6 +26,7 @@ dependencies = [ "postgrest==0.16.11", # required by supabase, bug when using previous versions "openpyxl == 3.1.5", "psutil == 6.0.0", + "prometheus-fastapi-instrumentator == 7.0.0", "rerankers[flashrank] == 0.5.3" ] requires-python = "~=3.11"