Skip to content

Commit

Permalink
fix(opentelemetry): fix a bug that will cause a failure of sending tr…
Browse files Browse the repository at this point in the history
…acing data to datadog agent (#11599)

* 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

* add changelog
  • Loading branch information
liverpool8056 committed Sep 20, 2023
1 parent 7e9e88f commit e0d53a8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
7 changes: 7 additions & 0 deletions 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"
28 changes: 19 additions & 9 deletions kong/plugins/opentelemetry/otlp.lua
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
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)

1 comment on commit e0d53a8

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:e0d53a8993f215a02351a2dfe3884ca3ed884f76
Artifacts available https://github.com/Kong/kong/actions/runs/6251017690

Please sign in to comment.