From 861a3d18d3719c5a7b434649726c2566263558ac Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Mon, 13 Sep 2021 11:29:00 +0800 Subject: [PATCH 1/5] chore(debug-mode): move 'enable_debug' form config.yaml to debug.yaml --- apisix/debug.lua | 11 +++++++- apisix/plugin.lua | 9 +++---- conf/config-default.yaml | 1 - conf/debug.yaml | 1 + .../latest/architecture-design/debug-mode.md | 2 +- .../latest/architecture-design/debug-mode.md | 2 +- t/config-center-yaml/consumer.t | 1 - t/config-center-yaml/plugin.t | 2 -- t/debug/debug-mode.t | 26 ++++++++++++------- 9 files changed, 33 insertions(+), 22 deletions(-) diff --git a/apisix/debug.lua b/apisix/debug.lua index 8cd0135c6c94..45ef5713d932 100644 --- a/apisix/debug.lua +++ b/apisix/debug.lua @@ -19,7 +19,6 @@ local yaml = require("tinyyaml") local log = require("apisix.core.log") local json = require("apisix.core.json") local profile = require("apisix.core.profile") -local process = require("ngx.process") local lfs = require("lfs") local io = io local ngx = ngx @@ -200,7 +199,17 @@ local function sync_debug_status(premature) end +function _M.enable_debug() + if not debug_yaml then + return false + end + + return debug_yaml.enable_debug +end + + function _M.init_worker() + local process = require("ngx.process") if process.type() ~= "worker" then return end diff --git a/apisix/plugin.lua b/apisix/plugin.lua index 307e1025b61e..91bee2b577b9 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -17,6 +17,7 @@ local require = require local core = require("apisix.core") local config_util = require("apisix.core.config_util") +local enable_debug = require("apisix.debug").enable_debug local ngx_exit = ngx.exit local pkg_loaded = package.loaded local sort_tab = table.sort @@ -167,8 +168,7 @@ local function load(plugin_names) for i, plugin in ipairs(local_plugins) do local_plugins_hash[plugin.name] = plugin - if local_conf and local_conf.apisix - and local_conf.apisix.enable_debug then + if enable_debug then core.log.warn("loaded plugin and sort by priority:", " ", plugin.priority, " name: ", plugin.name) @@ -209,8 +209,7 @@ local function load_stream(plugin_names) for i, plugin in ipairs(stream_local_plugins) do stream_local_plugins_hash[plugin.name] = plugin - if local_conf and local_conf.apisix - and local_conf.apisix.enable_debug then + if enable_debug then core.log.warn("loaded stream plugin and sort by priority:", " ", plugin.priority, " name: ", plugin.name) @@ -280,7 +279,7 @@ end local function trace_plugins_info_for_debug(ctx, plugins) - if not (local_conf and local_conf.apisix.enable_debug) then + if not enable_debug then return end diff --git a/conf/config-default.yaml b/conf/config-default.yaml index 781239bdeff4..8de1b310e81f 100644 --- a/conf/config-default.yaml +++ b/conf/config-default.yaml @@ -30,7 +30,6 @@ apisix: # enable_http2: true enable_admin: true enable_admin_cors: true # Admin API support CORS response headers. - enable_debug: false enable_dev_mode: false # Sets nginx worker_processes to 1 if set to true enable_reuseport: true # Enable nginx SO_REUSEPORT switch if set to true. enable_ipv6: true diff --git a/conf/debug.yaml b/conf/debug.yaml index b3527e1d5f48..a4488e8617fa 100644 --- a/conf/debug.yaml +++ b/conf/debug.yaml @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +enable_debug: false hook_conf: enable: false # enable or disable this feature name: hook_phase # the name of module and function list diff --git a/docs/en/latest/architecture-design/debug-mode.md b/docs/en/latest/architecture-design/debug-mode.md index bcf17c006201..f0c428e1ed41 100644 --- a/docs/en/latest/architecture-design/debug-mode.md +++ b/docs/en/latest/architecture-design/debug-mode.md @@ -23,7 +23,7 @@ title: Debug Mode ### Basic Debug Mode -Enable basic debug mode just by setting `apisix.enable_debug = true` in `conf/config.yaml` file. +Enable basic debug mode just by setting `enable_debug = true` in `conf/debug.yaml` file. e.g Using both `limit-conn` and `limit-count` plugins for a `/hello` request, there will have a response header called `Apisix-Plugins: limit-conn, limit-count`. diff --git a/docs/zh/latest/architecture-design/debug-mode.md b/docs/zh/latest/architecture-design/debug-mode.md index ce733cf4be49..9c3399b54783 100644 --- a/docs/zh/latest/architecture-design/debug-mode.md +++ b/docs/zh/latest/architecture-design/debug-mode.md @@ -23,7 +23,7 @@ title: Debug Mode ### 基本调试模式 -设置 `conf/config.yaml` 中的 `apisix.enable_debug` 为 `true`,即可开启基本调试模式。 +设置 `conf/debug.yaml` 中的 `enable_debug` 为 `true`,即可开启基本调试模式。 比如对 `/hello` 开启了 `limit-conn`和`limit-count`插件,这时候应答头中会有 `Apisix-Plugins: limit-conn, limit-count`。 diff --git a/t/config-center-yaml/consumer.t b/t/config-center-yaml/consumer.t index 1f1493efb129..0e7ae502f0f4 100644 --- a/t/config-center-yaml/consumer.t +++ b/t/config-center-yaml/consumer.t @@ -29,7 +29,6 @@ apisix: node_listen: 1984 config_center: yaml enable_admin: false - enable_debug: true _EOC_ $block->set_value("yaml_config", $yaml_config); diff --git a/t/config-center-yaml/plugin.t b/t/config-center-yaml/plugin.t index f3246b91c355..2cc3a32004ce 100644 --- a/t/config-center-yaml/plugin.t +++ b/t/config-center-yaml/plugin.t @@ -29,7 +29,6 @@ apisix: node_listen: 1984 config_center: yaml enable_admin: false - enable_debug: true _EOC_ $block->set_value("yaml_config", $yaml_config); @@ -88,7 +87,6 @@ apisix: node_listen: 1984 config_center: yaml enable_admin: false - enable_debug: true plugins: - ip-restriction - jwt-auth diff --git a/t/debug/debug-mode.t b/t/debug/debug-mode.t index 9c9e89f000bc..5ff717849d67 100644 --- a/t/debug/debug-mode.t +++ b/t/debug/debug-mode.t @@ -20,17 +20,24 @@ repeat_each(1); no_long_string(); no_root_location(); -our $yaml_config = <<_EOC_; -apisix: - node_listen: 1984 - enable_debug: true -_EOC_ +sub read_file($) { + my $infile = shift; + open my $in, $infile + or die "cannot open $infile for reading: $!"; + my $cert = do { local $/; <$in> }; + close $in; + $cert; +} + +our $debug_config = read_file("conf/debug.yaml"); +$debug_config =~ s/enable_debug: false/enable_debug: true/; run_tests; __DATA__ === TEST 1: loaded plugin +--- debug_config eval: $::debug_config --- config location /t { content_by_lua_block { @@ -38,7 +45,6 @@ __DATA__ ngx.say("done") } } ---- yaml_config eval: $::yaml_config --- request GET /t --- response_body @@ -127,9 +133,9 @@ passed === TEST 3: hit routes +--- debug_config eval: $::debug_config --- request GET /hello ---- yaml_config eval: $::yaml_config --- response_body hello world --- response_headers @@ -189,7 +195,7 @@ passed === TEST 5: hit routes ---- yaml_config eval: $::yaml_config +--- debug_config eval: $::debug_config --- config location /t { content_by_lua_block { @@ -253,7 +259,7 @@ passed === TEST 7: hit routes ---- yaml_config eval: $::yaml_config +--- debug_config eval: $::debug_config --- config location /t { content_by_lua_block { @@ -349,7 +355,7 @@ passed === TEST 10: hit route ---- yaml_config eval: $::yaml_config +--- debug_config eval: $::debug_config --- stream_enable --- stream_request eval "\x10\x0f\x00\x04\x4d\x51\x54\x54\x04\x02\x00\x3c\x00\x03\x66\x6f\x6f" From c63f2097e77e4a6609e8a7e22c3a6e30a644061b Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Tue, 14 Sep 2021 00:54:32 +0800 Subject: [PATCH 2/5] fix CI error --- apisix/plugin.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apisix/plugin.lua b/apisix/plugin.lua index 91bee2b577b9..dbcce7fed8ea 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -168,7 +168,7 @@ local function load(plugin_names) for i, plugin in ipairs(local_plugins) do local_plugins_hash[plugin.name] = plugin - if enable_debug then + if enable_debug() then core.log.warn("loaded plugin and sort by priority:", " ", plugin.priority, " name: ", plugin.name) @@ -209,7 +209,7 @@ local function load_stream(plugin_names) for i, plugin in ipairs(stream_local_plugins) do stream_local_plugins_hash[plugin.name] = plugin - if enable_debug then + if enable_debug() then core.log.warn("loaded stream plugin and sort by priority:", " ", plugin.priority, " name: ", plugin.name) @@ -279,7 +279,7 @@ end local function trace_plugins_info_for_debug(ctx, plugins) - if not enable_debug then + if not enable_debug() then return end From 041d70d22c5da04b5505a62185beb853d24fc967 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Tue, 14 Sep 2021 10:41:32 +0800 Subject: [PATCH 3/5] fix CI error --- apisix/init.lua | 3 ++- t/config-center-yaml/plugin.t | 44 +++++++++++++++++++++++++++++++++-- t/debug/debug-mode.t | 2 +- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/apisix/init.lua b/apisix/init.lua index b0c50d685ed9..1a3358a4b99b 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -113,6 +113,8 @@ function _M.http_init_worker() require("apisix.timers").init_worker() + require("apisix.debug").init_worker() + plugin.init_worker() router.http_init_worker() require("apisix.http.service").init_worker() @@ -123,7 +125,6 @@ function _M.http_init_worker() core.config.init_worker() end - require("apisix.debug").init_worker() apisix_upstream.init_worker() require("apisix.plugins.ext-plugin.init").init_worker() diff --git a/t/config-center-yaml/plugin.t b/t/config-center-yaml/plugin.t index 2cc3a32004ce..7e2fae3ed369 100644 --- a/t/config-center-yaml/plugin.t +++ b/t/config-center-yaml/plugin.t @@ -51,6 +51,18 @@ _EOC_ } }); +sub read_file($) { + my $infile = shift; + open my $in, $infile + or die "cannot open $infile for reading: $!"; + my $cert = do { local $/; <$in> }; + close $in; + $cert; +} + +our $debug_config = read_file("conf/debug.yaml"); +$debug_config =~ s/enable_debug: false/enable_debug: true/; + run_tests(); __DATA__ @@ -62,8 +74,22 @@ plugins: - name: jwt-auth - name: mqtt-proxy stream: true +--- debug_config eval: $::debug_config +--- config + location /t { + content_by_lua_block { + ngx.sleep(0.3) + local http = require "resty.http" + local httpc = http.new() + local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello" + local res, err = httpc:request_uri(uri, { + method = "GET", + }) + ngx.print(res.body) + } + } --- request -GET /hello +GET /t --- response_body hello world --- error_log @@ -98,8 +124,22 @@ plugins: - name: jwt-auth - name: mqtt-proxy stream: true +--- debug_config eval: $::debug_config +--- config + location /t { + content_by_lua_block { + ngx.sleep(0.3) + local http = require "resty.http" + local httpc = http.new() + local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello" + local res, err = httpc:request_uri(uri, { + method = "GET", + }) + ngx.print(res.body) + } + } --- request -GET /hello +GET /t --- response_body hello world --- grep_error_log eval diff --git a/t/debug/debug-mode.t b/t/debug/debug-mode.t index 5ff717849d67..f5269c5fe307 100644 --- a/t/debug/debug-mode.t +++ b/t/debug/debug-mode.t @@ -362,6 +362,6 @@ passed --- stream_response hello world --- error_log -Apisix-Plugins: mqtt-proxy +mqtt client id: foo while prereading client data --- no_error_log [error] From c827d963f3314ee63a2eeefeb7b037e21c9d93c6 Mon Sep 17 00:00:00 2001 From: spacewander Date: Tue, 14 Sep 2021 11:31:39 +0800 Subject: [PATCH 4/5] rename Signed-off-by: spacewander --- apisix/debug.lua | 2 +- conf/debug.yaml | 3 ++- docs/en/latest/architecture-design/debug-mode.md | 9 ++++++++- docs/zh/latest/architecture-design/debug-mode.md | 9 ++++++++- t/debug/debug-mode.t | 13 ++----------- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/apisix/debug.lua b/apisix/debug.lua index 45ef5713d932..29f367a83d09 100644 --- a/apisix/debug.lua +++ b/apisix/debug.lua @@ -204,7 +204,7 @@ function _M.enable_debug() return false end - return debug_yaml.enable_debug + return debug_yaml.basic.enable end diff --git a/conf/debug.yaml b/conf/debug.yaml index a4488e8617fa..3d53486e9e6b 100644 --- a/conf/debug.yaml +++ b/conf/debug.yaml @@ -14,7 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # -enable_debug: false +basic: + enable: false hook_conf: enable: false # enable or disable this feature name: hook_phase # the name of module and function list diff --git a/docs/en/latest/architecture-design/debug-mode.md b/docs/en/latest/architecture-design/debug-mode.md index f0c428e1ed41..1d0ad8c3223d 100644 --- a/docs/en/latest/architecture-design/debug-mode.md +++ b/docs/en/latest/architecture-design/debug-mode.md @@ -23,7 +23,14 @@ title: Debug Mode ### Basic Debug Mode -Enable basic debug mode just by setting `enable_debug = true` in `conf/debug.yaml` file. +Enable basic debug mode via `conf/debug.yaml` file: + +``` +basic: + enable: true +``` + +Note: before APISIX 2.10, we enabled basic debug mode by setting `apisix.enable_debug = true` in `conf/config.yaml` file. e.g Using both `limit-conn` and `limit-count` plugins for a `/hello` request, there will have a response header called `Apisix-Plugins: limit-conn, limit-count`. diff --git a/docs/zh/latest/architecture-design/debug-mode.md b/docs/zh/latest/architecture-design/debug-mode.md index 9c3399b54783..0e647d843641 100644 --- a/docs/zh/latest/architecture-design/debug-mode.md +++ b/docs/zh/latest/architecture-design/debug-mode.md @@ -23,7 +23,14 @@ title: Debug Mode ### 基本调试模式 -设置 `conf/debug.yaml` 中的 `enable_debug` 为 `true`,即可开启基本调试模式。 +设置 `conf/debug.yaml` 即可开启基本调试模式: + +``` +basic: + enable: true +``` + +注意:在 APISIX 2.10 之前,开启基本调试模式曾经是设置 `conf/config.yaml` 中的 `apisix.enable_debug` 为 `true`。 比如对 `/hello` 开启了 `limit-conn`和`limit-count`插件,这时候应答头中会有 `Apisix-Plugins: limit-conn, limit-count`。 diff --git a/t/debug/debug-mode.t b/t/debug/debug-mode.t index f5269c5fe307..2f38dcbf5151 100644 --- a/t/debug/debug-mode.t +++ b/t/debug/debug-mode.t @@ -20,17 +20,8 @@ repeat_each(1); no_long_string(); no_root_location(); -sub read_file($) { - my $infile = shift; - open my $in, $infile - or die "cannot open $infile for reading: $!"; - my $cert = do { local $/; <$in> }; - close $in; - $cert; -} - -our $debug_config = read_file("conf/debug.yaml"); -$debug_config =~ s/enable_debug: false/enable_debug: true/; +our $debug_config = t::APISIX::read_file("conf/debug.yaml"); +$debug_config =~ s/basic:\n enable: false/basic:\n enable: true/; run_tests; From d33254790487a1a18b7c8c782f87f4fbdcac87e7 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Tue, 14 Sep 2021 12:16:28 +0800 Subject: [PATCH 5/5] fix CI error --- apisix/debug.lua | 2 +- t/config-center-yaml/plugin.t | 2 +- t/debug/hook.t | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apisix/debug.lua b/apisix/debug.lua index 29f367a83d09..f07aff553b44 100644 --- a/apisix/debug.lua +++ b/apisix/debug.lua @@ -200,7 +200,7 @@ end function _M.enable_debug() - if not debug_yaml then + if not debug_yaml or not debug_yaml.basic then return false end diff --git a/t/config-center-yaml/plugin.t b/t/config-center-yaml/plugin.t index 7e2fae3ed369..fa411cc2d16c 100644 --- a/t/config-center-yaml/plugin.t +++ b/t/config-center-yaml/plugin.t @@ -61,7 +61,7 @@ sub read_file($) { } our $debug_config = read_file("conf/debug.yaml"); -$debug_config =~ s/enable_debug: false/enable_debug: true/; +$debug_config =~ s/basic:\n enable: false/basic:\n enable: true/; run_tests(); diff --git a/t/debug/hook.t b/t/debug/hook.t index 5b64d3c06843..90794f1720db 100644 --- a/t/debug/hook.t +++ b/t/debug/hook.t @@ -31,7 +31,7 @@ sub read_file($) { } our $debug_config = read_file("conf/debug.yaml"); -$debug_config =~ s/enable: false/enable: true/; +$debug_config =~ s/hook_conf:\n enable: false/hook_conf:\n enable: true/; run_tests();