Skip to content

Commit

Permalink
fix: ngx.re.match usage in w3c_propagation.lua (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmehala authored Apr 15, 2024
1 parent 00a5bd3 commit 2685c6a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
21 changes: 15 additions & 6 deletions kong/plugins/ddtrace/w3c_propagation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,22 @@ local function parse_datadog_tracestate(tracestate)
if k == "s" then
local sampling, err = tonumber(v)
if err then
return result, err
return result, 'datadog sampling "' .. v .. '" is improperly formatted (' .. err .. ")"
end
result["sampling_priority"] = sampling
elseif k == "o" then
result["origin"] = v
elseif k == "p" then
result["parent_id"] = v
elseif k == "t.dm" then
local m, _ = re_match(v, "-[0-9]+", "ajo")
local m, err = re_match(v, "-[0-9]+", "ajo")
if not m then
return result, "extracted t.dm is improperly formatted"
local err_msg = 't.dm "' .. v .. '" is improperly formatted'
if err then
err_msg = err_msg .. "(" .. err .. ")"
end

return result, err_msg
end

result["_dd.p.dm"] = v
Expand All @@ -60,8 +65,12 @@ local function extract(get_header, _)
-- * parentid is 8-byte hex-encoded (64b)
-- * trace_flags is 8-byte hex-encoded and bit-field
local m, err = re_match(traceparent, traceparent_format, "ajo")
if err then
return nil, err
if not m then
local err_msg = 'failed to parse traceparent "' .. traceparent .. '"'
if err then
return nil, err_msg .. "(" .. err .. ")"
end
return nil, err_msg
end

local hex_trace_id, parent_id, trace_flags = table.unpack(m, 2) -- luacheck: ignore 143
Expand Down Expand Up @@ -101,7 +110,7 @@ local function extract(get_header, _)
if tracestate then
dd_state, err = parse_datadog_tracestate(tracestate)
if err then
kong.log.warn(err)
kong.log.warn("failed to extract datadog tracestate: " .. err)
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/07_w3c_propagation_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ describe("extract w3c", function()
assert.is_not_nil(err)
end)

it("bad format", function()
local get_header = make_getter({
traceparent = "00-ysjbsgwcpqmrahmydynyllupquotexmvzogsvvhfwyxxlmlkbgegekd",
})

local extracted, err = extract_w3c(get_header, get_header)
assert.is_nil(extracted)
assert.is_not_nil(err)
end)

it("valid", function()
local get_header = make_getter({
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
Expand Down

0 comments on commit 2685c6a

Please sign in to comment.