From 7818afef69808068accd678399cb9798e03baf3f Mon Sep 17 00:00:00 2001 From: kurt Date: Tue, 3 Sep 2024 17:14:09 +0800 Subject: [PATCH] fix(opentelemetry): tostring `header_type` avoid concatenation nil error in log (#10137) The require of header_type is false, so the consumer can choose `header_type` to be nil in Kong Manager; if this is, Kong Manager would pass `json.null` to Kong, `header_type` could be nil in this case. This PR respects this behavior and handles concatenation nil error when `header_type` is nil. Note that this is a fix that only exists in the 3.4 series. Fix: [FTI-6119](https://konghq.atlassian.net/browse/FTI-6119) Signed-off-by: tzssangglass --- .../fix-opentelemetry-header-type-is-null.yml | 3 ++ kong/tracing/propagation.lua | 2 +- .../37-opentelemetry/03-propagation_spec.lua | 28 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/kong-ee/fix-opentelemetry-header-type-is-null.yml diff --git a/changelog/unreleased/kong-ee/fix-opentelemetry-header-type-is-null.yml b/changelog/unreleased/kong-ee/fix-opentelemetry-header-type-is-null.yml new file mode 100644 index 000000000000..1f4ee9a85b4f --- /dev/null +++ b/changelog/unreleased/kong-ee/fix-opentelemetry-header-type-is-null.yml @@ -0,0 +1,3 @@ +message: "**OpenTelemetry**: Fixed an issue where header_type being nil caused a concatenation error." +type: bugfix +scope: Plugin diff --git a/kong/tracing/propagation.lua b/kong/tracing/propagation.lua index bf47ec442221..9c25e65ce00d 100644 --- a/kong/tracing/propagation.lua +++ b/kong/tracing/propagation.lua @@ -583,7 +583,7 @@ local function set(conf_header_type, found_header_type, proxy_span, conf_default found_header_type ~= nil and conf_header_type ~= found_header_type then - kong.log.warn("Mismatched header types. conf: " .. conf_header_type .. ". found: " .. found_header_type) + kong.log.warn("Mismatched header types. conf: " .. tostring(conf_header_type) .. ". found: " .. found_header_type) end found_header_type = found_header_type or conf_default_header_type or "b3" diff --git a/spec/03-plugins/37-opentelemetry/03-propagation_spec.lua b/spec/03-plugins/37-opentelemetry/03-propagation_spec.lua index daf0a6ee2d84..df4b2d17cf38 100644 --- a/spec/03-plugins/37-opentelemetry/03-propagation_spec.lua +++ b/spec/03-plugins/37-opentelemetry/03-propagation_spec.lua @@ -71,6 +71,11 @@ describe("propagation tests #" .. strategy, function() service = service, }) + local null_header_type_route = bp.routes:insert({ + hosts = { "null-header-type" }, + service = service, + }) + bp.plugins:insert({ name = "opentelemetry", route = {id = bp.routes:insert({ @@ -123,6 +128,15 @@ describe("propagation tests #" .. strategy, function() } }) + bp.plugins:insert({ + name = "opentelemetry", + route = null_header_type_route, + config = { + endpoint = "http://localhost:8080/v1/traces", + header_type = require("cjson").null, + } + }) + helpers.start_kong({ database = strategy, plugins = "bundled, trace-propagator", @@ -258,6 +272,20 @@ describe("propagation tests #" .. strategy, function() assert.not_matches("00%-" .. trace_id .. "%-%x+-01", json.headers.traceparent) end) + it("null_header_type", function() + local trace_id = gen_trace_id() + local r = proxy_client:get("/", { + headers = { + ["x-b3-sampled"] = "1", + ["x-b3-traceid"] = trace_id, + host = "null-header-type", + }, + }) + local body = assert.response(r).has.status(200) + local json = cjson.decode(body) + assert.is_nil(json.headers.traceparent) + end) + it("propagates w3c tracing headers when header_type set to w3c", function() local trace_id = gen_trace_id() local parent_id = gen_span_id()