You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/09/14 05:05:30 UTC

[apisix] branch master updated: change(debug): move 'enable_debug' form config.yaml to debug.yaml (#5046)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new b5aca8c  change(debug): move 'enable_debug' form config.yaml to debug.yaml (#5046)
b5aca8c is described below

commit b5aca8cd363d262d4c8b66f2cc75269701d00220
Author: tzssangglass <tz...@gmail.com>
AuthorDate: Tue Sep 14 13:05:25 2021 +0800

    change(debug): move 'enable_debug' form config.yaml to debug.yaml (#5046)
    
    Co-authored-by: spacewander <sp...@gmail.com>
---
 apisix/debug.lua                                 | 11 +++++-
 apisix/init.lua                                  |  3 +-
 apisix/plugin.lua                                |  9 +++--
 conf/config-default.yaml                         |  1 -
 conf/debug.yaml                                  |  2 ++
 docs/en/latest/architecture-design/debug-mode.md |  9 ++++-
 docs/zh/latest/architecture-design/debug-mode.md |  9 ++++-
 t/config-center-yaml/consumer.t                  |  1 -
 t/config-center-yaml/plugin.t                    | 46 +++++++++++++++++++++---
 t/debug/debug-mode.t                             | 19 +++++-----
 t/debug/hook.t                                   |  2 +-
 11 files changed, 85 insertions(+), 27 deletions(-)

diff --git a/apisix/debug.lua b/apisix/debug.lua
index 8cd0135..f07aff5 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 or not debug_yaml.basic then
+        return false
+    end
+
+    return debug_yaml.basic.enable
+end
+
+
 function _M.init_worker()
+    local process = require("ngx.process")
     if process.type() ~= "worker" then
         return
     end
diff --git a/apisix/init.lua b/apisix/init.lua
index b0c50d6..1a3358a 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/apisix/plugin.lua b/apisix/plugin.lua
index 307e102..dbcce7f 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 5e3a0db..402296e 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 b3527e1..3d53486 100644
--- a/conf/debug.yaml
+++ b/conf/debug.yaml
@@ -14,6 +14,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
+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 bcf17c0..1d0ad8c 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 `apisix.enable_debug = true` in `conf/config.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 ce733cf..0e647d8 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/config.yaml` 中的 `apisix.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/config-center-yaml/consumer.t b/t/config-center-yaml/consumer.t
index 1f1493e..0e7ae50 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 f3246b9..fa411cc 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);
@@ -52,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/basic:\n  enable: false/basic:\n  enable: true/;
+
 run_tests();
 
 __DATA__
@@ -63,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
@@ -88,7 +113,6 @@ apisix:
     node_listen: 1984
     config_center: yaml
     enable_admin: false
-    enable_debug: true
 plugins:
     - ip-restriction
     - jwt-auth
@@ -100,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 9c9e89f..2f38dcb 100644
--- a/t/debug/debug-mode.t
+++ b/t/debug/debug-mode.t
@@ -20,17 +20,15 @@ repeat_each(1);
 no_long_string();
 no_root_location();
 
-our $yaml_config = <<_EOC_;
-apisix:
-    node_listen: 1984
-    enable_debug: true
-_EOC_
+our $debug_config = t::APISIX::read_file("conf/debug.yaml");
+$debug_config =~ s/basic:\n  enable: false/basic:\n  enable: true/;
 
 run_tests;
 
 __DATA__
 
 === TEST 1: loaded plugin
+--- debug_config eval: $::debug_config
 --- config
     location /t {
         content_by_lua_block {
@@ -38,7 +36,6 @@ __DATA__
             ngx.say("done")
         }
     }
---- yaml_config eval: $::yaml_config
 --- request
 GET /t
 --- response_body
@@ -127,9 +124,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 +186,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 +250,7 @@ passed
 
 
 === TEST 7: hit routes
---- yaml_config eval: $::yaml_config
+--- debug_config eval: $::debug_config
 --- config
     location /t {
         content_by_lua_block {
@@ -349,13 +346,13 @@ 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"
 --- stream_response
 hello world
 --- error_log
-Apisix-Plugins: mqtt-proxy
+mqtt client id: foo while prereading client data
 --- no_error_log
 [error]
diff --git a/t/debug/hook.t b/t/debug/hook.t
index 5b64d3c..90794f1 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();