Skip to content

Commit

Permalink
working & improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
extreme4all committed Nov 29, 2024
1 parent 33e24d2 commit a8a5681
Show file tree
Hide file tree
Showing 7 changed files with 587 additions and 92 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.10-slim as base
FROM python:3.10-slim AS base

ARG api_port
ENV UVICORN_PORT ${api_port}
Expand All @@ -23,9 +23,9 @@ RUN pip install --no-cache-dir -r requirements.txt
COPY ./src /project/src

# production image
FROM base as production
FROM base AS production
# Creates a non-root user with an explicit UID and adds permission to access the /project folder
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /project
USER appuser

CMD ["uvicorn", "src.core.server:app", "--proxy-headers", "--host", "0.0.0.0"]
CMD ["uvicorn", "src.core.server:app", "--proxy-headers", "--host", "0.0.0.0", "--log-level", "warning"]
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
args:
root_path: /
api_port: 5000
command: uvicorn src.core.server:app --host 0.0.0.0 --reload --reload-include src/*
command: uvicorn src.core.server:app --host 0.0.0.0 --reload --reload-include src/* --log-level warning
container_name: bd-dev-api
environment:
- sql_uri=mysql+asyncmy://root:root_bot_buster@mysql/playerdata
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"aiohttp==3.9.5",
"aiohttp>=3.9.5",
"asyncmy==0.2.8",
"fastapi==0.100.1",
"fastapi[standard]>=0.115.5",
"pandas>=2.0.3",
"prometheus-client>=0.21.0",
"python-dotenv==1.0.0",
"sqlalchemy==2.0.19",
"starlette-prometheus==0.9.0",
"uvicorn==0.23.1",
"starlette-prometheus>=0.9.0",
]

[dependency-groups]
Expand Down
76 changes: 67 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,60 @@ aiosignal==1.3.1
annotated-types==0.7.0
# via pydantic
anyio==4.6.2.post1
# via starlette
# via
# httpx
# starlette
# watchfiles
asyncmy==0.2.8
# via core-api (pyproject.toml)
attrs==24.2.0
# via aiohttp
certifi==2024.8.30
# via
# httpcore
# httpx
click==8.1.7
# via uvicorn
fastapi==0.100.1
# via
# typer
# uvicorn
dnspython==2.7.0
# via email-validator
email-validator==2.2.0
# via fastapi
fastapi==0.115.5
# via core-api (pyproject.toml)
fastapi-cli==0.0.5
# via fastapi
frozenlist==1.5.0
# via
# aiohttp
# aiosignal
greenlet==3.1.1
# via sqlalchemy
h11==0.14.0
# via
# httpcore
# uvicorn
httpcore==1.0.7
# via httpx
httptools==0.6.4
# via uvicorn
httpx==0.28.0
# via fastapi
idna==3.10
# via
# anyio
# email-validator
# httpx
# yarl
jinja2==3.1.4
# via fastapi
markdown-it-py==3.0.0
# via rich
markupsafe==3.0.2
# via jinja2
mdurl==0.1.2
# via markdown-it-py
multidict==6.1.0
# via
# aiohttp
Expand All @@ -36,41 +69,66 @@ numpy==2.1.3
# via pandas
pandas==2.2.3
# via core-api (pyproject.toml)
prometheus-client==0.12.0
# via starlette-prometheus
prometheus-client==0.21.0
# via
# core-api (pyproject.toml)
# starlette-prometheus
propcache==0.2.0
# via yarl
pydantic==2.10.2
# via fastapi
pydantic-core==2.27.1
# via pydantic
pygments==2.18.0
# via rich
python-dateutil==2.9.0.post0
# via pandas
python-dotenv==1.0.0
# via core-api (pyproject.toml)
# via
# core-api (pyproject.toml)
# uvicorn
python-multipart==0.0.18
# via fastapi
pytz==2024.2
# via pandas
pyyaml==6.0.2
# via uvicorn
rich==13.9.4
# via typer
shellingham==1.5.4
# via typer
six==1.16.0
# via python-dateutil
sniffio==1.3.1
# via anyio
sqlalchemy==2.0.19
# via core-api (pyproject.toml)
starlette==0.27.0
starlette==0.41.3
# via
# fastapi
# starlette-prometheus
starlette-prometheus==0.9.0
starlette-prometheus==0.10.0
# via core-api (pyproject.toml)
typer==0.14.0
# via fastapi-cli
typing-extensions==4.12.2
# via
# fastapi
# pydantic
# pydantic-core
# sqlalchemy
# typer
tzdata==2024.2
# via pandas
uvicorn==0.23.1
# via core-api (pyproject.toml)
# via
# fastapi
# fastapi-cli
uvloop==0.21.0
# via uvicorn
watchfiles==1.0.0
# via uvicorn
websockets==14.1
# via uvicorn
yarl==1.18.0
# via aiohttp
14 changes: 7 additions & 7 deletions src/core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def filter(self, record):
logging.basicConfig(level=logging.INFO, handlers=[handler])

# set imported loggers to warning
logging.getLogger("requests").setLevel(logging.DEBUG)
logging.getLogger("urllib3").setLevel(logging.DEBUG)
logging.getLogger("uvicorn").setLevel(logging.DEBUG)
logging.getLogger("apscheduler").setLevel(logging.WARNING)
logging.getLogger("aiomysql").setLevel(logging.ERROR)
logging.getLogger("asyncmy").setLevel(logging.ERROR)
logging.getLogger("aiokafka").setLevel(logging.WARNING)
# logging.getLogger("requests").setLevel(logging.DEBUG)
# logging.getLogger("urllib3").setLevel(logging.DEBUG)
# logging.getLogger("uvicorn").setLevel(logging.DEBUG)
# logging.getLogger("apscheduler").setLevel(logging.WARNING)
# logging.getLogger("aiomysql").setLevel(logging.ERROR)
# logging.getLogger("asyncmy").setLevel(logging.ERROR)
# logging.getLogger("aiokafka").setLevel(logging.WARNING)
36 changes: 3 additions & 33 deletions src/core/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from src import api
from starlette_prometheus import metrics, PrometheusMiddleware

from src.core.fastapi.middleware import LoggingMiddleware
logger = logging.getLogger(__name__)


Expand All @@ -32,6 +32,7 @@ def make_middleware() -> list[Middleware]:
allow_methods=["*"],
allow_headers=["*"],
),
Middleware(LoggingMiddleware)
]
return middleware

Expand Down Expand Up @@ -60,29 +61,6 @@ async def root():
async def favicon():
return {}


@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time

query_params_list = [
(key, value if key != "token" else "***")
for key, value in request.query_params.items()
]

url_path = request.url.path
logger.debug(
{
"url": url_path,
"params": query_params_list,
"process_time": f"{process_time:.4f}",
}
)
return response


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
error = json.loads(exc.json())
Expand All @@ -95,12 +73,4 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
"error": error,
}
)
return JSONResponse(content={"detail": error}, status_code=422)


# @app.on_event("startup")
# async def startup_event():
# if config.env != "DEV":
# logger.info("startup initiated")
# highscore_processor = HighscoreProcessor(batch_size=100)
# asyncio.ensure_future(highscore_processor.start())
return JSONResponse(content={"detail": error}, status_code=422)
Loading

0 comments on commit a8a5681

Please sign in to comment.