Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reports) custom immutable items #3180

Merged
merged 1 commit into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions kong/core/reports.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ local function log(lvl, ...)
end


local function serialize_report_value(v)
if type(v) == "function" then
v = v()
end

if type(v) == "table" then
local json, err = cjson.encode(v)
if err then
log(ERR, "could not JSON encode given table entity: ", err)
end

v = json
end

return v ~= nil and tostring(v) or nil
end


-- UDP logger


Expand All @@ -74,22 +92,10 @@ local function send_report(signal_type, t, host, port)

for k, v in pairs(t) do
if k == "unique_id" or (k ~= "created_at" and sub(k, -2) ~= "id") then
if type(v) == "function" then
v = v()
end

if type(v) == "table" then
local json, err = cjson.encode(v)
if err then
log(ERR, "could not JSON encode given table entity: ", err)
end

v = json
end

v = serialize_report_value(v)
if v ~= nil then
mutable_idx = mutable_idx + 1
_buffer[mutable_idx] = k .. "=" .. tostring(v)
_buffer[mutable_idx] = k .. "=" .. v
end
end
end
Expand Down Expand Up @@ -181,6 +187,15 @@ local function add_ping_value(k, v)
end


local function add_immutable_value(k, v)
v = serialize_report_value(v)
if v ~= nil then
_buffer_immutable_idx = _buffer_immutable_idx + 1
_buffer[_buffer_immutable_idx] = k .. "=" .. v
end
end


local retrieve_redis_version


Expand Down Expand Up @@ -226,6 +241,7 @@ return {

create_timer(PING_INTERVAL, ping_handler)
end,
add_immutable_value = add_immutable_value,
add_ping_value = add_ping_value,
get_ping_value = function(k)
return _ping_infos[k]
Expand Down
16 changes: 16 additions & 0 deletions spec/01-unit/013-reports_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ describe("reports", function()
assert.is_nil(res)
assert.equal("timeout", err)
end)
it("accepts custom immutable items", function()
reports.toggle(true)

local thread = helpers.udp_server(8189)

reports.add_immutable_value("imm1", "fooval")
reports.add_immutable_value("imm2", "barval")

reports.send("stub", {k1 = "bazval"}, "127.0.0.1", 8189)

local ok, res = thread:join()
assert.True(ok)
assert.matches("imm1=fooval", res)
assert.matches("imm2=barval", res)
assert.matches("k1=bazval", res)
end)
end)

describe("retrieve_redis_version()", function()
Expand Down