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

Commit

Permalink
feat(*) adds config.default_header_type property
Browse files Browse the repository at this point in the history
  • Loading branch information
kikito authored and gszr committed Aug 11, 2020
1 parent 86e9724 commit b3a9807
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion kong/plugins/zipkin/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ if subsystem == "http" then
or ngx_now_mu()
get_or_add_proxy_span(zipkin, access_start)

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


Expand Down
2 changes: 2 additions & 0 deletions kong/plugins/zipkin/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ return {
{ traceid_byte_count = { type = "integer", required = true, default = 16, one_of = { 8, 16 } } },
{ header_type = { type = "string", required = true, default = "preserve",
one_of = { "preserve", "b3", "b3-single", "w3c" } } },
{ default_header_type = { type = "string", required = true, default = "b3",
one_of = { "b3", "b3-single", "w3c" } } },
{ static_tags = { type = "array", elements = static_tag,
custom_validator = validate_static_tags } }
},
Expand Down
5 changes: 3 additions & 2 deletions kong/plugins/zipkin/tracing_headers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ local function parse(headers)
end


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

if conf_header_type ~= "preserve" and
Expand All @@ -283,8 +283,9 @@ local function set(conf_header_type, found_header_type, proxy_span)
kong.log.warn("Mismatched header types. conf: " .. conf_header_type .. ". found: " .. found_header_type)
end

found_header_type = found_header_type or conf_default_header_type or "b3"

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))
Expand Down
23 changes: 19 additions & 4 deletions spec/tracing_headers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -364,20 +364,35 @@ describe("tracing_headers.set", function()

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)

it("sets headers according to default_header_type when no headers are provided", function()
set("preserve", nil, proxy_span)
assert.same(b3_headers, headers)

headers = {}

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

headers = {}

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

headers = {}

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

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

Expand Down
18 changes: 16 additions & 2 deletions spec/zipkin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ describe("http integration tests with zipkin server [#"
traceid_byte_count = traceid_byte_count,
static_tags = {
{ name = "static", value = "ok" },
}
},
default_header_type = "b3-single",
}
})

Expand Down Expand Up @@ -532,7 +533,6 @@ describe("http integration tests with zipkin server [#"
local span_id = gen_span_id()
local parent_id = gen_span_id()


local r = proxy_client:get("/", {
headers = {
b3 = fmt("%s-%s-%s-%s", trace_id, span_id, "1", parent_id),
Expand Down Expand Up @@ -687,6 +687,20 @@ describe("http integration tests with zipkin server [#"
assert.equals(trace_id, proxy_span.traceId)
end)
end)

describe("header type with 'preserve' config and no inbound headers", function()
it("uses whatever is set in the plugin's config.default_header_type property", function()
local r = proxy_client:get("/", {
headers = {
-- no tracing header
host = "http-route"
},
})
local body = assert.response(r).has.status(200)
local json = cjson.decode(body)
assert.not_nil(json.headers.b3)
end)
end)
end)
end
end

0 comments on commit b3a9807

Please sign in to comment.