Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zipkin) support adding trace id in response header #9173

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local xxx = conf.http_response_header_for_traceid then use it.

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