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

chore(plugin): convert traceid in http response headers to hex format #10534

Merged
merged 5 commits into from Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
[#9746](https://github.com/Kong/kong/pull/9746)
- **Proxy-Cache**: add `ignore_uri_case` to configuring cache-key uri to be handled as lowercase
[#10453](https://github.com/Kong/kong/pull/10453)
- **Zipkin&Opentelemetry**: convert traceid in http response headers to hex format
[#10534](https://github.com/Kong/kong/pull/10534)

#### PDK

Expand Down
2 changes: 2 additions & 0 deletions kong/plugins/opentelemetry/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local null = ngx.null
local encode_traces = otlp.encode_traces
local translate_span_trace_id = otlp.translate_span
local encode_span = otlp.transform_span
local to_hex = require "resty.string".to_hex

local _log_prefix = "[otel] "

Expand Down Expand Up @@ -158,6 +159,7 @@ 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
local trace_id = to_hex(trace_id)
This conversation was marked as resolved.
Show resolved Hide resolved
kong.response.add_header(conf.http_response_header_for_traceid, trace_id)
end
end
Expand Down
4 changes: 3 additions & 1 deletion kong/plugins/zipkin/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local split = ngx_re.split
local subsystem = ngx.config.subsystem
local fmt = string.format
local rand_bytes = utils.get_rand_bytes
local to_hex = require "resty.string".to_hex

local ZipkinLogHandler = {
VERSION = kong_meta.version,
Expand Down Expand Up @@ -221,7 +222,8 @@ if subsystem == "http" then
end

if conf.http_response_header_for_traceid then
kong.response.add_header(conf.http_response_header_for_traceid, proxy_span.trace_id)
local trace_id = to_hex(proxy_span.trace_id)
kong.response.add_header(conf.http_response_header_for_traceid, trace_id)
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/03-plugins/34-zipkin/zipkin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ for _, strategy in helpers.each_strategy() do

assert.response(r).has.status(200)
assert.response(r).has.header("X-B3-TraceId")
local trace_id = r.headers["X-B3-TraceId"]
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)
end
Expand Down
4 changes: 4 additions & 0 deletions spec/03-plugins/37-opentelemetry/05-otelcol_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ for _, strategy in helpers.each_strategy() do
assert.is_nil(err)
assert.same(200, res.status)
assert.not_nil(res.headers["x-trace-id"])
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
httpc:close()
end)
Expand Down