Skip to content

Commit

Permalink
add prometheus support to app
Browse files Browse the repository at this point in the history
  • Loading branch information
lfunderburk committed Nov 26, 2024
1 parent 7d82542 commit c2dcf10
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 8 deletions.
3 changes: 3 additions & 0 deletions ch7/api-dockerization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Redis instance

```bash
gunicorn -b 127.0.0.1:8000 myapp:app -k uvicorn.workers.UvicornWorker
pip-compile requirements.in --output-file requirements.txt
docker run --restart always -p 8000:8000 fastapi-bytewax-app

```

17 changes: 15 additions & 2 deletions ch7/api-dockerization/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import logging
import os

from indexing_dataflow import run_pipeline_with_symbol # Your Bytewax indexing pipeline
from querying import RetrieveDocuments # Your query pipeline logic
from prometheus_client import make_asgi_app, CollectorRegistry, multiprocess

from indexing_dataflow import run_pipeline_with_symbol
from querying import RetrieveDocuments

# Set up logging
logging.basicConfig(level=logging.INFO)
Expand All @@ -18,6 +20,10 @@
# Initialize FastAPI app
app = FastAPI()

# Add prometheus asgi middleware to route /metrics requests
metrics_app = make_asgi_app()
app.mount("/metrics", metrics_app)

# OpenAI API key from environment
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

Expand Down Expand Up @@ -75,3 +81,10 @@ def run_bytewax(symbol):
def health_check():
"""Health check endpoint."""
return {"status": "API is running"}

# Function to create a Prometheus ASGI app with multiprocessing support
def make_metrics_app():
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry) # Collect metrics from multiple processes
return make_asgi_app(registry=registry)

50 changes: 50 additions & 0 deletions ch7/api-dockerization/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: fastapi-bytewax
spec:
replicas: 3
selector:
matchLabels:
app: fastapi-bytewax
template:
metadata:
labels:
app: fastapi-bytewax
spec:
containers:
- name: fastapi-bytewax
image: fastapi-bytewax-app:latest
ports:
- containerPort: 8000
resources:
limits:
memory: "512Mi"
cpu: "500m"
requests:
memory: "256Mi"
cpu: "250m"
livenessProbe:
httpGet:
path: /
port: 8000
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 8000
initialDelaySeconds: 5
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: fastapi-bytewax
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8000
selector:
app: fastapi-bytewax
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ dependencies = [
"fastapi>=0.115",
"uvicorn",
"transformers[torch,sentencepiece]",
"pip-tools"
"pip-tools",
"prometheus-client>=0.21",
"gunicorn"
]

[build-system]
Expand Down
9 changes: 8 additions & 1 deletion requirements/lib-py3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ gitpython==3.1.43 \
--hash=sha256:35f314a9f878467f5453cc1fee295c3e18e52f1b99f10f6cf5b1682e968a9e7c \
--hash=sha256:eec7ec56b92aad751f9912a73404bc02ba212a23adb2c7098ee668417051a1ff
# via wandb
gunicorn==23.0.0 \
--hash=sha256:ec400d38950de4dfd418cff8328b2c8faed0edb0d517d3394e457c317908ca4d \
--hash=sha256:f014447a0101dc57e294f6c18ca6b40227a4c90e9bdb586042628030cba004ec
# via rag-with-haystack (pyproject.toml)
h11==0.14.0 \
--hash=sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d \
--hash=sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761
Expand Down Expand Up @@ -1319,6 +1323,7 @@ packaging==24.2 \
# accelerate
# build
# datasets
# gunicorn
# huggingface-hub
# ipykernel
# langchain-core
Expand Down Expand Up @@ -1476,7 +1481,9 @@ posthog==3.7.2 \
prometheus-client==0.21.0 \
--hash=sha256:4fa6b4dd0ac16d58bb587c04b1caae65b8c5043e85f778f42f5f632f6af2e166 \
--hash=sha256:96c83c606b71ff2b0a433c98889d275f51ffec6c5e267de37c7a2b5c9aa9233e
# via bytewax
# via
# rag-with-haystack (pyproject.toml)
# bytewax
prompt-toolkit==3.0.48 \
--hash=sha256:d6623ab0477a80df74e646bdbc93621143f5caf104206aa29294d53de1a03d90 \
--hash=sha256:f49a827f90062e411f1ce1f854f2aedb3c23353244f8108b89283587397ac10e
Expand Down
9 changes: 8 additions & 1 deletion requirements/lib-py3.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ gitpython==3.1.43 \
--hash=sha256:35f314a9f878467f5453cc1fee295c3e18e52f1b99f10f6cf5b1682e968a9e7c \
--hash=sha256:eec7ec56b92aad751f9912a73404bc02ba212a23adb2c7098ee668417051a1ff
# via wandb
gunicorn==23.0.0 \
--hash=sha256:ec400d38950de4dfd418cff8328b2c8faed0edb0d517d3394e457c317908ca4d \
--hash=sha256:f014447a0101dc57e294f6c18ca6b40227a4c90e9bdb586042628030cba004ec
# via rag-with-haystack (pyproject.toml)
h11==0.14.0 \
--hash=sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d \
--hash=sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761
Expand Down Expand Up @@ -1306,6 +1310,7 @@ packaging==24.2 \
# accelerate
# build
# datasets
# gunicorn
# huggingface-hub
# ipykernel
# langchain-core
Expand Down Expand Up @@ -1463,7 +1468,9 @@ posthog==3.7.2 \
prometheus-client==0.21.0 \
--hash=sha256:4fa6b4dd0ac16d58bb587c04b1caae65b8c5043e85f778f42f5f632f6af2e166 \
--hash=sha256:96c83c606b71ff2b0a433c98889d275f51ffec6c5e267de37c7a2b5c9aa9233e
# via bytewax
# via
# rag-with-haystack (pyproject.toml)
# bytewax
prompt-toolkit==3.0.48 \
--hash=sha256:d6623ab0477a80df74e646bdbc93621143f5caf104206aa29294d53de1a03d90 \
--hash=sha256:f49a827f90062e411f1ce1f854f2aedb3c23353244f8108b89283587397ac10e
Expand Down
9 changes: 8 additions & 1 deletion requirements/lib-py3.12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,10 @@ gitpython==3.1.43 \
--hash=sha256:35f314a9f878467f5453cc1fee295c3e18e52f1b99f10f6cf5b1682e968a9e7c \
--hash=sha256:eec7ec56b92aad751f9912a73404bc02ba212a23adb2c7098ee668417051a1ff
# via wandb
gunicorn==23.0.0 \
--hash=sha256:ec400d38950de4dfd418cff8328b2c8faed0edb0d517d3394e457c317908ca4d \
--hash=sha256:f014447a0101dc57e294f6c18ca6b40227a4c90e9bdb586042628030cba004ec
# via rag-with-haystack (pyproject.toml)
h11==0.14.0 \
--hash=sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d \
--hash=sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761
Expand Down Expand Up @@ -1302,6 +1306,7 @@ packaging==24.2 \
# accelerate
# build
# datasets
# gunicorn
# huggingface-hub
# ipykernel
# langchain-core
Expand Down Expand Up @@ -1459,7 +1464,9 @@ posthog==3.7.2 \
prometheus-client==0.21.0 \
--hash=sha256:4fa6b4dd0ac16d58bb587c04b1caae65b8c5043e85f778f42f5f632f6af2e166 \
--hash=sha256:96c83c606b71ff2b0a433c98889d275f51ffec6c5e267de37c7a2b5c9aa9233e
# via bytewax
# via
# rag-with-haystack (pyproject.toml)
# bytewax
prompt-toolkit==3.0.48 \
--hash=sha256:d6623ab0477a80df74e646bdbc93621143f5caf104206aa29294d53de1a03d90 \
--hash=sha256:f49a827f90062e411f1ce1f854f2aedb3c23353244f8108b89283587397ac10e
Expand Down
9 changes: 8 additions & 1 deletion requirements/lib-py3.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ gitpython==3.1.43 \
--hash=sha256:35f314a9f878467f5453cc1fee295c3e18e52f1b99f10f6cf5b1682e968a9e7c \
--hash=sha256:eec7ec56b92aad751f9912a73404bc02ba212a23adb2c7098ee668417051a1ff
# via wandb
gunicorn==23.0.0 \
--hash=sha256:ec400d38950de4dfd418cff8328b2c8faed0edb0d517d3394e457c317908ca4d \
--hash=sha256:f014447a0101dc57e294f6c18ca6b40227a4c90e9bdb586042628030cba004ec
# via rag-with-haystack (pyproject.toml)
h11==0.14.0 \
--hash=sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d \
--hash=sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761
Expand Down Expand Up @@ -1205,6 +1209,7 @@ packaging==24.2 \
# accelerate
# build
# datasets
# gunicorn
# huggingface-hub
# ipykernel
# transformers
Expand Down Expand Up @@ -1360,7 +1365,9 @@ posthog==3.7.2 \
prometheus-client==0.21.0 \
--hash=sha256:4fa6b4dd0ac16d58bb587c04b1caae65b8c5043e85f778f42f5f632f6af2e166 \
--hash=sha256:96c83c606b71ff2b0a433c98889d275f51ffec6c5e267de37c7a2b5c9aa9233e
# via bytewax
# via
# rag-with-haystack (pyproject.toml)
# bytewax
prompt-toolkit==3.0.48 \
--hash=sha256:d6623ab0477a80df74e646bdbc93621143f5caf104206aa29294d53de1a03d90 \
--hash=sha256:f49a827f90062e411f1ce1f854f2aedb3c23353244f8108b89283587397ac10e
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ bytewax-redis
fastapi>=0.115
uvicorn
transformers[torch,sentencepiece]
prometheus-client>=0.21
4 changes: 3 additions & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ platformdirs==4.3.6
posthog==3.7.3
# via haystack-ai
prometheus-client==0.21.0
# via bytewax
# via
# -r requirements.in
# bytewax
prompt-toolkit==3.0.48
# via ipython
propcache==0.2.0
Expand Down

0 comments on commit c2dcf10

Please sign in to comment.