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

fix(tracing): don't print error when passing nil to span:set_attribute #12079

Merged
merged 1 commit into from
Dec 5, 2023
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
13 changes: 10 additions & 3 deletions kong/pdk/tracing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,27 @@ end
--
-- @function span:set_attribute
-- @tparam string key
-- @tparam string|number|boolean value
-- @tparam string|number|boolean|nil value
-- @usage
-- span:set_attribute("net.transport", "ip_tcp")
-- span:set_attribute("net.peer.port", 443)
-- span:set_attribute("exception.escaped", true)
-- span:set_attribute("unset.this", nil)
function span_mt:set_attribute(key, value)
-- key is decided by the programmer, so if it is not a string, we should
-- error out.
if type(key) ~= "string" then
error("invalid key", 2)
end

local vtyp = type(value)
if vtyp ~= "string" and vtyp ~= "number" and vtyp ~= "boolean" then
local vtyp
if value == nil then
vtyp = value
else
vtyp = type(value)
end

if vtyp ~= "string" and vtyp ~= "number" and vtyp ~= "boolean" and vtyp ~= nil then
-- we should not error out here, as most of the caller does not catch
-- errors, and they are hooking to core facilities, which may cause
-- unexpected behavior.
Expand Down
13 changes: 11 additions & 2 deletions spec/01-unit/26-tracing/01-tracer_pdk_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,20 @@ describe("Tracer PDK", function()
assert.has_no.error(function () span:finish() end)
end)

it("fails set_attribute", function ()
it("set_attribute validation", function ()
local span = c_tracer.start_span("meow")

-- nil value is allowed as a noop
span:set_attribute("key1")
assert.spy(log_spy).was_called_with(ngx.ERR, match.is_string())
assert.spy(log_spy).was_not_called_with(ngx.ERR, match.is_string())
assert.is_nil(span.attributes["key1"])

span:set_attribute("key1", "value1")
assert.equal("value1", span.attributes["key1"])

-- nil value unsets the attribute
span:set_attribute("key1")
assert.is_nil(span.attributes["key1"])

span:set_attribute("key1", function() end)
assert.spy(log_spy).was_called_with(ngx.ERR, match.is_string())
Expand Down
Loading