From 16aa4b67abfee8f1e9ccbb07949c0e331d71a8e6 Mon Sep 17 00:00:00 2001 From: Water-Melon Date: Fri, 25 Oct 2024 03:42:15 +0000 Subject: [PATCH 1/3] fix(loggly): fix the error caused by the missing /bin/hostname 1. Refactor the PDK's get_hostname to use only the gethostname system call to retrieve the hostname. 2. Loggly uses the PDK's get_hostname to retrieve the hostname. FTI-6046 --- .../kong/fix-loggly-hostname-notfound.yml | 2 ++ kong/pdk/node.lua | 14 +++++++------- kong/plugins/loggly/handler.lua | 10 +--------- 3 files changed, 10 insertions(+), 16 deletions(-) create mode 100644 changelog/unreleased/kong/fix-loggly-hostname-notfound.yml diff --git a/changelog/unreleased/kong/fix-loggly-hostname-notfound.yml b/changelog/unreleased/kong/fix-loggly-hostname-notfound.yml new file mode 100644 index 000000000000..5e2cadb73bdd --- /dev/null +++ b/changelog/unreleased/kong/fix-loggly-hostname-notfound.yml @@ -0,0 +1,2 @@ +message: "**loggly**: fixed the error caused by the missing `/bin/hostname`." +type: bugfix diff --git a/kong/pdk/node.lua b/kong/pdk/node.lua index 9302c17cde1a..23f83ca449d1 100644 --- a/kong/pdk/node.lua +++ b/kong/pdk/node.lua @@ -252,15 +252,15 @@ local function new(self) local buf = ffi_new("unsigned char[?]", SIZE) local res = C.gethostname(buf, SIZE) - if res == 0 then - local hostname = ffi_str(buf, SIZE) - return gsub(hostname, "%z+$", "") + if res ~= 0 then + -- Return an empty string "" instead of nil and error message, + -- because strerror is not thread-safe and the behavior of strerror_r + -- is inconsistent across different systems. + return "" end - local f = io.popen("/bin/hostname") - local hostname = f:read("*a") or "" - f:close() - return gsub(hostname, "\n$", "") + local hostname = ffi_str(buf, SIZE) + return gsub(hostname, "%z+$", "") end diff --git a/kong/plugins/loggly/handler.lua b/kong/plugins/loggly/handler.lua index 9288adb37b7c..39e70547e66f 100644 --- a/kong/plugins/loggly/handler.lua +++ b/kong/plugins/loggly/handler.lua @@ -1,6 +1,7 @@ local cjson = require "cjson" local sandbox = require "kong.tools.sandbox".sandbox local kong_meta = require "kong.meta" +local get_host_name = kong.node.get_hostname local kong = kong @@ -16,15 +17,6 @@ local insert = table.insert local sandbox_opts = { env = { kong = kong, ngx = ngx } } -local function get_host_name() - local f = io.popen("/bin/hostname") - local hostname = f:read("*a") or "" - f:close() - hostname = string.gsub(hostname, "\n$", "") - return hostname -end - - local HOSTNAME = get_host_name() local SENDER_NAME = "kong" local LOG_LEVELS = { From 7cedd871349dc054cd823b6897f87e18c95d3cd5 Mon Sep 17 00:00:00 2001 From: Water-Melon Date: Mon, 28 Oct 2024 08:48:38 +0000 Subject: [PATCH 2/3] cache hostname --- kong/pdk/node.lua | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/kong/pdk/node.lua b/kong/pdk/node.lua index 23f83ca449d1..fe8bfbc72392 100644 --- a/kong/pdk/node.lua +++ b/kong/pdk/node.lua @@ -57,7 +57,9 @@ end local function new(self) - local _NODE = {} + local _NODE = { + hostname = nil, + } --- @@ -247,20 +249,23 @@ local function new(self) -- @usage -- local hostname = kong.node.get_hostname() function _NODE.get_hostname() - local SIZE = 253 -- max number of chars for a hostname + if not _NODE.hostname then + local SIZE = 253 -- max number of chars for a hostname - local buf = ffi_new("unsigned char[?]", SIZE) - local res = C.gethostname(buf, SIZE) + local buf = ffi_new("unsigned char[?]", SIZE) + local res = C.gethostname(buf, SIZE) - if res ~= 0 then - -- Return an empty string "" instead of nil and error message, - -- because strerror is not thread-safe and the behavior of strerror_r - -- is inconsistent across different systems. - return "" + if res ~= 0 then + -- Return an empty string "" instead of nil and error message, + -- because strerror is not thread-safe and the behavior of strerror_r + -- is inconsistent across different systems. + return "" + end + + _NODE.hostname = gsub(ffi_str(buf, SIZE), "%z+$", "") end - local hostname = ffi_str(buf, SIZE) - return gsub(hostname, "%z+$", "") + return _NODE.hostname end From 73d40c8a3159899f8662f738bff22aab8cc7b978 Mon Sep 17 00:00:00 2001 From: Water-Melon Date: Mon, 28 Oct 2024 08:48:47 +0000 Subject: [PATCH 3/3] update changelog --- changelog/unreleased/kong/fix-loggly-hostname-notfound.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/kong/fix-loggly-hostname-notfound.yml b/changelog/unreleased/kong/fix-loggly-hostname-notfound.yml index 5e2cadb73bdd..23ad46d028a5 100644 --- a/changelog/unreleased/kong/fix-loggly-hostname-notfound.yml +++ b/changelog/unreleased/kong/fix-loggly-hostname-notfound.yml @@ -1,2 +1,2 @@ -message: "**loggly**: fixed the error caused by the missing `/bin/hostname`." +message: "**Loggly**: Fixed an issue where `/bin/hostname` missing caused an error warning on startup." type: bugfix