Skip to content

Commit

Permalink
fix(otel): fix otel sampling mode lua panic bug when http_response_he…
Browse files Browse the repository at this point in the history
…ader_for_traceid option enable (#12544)

* fix(otel): fix otel sampling mode lua panic bug when http_response_header_for_traceid option enable

* fix(otel): fix code

* fix(otel): fix code

* fix(otel): fix code

---------

Co-authored-by: Hans Hübner <[email protected]>
  • Loading branch information
oowl and hanshuebner authored Feb 12, 2024
1 parent bc9e00b commit d48c63d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "**Opentelemetry**: fix otel sampling mode lua panic bug when http_response_header_for_traceid option enable"
type: bugfix
scope: Plugin
6 changes: 4 additions & 2 deletions kong/plugins/opentelemetry/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ function OpenTelemetryHandler:header_filter(conf)
local root_span = ngx.ctx.KONG_SPANS and ngx.ctx.KONG_SPANS[1]
trace_id = root_span and root_span.trace_id
end
trace_id = to_hex(trace_id)
kong.response.add_header(conf.http_response_header_for_traceid, trace_id)
if trace_id then
trace_id = to_hex(trace_id)
kong.response.add_header(conf.http_response_header_for_traceid, trace_id)
end
end
end

Expand Down
28 changes: 28 additions & 0 deletions spec/03-plugins/37-opentelemetry/05-otelcol_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,34 @@ for _, strategy in helpers.each_strategy() do
return #parts > 0
end, 10)
end)

it("send traces with config http_response_header_for_traceid enable and tracing_sampling_rate option", function()
assert(helpers.restart_kong {
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
plugins = "opentelemetry",
tracing_instrumentations = "all",
tracing_sampling_rate = 0.00005,
})

proxy_url = fmt("http://%s:%s", helpers.get_proxy_ip(), helpers.get_proxy_port())
proxy_url_enable_traceid = fmt("http://%s:%s/enable_response_header_traceid", helpers.get_proxy_ip(), helpers.get_proxy_port())

local httpc = http.new()
for i = 1, 100 do
local res, err = httpc:request_uri(proxy_url_enable_traceid)
assert.is_nil(err)
assert.same(200, res.status)
if res.headers["x-trace-id"] then
local trace_id = res.headers["x-trace-id"]
local trace_id_regex = [[^[a-f0-9]{32}$]]
local m = ngx.re.match(trace_id, trace_id_regex, "jo")
assert.True(m ~= nil, "trace_id does not match regex: " .. trace_id_regex)
end
end
httpc:close()
end)

end)

end)
Expand Down

0 comments on commit d48c63d

Please sign in to comment.