Skip to content

Commit

Permalink
fix(zipkin): send trace IDs with a reject sampling decision (#7833)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed Nov 10, 2022
1 parent 55082f6 commit 4bb4acf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apisix/plugins/zipkin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ function _M.rewrite(plugin_conf, ctx)
local b3 = headers["b3"]
if b3 then
-- don't pass b3 header by default
-- TODO: add an option like 'single_b3_header' so we can adapt to the upstream
-- which doesn't support b3 header without always breaking down the header
core.request.set_header(ctx, "b3", nil)

local err
Expand Down Expand Up @@ -158,6 +160,12 @@ function _M.rewrite(plugin_conf, ctx)
ctx.opentracing_sample = tracer.sampler:sample(per_req_sample_ratio or conf.sample_ratio)
if not ctx.opentracing_sample then
core.request.set_header(ctx, "x-b3-sampled", "0")
-- pass the trace ids even the sample is rejected
-- see https://github.com/openzipkin/b3-propagation#why-send-
-- trace-ids-with-a-reject-sampling-decision
core.request.set_header(ctx, "x-b3-traceid", trace_id)
core.request.set_header(ctx, "x-b3-parentspanid", parent_span_id)
core.request.set_header(ctx, "x-b3-spanid", request_span_id)
return
end

Expand Down
7 changes: 7 additions & 0 deletions t/plugin/zipkin2.t
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ passed
b3: 80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1-05e3ac9a4f6e3b90
--- response_headers
x-b3-sampled: 1
x-b3-traceid: 80f198ee56343ba864fe8b2a57d3eff7
--- raw_response_headers_unlike
b3:
--- error_log
Expand All @@ -124,6 +125,9 @@ invalid b3 header
b3: 80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-0-05e3ac9a4f6e3b90
--- response_headers
x-b3-sampled: 0
x-b3-traceid: 80f198ee56343ba864fe8b2a57d3eff7
x-b3-parentspanid: 05e3ac9a4f6e3b90
x-b3-spanid: e457b5a2e4d86bd1
Expand All @@ -132,6 +136,9 @@ x-b3-sampled: 0
b3: 0
--- response_headers
x-b3-sampled: 0
x-b3-traceid:
x-b3-parentspanid:
x-b3-spanid:
Expand Down

0 comments on commit 4bb4acf

Please sign in to comment.