Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(opentelemetry): fix a bug that will cause a failure of sending tracing data to datadog agent #11599

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG/unreleased/kong/11599.yaml
Original file line number Diff line number Diff line change
@@ -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"
28 changes: 19 additions & 9 deletions kong/plugins/opentelemetry/otlp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
47 changes: 47 additions & 0 deletions spec/03-plugins/37-opentelemetry/01-otlp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)