-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The change provides a hook in the init_worker phase to start the EmmyLua debugger. The debugger is enabled by setting the `KONG_EMMY_DEBUGGER` to the absolute pathname of the EmmyLuaDebugger shared library (e.g. /usr/local/bin/emmy_core.dylib). The host and port that the debugger interface listens on can be defined by setting the `KONG_EMMY_DEBUGGER_HOST` and `KONG_EMMY_DEBUGGER_PORT` environment variables, respectively. If the environment variable `KONG_EMMY_DEBUGGER_WAIT` is set, the debugger hook waits for the IDE to connect at the beginning of the worker startup process to allow debugging of startup issues. Kong should be started with KONG_NGINX_WORKER_PROCESSES set to 1 to enable only one worker process. Debugging multiple worker processes is not supported. This feature is based on work by @mehuled, published in https://dev.to/mehuled/breakpoint-debugging-with-kong-plugin-development-75a
- Loading branch information
1 parent
7a1d8d2
commit 23f751f
Showing
5 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
message: | | ||
Added support for debugging with EmmyLuaDebugger | ||
type: feature |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
local pl_path = require "pl.path" | ||
local utils = require "kong.tools.utils" | ||
|
||
local debugger = os.getenv("KONG_EMMY_DEBUGGER") | ||
local emmy_debugger_host = os.getenv("KONG_EMMY_DEBUGGER_HOST") or "localhost" | ||
local emmy_debugger_port = os.getenv("KONG_EMMY_DEBUGGER_PORT") or 9966 | ||
local emmy_debugger_wait = os.getenv("KONG_EMMY_DEBUGGER_WAIT") | ||
local emmy_debugger_source_path = utils.split(os.getenv("KONG_EMMY_DEBUGGER_SOURCE_PATH") or "", ":") | ||
|
||
local function find_source(path) | ||
if pl_path.exists(path) then | ||
return path | ||
end | ||
|
||
if path:match("^=") then | ||
-- code is executing from .conf file, don't attempt to map | ||
return path | ||
end | ||
|
||
for _, source_path in ipairs(emmy_debugger_source_path) do | ||
local full_path = pl_path.join(source_path, path) | ||
if pl_path.exists(full_path) then | ||
return full_path | ||
end | ||
end | ||
|
||
ngx.log(ngx.ERR, "source file " .. path .. " not found in KONG_EMMY_DEBUGGER_SOURCE_PATH") | ||
|
||
return path | ||
end | ||
|
||
local function init() | ||
if not debugger then | ||
return | ||
end | ||
|
||
if not pl_path.isabs(debugger) then | ||
ngx.log(ngx.ERR, "KONG_EMMY_DEBUGGER (" .. debugger .. ") must be an absolute path") | ||
return | ||
end | ||
if not pl_path.exists(debugger) then | ||
ngx.log(ngx.ERR, "KONG_EMMY_DEBUGGER (" .. debugger .. ") file not found") | ||
return | ||
end | ||
local ext = pl_path.extension(debugger) | ||
if ext ~= ".so" and ext ~= ".dylib" then | ||
ngx.log(ngx.ERR, "KONG_EMMY_DEBUGGER (" .. debugger .. ") must be a .so (Linux) or .dylib (macOS) file") | ||
return | ||
end | ||
if ngx.worker.id() ~= 0 then | ||
ngx.log(ngx.ERR, "KONG_EMMY_DEBUGGER is only supported in the first worker process, suggest setting KONG_NGINX_WORKER_PROCESSES to 1") | ||
return | ||
end | ||
|
||
ngx.log(ngx.NOTICE, "loading EmmyLua debugger " .. debugger) | ||
|
||
_G.emmy = { | ||
fixPath = find_source | ||
} | ||
|
||
local name = pl_path.basename(debugger):sub(1, -#ext - 1) | ||
|
||
local save_cpath = package.cpath | ||
package.cpath = pl_path.dirname(debugger) .. '/?' .. ext | ||
local dbg = require(name) | ||
package.cpath = save_cpath | ||
|
||
dbg.tcpListen(emmy_debugger_host, emmy_debugger_port) | ||
|
||
ngx.log(ngx.NOTICE, "EmmyLua debugger loaded, listening on port ", emmy_debugger_port) | ||
|
||
if emmy_debugger_wait then | ||
-- Wait for IDE connection | ||
ngx.log(ngx.NOTICE, "waiting for IDE to connect") | ||
dbg.waitIDE() | ||
ngx.log(ngx.NOTICE, "IDE connected") | ||
end | ||
end | ||
|
||
return { | ||
init = init | ||
} |