เอา Prometheus Metrics ของ APISIX ขึ้น Grafana Dashboard
ก่อนหน้านี้ผมมีโอากาสได้เข้าไปช่วยทีม AI ในการ Develop AI Project ของบริษัท ทีมได้มีการ Deploy LLM service ไว้ใช้งานเอง และเปิดให้ทีมต่าง ๆ ภายในบริษัทได้เข้ามาใช้งาน LLM Sevice นี้
Service นี้เราใช้ APISIX ช่วยในการทำ API Gateway ซึ่งก่อนหน้านี้ เราใช้ APISIX แค่ช่วยในการทำ
- Routing
- Authentication ด้วย API Key
- SSL termination proxy
สามารถอ่านได้จากบทความก่อนหน้า
เมื่อมีการใช้งานไปสักพัก ก็เจอโจทย์จากทีมว่า อยากให้สามารถ Monitor ดูได้ว่า แต่ละทีมมีการใช้งาน LLM Service ของเรามากน้อยแค่ไหน เลยเป็นที่มาของบทความนี้
มาเริ่มกันเลย!
อันนี้คือภาพที่เราจะ Setup กัน
Steps:
- Config APISIX ให้พ่น Prometheus metrics ออกมา
- Setup Prometheus ให้ polling metrics จาก APISIX มาเก็บไว้
- Config Grafana ให้ดึง metrics จาก Prometheus ไปแสดง
บทความนี้ผมจะใช้ Git repository นี้เป็น example นะครับ
https://github.com/jittagornp/llm-setup
สามารถ Clone repository นี้ไป แล้วแก้ + run ได้เลย
1. Config APISIX ให้พ่น Prometheus metrics ออกมา
กำหนด Prometheus config บน APISIX
https://github.com/jittagornp/llm-setup/blob/main/apisix/apisix/config.yaml#L20
ใน Config ของ APISIX ตรง plugin_attr ให้เราเพิ่ม Config ของ Prometheus เข้าไปตามรูป เช่น กำหนดว่า อยากให้ expose metrics ที่ path ไหน port อะไร ใครสามารถ access metrics นี้ได้บ้าง (0.0.0.0 คือ ทุก source IP สามารถ access ได้) และอื่น ๆ
หมายเหตุ:
ถ้าเรา Deploy APISIX ผ่าน Docker compose อย่าลืมเปิด port 9091 ใน file compose ด้วยนะ แบบนี้
https://github.com/jittagornp/llm-setup/blob/main/apisix/docker-compose.yml#L31
จากนั้นทำการ restart APISIX
ลองทดสอบ metrics ที่ config ไว้ โดยการเปิด browser ขึ้นมา แล้วเข้า http://localhost:9091/apisix/prometheus/metrics
2. Setup Prometheus ให้ polling metrics จาก APISIX มาเก็บไว้
ต่อมาให้เรา Setup Prometheus ให้ไป polling metrics จาก APISIX มาเก็บไว้
แต่หากใครยังไม่เคยติดตั้ง Prometheus สามารถใช้ Docker compose นี้ run ได้เลย
https://github.com/jittagornp/llm-setup/blob/main/apisix/docker-compose.yml#L54
ทำการกำหนด Prometheus ให้ไปดึง metrics จาก APISIX แบบนี้
https://github.com/jittagornp/llm-setup/blob/main/apisix/prometheus/prometheus.yml#L13
จากรูปคือ ให้ไปดึง metrics จาก path /apisix/prometheus/metrics มาทุก ๆ 5 วินาที
ลองเข้า Prometheus โดยการเปิด browser ขึ้นมา แล้วเข้า http://localhost:19090/
จากนั้นลอง Query metrics ดู ก็จะได้ result แบบนี้
apisix_nginx_http_current_connections
ตอนนี้ APISIX + Prometheus ของเรา สามารถทำงานได้ร่วมกันได้แล้ว!
3. Config Grafana ให้ดึง metrics จาก Prometheus ไปแสดง
มาถึงขั้นตอนสุดท้ายแล้ว เราจะ Config ให้ Grafana dashboard ไปดึง metrics จาก Prometheus มาแสดง
3.1 เข้าไปที่ Grafana
โดยการเปิด browser ขึ้นมา แล้วเข้าไปที่ http://localhost:13000/
3.2 Add Data source
ไปที่เมนู Connections > Data sources
ทำการ Add Data source เพื่อให้ Grafana รู้จักกับ Prometheus
เลือกเป็น Prometheus
จากนั้น Config ค่าต่าง ๆ ลงไปดังนี้ (แค่นี้พอ)
- Name: Prometheus
- Prometheus server URL: http://prometheus:9090
จากนั้นคลิก Save & test ดู
Grafana สามารถเชื่อมต่อไปที่ Prometheus ได้แล้ว
3.3 เพิ่ม APISIX Dashboard เข้าไปใน Grafana
ไปที่เมนู Dashboard แล้ว New dashboard ขึ้นมา โดยเลือกเป็น Import แบบนี้
ให้เราไป Download .json ในหน้านี้มา Import ใส่
https://grafana.com/grafana/dashboards/11719-apache-apisix/
เลือก Data Source ที่เรา Config ไว้ในข้อ 3.2 แล้วคลิก Import
เราก็จะได้หน้า Dashboard ที่เอาไว้ดู Metrics ของ APISIX แบบนี้
ถ้าอยากได้ค่าอะไรต่าง ๆ เพิ่มเติม เราสามารถ Custom Dashboard เพิ่มเติมเองได้โดยเขียน PromQL ให้ไปดึงค่าจาก Prometheus มาแสดงแบบนี้
แค่นี้เราก็มี Dashboard สวย ๆ ที่เอาไว้ Mornitor ดู APISIX Metrics ของเราได้แล้ว