Best practices for Kong debugging + example

I think some public documentation around best dev practices for Kong debugging around source code would be great. Any developers in the company have a stack of free applications or tools that help make working with Kong+Lua+Nginx+OpenResty a bit easier? I know of running Kong in debug move with the --vv command but say I want to echo lua variables to terminal while Kong is running can I do so? Currently we are wrapping variables in error() methods, running it once then going to look in the error logs and it feels painful. I personally come from a Java background working through IntelliJ and Eclipse, where you can step through every portion of the code in real time and evaluate objects. As a newbie to programming on Kong, are their any recommendations that will enable easy debugging within the Kong universe?

Just as a reference for instance, we are attempting to SHA hash the nginx request body() to enable non-repudiation as a nice feature and drop it in a JWT to the backend API providers. Some similar code thats from your HMAC auth plugin even, we hope to release this publicly soon and many more cool plugins to follow!:

req_read_body()
local body = req_get_body_data()
– request must have body as client sent a digest header
if not body then
return false
end

local sha256 = resty_sha256:new()
sha256:update(body)
local digest_created = “SHA-256=” … ngx_encode_base64(sha256:final())

I would think something like this in a custom plugin should work:

local sha256 = resty_sha256:new()
sha256:update(body)
local hashedBody = sha256:final()

Then I could drop hashedBody into my jwt but it does not. Maybe I am misreading the original source though.

Thanks,
Jeremy

1 Like

I don’t think the experience will be as polished as with IntelliJ/Eclipse in the Java land, but I have heard some people manage to use Zerobrane studio which has some nginx hooks.

I have no experience with it myself, so I bear that in mind.

My approach to debugging is more … primordial: I just add lots of log calls, similar to ngx.log(ngx.WARN, "Your message"), and then I read the logs. Since sometimes I need to log tables, I often use my personal library, inspect.lua to transform those values in human-readable strings: ngx.log(ngx.WARN, require("inspect")(my_var)) (You will have to luarocks install inspect first to make it available).

4 Likes

same here. Despite that I use ZeroBraneStudio for coding, I do not use the debugger, but use a tail on the regular logs.

for quick stuff I add debug lines like:
print(require("pl.pretty").write({..parameters here...})

For more elaborate work I temporarily insert a function like this:

--- Debug function for development purposes.
-- Will dump all passed in parameters in a pretty-printed way
-- as a `warning` log message. Includes color markers to make it stand out.
-- @param ... list of parameters to dump
local dump = function(...)
  local info = debug.getinfo(2) or {}
  local input = { n = select("#", ...), ...}
  local write = require("pl.pretty").write
  local serialized
  if input.n == 1 and type(input[1]) == "table" then
    serialized = "(" .. type(input[1]) .. "): " .. write(input[1])
  elseif input.n == 1 then
    serialized = "(" .. type(input[1]) .. "): " .. tostring(input[1]) .. "\n"
  else
    local n
    n, input.n = input.n, nil
    serialized = "(list, #" .. n .. "): " .. write(input)
  end

  ngx.log(ngx.WARN,
          "\027[31m\n",
          "function '", tostring(info.name), ":" , tostring(info.currentline),
          "' in '", tostring(info.short_src), "' wants you to know:\n",
          serialized,
          "\027[0m")
end
5 Likes

Thanks for both of your great feedback! Awesome to learn a few techniques from the pros :slight_smile: . Hopefully other curious minds that want to know some best practices for debugging with Kong code will stumble upon this topic.

The Kong-Vagrant box might also aid in making development a bit easier. See https://github.com/Kong/kong-vagrant

3 Likes

just adding this here: the Vagrant box has pretty much been retired, please use kong-pongo instead

1 Like