From f55a8dc91a77be35873b5e0b90c3e0779e6f5cfc Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 9 Apr 2024 15:52:05 -0300 Subject: [PATCH 1/2] feat(conf): support Nginx fine-grained debug levels Expose seven additional Nginx log levels that are not normally exposed by OpenResty, which allows for finer-grained debugging with less noise in the debug-level logs: `debug_core`, `debug_alloc`, `debug_mutex`, `debug_event`, `debug_http`, `debug_mail`, `debug_stream`. --- .luacheckrc | 7 ++++++ .../kong/feat-extra-debug-modes.yml | 7 ++++++ kong.conf.default | 6 +++++ kong/conf_loader/constants.lua | 7 ++++++ kong/constants.lua | 22 ++++++++++++++++ .../04-admin_api/22-debug_spec.lua | 25 +++++++++++++++++++ 6 files changed, 74 insertions(+) create mode 100644 changelog/unreleased/kong/feat-extra-debug-modes.yml diff --git a/.luacheckrc b/.luacheckrc index 6bb537398ef..be7ffe0a2de 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -8,6 +8,13 @@ globals = { "_KONG", "kong", "ngx.IS_CLI", + "ngx.DEBUG_CORE", + "ngx.DEBUG_ALLOC", + "ngx.DEBUG_MUTEX", + "ngx.DEBUG_EVENT", + "ngx.DEBUG_HTTP", + "ngx.DEBUG_MAIL", + "ngx.DEBUG_STREAM", } diff --git a/changelog/unreleased/kong/feat-extra-debug-modes.yml b/changelog/unreleased/kong/feat-extra-debug-modes.yml new file mode 100644 index 00000000000..7b130d5c713 --- /dev/null +++ b/changelog/unreleased/kong/feat-extra-debug-modes.yml @@ -0,0 +1,7 @@ +message: | + Expose seven additional Nginx log levels that are not normally + exposed by OpenResty, which allows for finer-grained debugging + with less noise in the debug-level logs: `debug_core`, `debug_alloc`, + `debug_mutex`, `debug_event`, `debug_http`, `debug_mail`, `debug_stream`. +type: feature +scope: Configuration diff --git a/kong.conf.default b/kong.conf.default index ff99a36909e..ea957b4fcbf 100644 --- a/kong.conf.default +++ b/kong.conf.default @@ -31,6 +31,12 @@ #log_level = notice # Log level of the Nginx server. Logs are # found at `/logs/error.log`. + # Supported values are: `debug`, `info`, + # `notice`, `warn`, `error`, `crit`, `alert`, + # `emerg`, `debug_core`, `debug_alloc`, + # `debug_mutex`, `debug_event`, `debug_http`, + # `debug_mail`, `debug_stream`. + # See http://nginx.org/en/docs/ngx_core_module.html#error_log for a list # of accepted values. diff --git a/kong/conf_loader/constants.lua b/kong/conf_loader/constants.lua index dcda1cf0606..bd00122d95d 100644 --- a/kong/conf_loader/constants.lua +++ b/kong/conf_loader/constants.lua @@ -468,6 +468,13 @@ local CONF_PARSERS = { "crit", "alert", "emerg", + "debug_core", + "debug_alloc", + "debug_mutex", + "debug_event", + "debug_http", + "debug_mail", + "debug_stream", } }, vaults = { typ = "array" }, diff --git a/kong/constants.lua b/kong/constants.lua index e94e555383e..db324539ced 100644 --- a/kong/constants.lua +++ b/kong/constants.lua @@ -97,6 +97,14 @@ for k in pairs(key_formats_map) do key_formats[#key_formats + 1] = k end +ngx.DEBUG_CORE = 0x010 +ngx.DEBUG_ALLOC = 0x020 +ngx.DEBUG_MUTEX = 0x040 +ngx.DEBUG_EVENT = 0x080 +ngx.DEBUG_HTTP = 0x100 +ngx.DEBUG_MAIL = 0x200 +ngx.DEBUG_STREAM = 0x400 + local constants = { CJSON_MAX_PRECISION = 16, BUNDLED_PLUGINS = plugin_map, @@ -244,6 +252,20 @@ local constants = { crit = ngx.CRIT, alert = ngx.ALERT, emerg = ngx.EMERG, + debug_core = ngx.DEBUG_CORE, + debug_alloc = ngx.DEBUG_ALLOC, + debug_mutex = ngx.DEBUG_MUTEX, + debug_event = ngx.DEBUG_EVENT, + debug_http = ngx.DEBUG_HTTP, + debug_mail = ngx.DEBUG_MAIL, + debug_stream = ngx.DEBUG_STREAM, + [ngx.DEBUG_CORE] = "debug_core", + [ngx.DEBUG_ALLOC] = "debug_alloc", + [ngx.DEBUG_MUTEX] = "debug_mutex", + [ngx.DEBUG_EVENT] = "debug_event", + [ngx.DEBUG_HTTP] = "debug_http", + [ngx.DEBUG_MAIL] = "debug_mail", + [ngx.DEBUG_STREAM] = "debug_stream", [ngx.DEBUG] = "debug", [ngx.INFO] = "info", [ngx.NOTICE] = "notice", diff --git a/spec/02-integration/04-admin_api/22-debug_spec.lua b/spec/02-integration/04-admin_api/22-debug_spec.lua index 620702bfe64..1e8ad9eed3c 100644 --- a/spec/02-integration/04-admin_api/22-debug_spec.lua +++ b/spec/02-integration/04-admin_api/22-debug_spec.lua @@ -1,6 +1,7 @@ local helpers = require("spec.helpers") local cjson = require("cjson") local fmt = string.format +require("kong.constants") local strategies = {} for _, strategy in helpers.each_strategy() do @@ -169,6 +170,30 @@ describe("Admin API - Kong debug route with strategy #" .. strategy, function() -- from timers pre-created by timer-ng (datadog plugin) assert.logfile().has.no.line("failed to send data to", true, 2) + -- can change to Nginx fine-grained debug levels (debug_mail) + res = assert(helpers.admin_client():send { + method = "PUT", + path = "/debug/node/log-level/debug_mail", + }) + body = assert.res_status(200, res) + json = cjson.decode(body) + message = "log level changed" + assert(json.message == message) + + -- make sure we changed to debug + helpers.wait_until(function() + res = assert(helpers.admin_client():send { + method = "GET", + path = "/debug/node/log-level", + }) + body = assert.res_status(200, res) + json = cjson.decode(body) + message = "log level: debug_mail" + return json.message == message + end, 30) + + assert.logfile().has.line(fmt("log level changed to %s", ngx.DEBUG_MAIL), true, 2) + -- go back to default (debug) res = assert(helpers.admin_client():send { method = "PUT", From e8defcbc3f41805e0bb8070af69adc6d354d0991 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 9 Apr 2024 18:00:57 -0300 Subject: [PATCH 2/2] tests(conf): disable debug_mail dynamic log_level test Dynamically enabling the extended debug log levels requires a new version of lua-kong-nginx-module which incorporates https://github.com/Kong/lua-kong-nginx-module/pull/87 --- spec/02-integration/04-admin_api/22-debug_spec.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/02-integration/04-admin_api/22-debug_spec.lua b/spec/02-integration/04-admin_api/22-debug_spec.lua index 1e8ad9eed3c..5a60648b328 100644 --- a/spec/02-integration/04-admin_api/22-debug_spec.lua +++ b/spec/02-integration/04-admin_api/22-debug_spec.lua @@ -170,6 +170,9 @@ describe("Admin API - Kong debug route with strategy #" .. strategy, function() -- from timers pre-created by timer-ng (datadog plugin) assert.logfile().has.no.line("failed to send data to", true, 2) + --[[ + -- TODO: needs https://github.com/Kong/lua-kong-nginx-module/pull/87 + -- can change to Nginx fine-grained debug levels (debug_mail) res = assert(helpers.admin_client():send { method = "PUT", @@ -194,6 +197,8 @@ describe("Admin API - Kong debug route with strategy #" .. strategy, function() assert.logfile().has.line(fmt("log level changed to %s", ngx.DEBUG_MAIL), true, 2) + --]] + -- go back to default (debug) res = assert(helpers.admin_client():send { method = "PUT",