APISIX runtime debugging

0 x00 instructions

This article is based on APISIX 2.5, IDEA 2021.1, Intellij-Emmylua plug-in 1.3.6.215, MobDebug 0.709.

0 x01 environment

I was developed in CentOs 7 system, first build APISIX development environment, according to the official documents of APISIX, install dependencies first, and then install APISIX through the source package.

The APISIX installation directory is /usr/local/apisix. After the development environment is set up, go to the directory and run the./bin/apisix start command to start APISIX.

The process of installing IDEA and Emmyua plug-ins in CentOs 7 is not described.

0 x02 project

/usr/local/apisix = /usr/local/apisix = /usr/local/apisix = /usr/local/apisix

You must set the source root to the Sources directory. To do this, go to File -> Project Structure. Open the Project Structure Settings panel, click Add Content Root on the right to Add your source Root directory, and then click the Mark as Sources tag.Copy the code

The project structure is as follows

Create a lualib directory under /usr/local/apisix, download the mobdebug.lua code and place it in the lualib directory

Then refer to the configuration project dependencies library, set Lualib as the lib of the project, the result is as follows

You can also add emmylua_ngx.lua to Lualib to complement the OpenResty function hint library and get more hints when writing code.

0 x03 debugging

New Lua Remote (Mobdebug), the specific process reference: emmylua. Making. IO/run/Remote….

The effect is as follows

Add the following functions where debug is needed

Require (" lualib mobdebug "). The start (" 127.0.0.1 ", 8172)Copy the code

Start the remote debug process in IDEA. The result is as follows

A debug message is displayed on the IDEA Console, indicating that a process is started to listen on port 8172. This process belongs to IDEA and has nothing to do with APISIX

Debug starts a local process and listens on port 8172. When APISIX runs the debug function, suspend the current coroutine, and then call the lualib/mobdebug.lua module to send the current function related variables, stack information via LuaSocket to 127.0.0.1:8172. That is, the local port 8172. The process that listens to port 8172 receives the information and displays it in IDEA.

Start APISIX with the following command

./bin/apisix start
Copy the code

It should be noted that the route upstream and other information in APISIX are configured in advance. After starting APISIX, a request is constructed to access APISIX and trigger the code to run to the place where debugging is required.

For example, I configured the route as follows

{
    "id": "1"."update_time": 1611044949."uri": "/foo/*"."status": 1."priority": 0."plugins": {},
    "upstream": {
        "nodes": {
            "127.0.0.1:1980": 2."127.0.0.1:1981": 1
        },
        "type": "roundrobin"
    },
    "create_time": 1611044129
}
Copy the code

Upstream configured 2 nodes while I was accessing

The curl 127.0.0.1:9080 / foo / 1Copy the code

, APISIX code will run to/usr/local/APISIX APISIX/balancer. The lua module line 230, namely the place where I add the debug function.

The debug effect is as follows

You can also evaluate expressions

You can step into or out of a function. Let’s say 230 lines of function

local ok, err = balancer.set_current_peer(server.host, server.port)
Copy the code

The set_CURRENT_PEER function is called, and when debugged to this line, press F7 to enter the function

0 x04 end

APISIX dynamic debugging function introduced in this paper is mainly used to learn source code, understand its operation process, improve learning efficiency. I hope it can help friends like me who are used to dynamic debugging and just started to contact APISIX/OpenResty/Kong and other related technology stacks.