From aa045d9f1047a8aa1bc976d8e38df8fae313ed00 Mon Sep 17 00:00:00 2001 From: Robin Xiang Date: Tue, 19 Sep 2023 11:08:02 +0800 Subject: [PATCH 1/2] fix(opentelemetry): fix a bug that will cause a failure of sending tracing data to datadog agent when the value of `x-datadog-parent-id` header from downstream is a short dec string. In this PR, a formatter is added for ensuring the field of `parent-span-id` meet specification of the otel protocol. FTI-5375 --- kong/plugins/opentelemetry/otlp.lua | 28 +++++++---- .../37-opentelemetry/01-otlp_spec.lua | 47 +++++++++++++++++++ 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/kong/plugins/opentelemetry/otlp.lua b/kong/plugins/opentelemetry/otlp.lua index 85f69a2ec85f..1941d73c3d52 100644 --- a/kong/plugins/opentelemetry/otlp.lua +++ b/kong/plugins/opentelemetry/otlp.lua @@ -13,6 +13,7 @@ local table_merge = utils.table_merge local setmetatable = setmetatable local TRACE_ID_LEN = 16 +local SPAN_ID_LEN = 8 local NULL = "\0" local POOL_OTLP = "KONG_OTLP" local EMPTY_TAB = {} @@ -74,17 +75,25 @@ local function transform_events(events) return pb_events end --- translate the trace_id to otlp format -local function to_ot_trace_id(trace_id) - local len = #trace_id - if len > TRACE_ID_LEN then - return trace_id:sub(-TRACE_ID_LEN) +local function id_formatter(length) + return function(id) + local len = #id + if len > length then + return id:sub(-length) - elseif len < TRACE_ID_LEN then - return NULL:rep(TRACE_ID_LEN - len) .. trace_id + elseif len < length then + return NULL:rep(length - len) .. id + end + + return id end +end - return trace_id +local to_ot_trace_id, to_ot_span_id +do + -- translate the trace_id and span_id to otlp format + to_ot_trace_id = id_formatter(TRACE_ID_LEN) + to_ot_span_id = id_formatter(SPAN_ID_LEN) end -- this function is to prepare span to be encoded and sent via grpc @@ -96,7 +105,8 @@ local function transform_span(span) trace_id = to_ot_trace_id(span.trace_id), span_id = span.span_id, -- trace_state = "", - parent_span_id = span.parent_id or "", + parent_span_id = span.parent_id and + to_ot_span_id(span.parent_id) or "", name = span.name, kind = span.kind or 0, start_time_unix_nano = span.start_time_ns, diff --git a/spec/03-plugins/37-opentelemetry/01-otlp_spec.lua b/spec/03-plugins/37-opentelemetry/01-otlp_spec.lua index c49abaf8ceff..eead16142b2e 100644 --- a/spec/03-plugins/37-opentelemetry/01-otlp_spec.lua +++ b/spec/03-plugins/37-opentelemetry/01-otlp_spec.lua @@ -119,4 +119,51 @@ describe("Plugin: opentelemetry (otlp)", function() end end) + it("check lengths of trace_id and span_id ", function () + local TRACE_ID_LEN, PARENT_SPAN_ID_LEN = 16, 8 + local default_span = { + name = "full-span", + trace_id = rand_bytes(16), + span_id = rand_bytes(8), + parent_id = rand_bytes(8), + start_time_ns = time_ns(), + end_time_ns = time_ns() + 1000, + should_sample = true, + attributes = { + foo = "bar", + test = true, + version = 0.1, + }, + events = { + { + name = "evt1", + time_ns = time_ns(), + attributes = { + debug = true, + } + } + }, + } + + local test_spans = {} + local span1 = utils.deep_copy(default_span) + local span2 = utils.deep_copy(default_span) + span1.trace_id = rand_bytes(17) + span1.expected_tid = span1.trace_id:sub(-TRACE_ID_LEN) + span1.parent_id = rand_bytes(9) + span1.expected_pid = span1.parent_id:sub(-PARENT_SPAN_ID_LEN) + span2.trace_id = rand_bytes(15) + span2.expected_tid = '\0' .. span2.trace_id + span2.parent_id = rand_bytes(7) + span2.expected_pid = '\0' .. span2.parent_id + insert(test_spans, span1) + insert(test_spans, span2) + + for _, span in ipairs(test_spans) do + local pb_span = otlp.transform_span(span) + assert(pb_span.parent_span_id == span.expected_pid) + assert(pb_span.trace_id == span.expected_tid) + end + end) + end) From 54be856a2a6a1fc815d79f9c8b74e9a5f38fc235 Mon Sep 17 00:00:00 2001 From: Robin Xiang Date: Tue, 19 Sep 2023 11:24:22 +0800 Subject: [PATCH 2/2] add changelog --- CHANGELOG/unreleased/kong/11599.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 CHANGELOG/unreleased/kong/11599.yaml diff --git a/CHANGELOG/unreleased/kong/11599.yaml b/CHANGELOG/unreleased/kong/11599.yaml new file mode 100644 index 000000000000..be4f4aadfd22 --- /dev/null +++ b/CHANGELOG/unreleased/kong/11599.yaml @@ -0,0 +1,7 @@ +message: Fix a bug that will cause a failure of sending tracing data to datadog when value of x-datadog-parent-id header in requests is a short dec string +type: bugfix +scope: Core +prs: + - 11599 +jiras: + - "FTI-5375"