Skip to content

Commit c2dcf10

Browse files
committed
add prometheus support to app
1 parent 7d82542 commit c2dcf10

File tree

10 files changed

+107
-8
lines changed

10 files changed

+107
-8
lines changed

ch7/api-dockerization/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
Redis instance
44

55
```bash
6+
gunicorn -b 127.0.0.1:8000 myapp:app -k uvicorn.workers.UvicornWorker
67
pip-compile requirements.in --output-file requirements.txt
8+
docker run --restart always -p 8000:8000 fastapi-bytewax-app
9+
710
```
811

ch7/api-dockerization/app.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import logging
99
import os
1010

11-
from indexing_dataflow import run_pipeline_with_symbol # Your Bytewax indexing pipeline
12-
from querying import RetrieveDocuments # Your query pipeline logic
11+
from prometheus_client import make_asgi_app, CollectorRegistry, multiprocess
12+
13+
from indexing_dataflow import run_pipeline_with_symbol
14+
from querying import RetrieveDocuments
1315

1416
# Set up logging
1517
logging.basicConfig(level=logging.INFO)
@@ -18,6 +20,10 @@
1820
# Initialize FastAPI app
1921
app = FastAPI()
2022

23+
# Add prometheus asgi middleware to route /metrics requests
24+
metrics_app = make_asgi_app()
25+
app.mount("/metrics", metrics_app)
26+
2127
# OpenAI API key from environment
2228
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
2329

@@ -75,3 +81,10 @@ def run_bytewax(symbol):
7581
def health_check():
7682
"""Health check endpoint."""
7783
return {"status": "API is running"}
84+
85+
# Function to create a Prometheus ASGI app with multiprocessing support
86+
def make_metrics_app():
87+
registry = CollectorRegistry()
88+
multiprocess.MultiProcessCollector(registry) # Collect metrics from multiple processes
89+
return make_asgi_app(registry=registry)
90+

ch7/api-dockerization/config.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: fastapi-bytewax
5+
spec:
6+
replicas: 3
7+
selector:
8+
matchLabels:
9+
app: fastapi-bytewax
10+
template:
11+
metadata:
12+
labels:
13+
app: fastapi-bytewax
14+
spec:
15+
containers:
16+
- name: fastapi-bytewax
17+
image: fastapi-bytewax-app:latest
18+
ports:
19+
- containerPort: 8000
20+
resources:
21+
limits:
22+
memory: "512Mi"
23+
cpu: "500m"
24+
requests:
25+
memory: "256Mi"
26+
cpu: "250m"
27+
livenessProbe:
28+
httpGet:
29+
path: /
30+
port: 8000
31+
initialDelaySeconds: 5
32+
periodSeconds: 10
33+
readinessProbe:
34+
httpGet:
35+
path: /
36+
port: 8000
37+
initialDelaySeconds: 5
38+
periodSeconds: 10
39+
---
40+
apiVersion: v1
41+
kind: Service
42+
metadata:
43+
name: fastapi-bytewax
44+
spec:
45+
type: LoadBalancer
46+
ports:
47+
- port: 80
48+
targetPort: 8000
49+
selector:
50+
app: fastapi-bytewax

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ dependencies = [
3636
"fastapi>=0.115",
3737
"uvicorn",
3838
"transformers[torch,sentencepiece]",
39-
"pip-tools"
39+
"pip-tools",
40+
"prometheus-client>=0.21",
41+
"gunicorn"
4042
]
4143

4244
[build-system]

requirements/lib-py3.10.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ gitpython==3.1.43 \
522522
--hash=sha256:35f314a9f878467f5453cc1fee295c3e18e52f1b99f10f6cf5b1682e968a9e7c \
523523
--hash=sha256:eec7ec56b92aad751f9912a73404bc02ba212a23adb2c7098ee668417051a1ff
524524
# via wandb
525+
gunicorn==23.0.0 \
526+
--hash=sha256:ec400d38950de4dfd418cff8328b2c8faed0edb0d517d3394e457c317908ca4d \
527+
--hash=sha256:f014447a0101dc57e294f6c18ca6b40227a4c90e9bdb586042628030cba004ec
528+
# via rag-with-haystack (pyproject.toml)
525529
h11==0.14.0 \
526530
--hash=sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d \
527531
--hash=sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761
@@ -1319,6 +1323,7 @@ packaging==24.2 \
13191323
# accelerate
13201324
# build
13211325
# datasets
1326+
# gunicorn
13221327
# huggingface-hub
13231328
# ipykernel
13241329
# langchain-core
@@ -1476,7 +1481,9 @@ posthog==3.7.2 \
14761481
prometheus-client==0.21.0 \
14771482
--hash=sha256:4fa6b4dd0ac16d58bb587c04b1caae65b8c5043e85f778f42f5f632f6af2e166 \
14781483
--hash=sha256:96c83c606b71ff2b0a433c98889d275f51ffec6c5e267de37c7a2b5c9aa9233e
1479-
# via bytewax
1484+
# via
1485+
# rag-with-haystack (pyproject.toml)
1486+
# bytewax
14801487
prompt-toolkit==3.0.48 \
14811488
--hash=sha256:d6623ab0477a80df74e646bdbc93621143f5caf104206aa29294d53de1a03d90 \
14821489
--hash=sha256:f49a827f90062e411f1ce1f854f2aedb3c23353244f8108b89283587397ac10e

requirements/lib-py3.11.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,10 @@ gitpython==3.1.43 \
513513
--hash=sha256:35f314a9f878467f5453cc1fee295c3e18e52f1b99f10f6cf5b1682e968a9e7c \
514514
--hash=sha256:eec7ec56b92aad751f9912a73404bc02ba212a23adb2c7098ee668417051a1ff
515515
# via wandb
516+
gunicorn==23.0.0 \
517+
--hash=sha256:ec400d38950de4dfd418cff8328b2c8faed0edb0d517d3394e457c317908ca4d \
518+
--hash=sha256:f014447a0101dc57e294f6c18ca6b40227a4c90e9bdb586042628030cba004ec
519+
# via rag-with-haystack (pyproject.toml)
516520
h11==0.14.0 \
517521
--hash=sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d \
518522
--hash=sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761
@@ -1306,6 +1310,7 @@ packaging==24.2 \
13061310
# accelerate
13071311
# build
13081312
# datasets
1313+
# gunicorn
13091314
# huggingface-hub
13101315
# ipykernel
13111316
# langchain-core
@@ -1463,7 +1468,9 @@ posthog==3.7.2 \
14631468
prometheus-client==0.21.0 \
14641469
--hash=sha256:4fa6b4dd0ac16d58bb587c04b1caae65b8c5043e85f778f42f5f632f6af2e166 \
14651470
--hash=sha256:96c83c606b71ff2b0a433c98889d275f51ffec6c5e267de37c7a2b5c9aa9233e
1466-
# via bytewax
1471+
# via
1472+
# rag-with-haystack (pyproject.toml)
1473+
# bytewax
14671474
prompt-toolkit==3.0.48 \
14681475
--hash=sha256:d6623ab0477a80df74e646bdbc93621143f5caf104206aa29294d53de1a03d90 \
14691476
--hash=sha256:f49a827f90062e411f1ce1f854f2aedb3c23353244f8108b89283587397ac10e

requirements/lib-py3.12.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,10 @@ gitpython==3.1.43 \
509509
--hash=sha256:35f314a9f878467f5453cc1fee295c3e18e52f1b99f10f6cf5b1682e968a9e7c \
510510
--hash=sha256:eec7ec56b92aad751f9912a73404bc02ba212a23adb2c7098ee668417051a1ff
511511
# via wandb
512+
gunicorn==23.0.0 \
513+
--hash=sha256:ec400d38950de4dfd418cff8328b2c8faed0edb0d517d3394e457c317908ca4d \
514+
--hash=sha256:f014447a0101dc57e294f6c18ca6b40227a4c90e9bdb586042628030cba004ec
515+
# via rag-with-haystack (pyproject.toml)
512516
h11==0.14.0 \
513517
--hash=sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d \
514518
--hash=sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761
@@ -1302,6 +1306,7 @@ packaging==24.2 \
13021306
# accelerate
13031307
# build
13041308
# datasets
1309+
# gunicorn
13051310
# huggingface-hub
13061311
# ipykernel
13071312
# langchain-core
@@ -1459,7 +1464,9 @@ posthog==3.7.2 \
14591464
prometheus-client==0.21.0 \
14601465
--hash=sha256:4fa6b4dd0ac16d58bb587c04b1caae65b8c5043e85f778f42f5f632f6af2e166 \
14611466
--hash=sha256:96c83c606b71ff2b0a433c98889d275f51ffec6c5e267de37c7a2b5c9aa9233e
1462-
# via bytewax
1467+
# via
1468+
# rag-with-haystack (pyproject.toml)
1469+
# bytewax
14631470
prompt-toolkit==3.0.48 \
14641471
--hash=sha256:d6623ab0477a80df74e646bdbc93621143f5caf104206aa29294d53de1a03d90 \
14651472
--hash=sha256:f49a827f90062e411f1ce1f854f2aedb3c23353244f8108b89283587397ac10e

requirements/lib-py3.9.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,10 @@ gitpython==3.1.43 \
511511
--hash=sha256:35f314a9f878467f5453cc1fee295c3e18e52f1b99f10f6cf5b1682e968a9e7c \
512512
--hash=sha256:eec7ec56b92aad751f9912a73404bc02ba212a23adb2c7098ee668417051a1ff
513513
# via wandb
514+
gunicorn==23.0.0 \
515+
--hash=sha256:ec400d38950de4dfd418cff8328b2c8faed0edb0d517d3394e457c317908ca4d \
516+
--hash=sha256:f014447a0101dc57e294f6c18ca6b40227a4c90e9bdb586042628030cba004ec
517+
# via rag-with-haystack (pyproject.toml)
514518
h11==0.14.0 \
515519
--hash=sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d \
516520
--hash=sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761
@@ -1205,6 +1209,7 @@ packaging==24.2 \
12051209
# accelerate
12061210
# build
12071211
# datasets
1212+
# gunicorn
12081213
# huggingface-hub
12091214
# ipykernel
12101215
# transformers
@@ -1360,7 +1365,9 @@ posthog==3.7.2 \
13601365
prometheus-client==0.21.0 \
13611366
--hash=sha256:4fa6b4dd0ac16d58bb587c04b1caae65b8c5043e85f778f42f5f632f6af2e166 \
13621367
--hash=sha256:96c83c606b71ff2b0a433c98889d275f51ffec6c5e267de37c7a2b5c9aa9233e
1363-
# via bytewax
1368+
# via
1369+
# rag-with-haystack (pyproject.toml)
1370+
# bytewax
13641371
prompt-toolkit==3.0.48 \
13651372
--hash=sha256:d6623ab0477a80df74e646bdbc93621143f5caf104206aa29294d53de1a03d90 \
13661373
--hash=sha256:f49a827f90062e411f1ce1f854f2aedb3c23353244f8108b89283587397ac10e

requirements/requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ bytewax-redis
1313
fastapi>=0.115
1414
uvicorn
1515
transformers[torch,sentencepiece]
16+
prometheus-client>=0.21

requirements/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ platformdirs==4.3.6
285285
posthog==3.7.3
286286
# via haystack-ai
287287
prometheus-client==0.21.0
288-
# via bytewax
288+
# via
289+
# -r requirements.in
290+
# bytewax
289291
prompt-toolkit==3.0.48
290292
# via ipython
291293
propcache==0.2.0

0 commit comments

Comments
 (0)