From 5db8356286de05ff833bf6ae2bc574ca8f63ee46 Mon Sep 17 00:00:00 2001 From: yankun Date: Tue, 7 Dec 2021 10:01:49 +0900 Subject: [PATCH 1/5] feat(zipkin) include http path to span name Add a new parameter span_include_path to decide whether to include http path to span name. Add test case. --- kong/plugins/zipkin/handler.lua | 10 +++- kong/plugins/zipkin/schema.lua | 1 + spec/03-plugins/34-zipkin/zipkin_spec.lua | 66 +++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/kong/plugins/zipkin/handler.lua b/kong/plugins/zipkin/handler.lua index ecde2896a77..5a6255c3528 100644 --- a/kong/plugins/zipkin/handler.lua +++ b/kong/plugins/zipkin/handler.lua @@ -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.span_include_path == true then + span_name = method .. ' ' .. path + end + local request_span = new_span( "SERVER", - method, + span_name, ngx_req_start_time_mu(), should_sample, trace_id, @@ -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 diff --git a/kong/plugins/zipkin/schema.lua b/kong/plugins/zipkin/schema.lua index 9138d2b5df9..38f2b353190 100644 --- a/kong/plugins/zipkin/schema.lua +++ b/kong/plugins/zipkin/schema.lua @@ -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 } }, + { span_include_path = { type = "boolean", default = false } }, }, }, }, }, diff --git a/spec/03-plugins/34-zipkin/zipkin_spec.lua b/spec/03-plugins/34-zipkin/zipkin_spec.lua index 151f6702beb..ffa3ef6d616 100644 --- a/spec/03-plugins/34-zipkin/zipkin_spec.lua +++ b/spec/03-plugins/34-zipkin/zipkin_spec.lua @@ -280,6 +280,72 @@ for _, strategy in helpers.each_strategy() do end +for _, strategy in helpers.each_strategy() do + describe("span_include_path 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", + span_include_path = true, + } + }) + + 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("span_include_path = 'true' 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 [#" From 6c9ed4909bcf6a602fe284f0b878b1d19f3916ac Mon Sep 17 00:00:00 2001 From: Mayo Date: Thu, 10 Feb 2022 20:11:48 +0800 Subject: [PATCH 2/5] Update kong/plugins/zipkin/handler.lua --- kong/plugins/zipkin/handler.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/plugins/zipkin/handler.lua b/kong/plugins/zipkin/handler.lua index 5a6255c3528..640b16cf046 100644 --- a/kong/plugins/zipkin/handler.lua +++ b/kong/plugins/zipkin/handler.lua @@ -129,7 +129,7 @@ if subsystem == "http" then local span_name = method local path = req.get_path() - if conf.span_include_path == true then + if conf.span_include_path then span_name = method .. ' ' .. path end From 6740761a645c66e4dd214259311b254f3f699e34 Mon Sep 17 00:00:00 2001 From: Mayo Date: Thu, 10 Feb 2022 20:12:33 +0800 Subject: [PATCH 3/5] Update kong/plugins/zipkin/schema.lua --- kong/plugins/zipkin/schema.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/plugins/zipkin/schema.lua b/kong/plugins/zipkin/schema.lua index 38f2b353190..247414b8bb6 100644 --- a/kong/plugins/zipkin/schema.lua +++ b/kong/plugins/zipkin/schema.lua @@ -61,7 +61,7 @@ return { { tags_header = { type = "string", required = true, default = "Zipkin-Tags" } }, { static_tags = { type = "array", elements = static_tag, custom_validator = validate_static_tags } }, - { span_include_path = { type = "boolean", default = false } }, + { span_name_include_path = { type = "boolean", default = false } }, }, }, }, }, From a5538cee6511a5b8dc3acd7b6b5193e878c8dabe Mon Sep 17 00:00:00 2001 From: Mayo Date: Fri, 25 Mar 2022 18:55:56 +0800 Subject: [PATCH 4/5] fix(zipkin) rename `span_include_path` to `http_span_name` --- kong/plugins/zipkin/handler.lua | 2 +- kong/plugins/zipkin/schema.lua | 2 +- spec/03-plugins/34-zipkin/zipkin_spec.lua | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/kong/plugins/zipkin/handler.lua b/kong/plugins/zipkin/handler.lua index 640b16cf046..0b6c501a7bd 100644 --- a/kong/plugins/zipkin/handler.lua +++ b/kong/plugins/zipkin/handler.lua @@ -129,7 +129,7 @@ if subsystem == "http" then local span_name = method local path = req.get_path() - if conf.span_include_path then + if conf.http_span_name == "method_path" then span_name = method .. ' ' .. path end diff --git a/kong/plugins/zipkin/schema.lua b/kong/plugins/zipkin/schema.lua index 247414b8bb6..4a9b576a4a6 100644 --- a/kong/plugins/zipkin/schema.lua +++ b/kong/plugins/zipkin/schema.lua @@ -61,7 +61,7 @@ return { { tags_header = { type = "string", required = true, default = "Zipkin-Tags" } }, { static_tags = { type = "array", elements = static_tag, custom_validator = validate_static_tags } }, - { span_name_include_path = { type = "boolean", default = false } }, + { http_span_name = { type = "string", required = true, default = "method", one_of = { "method", "method_path" } } }, }, }, }, }, diff --git a/spec/03-plugins/34-zipkin/zipkin_spec.lua b/spec/03-plugins/34-zipkin/zipkin_spec.lua index ffa3ef6d616..4b36c04ea11 100644 --- a/spec/03-plugins/34-zipkin/zipkin_spec.lua +++ b/spec/03-plugins/34-zipkin/zipkin_spec.lua @@ -281,7 +281,7 @@ end for _, strategy in helpers.each_strategy() do - describe("span_include_path configuration", function() + describe("http_span_name configuration", function() local proxy_client, zipkin_client, service setup(function() @@ -306,7 +306,7 @@ for _, strategy in helpers.each_strategy() do sample_ratio = 1, http_endpoint = fmt("http://%s:%d/api/v2/spans", ZIPKIN_HOST, ZIPKIN_PORT), default_header_type = "b3-single", - span_include_path = true, + http_span_name = "method_path", } }) @@ -324,7 +324,7 @@ for _, strategy in helpers.each_strategy() do helpers.stop_kong() end) - it("span_include_path = 'true' includes path to span name", function() + it("http_span_name = 'method_path' includes path to span name", function() local start_s = ngx.now() local r = proxy_client:get("/", { From da6a8a55a68b6e030c745fc1f2f6601ab3afa5e2 Mon Sep 17 00:00:00 2001 From: Mayo Date: Tue, 29 Mar 2022 16:22:43 +0800 Subject: [PATCH 5/5] docs(changelog) add entry for #8150 --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c614efef5a6..77e5f679ece 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,14 @@ - Bumped resty.openssl from 0.8.5 to 0.8.6 [#8545](https://github.com/Kong/kong/pull/8545) +### 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