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

Commit 8b55eb4

Browse files
committed
feat(span-tag): add header x-request-id to span tag
1 parent 1e7db29 commit 8b55eb4

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

kong/plugins/zipkin/handler.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ if subsystem == "http" then
184184
get_or_add_proxy_span(zipkin, access_start)
185185

186186
tracing_headers.set(conf.header_type, zipkin.header_type, zipkin.proxy_span, conf.default_header_type)
187+
188+
if conf.include_header_x_request_id then
189+
local x_request_id = kong.request.get_header('x-request-id')
190+
if x_request_id then
191+
zipkin.request_span:set_tag(conf.tag_name_for_x_request_id, x_request_id)
192+
end
193+
end
187194
end
188195

189196

kong/plugins/zipkin/schema.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ return {
5252
between = { 0, 1 } } },
5353
{ default_service_name = { type = "string", default = nil } },
5454
{ include_credential = { type = "boolean", required = true, default = true } },
55+
{ include_header_x_request_id = { type = "boolean", required = true, default = false } },
56+
{ tag_name_for_x_request_id = { type = "string", required = true, default = "guid:x-request-id",
57+
not_one_of = PROTECTED_TAGS } },
5558
{ traceid_byte_count = { type = "integer", required = true, default = 16, one_of = { 8, 16 } } },
5659
{ header_type = { type = "string", required = true, default = "preserve",
5760
one_of = { "preserve", "b3", "b3-single", "w3c", "jaeger", "ot" } } },

spec/zipkin_spec.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ describe("http integration tests with zipkin server [#"
164164
{ name = "static", value = "ok" },
165165
},
166166
default_header_type = "b3-single",
167+
include_header_x_request_id = true,
167168
}
168169
})
169170

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

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

225228
local r = proxy_client:get("/", {
226229
headers = {
227230
["x-b3-sampled"] = "1",
228231
host = "http-route",
229-
["zipkin-tags"] = "foo=bar; baz=qux"
232+
["zipkin-tags"] = "foo=bar; baz=qux",
233+
["x-request-id"] = x_request_id
230234
},
231235
})
232236
assert.response(r).has.status(200)
@@ -244,6 +248,7 @@ describe("http integration tests with zipkin server [#"
244248
["http.method"] = "GET",
245249
["http.path"] = "/",
246250
["http.status_code"] = "200", -- found (matches server status)
251+
["guid:x-request-id"] = x_request_id,
247252
lc = "kong",
248253
static = "ok",
249254
foo = "bar",
@@ -292,6 +297,8 @@ describe("http integration tests with zipkin server [#"
292297

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

296303
local ok, resp = proxy_client_grpc({
297304
service = "hello.HelloService.SayHello",
@@ -300,6 +307,7 @@ describe("http integration tests with zipkin server [#"
300307
},
301308
opts = {
302309
["-H"] = "'x-b3-sampled: 1'",
310+
["-H"] = "'x-request-id: " .. x_request_id .. "'",
303311
["-authority"] = "grpc-route",
304312
}
305313
})
@@ -320,6 +328,7 @@ describe("http integration tests with zipkin server [#"
320328
["http.method"] = "POST",
321329
["http.path"] = "/hello.HelloService/SayHello",
322330
["http.status_code"] = "200", -- found (matches server status)
331+
["guid:x-request-id"] = x_request_id,
323332
lc = "kong",
324333
static = "ok",
325334
}, request_tags)

0 commit comments

Comments
 (0)