監視とロギング

FastAPI を使った AI のプロダクション環境へのデプロイ

Matt Eckerle

Software and Data Engineering Leader

なぜ監視とロギングが必要か?

 

  • 本番環境でデバッグできない
  • アプリ管理者にシンプルなヘルスチェックが必要
  • 主要メトリクスを継続的にロギングする

監視・ロギングソフトウェア

FastAPI を使った AI のプロダクション環境へのデプロイ

カスタムロギングの設定

 

 

  • uvicornエラーロガーの読み込み

 

  • アプリ起動時にカスタムログを追加

 

  • エンドポイントにカスタムログを追加
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'
FastAPI を使った AI のプロダクション環境へのデプロイ

モデル読み込み時のロギング

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()
FastAPI を使った AI のプロダクション環境へのデプロイ

ミドルウェアによる処理時間のロギング

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/
FastAPI を使った AI のプロダクション環境へのデプロイ

ロギングレベルの設定

 

Pythonのロギングレベル

uvicorn main:app --log-level debug
FastAPI を使った AI のプロダクション環境へのデプロイ

監視

 

from fastapi import FastAPI

app = FastAPI()

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

 

 

 

  • "正常です!"
FastAPI を使った AI のプロダクション環境へのデプロイ

監視によるモデルパラメータの共有

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}

 

 

 

 

 

  • "正常です!"
  • "モデルの情報をお伝えします!"
FastAPI を使った AI のプロダクション環境へのデプロイ

練習しましょう!

FastAPI を使った AI のプロダクション環境へのデプロイ

Preparing Video For Download...