fix(logging): preserve bounded upstream responses in error logs

- Capture bounded per-attempt upstream status, masked headers, response chunks, and transport or stream errors when request-log is false.
- Bind synchronized deferred capture to the HTTP request context and preserve partial HTTP bodies and WebSocket dial or reconnect failures.
- Mask Cookie headers alongside Set-Cookie so forced error logs do not expose session credentials.

Closes: #7420
Co-authored-by: Dylan <1990016@gmail.com>
Co-authored-by: lzt404 <2596933790@qq.com>
This commit is contained in:
Luis Pater
2026-11-11 03:26:27 +08:00
parent 949b43b330
commit ece3f52a41
39 changed files with 2599 additions and 157 deletions

View File

@@ -194,15 +194,50 @@ func recordAPIWebsocketHandshake(ctx context.Context, cfg *config.Config, resp *
}
func websocketHandshakeBody(resp *http.Response) []byte {
body, _ := websocketHandshakeBodyWithReadErr(resp)
return body
}
func websocketHandshakeBodyWithReadErr(resp *http.Response) ([]byte, error) {
if resp == nil || resp.Body == nil {
return nil
return nil, nil
}
body, _ := io.ReadAll(resp.Body)
body, errRead := io.ReadAll(resp.Body)
closeHTTPResponseBody(resp, "codex websockets executor: close handshake response body error")
if len(body) == 0 {
return nil
return nil, errRead
}
return body
return body, errRead
}
func recordWebsocketUpgradeRejection(ctx context.Context, cfg *config.Config, wsReqLog helps.UpstreamRequestLog, resp *http.Response) []byte {
bodyErr, errRead := websocketHandshakeBodyWithReadErr(resp)
if resp != nil {
helps.RecordAPIWebsocketUpgradeRejection(ctx, cfg, websocketUpgradeRequestLog(wsReqLog), resp.StatusCode, resp.Header.Clone(), bodyErr)
if errRead != nil {
helps.RecordAPIResponseError(ctx, cfg, errRead)
}
}
return bodyErr
}
func recordWebsocketDialFailure(ctx context.Context, cfg *config.Config, wsReqLog helps.UpstreamRequestLog, resp *http.Response, errDial error) []byte {
bodyErr := recordWebsocketUpgradeRejection(ctx, cfg, wsReqLog, resp)
helps.RecordAPIWebsocketError(ctx, cfg, "dial", errDial)
return bodyErr
}
func recordWebsocketDialRetryFailure(ctx context.Context, cfg *config.Config, wsReqLog helps.UpstreamRequestLog, resp *http.Response, errDial error) []byte {
if resp != nil {
bodyErr := recordWebsocketUpgradeRejection(ctx, cfg, wsReqLog, resp)
helps.RecordAPIWebsocketError(ctx, cfg, "dial_retry", errDial)
return bodyErr
}
if cfg != nil && !cfg.CommercialMode && !cfg.RequestLog {
helps.RecordAPIRequest(ctx, cfg, websocketUpgradeRequestLog(wsReqLog))
}
helps.RecordAPIWebsocketError(ctx, cfg, "dial_retry", errDial)
return nil
}
func closeHTTPResponseBody(resp *http.Response, logPrefix string) {