From 21cc6f895bcf8af22da233e929e36ac0e4963321 Mon Sep 17 00:00:00 2001 From: yankun-li-kong <77371186+yankun-li-kong@users.noreply.github.com> Date: Fri, 1 Apr 2022 16:09:04 +0900 Subject: [PATCH] feat(zipkin) add support to include http path to span name (#8150) Co-authored-by: Mayo --- CHANGELOG.md | 8 +++ kong/plugins/zipkin/handler.lua | 10 +++- kong/plugins/zipkin/schema.lua | 1 + spec/03-plugins/34-zipkin/zipkin_spec.lua | 66 +++++++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9772800e4be..ed721fdcbbd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/kong/plugins/zipkin/handler.lua b/kong/plugins/zipkin/handler.lua index ecde2896a77c..0b6c501a7bd9 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.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, @@ -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 9138d2b5df94..4a9b576a4a63 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 } }, + { 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 887ed7e968e4..eab8cba20377 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("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 [#"