Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
feat(*) new header_type config option
Browse files Browse the repository at this point in the history
  • Loading branch information
kikito committed Mar 31, 2020
1 parent 07309bb commit 059d937
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 50 deletions.
2 changes: 1 addition & 1 deletion kong/plugins/zipkin/handler.lua
Expand Up @@ -152,7 +152,7 @@ if subsystem == "http" then
or ngx_now_mu()
get_or_add_proxy_span(zipkin, access_start)

tracing_headers.set(zipkin.header_type, zipkin.proxy_span)
tracing_headers.set(conf.header_type, zipkin.header_type, zipkin.proxy_span)
end


Expand Down
2 changes: 2 additions & 0 deletions kong/plugins/zipkin/schema.lua
Expand Up @@ -12,6 +12,8 @@ return {
between = { 0, 1 } } },
{ default_service_name = { type = "string", default = nil } },
{ include_credential = { type = "boolean", required = true, default = true } },
{ header_type = { type = "string", required = true, default = "preserve",
one_of = { "preserve", "b3", "b3-single", "w3c" } } },
},
}, },
},
Expand Down
20 changes: 16 additions & 4 deletions kong/plugins/zipkin/tracing_headers.lua
Expand Up @@ -273,10 +273,20 @@ local function parse(headers)
end


local function set(header_type, proxy_span)
local function set(conf_header_type, found_header_type, proxy_span)
local set_header = kong.service.request.set_header

if header_type == nil or header_type == "b3" then
if conf_header_type ~= "preserve" and
found_header_type ~= nil and
conf_header_type ~= found_header_type
then
kong.log.warn("Mismatched header types. conf: " .. conf_header_type .. ". found: " .. found_header_type)
end

if conf_header_type == "b3"
or found_header_type == nil
or found_header_type == "b3"
then
set_header("x-b3-traceid", to_hex(proxy_span.trace_id))
set_header("x-b3-spanid", to_hex(proxy_span.span_id))
if proxy_span.parent_id then
Expand All @@ -288,15 +298,17 @@ local function set(header_type, proxy_span)
else
set_header("x-b3-sampled", proxy_span.should_sample and "1" or "0")
end
end

elseif header_type == "b3-single" then
if conf_header_type == "b3-single" or found_header_type == "b3-single" then
set_header("b3", fmt("%s-%s-%s-%s",
to_hex(proxy_span.trace_id),
to_hex(proxy_span.span_id),
proxy_span.should_sample and "1" or "0",
to_hex(proxy_span.parent_id)))
end

elseif header_type == "w3c" then
if conf_header_type == "w3c" or found_header_type == "w3c" then
set_header("traceparent", fmt("00-%s-%s-%s",
to_hex(proxy_span.trace_id),
to_hex(proxy_span.span_id),
Expand Down
191 changes: 146 additions & 45 deletions spec/tracing_headers_spec.lua
Expand Up @@ -2,6 +2,8 @@ local tracing_headers = require "kong.plugins.zipkin.tracing_headers"

local to_hex = require "resty.string".to_hex

local table_merge = require "kong.tools.utils".table_merge

local fmt = string.format

local function to_hex_ids(arr)
Expand Down Expand Up @@ -301,67 +303,166 @@ describe("tracing_headers.parse", function()
end)
end)
end)
end)

describe("tracing_headers.set", function()
local nop = function() end
_G.kong = {
service = {
request = {},
},
describe("tracing_headers.set", function()
local nop = function() end

local headers
local warnings

_G.kong = {
service = {
request = {
get_header = nop,
set_header = function(name, value)
headers[name] = value
end,
},
},
request = {
get_header = nop,
},
log = {
warn = function(msg)
warnings[#warnings + 1] = msg
end
}
}

local proxy_span = {
trace_id = from_hex(trace_id),
span_id = from_hex(span_id),
parent_id = from_hex(parent_id),
should_sample = true,
each_baggage_item = function() return nop end,
}

local b3_headers = {
["x-b3-traceid"] = trace_id,
["x-b3-spanid"] = span_id,
["x-b3-parentspanid"] = parent_id,
["x-b3-sampled"] = "1"
}

local b3_single_headers = {
b3 = fmt("%s-%s-1-%s", trace_id, span_id, parent_id)
}

local w3c_headers = {
traceparent = fmt("00-%s-%s-01", trace_id, span_id)
}

before_each(function()
headers = {}
warnings = {}
end)

describe("conf.header_type = 'preserve'", function()
it("sets headers according to their found state when conf.header_type = preserve", function()
set("preserve", "b3", proxy_span)
assert.same(b3_headers, headers)

local headers
before_each(function()
headers = {}
kong.service.request.set_header = function(name, value)
headers[name] = value
end

set("preserve", nil, proxy_span)
assert.same(b3_headers, headers)

headers = {}

set("preserve", "b3-single", proxy_span)
assert.same(b3_single_headers, headers)

headers = {}

set("preserve", "w3c", proxy_span)
assert.same(w3c_headers, headers)

assert.same({}, warnings)
end)
end)

local proxy_span = {
trace_id = from_hex(trace_id),
span_id = from_hex(span_id),
parent_id = from_hex(parent_id),
should_sample = true,
each_baggage_item = function() return nop end,
}
describe("conf.header_type = 'b3'", function()
it("sets headers to b3 when conf.header_type = b3", function()
set("b3", "b3", proxy_span)
assert.same(b3_headers, headers)

headers = {}

set("b3", nil, proxy_span)
assert.same(b3_headers, headers)

assert.same({}, warnings)
end)

it("sets both the b3 and b3-single headers when a b3-single header is encountered.", function()
set("b3", "b3-single", proxy_span)
assert.same(table_merge(b3_headers, b3_single_headers), headers)

-- but it generates a warning
assert.equals(1, #warnings)
assert.matches("Mismatched header types", warnings[1])
end)

it("sets b3 headers when header_type is b3", function()
set("b3", proxy_span)
assert.same({
["x-b3-traceid"] = trace_id,
["x-b3-spanid"] = span_id,
["x-b3-parentspanid"] = parent_id,
["x-b3-sampled"] = "1"
}, headers)
it("sets both the b3 and w3c headers when a w3c header is encountered.", function()
set("b3", "w3c", proxy_span)
assert.same(table_merge(b3_headers, w3c_headers), headers)

-- but it generates a warning
assert.equals(1, #warnings)
assert.matches("Mismatched header types", warnings[1])
end)
end)

it("sets b3 headers when header_type is nil", function()
set(nil, proxy_span)
assert.same({
["x-b3-traceid"] = trace_id,
["x-b3-spanid"] = span_id,
["x-b3-parentspanid"] = parent_id,
["x-b3-sampled"] = "1"
}, headers)
describe("conf.header_type = 'b3-single'", function()
it("sets headers to b3-single when conf.header_type = b3-single", function()
set("b3-single", "b3-single", proxy_span)
assert.same(b3_single_headers, headers)
assert.same({}, warnings)
end)

it("sets b3-single header when header_type is b3-sinpgle", function()
set("b3-single", proxy_span)
assert.same({
b3 = fmt("%s-%s-1-%s", trace_id, span_id, parent_id)
}, headers)
it("sets both the b3 and b3-single headers when a b3 header is encountered.", function()
set("b3-single", "b3", proxy_span)
assert.same(table_merge(b3_headers, b3_single_headers), headers)

-- but it generates a warning
assert.equals(1, #warnings)
assert.matches("Mismatched header types", warnings[1])
end)

it("sets w3c header when header_type is w3c", function()
set("w3c", proxy_span)
assert.same({
traceparent = fmt("00-%s-%s-01", trace_id, span_id)
}, headers)
it("sets both the b3 and w3c headers when a w3c header is encountered.", function()
set("b3-single", "w3c", proxy_span)
assert.same(table_merge(b3_single_headers, w3c_headers), headers)

-- but it generates a warning
assert.equals(1, #warnings)
assert.matches("Mismatched header types", warnings[1])
end)
end)

describe("conf.header_type = 'w3c'", function()
it("sets headers to w3c when conf.header_type = w3c", function()
set("w3c", "w3c", proxy_span)
assert.same(w3c_headers, headers)
assert.same({}, warnings)
end)

it("sets both the b3 and w3c headers when a w3c header is encountered.", function()
set("w3c", "b3", proxy_span)
assert.same(table_merge(b3_headers, w3c_headers), headers)

-- but it generates a warning
assert.equals(1, #warnings)
assert.matches("Mismatched header types", warnings[1])
end)

it("sets both the b3-single and w3c headers when a b3-single header is encountered.", function()
set("w3c", "b3-single", proxy_span)
assert.same(table_merge(b3_single_headers, w3c_headers), headers)

-- but it generates a warning
assert.equals(1, #warnings)
assert.matches("Mismatched header types", warnings[1])
end)
end)
end)

0 comments on commit 059d937

Please sign in to comment.