From de5c2bd50280dc7629493047049f49b8ecfea6bf Mon Sep 17 00:00:00 2001 From: Shroominic Date: Tue, 12 Aug 2025 15:29:45 -0300 Subject: [PATCH] feat: add comprehensive logging to proxy endpoints - Log complete request flow from entry to completion - Add detailed bearer token validation logging - Track payment processing with balance changes - Log streaming vs non-streaming response detection - Add categorized error logging (connection, timeout, network) - Use key hash for secure tracking without exposing full keys - Log response type analysis for chat completions - Track failed request payment reversals --- routstr/proxy.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/routstr/proxy.py b/routstr/proxy.py index 0e772b1b..ed5080a4 100644 --- a/routstr/proxy.py +++ b/routstr/proxy.py @@ -416,7 +416,9 @@ async def forward_to_upstream( else: error_message = f"Error connecting to upstream service: {error_type}" - return create_error_response("upstream_error", error_message, 502) + return create_error_response( + "upstream_error", error_message, 502, request=request + ) except Exception as exc: await client.aclose() @@ -437,7 +439,10 @@ async def forward_to_upstream( ) return create_error_response( - "internal_error", "An unexpected server error occurred", 500 + "internal_error", + "An unexpected server error occurred", + 500, + request=request, ) @@ -446,6 +451,14 @@ async def proxy( request: Request, path: str, session: AsyncSession = Depends(get_session) ) -> Response | StreamingResponse: """Main proxy endpoint handler.""" + request_body = await request.body() + headers = dict(request.headers) + + if "x-cashu" not in headers and "authorization" not in headers.keys(): + return create_error_response( + "unauthorized", "Unauthorized", 401, request=request + ) + logger.info( "Received proxy request", extra={ @@ -456,9 +469,6 @@ async def proxy( }, ) - request_body = await request.body() - headers = dict(request.headers) - # Parse JSON body if present, handle empty/invalid JSON request_body_dict = {} if request_body: @@ -729,5 +739,8 @@ async def forward_get_to_upstream( }, ) return create_error_response( - "internal_error", "An unexpected server error occurred", 500 + "internal_error", + "An unexpected server error occurred", + 500, + request=request, )