inspect

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.

This plugin supports setting breakpoints within both interpretd function and jit compiled function. The breakpoint could be at any position within the function. The function could be global/local/module/ananymous.

Features

  • Set breakpoint at any position
  • Dynamic breakpoint
  • customized breakpoint handler
  • You could define one-shot breakpoint
  • Work for jit compiled function
  • If function reference specified, then performance impact is only bound to that function (JIT compiled code will not trigger debug hook, so they would run fast even if hook is enabled)
  • If all breakpoints deleted, jit could recover

Operation Graph

Operation Graph

API to define hook in hooks file

require(“apisix.inspect.dbg”).set_hook(file, line, func, filter_func)

The breakpoint is specified by file (full qualified or short file name) and the line number.

The func specified the scope (which function or global) of jit cache to flush:

  • If the breakpoint is related to a module function or global function, you should set it that function reference, then only the jit cache of that function would be flushed, and it would not affect other caches to avoid slowing down other parts of the program.

  • If the breakpointis related to local function or anonymous function, then you have to set it to nil (because no way to get function reference), which would flush the whole jit cache of Lua vm.

You attach a filter_func function of the breakpoint, the function takes the info as argument and returns true of false to determine whether the breakpoint would be removed. You could setup one-shot breakpoint at ease.

The info is a hash table which contains below keys:

  • finfo: debug.getinfo(level, "nSlf")
  • uv: upvalues hash table
  • vals: local variables hash table

Attributes

NameTypeRequiredDefaultDescription
delayintegerFalse3Time in seconds specifying how often to check the hooks file.
hooks_filestringFalse“/usr/local/apisix/plugin_inspect_hooks.lua”Lua file to define hooks, which could be a link file. Ensure only administrator could write this file, otherwise it may be a security risk.

Enable Plugin

Plugin is enabled by default:

apisix/cli/config.lua

  1. local _M = {
  2. plugins = {
  3. "inspect",
  4. ...
  5. },
  6. plugin_attr = {
  7. inspect = {
  8. delay = 3,
  9. hooks_file = "/usr/local/apisix/plugin_inspect_hooks.lua"
  10. },
  11. ...
  12. },
  13. ...
  14. }

Example usage

inspect - 图2note

You can fetch the admin_key from config.yaml and save to an environment variable with the following command:

  1. admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
  1. # create test route
  2. curl http://127.0.0.1:9180/apisix/admin/routes/test_limit_req -H "X-API-KEY: $admin_key" -X PUT -d '
  3. {
  4. "methods": ["GET"],
  5. "uri": "/get",
  6. "plugins": {
  7. "limit-req": {
  8. "rate": 100,
  9. "burst": 0,
  10. "rejected_code": 503,
  11. "key_type": "var",
  12. "key": "remote_addr"
  13. }
  14. },
  15. "upstream": {
  16. "type": "roundrobin",
  17. "nodes": {
  18. "httpbin.org": 1
  19. }
  20. }
  21. }'
  22. # create a hooks file to set a test breakpoint
  23. # Note that the breakpoint is associated with the line number,
  24. # so if the Lua code changes, you need to adjust the line number in the hooks file
  25. cat <<EOF >/usr/local/apisix/example_hooks.lua
  26. local dbg = require "apisix.inspect.dbg"
  27. dbg.set_hook("limit-req.lua", 88, require("apisix.plugins.limit-req").access, function(info)
  28. ngx.log(ngx.INFO, debug.traceback("foo traceback", 3))
  29. ngx.log(ngx.INFO, dbg.getname(info.finfo))
  30. ngx.log(ngx.INFO, "conf_key=", info.vals.conf_key)
  31. return true
  32. end)
  33. --- more breakpoints could be defined via dbg.set_hook()
  34. --- ...
  35. EOF
  36. # enable the hooks file
  37. ln -sf /usr/local/apisix/example_hooks.lua /usr/local/apisix/plugin_inspect_hooks.lua
  38. # check errors.log to confirm the test breakpoint is enabled
  39. 2022/09/01 00:55:38 [info] 2754534#2754534: *3700 [lua] init.lua:29: setup_hooks(): set hooks: err=nil, hooks=["limit-req.lua#88"], context: ngx.timer
  40. # access the test route
  41. curl -i http://127.0.0.1:9080/get
  42. # check errors.log to confirm the test breakpoint is triggered
  43. 2022/09/01 00:55:52 [info] 2754534#2754534: *4070 [lua] resty_inspect_hooks.lua:4: foo traceback
  44. stack traceback:
  45. /opt/lua-resty-inspect/lib/resty/inspect/dbg.lua:50: in function </opt/lua-resty-inspect/lib/resty/inspect/dbg.lua:17>
  46. /opt/apisix.fork/apisix/plugins/limit-req.lua:88: in function 'phase_func'
  47. /opt/apisix.fork/apisix/plugin.lua:900: in function 'run_plugin'
  48. /opt/apisix.fork/apisix/init.lua:456: in function 'http_access_phase'
  49. access_by_lua(nginx.conf:303):2: in main chunk, client: 127.0.0.1, server: _, request: "GET /get HTTP/1.1", host: "127.0.0.1:9080"
  50. 2022/09/01 00:55:52 [info] 2754534#2754534: *4070 [lua] resty_inspect_hooks.lua:5: /opt/apisix.fork/apisix/plugins/limit-req.lua:88 (phase_func), client: 127.0.0.1, server: _, request: "GET /get HTTP/1.1", host: "127.0.0.1:9080"
  51. 2022/09/01 00:55:52 [info] 2754534#2754534: *4070 [lua] resty_inspect_hooks.lua:6: conf_key=remote_addr, client: 127.0.0.1, server: _, request: "GET /get HTTP/1.1", host: "127.0.0.1:9080"

Delete Plugin

To remove the inspect Plugin, you can remove it from your configuration file (conf/config.yaml):

conf/config.yaml

  1. plugins:
  2. # - inspect