Skip to content

Commit

Permalink
feat(zipkin) add support to include http path to span name (#8150)
Browse files Browse the repository at this point in the history
Co-authored-by: Mayo <i@shoujo.io>
  • Loading branch information
yankun-li-kong and mayocream authored Apr 1, 2022
1 parent 2536a95 commit 21cc6f8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@
- Bumped inspect from 3.1.2 to 3.1.3
[#8589](https://github.com/Kong/kong/pull/8589)

### Additions

#### Plugins

- **Zipkin**: add support for including HTTP path in span name
through configuration property `http_span_name`.
[#8150](https://github.com/Kong/kong/pull/8150)

### Fixes

#### Core
Expand Down
10 changes: 8 additions & 2 deletions kong/plugins/zipkin/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,15 @@ if subsystem == "http" then
trace_id = rand_bytes(conf.traceid_byte_count)
end

local span_name = method
local path = req.get_path()
if conf.http_span_name == "method_path" then
span_name = method .. ' ' .. path
end

local request_span = new_span(
"SERVER",
method,
span_name,
ngx_req_start_time_mu(),
should_sample,
trace_id,
Expand All @@ -146,7 +152,7 @@ if subsystem == "http" then
request_span:set_tag("lc", "kong")
request_span:set_tag("http.method", method)
request_span:set_tag("http.host", req.get_host())
request_span:set_tag("http.path", req.get_path())
request_span:set_tag("http.path", path)
if protocol then
request_span:set_tag("http.protocol", protocol)
end
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/zipkin/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ return {
{ tags_header = { type = "string", required = true, default = "Zipkin-Tags" } },
{ static_tags = { type = "array", elements = static_tag,
custom_validator = validate_static_tags } },
{ http_span_name = { type = "string", required = true, default = "method", one_of = { "method", "method_path" } } },
},
}, },
},
Expand Down
66 changes: 66 additions & 0 deletions spec/03-plugins/34-zipkin/zipkin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,72 @@ for _, strategy in helpers.each_strategy() do
end


for _, strategy in helpers.each_strategy() do
describe("http_span_name configuration", function()
local proxy_client, zipkin_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",
}
})

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()
zipkin_client = helpers.http_client(ZIPKIN_HOST, ZIPKIN_PORT)
end)

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

it("http_span_name = 'method_path' includes path to span name", function()
local start_s = ngx.now()

local r = proxy_client:get("/", {
headers = {
["x-b3-sampled"] = "1",
host = "http-route",
["zipkin-tags"] = "foo=bar; baz=qux"
},
})

assert.response(r).has.status(200)

local _, proxy_span, request_span =
wait_for_spans(zipkin_client, 3, service.name)
-- common assertions for request_span and proxy_span
assert_span_invariants(request_span, proxy_span, "get /", 16 * 2, start_s, "kong")
end)
end)
end


for _, strategy in helpers.each_strategy() do
for _, traceid_byte_count in ipairs({ 8, 16 }) do
describe("http integration tests with zipkin server [#"
Expand Down

0 comments on commit 21cc6f8

Please sign in to comment.