Skip to content

feat: add inspect plugin#8400

Merged
tzssangglass merged 26 commits into
apache:masterfrom
kingluo:inspect_plugin
Dec 16, 2022
Merged

feat: add inspect plugin#8400
tzssangglass merged 26 commits into
apache:masterfrom
kingluo:inspect_plugin

Conversation

@kingluo

@kingluo kingluo commented Nov 25, 2022

Copy link
Copy Markdown
Contributor

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)

@moonming moonming left a comment

Copy link
Copy Markdown
Member

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

Comment thread t/plugin/inspect.t Outdated
@kingluo
kingluo marked this pull request as draft November 25, 2022 09:00
@kingluo

kingluo commented Nov 25, 2022

Copy link
Copy Markdown
Contributor Author

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
Comment thread Makefile Outdated
Comment thread apisix/inspect/dbg.lua
Comment thread apisix/inspect/init.lua Outdated
Comment thread apisix/inspect/init.lua Outdated
if not f then
return false, err
end
local code = f:read("*all")

Copy link
Copy Markdown
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
Copy Markdown
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?

Comment thread apisix/inspect/init.lua Outdated
Comment thread apisix/plugins/inspect.lua
Comment thread apisix/plugins/inspect.lua Outdated
delay = attr.delay
hooks_file = attr.hooks_file
end
ngx.log(ngx.INFO, "delay=", delay, ", hooks_file=", hooks_file)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

core.log.info

Comment thread Makefile Outdated
$(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
Copy Markdown
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/

Comment thread apisix/inspect/init.lua Outdated
if not f then
return false, err
end
local code = f:read("*all")

Copy link
Copy Markdown
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

kingluo commented Dec 12, 2022

Copy link
Copy Markdown
Contributor Author

Comment thread apisix/inspect/dbg.lua Outdated
end
end

local function hook(evt, arg)

Copy link
Copy Markdown
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?

Comment thread apisix/inspect/init.lua Outdated
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
Copy Markdown
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?

Comment thread apisix/inspect/init.lua
if not func then
return false, err
end
func()

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread apisix/inspect/init.lua Outdated
local code = f:read("*all")
f:close()
if code == nil then
return false, "cannot read hooks file"

Copy link
Copy Markdown
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
Copy Markdown
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
Comment thread docs/en/latest/plugins/inspect.md Outdated
- APISIX
- Plugin
- inspect
- inspect

Copy link
Copy Markdown
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

Comment thread docs/en/latest/plugins/inspect.md Outdated

## Description

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lua -> Lua

Comment thread docs/en/latest/plugins/inspect.md Outdated

## Features

* set breakpoint at any position

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Capitalized?

Comment thread docs/en/latest/plugins/inspect.md Outdated
## Features

* set breakpoint at any position
* dynamic breakpoint

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto

Comment thread apisix/inspect/init.lua
-- limitations under the License.
--
local core = require("apisix.core")
local dbg = require("apisix.inspect.dbg")

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread conf/config-default.yaml Outdated
# redirect:
# https_port: 8443 # the default port for use by HTTP redirects to HTTPS
inspect:
delay: 3

Copy link
Copy Markdown
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
Copy Markdown
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
Comment thread conf/config-default.yaml Outdated
# 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
Copy Markdown
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?

@kingluo kingluo Dec 15, 2022

Copy link
Copy Markdown
Contributor Author

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.

@kingluo kingluo Dec 15, 2022

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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.

Comment thread docs/en/latest/plugins/inspect.md Outdated
# 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
Copy Markdown
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
Copy Markdown
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.

Comment thread conf/config-default.yaml Outdated
# 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
Copy Markdown
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

kingluo commented Dec 15, 2022

Copy link
Copy Markdown
Contributor Author

@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