Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add prometheus monitoring #1166

Merged
merged 16 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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