feat: populate upstream nginx variables when ai-proxy uses cosocket transport#13317
Merged
nic-6443 merged 2 commits intoapache:masterfrom Apr 30, 2026
Merged
feat: populate upstream nginx variables when ai-proxy uses cosocket transport#13317nic-6443 merged 2 commits intoapache:masterfrom
nic-6443 merged 2 commits intoapache:masterfrom
Conversation
…ransport When the ai-proxy plugin sends requests via cosocket (resty.http), nginx's upstream module is bypassed, leaving $upstream_status, $upstream_addr, $upstream_response_time and other upstream variables empty in access logs. This uses the FFI functions from apisix-nginx-module 1.19.4 to populate these variables from Lua after cosocket requests complete: - Capture timing info (connect_time, header_time, response_time) during the HTTP lifecycle in ai-transport/http.lua - Call push_upstream_state() and update_upstream_state() in ai-proxy/base.lua to populate the upstream state struct - Set upstream_host, upstream_scheme, and upstream_uri to reflect the actual LLM provider endpoint (not the client's Host header) - Close cosocket on error paths to prevent connection leaks - Bump apisix-runtime to 1.3.5 (includes apisix-nginx-module 1.19.4)
There was a problem hiding this comment.
Pull request overview
This PR ensures standard Nginx upstream variables are populated for ai-proxy requests sent via cosocket (resty.http), so access logs (and any log consumers) can report upstream address/status/timings even though Nginx’s upstream module is bypassed.
Changes:
- Capture upstream metadata + timings in
ai-transport/http.luaand propagate them via the response object (and via an error metadata return value on failures). - Populate Nginx upstream state/vars from
ai-proxy/base.luausingresty.apisix.upstream(push on response/error; update after body consumption). - Update docs and tests to reflect/log newly populated upstream variables; bump APISIX runtime.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
apisix/plugins/ai-transport/http.lua |
Captures connect/header timing and attaches upstream metadata to the cosocket response (and returns metadata on failure). |
apisix/plugins/ai-proxy/base.lua |
Pushes/updates upstream state for access logging and sets $upstream_scheme/$upstream_host/$upstream_uri based on the provider endpoint. |
t/plugin/ai-proxy3.t |
Updates access_log regex expectations to include upstream addr/status and the upstream URL variables. |
t/plugin/ai-proxy-upstream-vars.t |
Adds a new test intended to validate upstream variable population for streaming and non-streaming. |
docs/en/latest/plugins/ai-proxy.md |
Documents upstream variables being populated and updates the access-log example. |
.requirements |
Bumps APISIX_RUNTIME to 1.3.5. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…onse_time Now that nginx upstream variables are populated natively via FFI, the custom apisix_upstream_response_time variable is no longer needed. Use the standard $upstream_response_time instead.
membphis
approved these changes
Apr 30, 2026
Baoyuantop
reviewed
Apr 30, 2026
Contributor
Baoyuantop
left a comment
There was a problem hiding this comment.
The PR description declares upstream_response_length, but it doesn't appear to be implemented in the code?
shreemaan-abhishek
approved these changes
Apr 30, 2026
Baoyuantop
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.
Description
When the ai-proxy plugin sends requests via cosocket (
resty.http), nginx's upstream module is bypassed entirely, leaving$upstream_status,$upstream_addr,$upstream_response_time,$upstream_header_time,$upstream_connect_time,$upstream_response_length,$upstream_host,$upstream_scheme, and$upstream_uriempty or defaulted in access logs.This uses the FFI functions from apisix-nginx-module 1.19.4 (
push_upstream_state/update_upstream_state) to populate these variables from Lua after cosocket requests complete.Changes
Transport layer (
ai-transport/http.lua)Plugin layer (
ai-proxy/base.lua)push_upstream_state()after getting a response (or on transport errors) to populate addr, status, connect_time, header_timeupdate_upstream_state()after body is fully consumed to populate response_time and response_lengthupstream_uri,upstream_host, andupstream_schemeto reflect the actual LLM provider endpointai-proxyandai-proxy-multiRuntime
Docs
Tests
t/plugin/ai-proxy-upstream-vars.t: New test validating upstream variable population (addr, status, response_time, uri, host) for streaming and non-streamingt/plugin/ai-proxy3.t: Adjusted access_log regex patterns to match populated upstream variables