Skip to content

Commit

Permalink
Complete functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Puerari committed Sep 5, 2023
1 parent 3013759 commit 2346531
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
6 changes: 5 additions & 1 deletion TEMPLATE_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## 05/09/2023

### Changed

- Disabled the default logs and k8s routes logs

## 04/08/2023

Expand Down
3 changes: 2 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
uvicorn.run(
app,
host='0.0.0.0',
port=int(os.environ.get('HTTP_PORT', 3000))
port=int(os.environ.get('HTTP_PORT', 3000)),
log_config=None
)
2 changes: 1 addition & 1 deletion src/lib/mia_platform_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, headers, logger):
try:
self.headers_to_proxy[header_key] = headers[header_key]
except KeyError:
logger.warning(
logger.debug(
f'The parameter "{header_key}" is missing from the request headers'
)

Expand Down
20 changes: 19 additions & 1 deletion src/middlewares/logger_middleware.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import time
from starlette.middleware.base import BaseHTTPMiddleware

from src.utils.logger import logger


class LoggerMiddleware(BaseHTTPMiddleware):
"""
Middleware to add the logger to the request
Middleware to add the logger to the request and logs request info
"""

async def dispatch(self, request, call_next):
excluded_paths = [
'/-/ready',
'/-/healthz',
'/-/check-up',
]

if request.url.path not in excluded_paths:
logger.debug(f"{request.method} {request.url.path}")
start_time = time.time()

request.state.logger = logger
response = await call_next(request)

if request.url.path not in excluded_paths:
duration = time.time() - start_time
logger.debug(
f"{request.method} {request.url.path} {response.status_code} {duration:.6f}s"
)

return response
3 changes: 3 additions & 0 deletions src/utils/logger.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os
import sys
import logging
from dotenv import load_dotenv


load_dotenv('default.env')

# Stop uvicorn log propagation otherwise we would have duplicate logs
uvicorn_logger = logging.getLogger("uvicorn")
uvicorn_logger.propagate = False
Expand Down

0 comments on commit 2346531

Please sign in to comment.