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

feat(span-tag): add header x-request-id to span tag #109

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions kong/plugins/zipkin/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ if subsystem == "http" then
get_or_add_proxy_span(zipkin, access_start)

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

if conf.include_header_x_request_id then
local x_request_id = kong.request.get_header('x-request-id')
if x_request_id then
zipkin.request_span:set_tag(conf.tag_name_for_x_request_id, x_request_id)
end
end
end


Expand Down
3 changes: 3 additions & 0 deletions kong/plugins/zipkin/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ return {
between = { 0, 1 } } },
{ default_service_name = { type = "string", default = nil } },
{ include_credential = { type = "boolean", required = true, default = true } },
{ include_header_x_request_id = { type = "boolean", required = true, default = false } },
{ tag_name_for_x_request_id = { type = "string", required = true, default = "guid:x-request-id",
not_one_of = PROTECTED_TAGS } },
{ 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", "jaeger", "ot" } } },
Expand Down
12 changes: 10 additions & 2 deletions spec/zipkin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ describe("http integration tests with zipkin server [#"
{ name = "static", value = "ok" },
},
default_header_type = "b3-single",
include_header_x_request_id = true,
}
})

Expand Down Expand Up @@ -221,12 +222,15 @@ describe("http integration tests with zipkin server [#"

it("generates spans, tags and annotations for regular requests", function()
local start_s = ngx.now()
local uuid = require("resty.jit-uuid")
local x_request_id = uuid.generate_v4()

local r = proxy_client:get("/", {
headers = {
["x-b3-sampled"] = "1",
host = "http-route",
["zipkin-tags"] = "foo=bar; baz=qux"
["zipkin-tags"] = "foo=bar; baz=qux",
["x-request-id"] = x_request_id
},
})
assert.response(r).has.status(200)
Expand All @@ -244,6 +248,7 @@ describe("http integration tests with zipkin server [#"
["http.method"] = "GET",
["http.path"] = "/",
["http.status_code"] = "200", -- found (matches server status)
["guid:x-request-id"] = x_request_id,
lc = "kong",
static = "ok",
foo = "bar",
Expand Down Expand Up @@ -292,14 +297,16 @@ describe("http integration tests with zipkin server [#"

it("generates spans, tags and annotations for regular requests (#grpc)", function()
local start_s = ngx.now()
local uuid = require("resty.jit-uuid")
local x_request_id = uuid.generate_v4()

local ok, resp = proxy_client_grpc({
service = "hello.HelloService.SayHello",
body = {
greeting = "world!"
},
opts = {
["-H"] = "'x-b3-sampled: 1'",
["-H"] = "'x-request-id: " .. x_request_id .. "' -H 'x-b3-sampled: 1'",
["-authority"] = "grpc-route",
}
})
Expand All @@ -320,6 +327,7 @@ describe("http integration tests with zipkin server [#"
["http.method"] = "POST",
["http.path"] = "/hello.HelloService/SayHello",
["http.status_code"] = "200", -- found (matches server status)
["guid:x-request-id"] = x_request_id,
lc = "kong",
static = "ok",
}, request_tags)
Expand Down