การ Monitoring และ Logging

การ Deploy AI สู่ Production ด้วย FastAPI

Matt Eckerle

Software and Data Engineering Leader

ทำไมต้องใช้ Monitoring และ Logging?

 

  • ไม่สามารถ debug ใน production ได้
  • ผู้ดูแลแอปต้องการตรวจสอบสถานะแบบง่าย
  • บันทึก Metric สำคัญตามเวลา

ซอฟต์แวร์ Monitoring และ Logging

การ Deploy AI สู่ Production ด้วย FastAPI

ตั้งค่า Logging แบบกำหนดเอง

 

 

  • โหลด uvicorn error logger

 

  • เพิ่ม Log แบบกำหนดเองเมื่อแอปเริ่มทำงาน

 

  • เพิ่ม Log แบบกำหนดเองใน Endpoint
from fastapi import FastAPI
import logging

logger = logging.getLogger(
    'uvicorn.error'
)

app = FastAPI() logger.info("App is running!")
@app.get('/') async def main(): logger.debug('GET /') return 'ok'
การ Deploy AI สู่ Production ด้วย FastAPI

บันทึก Log เมื่อโหลดโมเดล

from fastapi import FastAPI
import logging
import joblib 

logger = logging.getLogger('uvicorn.error')

model = joblib.load('penguin_classifier.pkl') logger.info("Penguin classifier loaded successfully.") app = FastAPI()
การ Deploy AI สู่ Production ด้วย FastAPI

บันทึกเวลาประมวลผลด้วย Middleware

from fastapi import FastAPI, Request
import logging
import time
logger = logging.getLogger('uvicorn.error')
app = FastAPI()

@app.middleware("http")
async def log_process_time(request: Request, call_next):
    start_time = time.perf_counter()
    response = await call_next(request)
    process_time = time.perf_counter() - start_time
    logger.info(f"Process time was {process_time} seconds.")
    return response
1 https://fastapi.tiangolo.com/tutorial/middleware/
การ Deploy AI สู่ Production ด้วย FastAPI

กำหนดระดับ Logging

 

ระดับ Logging ของ Python

uvicorn main:app --log-level debug
การ Deploy AI สู่ Production ด้วย FastAPI

Monitoring

 

from fastapi import FastAPI

app = FastAPI()

@app.get("/health") async def get_health(): return {"status": "OK"}

 

 

 

  • "ฉันปกติดี!"
การ Deploy AI สู่ Production ด้วย FastAPI

แชร์ Parameter ของโมเดลผ่าน Monitoring

from fastapi import FastAPI
import joblib

model = joblib.load(
    'penguin_classifier.pkl'
)
app = FastAPI()

@app.get("/health") async def get_health(): params = model.get_params() return {"status": "OK", "params": params}

 

 

 

 

 

  • "ฉันปกติดี!"
  • "นี่คือข้อมูลเพิ่มเติมเกี่ยวกับฉัน!"
การ Deploy AI สู่ Production ด้วย FastAPI

มาฝึกกันเถอะ!

การ Deploy AI สู่ Production ด้วย FastAPI

Preparing Video For Download...