Skip to content

Commit

Permalink
Added requestId in slack alert
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilleo10 committed Nov 22, 2023
1 parent 43d2ac4 commit e637177
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

origins = ["*"]

app.add_middleware(RequestIdInjection)

# CORS middleware
app.add_middleware(
CORSMiddleware,
Expand All @@ -39,6 +37,7 @@
allow_headers=["*"],
)
app.add_middleware(RateLimitMiddleware)
app.add_middleware(RequestIdInjection)
app.include_router(user, prefix="/user")


Expand Down Expand Up @@ -92,8 +91,12 @@ async def http_exception_handler(request: Request, exc: HTTPException):
async def http_exception_handler(request: Request, exc: Exception):
error_message = f'Error: {str(exc)}'
# Include the traceback in the response for debugging purposes
traceback_str = traceback.format_exc()
send_slack_message({ "text": f'```{traceback_str}```', "request_url": str(request.url), "request_method": str(request.method)})
traceback_str = traceback.format_exc(chain=False)
send_slack_message(
{
"text": f'```\nRequestID: {request_id_contextvar.get()}\nRequest URL: {str(request.url)} \nRequest_method: {str(request.method)} \nTraceback: {traceback_str}```'
}
)

return JSONResponse(
status_code=500,
Expand Down
4 changes: 2 additions & 2 deletions middlewares/request_id_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
request_id_contextvar = contextvars.ContextVar("request_id", default=None)

class RequestIdInjection(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
def dispatch(self, request: Request, call_next):
request_id = str(uuid.uuid4())
request_id_contextvar.set(request_id)
try:
return await call_next(request)
return call_next(request)

except Exception as ex:
print(ex)
Expand Down

0 comments on commit e637177

Please sign in to comment.