Skip to content

Commit

Permalink
fix: header tag normalization (#63)
Browse files Browse the repository at this point in the history
An empty or `nil` tag was generating an error and preventing tracing.
This commit correctly handle `nil` value for the tag field.

Resolves #59

Changes:
  - Add tests.
  • Loading branch information
dmehala authored Jun 11, 2024
1 parent 39ccb24 commit 34f730a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion kong/plugins/ddtrace/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ local function normalize_header_tags(header_tags)
local normalized = {}

for i = 1, #header_tags do
local tag = header_tags[i].tag
local tag = header_tags[i].tag or ""
local header = header_tags[i].header

if not header then
goto continue
end

local norm_header = string.lower(string.gsub(header, "%s+", ""))
if #norm_header == 0 then
goto continue
Expand Down
6 changes: 5 additions & 1 deletion spec/05_utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ describe("utils.normalize_headers_tag", function()
end)
it("tag", function()
local header_tags = {
{ header = nil, tag = nil },
{ header = nil, tag = "foobar" },
{ header = "lorem", tag = nil },
{ header = "ConTeNt-Type", tag = "" },
{ header = "D!ata__d/o!g", tag = "_dd.header" },
{ header = "foo", tag = " " },
Expand All @@ -35,11 +38,12 @@ describe("utils.normalize_headers_tag", function()

local norm_header_tags = utils.normalize_header_tags(header_tags)

assert.equal(count_table(norm_header_tags), 4)
assert.equal(count_table(norm_header_tags), 5)
assert.same(norm_header_tags["content-type"], { normalized = true, value = "content-type" })
assert.same(norm_header_tags["d_ata__d_o_g"], { normalized = false, value = "_dd.header" })
assert.same(norm_header_tags["foo"], { normalized = true, value = "foo" })
assert.same(norm_header_tags["bar"], { normalized = false, value = "mytag" })
assert.same(norm_header_tags["lorem"], { normalized = true, value = "lorem" })
end)
end)

Expand Down

0 comments on commit 34f730a

Please sign in to comment.