Skip to content

Commit

Permalink
feat(cli): add a new sub-command 'status' to the debug CLI (#9685)
Browse files Browse the repository at this point in the history
This adds a new debug sub-command 'status', like 'kong debug status'.
Its output is similar to Kong Gateway’s Admin API `/status`. 

https://konghq.atlassian.net/browse/KAG-2589

---------

Co-authored-by: Xiaochen Wang <[email protected]>
Co-authored-by: Qi <[email protected]>
  • Loading branch information
3 people authored Jul 24, 2024
1 parent 0a4d8e0 commit a5ed390
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelog/unreleased/kong/new_debug_status_sumcommand.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
message: "Added a new sub-command `status` to the `kong debug` CLI tool."
type: feature
31 changes: 31 additions & 0 deletions kong/cmd/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,37 @@ local function log_level_handler(socket_path, args, options)
end


local function status_handler(socket_path, args, options)
local method, path

method = "GET"
path = "/status"

-- request to the gateway
local res = request_unix_domain_socket({
socket_path = socket_path,
path = path,
method = method,
body = cjson.encode(options),
verbose = args.v,
})

-- handle the result

if res.status == 400 then
log.error(res.body.message)
return EC_FAILURE
end

log(cjson.encode(res.body))
return EC_SUCCESS
end


local command_handlers = {
profiling = profiling_handler,
log_level = log_level_handler,
status = status_handler,
}


Expand Down Expand Up @@ -357,6 +385,8 @@ The available commands are:
log_level get Get the logging level.
status Get the status of the Kong node.
Options:
--pid (optional number) The worker’s PID for profiling.
Expand Down Expand Up @@ -394,5 +424,6 @@ return {
sub_commands = {
log_level = true,
profiling = true,
status = true,
},
}
2 changes: 2 additions & 0 deletions kong/debug/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ ngx.log(ngx.DEBUG, "Loading Debug API endpoints")

-- Load debug routes
api_helpers.attach_routes(app, require "kong.api.routes.debug")
-- Load status routes
api_helpers.attach_routes(app, require "kong.api.routes.health")


return app
17 changes: 17 additions & 0 deletions spec-ee/02-integration/11-cmd/03-debug_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ for __, deploy in ipairs({ "traditional", "hybrid" }) do
assert.matches("Current log level: ", stdout)
end)

it("kong debug status", function ()
code, stdout, stderr = kong_debug_exec("status")
assert.same(EC_SUCCESS, code)
assert.matches("server", string.sub(stdout, string.find(stdout, "server")))
assert.matches("memory", string.sub(stdout, string.find(stdout, "memory")))

if deploy == "traditional" then
assert.matches("database", string.sub(stdout, string.find(stdout, "database")))

elseif deploy == "hybrid" then
assert.matches("configuration_hash", string.sub(stdout, string.find(stdout, "configuration_hash")))

else
error("unknown deploy mode: " .. deploy)
end
end)

it("kong debug profiling cpu", function ()
code, stdout, stderr = kong_debug_exec("profiling cpu status")
assert.same(EC_SUCCESS, code)
Expand Down

0 comments on commit a5ed390

Please sign in to comment.