From a5ed390cd0a6bf8b8d827f6a11b5c57477b32cd4 Mon Sep 17 00:00:00 2001 From: xianghai2 Date: Wed, 24 Jul 2024 13:59:26 +0800 Subject: [PATCH] feat(cli): add a new sub-command 'status' to the `debug` CLI (#9685) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-authored-by: Qi --- .../kong/new_debug_status_sumcommand.yml | 2 ++ kong/cmd/debug.lua | 31 +++++++++++++++++++ kong/debug/init.lua | 2 ++ .../02-integration/11-cmd/03-debug_spec.lua | 17 ++++++++++ 4 files changed, 52 insertions(+) create mode 100644 changelog/unreleased/kong/new_debug_status_sumcommand.yml diff --git a/changelog/unreleased/kong/new_debug_status_sumcommand.yml b/changelog/unreleased/kong/new_debug_status_sumcommand.yml new file mode 100644 index 000000000000..c4ed13b14c11 --- /dev/null +++ b/changelog/unreleased/kong/new_debug_status_sumcommand.yml @@ -0,0 +1,2 @@ +message: "Added a new sub-command `status` to the `kong debug` CLI tool." +type: feature diff --git a/kong/cmd/debug.lua b/kong/cmd/debug.lua index fc7910a397b5..a8297aa89182 100644 --- a/kong/cmd/debug.lua +++ b/kong/cmd/debug.lua @@ -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, } @@ -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. @@ -394,5 +424,6 @@ return { sub_commands = { log_level = true, profiling = true, + status = true, }, } diff --git a/kong/debug/init.lua b/kong/debug/init.lua index 58e16a48b8ec..4dfeee355b07 100644 --- a/kong/debug/init.lua +++ b/kong/debug/init.lua @@ -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 diff --git a/spec-ee/02-integration/11-cmd/03-debug_spec.lua b/spec-ee/02-integration/11-cmd/03-debug_spec.lua index d82e8e6670a4..2da61915d0eb 100644 --- a/spec-ee/02-integration/11-cmd/03-debug_spec.lua +++ b/spec-ee/02-integration/11-cmd/03-debug_spec.lua @@ -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)