fix(ai-proxy): populate $upstream_response_length for cosocket transport#13323
Merged
nic-6443 merged 1 commit intoapache:masterfrom Apr 30, 2026
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes missing population of Nginx upstream variable $upstream_response_length when ai-proxy uses the cosocket transport by tracking bytes read from upstream responses and forwarding that to the upstream-state bookkeeping used for logging.
Changes:
- Track total upstream response body bytes in
parse_responseandparse_streaming_responseviares._upstream_bytes. - Pass
response_lengthinto the finalupdate_upstream_state()call inai-proxyafter the body has been consumed. - Add tests to assert
$upstream_response_lengthis non-zero for streaming and non-streaming requests; document the variable as supported.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
apisix/plugins/ai-providers/base.lua |
Tracks upstream body bytes for non-streaming and streaming parsing paths. |
apisix/plugins/ai-proxy/base.lua |
Forwards tracked byte count into upstream-state (response_length) for log vars. |
t/plugin/ai-proxy-upstream-vars.t |
Adds regression tests asserting $upstream_response_length becomes non-zero. |
docs/en/latest/plugins/ai-proxy.md |
Documents upstream_response_length as a populated upstream variable. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
e14442d to
e54e3a5
Compare
The previous commit (bda084d) added support for upstream nginx variables when ai-proxy uses cosocket transport, but missed passing response_length to update_upstream_state. This caused $upstream_response_length to always be 0 in access logs. Track total bytes read in both parse_response and parse_streaming_response, then pass the value as response_length to update_upstream_state after body consumption is complete.
e54e3a5 to
3d810d0
Compare
membphis
approved these changes
Apr 30, 2026
Baoyuantop
approved these changes
Apr 30, 2026
AlinsRan
approved these changes
Apr 30, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The previous commit (bda084d) added upstream nginx variable support for ai-proxy cosocket transport but missed passing
response_lengthtoupdate_upstream_state. This caused$upstream_response_lengthto always be 0 in access logs.This tracks total bytes read in both
parse_responseandparse_streaming_response, then passes the value asresponse_lengthtoupdate_upstream_stateafter body consumption is complete.Changes:
ai-providers/base.lua: storeres._upstream_bytesin all return paths of both parse methodsai-proxy/base.lua: passresponse_length = res._upstream_bytes or 0in the finalupdate_upstream_statecallai-proxy-upstream-vars.t) verifying non-zero$upstream_response_lengthfor both streaming and non-streamingupstream_response_lengthin the supported variables list