Skip to content
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: add inspect plugin #8400

Merged
merged 26 commits into from
Dec 16, 2022
Merged

feat: add inspect plugin #8400

merged 26 commits into from
Dec 16, 2022

Conversation

kingluo
Copy link
Contributor

@kingluo kingluo commented Nov 25, 2022

Description

It's useful to set arbitrary breakpoint in any lua file to inspect the context information, e.g. print local variables if some condition satisfied.

In this way, you don't need to modify the source code of your project, and just get diagnose information on demand, i.e. dynamic logging.

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

Copy link
Member

@moonming moonming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we need this plugin? And I think it's diagnostic not inspect

t/plugin/inspect.t Outdated Show resolved Hide resolved
@kingluo kingluo marked this pull request as draft November 25, 2022 09:00
@kingluo
Copy link
Contributor Author

kingluo commented Nov 25, 2022

why we need this plugin? And I think it's diagnostic not inspect

In my opinion, Inspect means we could set breakpoints to get context information from any line of the code, while diagnostic means the action you handle the issue.
After inspect gathers all information you need, you could diagnose where could be the source of the issue.
The plugin could not determine the reason but it could help you to locate the reason.
So I think inspect is a more accurate name for this plugin.

And, the naming comes from the design document, and the document was publicized for more than 3 weeks.

@kingluo kingluo marked this pull request as ready for review December 7, 2022 11:00
Makefile Outdated Show resolved Hide resolved
apisix/inspect/dbg.lua Show resolved Hide resolved
apisix/inspect/init.lua Outdated Show resolved Hide resolved
if not f then
return false, err
end
local code = f:read("*all")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do some err check?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about your opinion on this?

apisix/inspect/init.lua Outdated Show resolved Hide resolved
apisix/plugins/inspect.lua Show resolved Hide resolved
delay = attr.delay
hooks_file = attr.hooks_file
end
ngx.log(ngx.INFO, "delay=", delay, ", hooks_file=", hooks_file)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core.log.info

Makefile Outdated
@@ -315,6 +315,9 @@ install: runtime
$(ENV_INSTALL) -d $(ENV_INST_LUADIR)/apisix/plugins/ip-restriction
$(ENV_INSTALL) apisix/plugins/ip-restriction/*.lua $(ENV_INST_LUADIR)/apisix/plugins/ip-restriction/

$(ENV_INSTALL) -d $(ENV_INST_LUADIR)/apisix/inspect
$(ENV_INSTALL) apisix/inspect/*.lua $(ENV_INST_LUADIR)/apisix/inspect/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be moved under $(ENV_INSTALL) apisix/include/

if not f then
return false, err
end
local code = f:read("*all")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about your opinion on this?

@kingluo
Copy link
Contributor Author

kingluo commented Dec 12, 2022

@spacewander Add nil case handling already. GitHub failed to add an outdated flag here.

https://github.com/kingluo/apisix/blob/f4fe75f0f3855883b46a61d078018a4396639f13/apisix/inspect/init.lua#L46-L50

end
end

local function hook(evt, arg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

evt unused in hook function?

for _, hook in ipairs(dbg.hooks()) do
table_insert(hooks, hook.key)
end
core.log.info("set hooks: err=", err, ", hooks=", core.json.encode(hooks))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
core.log.info("set hooks: err=", err, ", hooks=", core.json.encode(hooks))
core.log.info("set hooks: err: ", err, ", hooks: ", core.json.delay_encode(hooks))

is better?

if not func then
return false, err
end
func()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need use pcall(func()) here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already did so in the outer function.

local code = f:read("*all")
f:close()
if code == nil then
return false, "cannot read hooks file"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can improve the error message here by adding the err returned from the f:read above.

https://github.com/LuaJIT/LuaJIT/blob/8625eee71f16a3a780ec92bc303c17456efc7fb3/src/lib_aux.c#L39
The second returned value is a string describing the err.

@spacewander
Copy link
Member

@spacewander Add nil case handling already. GitHub failed to add an outdated flag here.

kingluo/apisix@f4fe75f/apisix/inspect/init.lua#L46-L50

I see. Thanks for your clarification.

spacewander
spacewander previously approved these changes Dec 14, 2022
soulbird
soulbird previously approved these changes Dec 15, 2022
- APISIX
- Plugin
- inspect
- inspect
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should more this line


## Description

It's useful to set arbitrary breakpoint in any lua file to inspect the context information,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lua -> Lua


## Features

* set breakpoint at any position
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalized?

## Features

* set breakpoint at any position
* dynamic breakpoint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

-- limitations under the License.
--
local core = require("apisix.core")
local dbg = require("apisix.inspect.dbg")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What‘s dbg means?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abbreviation for "debug". Also to avoid conflict with the built-in Lua debug module.

@@ -555,6 +556,9 @@ plugin_attr:
send: 60s
# redirect:
# https_port: 8443 # the default port for use by HTTP redirects to HTTPS
inspect:
delay: 3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3s or 3 mins?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 seconds, I would add a comment for this line.

@kingluo kingluo dismissed stale reviews from soulbird and spacewander via 1916a2e December 15, 2022 07:40
@kingluo kingluo requested review from moonming and removed request for leslie-tsang December 15, 2022 07:59
@@ -555,6 +556,9 @@ plugin_attr:
send: 60s
# redirect:
# https_port: 8443 # the default port for use by HTTP redirects to HTTPS
inspect:
delay: 3 # in seconds
hooks_file: "/var/run/apisix_inspect_hooks.lua"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this file for? Has Lua code in it?

Copy link
Contributor Author

@kingluo kingluo Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moonming yes, this hooks file is used to define breakpoints, which itself is lua file. When the administrator needs to set breakpoints to inspect something, he would edit this file, or link another file to this path.

Copy link
Contributor Author

@kingluo kingluo Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally this file is non-exist. It's only available when you need to set breakpoints. Please refer to the plugin doc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are security risks.
This Lua code file must be in the APISIX directory, and users who do not have write permissions to the APISIX directory cannot write or modify this file.

# create a hooks file to set a test breakpoint
# Note that the breakpoint is associated with the line number,
# so if the Lua code changes, you need to adjust the line number in the hooks file
cat <<EOF >/tmp/hooks.lua
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use the /tmp directory, especially for documentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I would change it.

@@ -555,6 +556,9 @@ plugin_attr:
send: 60s
# redirect:
# https_port: 8443 # the default port for use by HTTP redirects to HTTPS
inspect:
delay: 3 # in seconds
hooks_file: "/var/run/apisix_inspect_hooks.lua"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are security risks.
This Lua code file must be in the APISIX directory, and users who do not have write permissions to the APISIX directory cannot write or modify this file.

@kingluo
Copy link
Contributor Author

kingluo commented Dec 15, 2022

@moonming /var/run/apisix_inspect_hooks.lua is only writeable for the root user. So I think no risk here. But it's up to you. Do you want to change it to /usr/local/openresty/apisix_inspect_hooks.lua?

@tzssangglass tzssangglass merged commit 8486800 into apache:master Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants