From cc05a10b03cb9c2c514d5694f8805b82ba66d3e9 Mon Sep 17 00:00:00 2001 From: windmgc Date: Mon, 1 Aug 2022 17:19:28 +0800 Subject: [PATCH 1/2] feat(zipkin) support adding trace id in response header --- kong/plugins/zipkin/handler.lua | 3 ++ kong/plugins/zipkin/schema.lua | 3 +- spec/03-plugins/34-zipkin/zipkin_spec.lua | 57 +++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/kong/plugins/zipkin/handler.lua b/kong/plugins/zipkin/handler.lua index 9bd0c436ebd8..d5f5b120d50b 100644 --- a/kong/plugins/zipkin/handler.lua +++ b/kong/plugins/zipkin/handler.lua @@ -226,6 +226,9 @@ if subsystem == "http" then local proxy_span = get_or_add_proxy_span(zipkin, header_filter_start_mu) proxy_span:annotate("khs", header_filter_start_mu) + if conf.response_header_for_traceid then + kong.response.add_header(conf.response_header_for_traceid, proxy_span.trace_id) + end end diff --git a/kong/plugins/zipkin/schema.lua b/kong/plugins/zipkin/schema.lua index c3f3ba8e28c3..d612bb4575aa 100644 --- a/kong/plugins/zipkin/schema.lua +++ b/kong/plugins/zipkin/schema.lua @@ -51,7 +51,7 @@ return { { sample_ratio = { type = "number", default = 0.001, between = { 0, 1 } } }, - { default_service_name = { type = "string", default = nil } }, + { default_service_name = { type = "string", default = nil } }, { include_credential = { type = "boolean", required = true, default = true } }, { traceid_byte_count = { type = "integer", required = true, default = 16, one_of = { 8, 16 } } }, { header_type = { type = "string", required = true, default = "preserve", @@ -65,6 +65,7 @@ return { { connect_timeout = typedefs.timeout { default = 2000 } }, { send_timeout = typedefs.timeout { default = 5000 } }, { read_timeout = typedefs.timeout { default = 5000 } }, + { response_header_for_traceid = { type = "string", default = nil }}, }, }, }, }, diff --git a/spec/03-plugins/34-zipkin/zipkin_spec.lua b/spec/03-plugins/34-zipkin/zipkin_spec.lua index b8449abd44d2..7cfc695f03a8 100644 --- a/spec/03-plugins/34-zipkin/zipkin_spec.lua +++ b/spec/03-plugins/34-zipkin/zipkin_spec.lua @@ -423,6 +423,63 @@ for _, strategy in helpers.each_strategy() do end) end +for _, strategy in helpers.each_strategy() do + describe("response_header_for_traceid configuration", function() + local proxy_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", + response_header_for_traceid = "X-B3-TraceId", + } + }) + + 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() + end) + + teardown(function() + helpers.stop_kong() + end) + + it("custom traceid header included in response headers", function() + local r = proxy_client:get("/", { + headers = { + host = "http-route", + }, + }) + + assert.response(r).has.status(200) + assert.response(r).has.header("X-B3-TraceId") + end) + end) +end + for _, strategy in helpers.each_strategy() do describe("http_span_name configuration", function() local proxy_client, zipkin_client, service From e6e19977ab7a6f9223c3e04c0200637e2ca47749 Mon Sep 17 00:00:00 2001 From: windmgc Date: Mon, 5 Sep 2022 11:09:04 +0800 Subject: [PATCH 2/2] rename field --- kong/plugins/zipkin/handler.lua | 4 ++-- kong/plugins/zipkin/schema.lua | 2 +- spec/03-plugins/34-zipkin/zipkin_spec.lua | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/kong/plugins/zipkin/handler.lua b/kong/plugins/zipkin/handler.lua index d5f5b120d50b..70ec63287edd 100644 --- a/kong/plugins/zipkin/handler.lua +++ b/kong/plugins/zipkin/handler.lua @@ -226,8 +226,8 @@ if subsystem == "http" then local proxy_span = get_or_add_proxy_span(zipkin, header_filter_start_mu) proxy_span:annotate("khs", header_filter_start_mu) - if conf.response_header_for_traceid then - kong.response.add_header(conf.response_header_for_traceid, proxy_span.trace_id) + if conf.http_response_header_for_traceid then + kong.response.add_header(conf.http_response_header_for_traceid, proxy_span.trace_id) end end diff --git a/kong/plugins/zipkin/schema.lua b/kong/plugins/zipkin/schema.lua index d612bb4575aa..dba47198356c 100644 --- a/kong/plugins/zipkin/schema.lua +++ b/kong/plugins/zipkin/schema.lua @@ -65,7 +65,7 @@ return { { connect_timeout = typedefs.timeout { default = 2000 } }, { send_timeout = typedefs.timeout { default = 5000 } }, { read_timeout = typedefs.timeout { default = 5000 } }, - { response_header_for_traceid = { type = "string", default = nil }}, + { http_response_header_for_traceid = { type = "string", default = nil }}, }, }, }, }, diff --git a/spec/03-plugins/34-zipkin/zipkin_spec.lua b/spec/03-plugins/34-zipkin/zipkin_spec.lua index 7cfc695f03a8..10eb9ab98ec8 100644 --- a/spec/03-plugins/34-zipkin/zipkin_spec.lua +++ b/spec/03-plugins/34-zipkin/zipkin_spec.lua @@ -424,7 +424,7 @@ for _, strategy in helpers.each_strategy() do end for _, strategy in helpers.each_strategy() do - describe("response_header_for_traceid configuration", function() + describe("http_response_header_for_traceid configuration", function() local proxy_client, service setup(function() @@ -450,7 +450,7 @@ for _, strategy in helpers.each_strategy() do http_endpoint = fmt("http://%s:%d/api/v2/spans", ZIPKIN_HOST, ZIPKIN_PORT), default_header_type = "b3-single", http_span_name = "method_path", - response_header_for_traceid = "X-B3-TraceId", + http_response_header_for_traceid = "X-B3-TraceId", } })