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

fix(zipkin): don't cache the per-req sample ratio #3522

Merged
merged 1 commit into from
Feb 6, 2021
Merged
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
42 changes: 21 additions & 21 deletions apisix/plugins/zipkin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,6 @@ end


local function create_tracer(conf,ctx)

local headers = core.request.headers(ctx)

-- X-B3-Sampled: if the client decided to sample this request, we do too.
local sample = headers["x-b3-sampled"]
if sample == "1" or sample == "true" then
conf.sample_ratio = 1
elseif sample == "0" or sample == "false" then
conf.sample_ratio = 0
end

-- X-B3-Flags: if it equals '1' then it overrides sampling policy
-- We still want to warn on invalid sample header, so do this after the above
local debug = headers["x-b3-flags"]
if debug == "1" then
conf.sample_ratio = 1
end

conf.route_id = ctx.route_id
local reporter = new_reporter(conf)
reporter:init_processor()
Expand All @@ -100,10 +82,28 @@ function _M.rewrite(plugin_conf, ctx)
conf.server_addr = ctx.var["server_addr"]
end

local tracer = core.lrucache.plugin_ctx(lrucache, ctx, conf.server_addr,
create_tracer, conf, ctx)
local tracer = core.lrucache.plugin_ctx(lrucache, ctx, conf.server_addr .. conf.server_port,
create_tracer, conf, ctx)

local headers = core.request.headers(ctx)
local per_req_sample_ratio

-- X-B3-Sampled: if the client decided to sample this request, we do too.
local sample = headers["x-b3-sampled"]
if sample == "1" or sample == "true" then
per_req_sample_ratio = 1
elseif sample == "0" or sample == "false" then
per_req_sample_ratio = 0
end

-- X-B3-Flags: if it equals '1' then it overrides sampling policy
-- We still want to warn on invalid sample header, so do this after the above
local debug = headers["x-b3-flags"]
if debug == "1" then
per_req_sample_ratio = 1
end

ctx.opentracing_sample = tracer.sampler:sample()
ctx.opentracing_sample = tracer.sampler:sample(per_req_sample_ratio or conf.sample_ratio)
if not ctx.opentracing_sample then
core.request.set_header("x-b3-sampled", "0")
return
Expand Down
10 changes: 4 additions & 6 deletions apisix/plugins/zipkin/random_sampler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ local _M = {}
local mt = { __index = _M }

function _M.new(conf)
local sample_ratio = conf.sample_ratio
assert(type(sample_ratio) == "number" and
sample_ratio >= 0 and sample_ratio <= 1, "invalid sample_ratio")
return setmetatable({
sample_ratio = sample_ratio
}, mt)
end

function _M.sample(self)
return math.random() < self.sample_ratio
function _M.sample(self, sample_ratio)
assert(type(sample_ratio) == "number" and
sample_ratio >= 0 and sample_ratio <= 1, "invalid sample_ratio")
return math.random() < sample_ratio
end


Expand Down
43 changes: 43 additions & 0 deletions t/plugin/zipkin.t
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,46 @@ GET /echo
x-b3-sampled: true
--- response_headers
x-b3-sampled: 1



=== TEST 23: don't cache the per-req sample ratio
--- config
location /t {
content_by_lua_block {
local http = require "resty.http"
local httpc = http.new()
local uri = "http://127.0.0.1:" .. ngx.var.server_port
.. "/echo"
-- force to trace
local res, err = httpc:request_uri(uri, {
method = "GET",
headers = {
['x-b3-sampled'] = 1
}
})
if not res then
ngx.say(err)
return
end
ngx.say(res.headers['x-b3-sampled'])

-- force not to trace
local res, err = httpc:request_uri(uri, {
method = "GET",
headers = {
['x-b3-sampled'] = 0
}
})
if not res then
ngx.say(err)
return
end
ngx.say(res.headers['x-b3-sampled'])
}
}
--- request
GET /t
--- response_body
1
0