Skip to content

Commit 67596e4

Browse files
authored
fix: trace id mismatch (#84)
Some 128-bit w3c trace IDs in the wild contain a zero-padded 64-bit portion. This commit sets the default high part of the trace ID to 0, ensuring the check passes correctly. Resolves #83 * add unit tests
1 parent a0632d4 commit 67596e4

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

kong/plugins/ddtrace/datadog_propagation.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ local function extract_datadog(get_header, max_header_size)
5656
end
5757

5858
-- other headers are expected but aren't provided in all cases
59-
local trace_id = { high = nil, low = trace_id_low }
6059
local parent_id = parse_uint64(get_header("x-datadog-parent-id"), 10)
6160
local sampling_priority = tonumber(get_header("x-datadog-sampling-priority"))
6261
local origin = get_header("x-datadog-origin")
@@ -74,24 +73,25 @@ local function extract_datadog(get_header, max_header_size)
7473
end
7574
end
7675

76+
local trace_id_high = 0
7777
local tid = dd_tags["_dd.p.tid"]
7878
if tid then
7979
if #tid ~= 16 then
8080
dd_tags["_dd.propagation_error"] = "malformed_tid " .. tid
8181
dd_tags["_dd.p.tid"] = nil
8282
else
83-
local trace_id_high, err = parse_uint64(tid, 16)
83+
local high, err = parse_uint64(tid, 16)
8484
if err then
8585
dd_tags["_dd.propagation_error"] = "malformed_tid " .. tid
8686
dd_tags["_dd.p.tid"] = nil
8787
else
88-
trace_id.high = trace_id_high
88+
trace_id_high = high
8989
end
9090
end
9191
end
9292

9393
return {
94-
trace_id = trace_id,
94+
trace_id = { high = trace_id_high, low = trace_id_low },
9595
parent_id = parent_id,
9696
sampling_priority = sampling_priority,
9797
origin = origin,

kong/plugins/ddtrace/w3c_propagation.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,15 @@ local function extract(get_header, _)
8181
return nil, "0 is an invalid parent ID"
8282
end
8383

84-
local trace_id = { high = 0, low = 0 }
84+
local high
85+
local low
8586

86-
trace_id.high, err = parse_uint64(string.sub(hex_trace_id, 1, 16), 16)
87+
high, err = parse_uint64(string.sub(hex_trace_id, 1, 16), 16)
8788
if err then
8889
return nil, "failed to parse trace ID: " .. err
8990
end
9091

91-
trace_id.low, err = parse_uint64(string.sub(hex_trace_id, 17, 32), 16)
92+
low, err = parse_uint64(string.sub(hex_trace_id, 17, 32), 16)
9293
if err then
9394
return nil, "failed to parse trace ID: " .. err
9495
end
@@ -137,7 +138,7 @@ local function extract(get_header, _)
137138
end
138139

139140
return {
140-
trace_id = trace_id,
141+
trace_id = { high = high, low = low },
141142
parent_id = parent_id,
142143
sampling_priority = sampling_priority,
143144
origin = dd_state["origin"],

spec/01-unit-tests/03_propagation_spec.lua

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
local new_span = require("kong.plugins.ddtrace.span").new
22
local new_propagator = require("kong.plugins.ddtrace.propagation").new
33

4+
local last_warning
45
_G.kong = {
56
log = {
6-
warn = function() end,
7+
warn = function(msg)
8+
last_warning = msg
9+
end,
710
},
811
}
912

@@ -76,11 +79,45 @@ describe("trace propagation", function()
7679

7780
local span = multi_propagator:extract_or_create_span(request, default_span_opts)
7881

79-
local expected_trace_id = { high = nil, low = 12345678901234567890ULL }
82+
local expected_trace_id = { high = 0, low = 12345678901234567890ULL }
8083
local expected_parent_id = 9876543210987654321ULL
8184
assert.same(expected_trace_id, span.trace_id)
8285
assert.same(expected_parent_id, span.parent_id)
8386
end)
87+
88+
it("64-bit and 128-bit match", function()
89+
local datadog_first = { "datadog", "tracecontext" }
90+
local multi_propagator = new_propagator(datadog_first, datadog_first, default_max_header_size)
91+
local request = {
92+
get_header = make_getter({
93+
traceparent = "00-0000000000000000a3ce929d0e0e4736-00f067aa0ba902b7-01",
94+
tracestate = "fizz=buzz:fizzbuzz,dd=s:2;o:rum;p:00f067aa0ba902b7;t.dm:-5",
95+
["x-datadog-trace-id"] = "11803532876627986230",
96+
["x-datadog-parent-id"] = "67667974448284343",
97+
}),
98+
}
99+
100+
last_warning = nil
101+
local _ = multi_propagator:extract_or_create_span(request, default_span_opts)
102+
assert.is_nil(last_warning)
103+
end)
104+
105+
it("mismatch trace ID log a warning", function()
106+
local datadog_first = { "datadog", "tracecontext" }
107+
local multi_propagator = new_propagator(datadog_first, datadog_first, default_max_header_size)
108+
local request = {
109+
get_header = make_getter({
110+
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
111+
tracestate = "fizz=buzz:fizzbuzz,dd=s:2;o:rum;p:00f067aa0ba902b7;t.dm:-5",
112+
["x-datadog-trace-id"] = "12345678901234567890",
113+
["x-datadog-parent-id"] = "9876543210987654321",
114+
}),
115+
}
116+
117+
last_warning = nil
118+
local _ = multi_propagator:extract_or_create_span(request, default_span_opts)
119+
assert.is_not_nil(last_warning)
120+
end)
84121
end)
85122

86123
describe("inject", function()

spec/01-unit-tests/06_datadog_propagation_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe("trace propagation", function()
4444
assert.is_nil(err)
4545
assert.is_not_nil(extracted)
4646

47-
local expected_trace_id = { high = nil, low = 12345678901234567890ULL }
47+
local expected_trace_id = { high = 0, low = 12345678901234567890ULL }
4848
assert.same(expected_trace_id, extracted.trace_id)
4949
assert.equal(9876543210987654321ULL, extracted.parent_id)
5050
assert.equal(1, extracted.sampling_priority)

0 commit comments

Comments
 (0)