fix: make debug-mode work with global rule (#3023)

Fix #1667.

Signed-off-by: spacewander <spacewanderlzx@gmail.com>
This commit is contained in:
罗泽轩 2020-12-18 17:14:51 +08:00 committed by GitHub
parent fd2db606a9
commit e9826b60f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 163 additions and 21 deletions

View File

@ -289,13 +289,40 @@ function _M.load(config)
end
local function trace_plugins_info_for_debug(plugins)
if not (local_conf and local_conf.apisix.enable_debug) then
return
end
local is_http = ngx.config.subsystem == "http"
if not plugins then
if is_http and not ngx.headers_sent then
core.response.add_header("Apisix-Plugins", "no plugin")
else
core.log.warn("Apisix-Plugins: no plugin")
end
return
end
local t = {}
for i = 1, #plugins, 2 do
core.table.insert(t, plugins[i].name)
end
if is_http and not ngx.headers_sent then
core.response.add_header("Apisix-Plugins", core.table.concat(t, ", "))
else
core.log.warn("Apisix-Plugins: ", core.table.concat(t, ", "))
end
end
function _M.filter(user_route, plugins)
local user_plugin_conf = user_route.value.plugins
if user_plugin_conf == nil or
core.table.nkeys(user_plugin_conf) == 0 then
if local_conf and local_conf.apisix.enable_debug then
core.response.set_header("Apisix-Plugins", "no plugin")
end
trace_plugins_info_for_debug(nil)
return core.empty_tab
end
@ -310,13 +337,7 @@ function _M.filter(user_route, plugins)
end
end
if local_conf.apisix.enable_debug then
local t = {}
for i = 1, #plugins, 2 do
core.table.insert(t, plugins[i].name)
end
core.response.set_header("Apisix-Plugins", core.table.concat(t, ", "))
end
trace_plugins_info_for_debug(plugins)
return plugins
end
@ -326,9 +347,7 @@ function _M.stream_filter(user_route, plugins)
plugins = plugins or core.table.new(#stream_local_plugins * 2, 0)
local user_plugin_conf = user_route.value.plugins
if user_plugin_conf == nil then
if local_conf and local_conf.apisix.enable_debug then
core.response.set_header("Apisix-Plugins", "no plugin")
end
trace_plugins_info_for_debug(nil)
return plugins
end
@ -342,13 +361,7 @@ function _M.stream_filter(user_route, plugins)
end
end
if local_conf.apisix.enable_debug then
local t = {}
for i = 1, #plugins, 2 do
core.table.insert(t, plugins[i].name)
end
core.response.set_header("Apisix-Plugins", core.table.concat(t, ", "))
end
trace_plugins_info_for_debug(plugins)
return plugins
end

View File

@ -657,9 +657,12 @@ Server: openresty
hello world
```
If the information can be delivered via HTTP response header, for example, the plugin is in stream
subsystem, the information will be logged in the error log with `warn` level.
### Advanced Debug Mode
Enable advanced debug mode by modifying the configuration in `conf/debug.yaml` file. Because there will have a check every second, only the checker reads the `#END` flag, and the file would consider as closed.
Enable advanced debug mode by modifying the configuration in `conf/debug.yaml` file. Because there will be a check every second, only the checker reads the `#END` flag, and the file would be considered as closed.
The checker would judge whether the file data changed according to the last modification time of the file. If there has any change, reload it. If there was no change, skip this check. So it's hot reload for enabling or disabling advanced debug mode.

View File

@ -675,6 +675,9 @@ Server: openresty
hello world
```
如果这个信息无法通过 HTTP 应答头传递,比如插件在 stream 子系统里面执行,
那么这个信息会以 warn 等级日志写入到错误日志中。
### 高级调试模式
设置 `conf/debug.yaml` 中的选项,开启高级调试模式。由于 APISIX 服务启动后是每秒定期检查该文件,

View File

@ -193,3 +193,126 @@ hello world
Apisix-Plugins: limit-conn, limit-count
--- no_error_log
[error]
=== TEST 6: global rule, header sent
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/global_rules/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"response-rewrite": {
"status_code": 200,
"body": "yes\n"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 7: hit routes
--- request
GET /hello
--- yaml_config eval: $::yaml_config
--- response_headers
Apisix-Plugins: response-rewrite, limit-conn, limit-count, response-rewrite
--- response_body
yes
--- error_log
Apisix-Plugins: response-rewrite
--- no_error_log
[error]
=== TEST 8: clear global routes
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/global_rules/1',
ngx.HTTP_DELETE
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 9: set stream route
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/stream_routes/1',
ngx.HTTP_PUT,
[[{
"remote_addr": "127.0.0.1",
"server_port": 1985,
"plugins": {
"mqtt-proxy": {
"protocol_name": "MQTT",
"protocol_level": 4,
"upstream": {
"ip": "127.0.0.1",
"port": 1995
}
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 10: hit route
--- yaml_config eval: $::yaml_config
--- stream_enable
--- stream_request eval
"\x10\x0f\x00\x04\x4d\x51\x54\x54\x04\x02\x00\x3c\x00\x03\x66\x6f\x6f"
--- stream_response
hello world
--- error_log
Apisix-Plugins: mqtt-proxy
--- no_error_log
[error]