Skip to content

Commit

Permalink
Merge pull request #393 from LACMTA:2023-api-optimization
Browse files Browse the repository at this point in the history
Refactor gunicorn process and add traceback in event of error
  • Loading branch information
albertkun authored Nov 20, 2023
2 parents d37d593 + 9d2f6c1 commit e4e62c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion fastapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ print(f'export DB_NAME={result.path[1:]}')" > /tmp/env.sh && \
echo "auth_file = /etc/pgbouncer/userlist.txt" >> /etc/pgbouncer/pgbouncer.ini && \
echo "client_login_timeout = 120" >> /etc/pgbouncer/pgbouncer.ini && \
gosu pgbouncer pgbouncer /etc/pgbouncer/pgbouncer.ini & \
gunicorn app.main:app --workers 2 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:80 --timeout 120 --keep-alive 5
gunicorn -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:80 app.main:app

EXPOSE 80
58 changes: 32 additions & 26 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from distutils.command.config import config
from typing import Union, List, Dict, Optional
from versiontag import get_version
import traceback
import sys

import http
import json
Expand Down Expand Up @@ -738,32 +740,36 @@ async def get_all_routes():

@app.on_event("startup")
async def startup_event():
redis_pool = await aioredis.from_url(Config.REDIS_URL)
redis = RedisBackend(redis_pool)
FastAPICache.init(backend=redis, prefix="fastapi-cache")
uvicorn_access_logger = logging.getLogger("uvicorn.access")
uvicorn_error_logger = logging.getLogger("uvicorn.error")
logger = logging.getLogger("uvicorn.app")
logzio_formatter = logging.Formatter("%(message)s")
logzio_uvicorn_access_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'uvicorn.access', 5, Config.LOGZIO_URL)
logzio_uvicorn_access_handler.setLevel(logging.INFO)
logzio_uvicorn_access_handler.setFormatter(logzio_formatter)

logzio_uvicorn_error_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'uvicorn.error', 5, Config.LOGZIO_URL)
logzio_uvicorn_error_handler.setLevel(logging.INFO)
logzio_uvicorn_error_handler.setFormatter(logzio_formatter)

logzio_app_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'fastapi.app', 5, Config.LOGZIO_URL)
logzio_app_handler.setLevel(logging.INFO)
logzio_app_handler.setFormatter(logzio_formatter)

uvicorn_access_logger.addHandler(logzio_uvicorn_access_handler)
uvicorn_error_logger.addHandler(logzio_uvicorn_error_handler)
logger.addHandler(logzio_app_handler)

uvicorn_access_logger.addFilter(LogFilter())
uvicorn_error_logger.addFilter(LogFilter())
logger.addFilter(LogFilter())
try:
redis_pool = await aioredis.from_url(Config.REDIS_URL)
redis = RedisBackend(redis_pool)
FastAPICache.init(backend=redis, prefix="fastapi-cache")
uvicorn_access_logger = logging.getLogger("uvicorn.access")
uvicorn_error_logger = logging.getLogger("uvicorn.error")
logger = logging.getLogger("uvicorn.app")
logzio_formatter = logging.Formatter("%(message)s")
logzio_uvicorn_access_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'uvicorn.access', 5, Config.LOGZIO_URL)
logzio_uvicorn_access_handler.setLevel(logging.INFO)
logzio_uvicorn_access_handler.setFormatter(logzio_formatter)

logzio_uvicorn_error_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'uvicorn.error', 5, Config.LOGZIO_URL)
logzio_uvicorn_error_handler.setLevel(logging.INFO)
logzio_uvicorn_error_handler.setFormatter(logzio_formatter)

logzio_app_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'fastapi.app', 5, Config.LOGZIO_URL)
logzio_app_handler.setLevel(logging.INFO)
logzio_app_handler.setFormatter(logzio_formatter)

uvicorn_access_logger.addHandler(logzio_uvicorn_access_handler)
uvicorn_error_logger.addHandler(logzio_uvicorn_error_handler)
logger.addHandler(logzio_app_handler)

uvicorn_access_logger.addFilter(LogFilter())
uvicorn_error_logger.addFilter(LogFilter())
logger.addFilter(LogFilter())
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stderr)

app.add_middleware(
CORSMiddleware,
Expand Down

0 comments on commit e4e62c5

Please sign in to comment.