From 3b2fd4471b826b528f36a4536418a1a3333982fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Mon, 6 Sep 2021 16:58:45 +0200 Subject: [PATCH] feat: ignore existing trace (#125) Co-authored-by: Yoan Blanc --- kong/plugins/zipkin/handler.lua | 3 ++- kong/plugins/zipkin/schema.lua | 2 +- kong/plugins/zipkin/tracing_headers.lua | 9 ++++++++- spec/tracing_headers_spec.lua | 7 +++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/kong/plugins/zipkin/handler.lua b/kong/plugins/zipkin/handler.lua index fe15156f899d..d4ecbef5a997 100644 --- a/kong/plugins/zipkin/handler.lua +++ b/kong/plugins/zipkin/handler.lua @@ -114,7 +114,8 @@ if subsystem == "http" then local req_headers = req.get_headers() local header_type, trace_id, span_id, parent_id, should_sample, baggage = - tracing_headers.parse(req_headers) + tracing_headers.parse(req_headers, conf.header_type) + local method = req.get_method() if should_sample == nil then diff --git a/kong/plugins/zipkin/schema.lua b/kong/plugins/zipkin/schema.lua index 5f91a9be6688..335efd282518 100644 --- a/kong/plugins/zipkin/schema.lua +++ b/kong/plugins/zipkin/schema.lua @@ -54,7 +54,7 @@ return { { 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", - one_of = { "preserve", "b3", "b3-single", "w3c", "jaeger", "ot" } } }, + one_of = { "preserve", "ignore", "b3", "b3-single", "w3c", "jaeger", "ot" } } }, { default_header_type = { type = "string", required = true, default = "b3", one_of = { "b3", "b3-single", "w3c", "jaeger", "ot" } } }, { tags_header = { type = "string", required = true, default = "Zipkin-Tags" } }, diff --git a/kong/plugins/zipkin/tracing_headers.lua b/kong/plugins/zipkin/tracing_headers.lua index 6a29bf221b23..89c834469572 100644 --- a/kong/plugins/zipkin/tracing_headers.lua +++ b/kong/plugins/zipkin/tracing_headers.lua @@ -364,7 +364,11 @@ local function find_header_type(headers) end -local function parse(headers) +local function parse(headers, conf_header_type) + if conf_header_type == "ignore" then + return nil + end + -- Check for B3 headers first local header_type, composed_header = find_header_type(headers) local trace_id, span_id, parent_id, should_sample @@ -401,7 +405,10 @@ end local function set(conf_header_type, found_header_type, proxy_span, conf_default_header_type) local set_header = kong.service.request.set_header + -- If conf_header_type is set to `preserve`, found_header_type is used over default_header_type; + -- if conf_header_type is set to `ignore`, found_header_type is not set, thus default_header_type is used. if conf_header_type ~= "preserve" and + conf_header_type ~= "ignore" and found_header_type ~= nil and conf_header_type ~= found_header_type then diff --git a/spec/tracing_headers_spec.lua b/spec/tracing_headers_spec.lua index c5b322464d60..a96d0e50409b 100644 --- a/spec/tracing_headers_spec.lua +++ b/spec/tracing_headers_spec.lua @@ -47,6 +47,13 @@ describe("tracing_headers.parse", function() warn:revert() end) + it("does not parse headers with ignore type", function() + local b3 = fmt("%s-%s-%s-%s", trace_id, span_id, "1", parent_id) + local t = { parse({ tracestate = "b3=" .. b3 }, "ignore") } + assert.spy(warn).not_called() + assert.same({}, t) + end) + it("1-char", function() local t = { parse({ b3 = "1" }) } assert.same({ "b3-single", nil, nil, nil, true }, t)