-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(debug-mode): add dynamic debug mode #5012
Changes from 10 commits
b395a9e
dbf2bb6
6b5ddaf
dc5c2ba
48849d4
4480d39
c28bb08
c85ce13
60d255d
67d39f9
dc9c202
0261e83
480dc74
01c35d9
6b17805
1f58c6f
bca20cc
bdd63ba
fd123a3
7ae81f4
e4d16b2
e919b6d
58ec3f9
e47e0a4
3f40625
be61095
46f19be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,8 @@ | |
local require = require | ||
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 request = require("apisix.core.request") | ||
local lfs = require("lfs") | ||
local io = io | ||
local ngx = ngx | ||
|
@@ -30,6 +29,7 @@ local setmetatable = setmetatable | |
local pcall = pcall | ||
local ipairs = ipairs | ||
local unpack = unpack | ||
local inspect = require "inspect" | ||
local debug_yaml_path = profile:yaml_path("debug") | ||
local debug_yaml | ||
local debug_yaml_ctime | ||
|
@@ -126,13 +126,13 @@ local function apple_new_fun(module, fun_name, file_path, hook_conf) | |
local arg = {...} | ||
if hook_conf.is_print_input_args then | ||
log[log_level]("call require(\"", file_path, "\").", fun_name, | ||
"() args:", json.delay_encode(arg, true)) | ||
"() args:", inspect(arg)) | ||
end | ||
|
||
local ret = {self.fun_org(...)} | ||
if hook_conf.is_print_return_value then | ||
log[log_level]("call require(\"", file_path, "\").", fun_name, | ||
"() return:", json.delay_encode(ret, true)) | ||
"() return:", inspect(ret)) | ||
end | ||
return unpack(ret) | ||
end | ||
|
@@ -148,7 +148,9 @@ end | |
|
||
function sync_debug_hooks() | ||
if not debug_yaml_ctime or debug_yaml_ctime == pre_mtime then | ||
return | ||
if not (debug_yaml and debug_yaml.http and debug_yaml.http.dynamic) then | ||
return | ||
end | ||
end | ||
|
||
for _, hook in pairs(enabled_hooks) do | ||
|
@@ -200,7 +202,43 @@ local function sync_debug_status(premature) | |
end | ||
|
||
|
||
local function check() | ||
if not debug_yaml or not debug_yaml.http then | ||
return false | ||
end | ||
|
||
local http = debug_yaml.http | ||
if not http or not http.enable_header_name or not http.dynamic then | ||
return false | ||
end | ||
|
||
return true | ||
end | ||
|
||
function _M.dynamic_enable(api_ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is called in the request level, but it changes a field at the module level. Look like it will affect other requests? |
||
if not check() then | ||
return | ||
end | ||
|
||
if request.header(api_ctx, debug_yaml.http.enable_header_name) then | ||
debug_yaml.hook_conf.enable = true | ||
sync_debug_hooks() | ||
end | ||
end | ||
|
||
|
||
function _M.dynamic_disable() | ||
if not check() then | ||
return | ||
end | ||
|
||
debug_yaml.hook_conf.enable = false | ||
sync_debug_hooks() | ||
end | ||
|
||
|
||
function _M.init_worker() | ||
local process = require("ngx.process") | ||
if process.type() ~= "worker" then | ||
return | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,3 +76,25 @@ hook_phase: # Module Function List, Name: hook_phase | |
- http_log_phase | ||
#END | ||
``` | ||
|
||
### Dynamic Advanced Debug Mode | ||
tzssangglass marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Dynamic advanced debug mode is based on advanced debug mode, which can be enabled dynamically by a single request and turned off automatically when the request ends. | ||
tzssangglass marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Example: | ||
|
||
```yaml | ||
http: | ||
dynamic: true # 是否动态开启高级调试模式 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need English comment in English doc |
||
enable_header_name: X-APISIX-Dynamic-Debug # 对携带此 header 的请求开启高级调试模式 | ||
...... | ||
#END | ||
``` | ||
|
||
Dynamically enable advanced debugging mode, example: | ||
tzssangglass marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```shell | ||
curl 127.0.0.1:9090/hello --header 'X-APISIX-Dynamic-Debug: foo' | ||
``` | ||
|
||
Notice: Dynamic advanced debug mode cannot debug the `apisix.http_access_phase` module (because the request enters the `apisix.http_access_phase` module before it determines whether advanced debug mode is dynamically enabled). | ||
tzssangglass marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to put all require together, and in the form
require("ngx.process")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
require("apisix.debug")
ininit.lua
-->require("ngx.process")
indebug.lua
, will result in error asprocess.lua:5: unsupported subsystem: stream
, see: https://github.com/apache/apisix/runs/3578221555ngx.process API for stream subsystem is not implemented on openresty 1.19.3.2(but is implemented on 1.19.9.1), so I move
require("ngx.process")
to function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I mean to use
require("inspect")
, therequire("ngx.process")
is just an example.