Skip to content

Commit

Permalink
feat(zipkin): support adding trace id in response header (#9173)
Browse files Browse the repository at this point in the history
This PR adds a new field in Zipkin plugin schema to support adding trace id in the response header.

Fix FTI-4198, #8124
  • Loading branch information
windmgc authored Oct 18, 2022
1 parent f05e4bf commit 997c8ce
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions kong/plugins/zipkin/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ if subsystem == "http" then

local proxy_span = get_or_add_proxy_span(zipkin, header_filter_start_mu)
proxy_span:annotate("khs", header_filter_start_mu)
if conf.http_response_header_for_traceid then
kong.response.add_header(conf.http_response_header_for_traceid, proxy_span.trace_id)
end
end


Expand Down
3 changes: 2 additions & 1 deletion kong/plugins/zipkin/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ return {
{ sample_ratio = { type = "number",
default = 0.001,
between = { 0, 1 } } },
{ default_service_name = { type = "string", default = nil } },
{ default_service_name = { type = "string", default = nil } },
{ include_credential = { type = "boolean", required = true, default = true } },
{ traceid_byte_count = { type = "integer", required = true, default = 16, one_of = { 8, 16 } } },
{ header_type = { type = "string", required = true, default = "preserve",
Expand All @@ -65,6 +65,7 @@ return {
{ connect_timeout = typedefs.timeout { default = 2000 } },
{ send_timeout = typedefs.timeout { default = 5000 } },
{ read_timeout = typedefs.timeout { default = 5000 } },
{ http_response_header_for_traceid = { type = "string", default = nil }},
},
}, },
},
Expand Down
57 changes: 57 additions & 0 deletions spec/03-plugins/34-zipkin/zipkin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,63 @@ for _, strategy in helpers.each_strategy() do
end)
end

for _, strategy in helpers.each_strategy() do
describe("http_response_header_for_traceid configuration", function()
local proxy_client, service

setup(function()
local bp = helpers.get_db_utils(strategy, { "services", "routes", "plugins" })

service = bp.services:insert {
name = string.lower("http-" .. utils.random_string()),
}

-- kong (http) mock upstream
bp.routes:insert({
name = string.lower("route-" .. utils.random_string()),
service = service,
hosts = { "http-route" },
preserve_host = true,
})

-- enable zipkin plugin globally, with sample_ratio = 1
bp.plugins:insert({
name = "zipkin",
config = {
sample_ratio = 1,
http_endpoint = fmt("http://%s:%d/api/v2/spans", ZIPKIN_HOST, ZIPKIN_PORT),
default_header_type = "b3-single",
http_span_name = "method_path",
http_response_header_for_traceid = "X-B3-TraceId",
}
})

helpers.start_kong({
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
stream_listen = helpers.get_proxy_ip(false) .. ":19000",
})

proxy_client = helpers.proxy_client()
end)

teardown(function()
helpers.stop_kong()
end)

it("custom traceid header included in response headers", function()
local r = proxy_client:get("/", {
headers = {
host = "http-route",
},
})

assert.response(r).has.status(200)
assert.response(r).has.header("X-B3-TraceId")
end)
end)
end

for _, strategy in helpers.each_strategy() do
describe("http_span_name configuration", function()
local proxy_client, zipkin_client, service
Expand Down

0 comments on commit 997c8ce

Please sign in to comment.