Skip to content

fix: improve logging parsing for empty 502 errors#184

Open
subpath wants to merge 1 commit into
mainfrom
fix-improve-logger-parsing
Open

fix: improve logging parsing for empty 502 errors#184
subpath wants to merge 1 commit into
mainfrom
fix-improve-logger-parsing

Conversation

@subpath

@subpath subpath commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

Context

Transport-level 502s logged as Failed to proxy request: with no detail. raise_and_log (utils.py) built its message from e.response.text or str(e); httpx transport errors (ConnectError, RemoteProtocolError, timeouts) have no .response and often stringify to "", so the line was empty. Plain logger.error also captured no traceback (record.exception was null), and lines carried no user/model context.

What's new:

  • raise_and_log: log type(e).name, fall back to repr(e), and use logger.opt(exception=e) to attach the traceback. Client-facing error text unchanged.
  • logger.py HTTPX _wrapper: include exception type + repr (first line of the 502 triple).
  • Bind request fields onto the loguru contextvar via logger.contextualize(*req.log_fields) in stream_completion/get_completion/get_search, so user/service_type/purpose/model ride in record.extra. (queryable in Cloud Logging). log_fields is a shared mixin on the chat/search request types.
  • The scope is bound inside the proxy functions, not middleware: the middleware contextualize exits at the 200 OK, before the SSE body iterates, so streamed errors lose context (the known streaming blind spot). The streaming wrapper forwards aclose() to preserve disconnect handling.

QA:

Three regression tests capture real loguru records and assert message, traceback, and bound fields:

  • empty-message transport error through get_completion;
  • mid-stream error (verifies streaming scope);
  • HTTPX wrapper line.

Verified by reverting the fix: the line collapsed back to Failed to proxy request: and both tests failed.

@subpath subpath requested a review from a team as a code owner June 18, 2026 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant