Skip to content

Commit

Permalink
feat(api): add prometheus monitoring (#1166)
Browse files Browse the repository at this point in the history
Adds a python library for exposing basic fastapi metrics in prometheus syntax on /metrics endpoint
Add a policy to block access to the /metrics from outside the cluster
Adds a service monitor to api deployment so prometheus will scrape metrics
Redirect base url to `/docs` to prevent not found errors.
  • Loading branch information
gphorvath authored Oct 3, 2024
1 parent 022e98d commit 81ff6a3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/api/chart/templates/istio-admin.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
5 changes: 5 additions & 0 deletions packages/api/chart/templates/uds-package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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" . }}
Expand Down
18 changes: 17 additions & 1 deletion src/leapfrogai_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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}")
Expand Down
1 change: 1 addition & 0 deletions src/leapfrogai_api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 81ff6a3

Please sign in to comment.