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

feat: support log with traceid when enable the zipkin plugin #10210

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 11 additions & 1 deletion apisix/core/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ local function update_log_level()
end
end

local function get_trace_id()
if ngx_get_phase() ~= "init" and ngx.config.subsystem == "http" then
local trace_id = ngx.ctx.trace_id
if trace_id then
return trace_id .. " "
end
end
return ''
end

function _M.new(prefix)
local m = {version = _M.version}
Expand Down Expand Up @@ -91,6 +100,7 @@ function _M.new(prefix)
end



setmetatable(_M, {__index = function(self, cmd)
local log_level = log_levels[cmd]
local method
Expand All @@ -101,7 +111,7 @@ setmetatable(_M, {__index = function(self, cmd)
method = do_nothing
else
method = function(...)
return ngx_log(log_level, ...)
return ngx_log(log_level, get_trace_id(), ...)
end
end

Expand Down
5 changes: 5 additions & 0 deletions apisix/plugins/zipkin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local ngx = ngx
local ngx_re = require("ngx.re")
local pairs = pairs
local tonumber = tonumber
local to_hex = require "resty.string".to_hex

local plugin_name = "zipkin"
local ZIPKIN_SPAN_VER_1 = 1
Expand Down Expand Up @@ -219,6 +220,10 @@ function _M.rewrite(plugin_conf, ctx)
ctx.opentracing.proxy_span = request_span:start_child_span("apisix.proxy",
start_timestamp)
end
-- To set the traceId for the purpose of conveniently retrieving it when logging with core.log,
-- it will be automatically injected into the text.
local trace_id = request_span:context().trace_id
ngx.ctx.trace_id = to_hex(trace_id)
end

function _M.access(conf, ctx)
Expand Down
Loading