nic-6443 commented on code in PR #13224:
URL: https://github.com/apache/apisix/pull/13224#discussion_r3077901286
##########
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:
Fixed — switched from `str_match` pattern to a `gmatch("%S+")` loop that
skips commas, so leading/trailing spaces are handled correctly. Also added
tests for spaced separators like `"- , -"` and `"- , 0.002"`.
##########
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:
Fixed — removed the flaky TEST 13 and replaced it with a proper unit test
for spaced separators. Integration tests (now TEST 16/17) use
`serverless-pre-function` log phase to explicitly log and assert
`response_source`.
##########
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:
Fixed — added `ctx.var and` guard before accessing
`ctx.var.upstream_header_time`. Also added TEST 15 that passes a ctx with nil
`var` field.
##########
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:
Fixed — TEST 16 and 17 now use `serverless-pre-function` in log phase to
call `get_response_source()` and log the result, then assert via `---
error_log`.
--
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]