Copilot commented on code in PR #13224:
URL: https://github.com/apache/apisix/pull/13224#discussion_r3077885226
##########
apisix/core/response.lua:
##########
@@ -159,6 +164,41 @@ function _M.get_upstream_status(ctx)
end
+--- Get the source of the current response.
+--
+-- @function core.response.get_response_source
+-- @tparam table ctx The APISIX request context (api_ctx).
+-- @treturn string One of:
+-- "apisix" — response generated by APISIX Lua code (e.g. route not found,
plugin rejection)
+-- "nginx" — error generated by NGINX proxy module (e.g. connection
refused, timeout)
+-- "upstream" — real HTTP response returned by the upstream service
+function _M.get_response_source(ctx)
+ if not ctx then
+ return "apisix"
+ end
+
+ -- Priority 1: explicitly marked by core.response.exit()
+ if ctx._resp_source then
+ return ctx._resp_source
+ end
+
+ -- Priority 2: request was proxied — inspect the last
$upstream_header_time token
+ if ctx._apisix_proxied then
+ local header_time = ctx.var.upstream_header_time
+ -- With retries, $upstream_header_time is comma-separated (e.g. "-,
0.002").
+ -- The last token corresponds to the final attempt that produced the
response.
+ local last = header_time and str_match(header_time, "([^,]+)%s*$")
+ if last and last ~= "-" then
+ return "upstream" -- received response headers from upstream
+ end
+ return "nginx" -- never received response headers (connection
error, timeout, etc.)
Review Comment:
`get_response_source()` misclassifies retry failure cases when
`$upstream_header_time` contains spaces after commas (e.g. "-, -"). The pattern
`([^,]+)%s*$` returns the last token with a leading space (" -"), so `last ~=
"-"` becomes true and the function incorrectly returns "upstream" (and will
fail TEST 8/9). Trim whitespace from `last` or change the pattern to capture
the last non-space token before comparing to "-".
##########
apisix/core/response.lua:
##########
@@ -159,6 +164,41 @@ function _M.get_upstream_status(ctx)
end
+--- Get the source of the current response.
+--
+-- @function core.response.get_response_source
+-- @tparam table ctx The APISIX request context (api_ctx).
+-- @treturn string One of:
+-- "apisix" — response generated by APISIX Lua code (e.g. route not found,
plugin rejection)
+-- "nginx" — error generated by NGINX proxy module (e.g. connection
refused, timeout)
+-- "upstream" — real HTTP response returned by the upstream service
+function _M.get_response_source(ctx)
+ if not ctx then
+ return "apisix"
+ end
+
+ -- Priority 1: explicitly marked by core.response.exit()
+ if ctx._resp_source then
+ return ctx._resp_source
+ end
+
+ -- Priority 2: request was proxied — inspect the last
$upstream_header_time token
+ if ctx._apisix_proxied then
+ local header_time = ctx.var.upstream_header_time
Review Comment:
`get_response_source()` assumes `ctx.var` exists when `_apisix_proxied` is
true (`ctx.var.upstream_header_time`). If a caller sets `_apisix_proxied` on a
synthetic/partial ctx, this will throw. Consider guarding with `ctx.var and
ctx.var.upstream_header_time` (or returning "nginx" when `ctx.var` is missing)
to keep the API safe to call.
```suggestion
local header_time = ctx.var and ctx.var.upstream_header_time
```
##########
t/core/response-source.t:
##########
@@ -0,0 +1,273 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+log_level("info");
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ if (!$block->request) {
+ $block->set_value("request", "GET /t");
+ }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: get_response_source returns "apisix" when ctx is nil
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local source = core.response.get_response_source(nil)
+ ngx.say(source)
+ }
+ }
+--- response_body
+apisix
+
+
+
+=== TEST 2: get_response_source returns "apisix" when no flags set
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+apisix
+
+
+
+=== TEST 3: get_response_source returns "apisix" when _resp_source is set
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {_resp_source = "apisix"}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+apisix
+
+
+
+=== TEST 4: get_response_source returns "nginx" when proxied but no header time
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {_apisix_proxied = true, var = {upstream_header_time =
"-"}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+nginx
+
+
+
+=== TEST 5: get_response_source returns "nginx" when proxied and header time
nil
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {_apisix_proxied = true, var = {}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+nginx
+
+
+
+=== TEST 6: get_response_source returns "upstream" when proxied with numeric
header time
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {_apisix_proxied = true, var = {upstream_header_time =
"0.002"}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+upstream
+
+
+
+=== TEST 7: get_response_source handles retry: last attempt succeeded
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ -- first attempt failed, second succeeded
+ local ctx = {_apisix_proxied = true, var = {upstream_header_time =
"-, 0.002"}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+upstream
+
+
+
+=== TEST 8: get_response_source handles retry: all attempts failed
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ -- both attempts failed
+ local ctx = {_apisix_proxied = true, var = {upstream_header_time =
"-, -"}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+nginx
+
+
+
+=== TEST 9: get_response_source handles retry: last attempt failed
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ -- first succeeded but retry failed (edge case)
+ local ctx = {_apisix_proxied = true, var = {upstream_header_time =
"0.001, -"}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+nginx
+
+
+
+=== TEST 10: get_response_source: _resp_source takes priority over
_apisix_proxied
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {
+ _resp_source = "apisix",
+ _apisix_proxied = true,
+ var = {upstream_header_time = "0.002"}
+ }
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+apisix
+
+
+
+=== TEST 11: resp_exit sets _resp_source = "apisix" for error codes
+--- config
+ location = /t {
+ access_by_lua_block {
+ ngx.ctx.api_ctx = {}
+ local core = require("apisix.core")
+ core.response.exit(403, "forbidden\n")
+ }
+ log_by_lua_block {
+ local ctx = ngx.ctx.api_ctx
+ ngx.log(ngx.INFO, "resp_source: ", ctx._resp_source or "nil")
+ }
+ }
+--- error_code: 403
+--- response_body
+forbidden
+--- error_log
+resp_source: apisix
+
+
+
+=== TEST 12: resp_exit does NOT set _resp_source for success codes
+--- config
+ location = /t {
+ access_by_lua_block {
+ ngx.ctx.api_ctx = {}
+ local core = require("apisix.core")
+ core.response.exit(200, "ok\n")
+ }
+ log_by_lua_block {
+ local ctx = ngx.ctx.api_ctx
+ ngx.log(ngx.INFO, "resp_source: ", ctx._resp_source or "nil")
+ }
+ }
+--- response_body
+ok
+--- error_log
+resp_source: nil
+
+
+
+=== TEST 13: route not found returns response_source = "apisix"
+--- apisix_yaml
+routes: []
+#END
+--- request
+GET /nonexistent
+--- error_code: 404
+--- error_log eval
+qr/resp_source: apisix|_resp_source/
+
+
+
+=== TEST 14: integration - upstream returns 200, response_source = "upstream"
+--- apisix_yaml
+routes:
+ -
+ uri: /hello
+ upstream:
+ nodes:
+ "127.0.0.1:1980": 1
+ type: roundrobin
+#END
+--- request
+GET /hello
+--- error_code: 200
+
+
+
+=== TEST 15: integration - upstream connection refused, response_source =
"nginx"
+--- apisix_yaml
+routes:
+ -
+ uri: /hello
+ upstream:
+ nodes:
+ "127.0.0.1:11111": 1
+ type: roundrobin
+#END
+--- request
+GET /hello
+--- error_code: 502
Review Comment:
TEST 14/15 descriptions claim to validate `response_source` for upstream
success vs connection refused, but they only assert `--- error_code` and never
check the computed source. Add an assertion (e.g., log
`core.response.get_response_source(ngx.ctx.api_ctx)` in `log_by_lua_block`, or
expose it in a response header) so these integration tests actually exercise
the new classification logic.
##########
t/core/response-source.t:
##########
@@ -0,0 +1,273 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+log_level("info");
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ if (!$block->request) {
+ $block->set_value("request", "GET /t");
+ }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: get_response_source returns "apisix" when ctx is nil
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local source = core.response.get_response_source(nil)
+ ngx.say(source)
+ }
+ }
+--- response_body
+apisix
+
+
+
+=== TEST 2: get_response_source returns "apisix" when no flags set
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+apisix
+
+
+
+=== TEST 3: get_response_source returns "apisix" when _resp_source is set
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {_resp_source = "apisix"}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+apisix
+
+
+
+=== TEST 4: get_response_source returns "nginx" when proxied but no header time
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {_apisix_proxied = true, var = {upstream_header_time =
"-"}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+nginx
+
+
+
+=== TEST 5: get_response_source returns "nginx" when proxied and header time
nil
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {_apisix_proxied = true, var = {}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+nginx
+
+
+
+=== TEST 6: get_response_source returns "upstream" when proxied with numeric
header time
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {_apisix_proxied = true, var = {upstream_header_time =
"0.002"}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+upstream
+
+
+
+=== TEST 7: get_response_source handles retry: last attempt succeeded
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ -- first attempt failed, second succeeded
+ local ctx = {_apisix_proxied = true, var = {upstream_header_time =
"-, 0.002"}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+upstream
+
+
+
+=== TEST 8: get_response_source handles retry: all attempts failed
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ -- both attempts failed
+ local ctx = {_apisix_proxied = true, var = {upstream_header_time =
"-, -"}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+nginx
+
+
+
+=== TEST 9: get_response_source handles retry: last attempt failed
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ -- first succeeded but retry failed (edge case)
+ local ctx = {_apisix_proxied = true, var = {upstream_header_time =
"0.001, -"}}
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+nginx
+
+
+
+=== TEST 10: get_response_source: _resp_source takes priority over
_apisix_proxied
+--- config
+ location = /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local ctx = {
+ _resp_source = "apisix",
+ _apisix_proxied = true,
+ var = {upstream_header_time = "0.002"}
+ }
+ local source = core.response.get_response_source(ctx)
+ ngx.say(source)
+ }
+ }
+--- response_body
+apisix
+
+
+
+=== TEST 11: resp_exit sets _resp_source = "apisix" for error codes
+--- config
+ location = /t {
+ access_by_lua_block {
+ ngx.ctx.api_ctx = {}
+ local core = require("apisix.core")
+ core.response.exit(403, "forbidden\n")
+ }
+ log_by_lua_block {
+ local ctx = ngx.ctx.api_ctx
+ ngx.log(ngx.INFO, "resp_source: ", ctx._resp_source or "nil")
+ }
+ }
+--- error_code: 403
+--- response_body
+forbidden
+--- error_log
+resp_source: apisix
+
+
+
+=== TEST 12: resp_exit does NOT set _resp_source for success codes
+--- config
+ location = /t {
+ access_by_lua_block {
+ ngx.ctx.api_ctx = {}
+ local core = require("apisix.core")
+ core.response.exit(200, "ok\n")
+ }
+ log_by_lua_block {
+ local ctx = ngx.ctx.api_ctx
+ ngx.log(ngx.INFO, "resp_source: ", ctx._resp_source or "nil")
+ }
+ }
+--- response_body
+ok
+--- error_log
+resp_source: nil
+
+
+
+=== TEST 13: route not found returns response_source = "apisix"
+--- apisix_yaml
+routes: []
+#END
+--- request
+GET /nonexistent
+--- error_code: 404
+--- error_log eval
+qr/resp_source: apisix|_resp_source/
Review Comment:
TEST 13 asserts the error log contains `resp_source: apisix|_resp_source`,
but this test case doesn’t emit such a log line (no `log_by_lua_block` like
TEST 11/12). As written, this assertion looks unrelated to the actual request
and will likely fail/flap depending on other logs. Recommend explicitly logging
`core.response.get_response_source(ngx.ctx.api_ctx)` in `log_by_lua_block` (or
asserting via response headers/body) for this case.
```suggestion
--- response_body_like
Route Not Found
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]