-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logging fixes and enhancements (#421)
- Loading branch information
1 parent
39169cf
commit 819d4e0
Showing
4 changed files
with
55 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,61 @@ | ||
from typing import Callable | ||
from typing import Callable, Optional | ||
|
||
import structlog | ||
from fastapi import Request, Response | ||
from fastapi import BackgroundTasks, HTTPException, Request, Response | ||
from fastapi.routing import APIRoute | ||
from starlette.background import BackgroundTask | ||
|
||
logger = structlog.get_logger() | ||
|
||
|
||
def log_request(request: Request): | ||
def log_endpoint( | ||
request: Request, | ||
status_code: Optional[int], | ||
): | ||
logger.info( | ||
"Velour API Call", | ||
method=request.method, | ||
path=request.url.path, | ||
hostname=request.url.hostname, | ||
status=status_code, | ||
) | ||
|
||
|
||
def add_task(response: Response, task: BackgroundTask) -> Response: | ||
if not response.background: | ||
response.background = BackgroundTasks([task]) | ||
elif isinstance(response.background, BackgroundTasks): | ||
response.background.add_task(task) | ||
else: # Empirically this doesn't happen but let's handle it anyway | ||
if not isinstance(response.background, BackgroundTask): | ||
logger.error( | ||
"Unexpected response.background", | ||
background_type=str(type(response.background)), | ||
) | ||
old_task = response.background | ||
response.background = BackgroundTasks([old_task, task]) | ||
|
||
return response | ||
|
||
|
||
class LoggingRoute(APIRoute): | ||
def get_route_handler(self) -> Callable: | ||
original_route_handler = super().get_route_handler() | ||
|
||
async def custom_route_handler(request: Request) -> Response: | ||
response = await original_route_handler(request) | ||
BackgroundTask(log_request, request) | ||
return response | ||
try: | ||
response = await original_route_handler(request) | ||
except HTTPException as e: | ||
log_endpoint(request, e.status_code) | ||
raise | ||
except Exception: | ||
log_endpoint(request, None) | ||
logger.exception("Uncaught exception") | ||
raise | ||
else: | ||
task = BackgroundTask( | ||
log_endpoint, request, response.status_code | ||
) | ||
return add_task(response, task) | ||
|
||
return custom_route_handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters