From d1fe72c0a0f3c37c7a7a6156b239452b9570d4e0 Mon Sep 17 00:00:00 2001 From: seven dickens <41516298+dickens7@users.noreply.github.com> Date: Wed, 16 Sep 2020 16:20:24 +0800 Subject: [PATCH] bugfix: fixed wrong zipkin Arguments (#2193) fix #2191 --- apisix/plugins/zipkin.lua | 22 ++++++++++++++++++++-- apisix/plugins/zipkin/codec.lua | 22 +--------------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/apisix/plugins/zipkin.lua b/apisix/plugins/zipkin.lua index eebf07e0..50d3728e 100644 --- a/apisix/plugins/zipkin.lua +++ b/apisix/plugins/zipkin.lua @@ -59,7 +59,25 @@ function _M.check_schema(conf) end -local function create_tracer(conf) +local function create_tracer(conf,ctx) + + local headers = core.request.headers(ctx) + +-- X-B3-Sampled: if an upstream 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 + local tracer = new_tracer(new_reporter(conf), new_random_sampler(conf)) tracer:register_injector("http_headers", zipkin_codec.new_injector()) tracer:register_extractor("http_headers", zipkin_codec.new_extractor()) @@ -91,7 +109,7 @@ function _M.rewrite(plugin_conf, ctx) end local tracer = core.lrucache.plugin_ctx(plugin_name .. '#' .. conf.server_addr, ctx, - create_tracer, conf) + create_tracer, conf, ctx) ctx.opentracing_sample = tracer.sampler:sample() if not ctx.opentracing_sample then diff --git a/apisix/plugins/zipkin/codec.lua b/apisix/plugins/zipkin/codec.lua index a6a27397..f1dad870 100644 --- a/apisix/plugins/zipkin/codec.lua +++ b/apisix/plugins/zipkin/codec.lua @@ -35,26 +35,6 @@ end local function new_extractor() return function(headers) --- X-B3-Sampled: if an upstream decided to sample this request, we do too. - local sample = headers["x-b3-sampled"] - if sample == "1" or sample == "true" then - sample = true - elseif sample == "0" or sample == "false" then - sample = false - elseif sample ~= nil then - core.log.warn("x-b3-sampled header invalid; ignoring.") - sample = nil - 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 - sample = true - elseif debug ~= nil then - core.log.warn("x-b3-flags header invalid; ignoring.") - end - local had_invalid_id = false local trace_id = headers["x-b3-traceid"] @@ -99,7 +79,7 @@ local function new_extractor() request_span_id = from_hex(request_span_id) return new_span_context(trace_id, request_span_id, parent_span_id, - sample, baggage) + baggage) end end