Project: APISIX (Orchestration & Management / API Gateway)
File: inspect.md

title: inspect keywords: Apache APISIX API Gateway Plugin Inspect Dynamic Lua Debugging description: This document contains information about the Apache APISIX inspect Plugin. <!-- --> 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. 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 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`:

"nSlf")` `uv`: upvalues hash table `vals`: local variables hash table | Name | Type | Required | Default | Description | |--||-||| | delay | integer | False | 3 | Time in seconds specifying how often to check the hooks file. | | hooksfile | string | False | "/usr/local/apisix/plugininspect_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. | Plugin is enabled by default (`conf/config-default.yaml`): ```yaml title="conf/config-default.yaml" plugins: inspect plugin_attr: inspect: delay: 3 hooksfile: "/usr/local/apisix/plugininspect_hooks.lua" ``` :::note You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command: ```bash adminkey=$(yq '.deployment.admin.adminkey[0].key' conf/config.yaml | sed 's/"//g') ``` ::: ```bash curl http://127.0.0.1:9180/apisix/admin/routes/testlimitreq -H "X-API-KEY: $admin_key" -X PUT -d ' { "methods": ["GET"], "uri": "/get", "plugins": { "limit-req": { "rate": 100, "burst": 0, "rejected_code": 503, "key_type": "var", "key": "remote_addr" } }, "upstream": { "type": "roundrobin", "nodes": { "httpbin.org": 1 } } }' cat <<EOF >/usr/local/apisix/example_hooks.lua local dbg = require "apisix.inspect.dbg" dbg.set_hook("limit-req.lua", 88, require("apisix.plugins.limit-req").access, function(info) ngx.log(ngx.INFO, debug.traceback("foo traceback", 3)) ngx.log(ngx.INFO, dbg.getname(info.finfo)) ngx.log(ngx.INFO, "confkey=", info.vals.confkey) return true end) more breakpoints could be defined via dbg.set_hook() ... EOF ln -sf /usr/local/apisix/examplehooks.lua /usr/local/apisix/plugininspect_hooks.lua 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 curl -i http://127.0.0.1:9080/get 2022/09/01 00:55:52 [info] 2754534#2754534: *4070 [lua] restyinspecthooks.lua:4: foo traceback stack traceback: /opt/lua-resty-inspect/lib/resty/inspect/dbg.lua:50: in function </opt/lua-resty-inspect/lib/resty/inspect/dbg.lua:17> /opt/apisix.fork/apisix/plugins/limit-req.lua:88: in function 'phase_func' /opt/apisix.fork/apisix/plugin.lua:900: in function 'run_plugin' /opt/apisix.fork/apisix/init.lua:456: in function 'httpaccessphase' accessbylua(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" 2022/09/01 00:55:52 [info] 2754534#2754534: *4070 [lua] restyinspecthooks.lua:5: /opt/apisix.fork/apisix/plugins/limit-req.lua:88 (phasefunc), client: 127.0.0.1, server: , request: "GET /get HTTP/1.1", host: "127.0.0.1:9080" 2022/09/01 00:55:52 [info] 2754534#2754534: *4070 [lua] restyinspecthooks.lua:6: confkey=remoteaddr, client: 127.0.0.1, server: _, request: "GET /get HTTP/1.1", host: "127.0.0.1:9080" ``` To remove the `inspect` Plugin, you can remove it from your configuration file (`conf/config.yaml`): ```yaml title="conf/config.yaml" plugins: ```