Skip to content

Commit

Permalink
fix(loggly): fix the error caused by the missing /bin/hostname (#13788)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Water-Melon authored Oct 29, 2024
1 parent 044b4c5 commit 0bcd6c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 2 additions & 0 deletions changelog/unreleased/kong/fix-loggly-hostname-notfound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
message: "**Loggly**: Fixed an issue where `/bin/hostname` missing caused an error warning on startup."
type: bugfix
27 changes: 16 additions & 11 deletions kong/pdk/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ end


local function new(self)
local _NODE = {}
local _NODE = {
hostname = nil,
}


---
Expand Down Expand Up @@ -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
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

_NODE.hostname = gsub(ffi_str(buf, SIZE), "%z+$", "")
end

local f = io.popen("/bin/hostname")
local hostname = f:read("*a") or ""
f:close()
return gsub(hostname, "\n$", "")
return _NODE.hostname
end


Expand Down
10 changes: 1 addition & 9 deletions kong/plugins/loggly/handler.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = {
Expand Down

1 comment on commit 0bcd6c5

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:0bcd6c58e51128e9ec1f5ef2d42ca9a1f97bd3bd
Artifacts available https://github.com/Kong/kong/actions/runs/11566487516

Please sign in to comment.