From b329231c97bb1bd7f2fe71ab0c10b6836ccfd4c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Fri, 2 Sep 2022 13:51:49 +0800 Subject: [PATCH] fix(zipkin): send trace IDs with a reject sampling decision (#7833) --- apisix/plugins/zipkin.lua | 8 ++++++++ t/plugin/zipkin2.t | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/apisix/plugins/zipkin.lua b/apisix/plugins/zipkin.lua index 3fafd29f0e2a..ab284cefe9fa 100644 --- a/apisix/plugins/zipkin.lua +++ b/apisix/plugins/zipkin.lua @@ -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 @@ -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 diff --git a/t/plugin/zipkin2.t b/t/plugin/zipkin2.t index 3175075d32ed..8423f6f67d63 100644 --- a/t/plugin/zipkin2.t +++ b/t/plugin/zipkin2.t @@ -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 @@ -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 @@ -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: