Skip to content

Commit bb70ec0

Browse files
perf(zipkin): skip span tag construction for unsampled requests (#13656)
1 parent a2d34cd commit bb70ec0

4 files changed

Lines changed: 48 additions & 20 deletions

File tree

apisix/plugins/zipkin.lua

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,34 @@ function _M.rewrite(plugin_conf, ctx)
182182

183183
local wire_context = tracer:extract("http_headers", ctx)
184184

185-
local start_timestamp = ngx.req.start_time()
186-
local request_span = tracer:start_span("apisix.request", {
187-
child_of = wire_context,
188-
start_timestamp = start_timestamp,
185+
-- Decide sampling before building the span. The reporter drops unsampled
186+
-- spans, so skip tag construction and remote-addr lookups for them.
187+
if sampled == "1" or sampled == "true" then
188+
per_req_sample_ratio = 1
189+
elseif sampled == "0" or sampled == "false" then
190+
per_req_sample_ratio = 0
191+
end
192+
local sample = tracer.sampler:sample(per_req_sample_ratio or conf.sample_ratio)
193+
ctx.opentracing_sample = sample
194+
195+
local tags
196+
if sample then
189197
tags = {
190198
component = "apisix",
191199
["span.kind"] = "server",
192200
["http.method"] = ctx.var.request_method,
193201
["http.url"] = ctx.var.request_uri,
194-
-- TODO: support ipv6
202+
-- TODO: support ipv6
195203
["peer.ipv4"] = core.request.get_remote_client_ip(ctx),
196204
["peer.port"] = core.request.get_remote_client_port(ctx),
197205
}
206+
end
207+
208+
local start_timestamp = ngx.req.start_time()
209+
local request_span = tracer:start_span("apisix.request", {
210+
child_of = wire_context,
211+
start_timestamp = start_timestamp,
212+
tags = tags,
198213
})
199214

200215
ctx.opentracing = {
@@ -203,18 +218,10 @@ function _M.rewrite(plugin_conf, ctx)
203218
request_span = request_span,
204219
}
205220

206-
-- Process sampled
207-
if sampled == "1" or sampled == "true" then
208-
per_req_sample_ratio = 1
209-
elseif sampled == "0" or sampled == "false" then
210-
per_req_sample_ratio = 0
211-
end
212-
213-
ctx.opentracing_sample = tracer.sampler:sample(per_req_sample_ratio or conf.sample_ratio)
214-
if not ctx.opentracing_sample then
215-
request_span:set_baggage_item("x-b3-sampled","0")
221+
if not sample then
222+
request_span:set_baggage_item("x-b3-sampled", "0")
216223
else
217-
request_span:set_baggage_item("x-b3-sampled","1")
224+
request_span:set_baggage_item("x-b3-sampled", "1")
218225
end
219226

220227
if plugin_info.set_ngx_var then
@@ -227,11 +234,10 @@ function _M.rewrite(plugin_conf, ctx)
227234
ngx_var.zipkin_span_id = to_hex(span_context.span_id)
228235
end
229236

230-
if not ctx.opentracing_sample then
237+
if not sample then
231238
return
232239
end
233240

234-
local request_span = ctx.opentracing.request_span
235241
if conf.span_version == ZIPKIN_SPAN_VER_1 then
236242
ctx.opentracing.rewrite_span = request_span:start_child_span("apisix.rewrite",
237243
start_timestamp)
@@ -245,9 +251,9 @@ end
245251

246252
function _M.access(conf, ctx)
247253
local opentracing = ctx.opentracing
248-
local tracer = opentracing.tracer
249254

250-
if conf.span_version == ZIPKIN_SPAN_VER_1 then
255+
if ctx.opentracing_sample and conf.span_version == ZIPKIN_SPAN_VER_1 then
256+
local tracer = opentracing.tracer
251257
opentracing.access_span = opentracing.request_span:start_child_span(
252258
"apisix.access", ctx.REWRITE_END_TIME)
253259

docs/en/latest/plugins/skywalking.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ Reload APISIX for changes to take effect.
6363
|--------------|--------|----------|---------|--------------|----------------------------------------------------------------------------|
6464
| sample_ratio | number | False | 1 | [0.00001, 1] | Frequency of request sampling. Setting the sample ratio to `1` means to sample all requests. |
6565

66+
:::tip Performance
67+
68+
The plugin hooks several request phases and builds a trace per sampled request, so tracing adds per-request overhead. Sampling is the primary lever to control it: unsampled requests skip trace creation entirely. Lower `sample_ratio` on high-throughput routes to reduce overhead while keeping representative traces.
69+
70+
:::
71+
6672
## Example
6773

6874
To follow along the example, start a storage, OAP and Booster UI with Docker Compose, following [Skywalking's documentation](https://skywalking.apache.org/docs/main/next/en/setup/backend/backend-docker/). Once set up, the OAP server should be listening on `12800` and you should be able to access the UI at [http://localhost:8080](http://localhost:8080).

docs/en/latest/plugins/zipkin.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ See the configuration file for configuration options available to all Plugins.
6363
|server_addr | string | False |the value of `$server_addr` | IPv4 address | IPv4 address for the Zipkin reporter. For example, you can set this to your external IP address. |
6464
|span_version | integer | False | 2 | [1, 2] | Version of the span type. |
6565

66+
:::tip Performance
67+
68+
The plugin hooks several request phases and builds a span per sampled request, so tracing adds per-request overhead. Sampling is the primary lever to control it: `sample_ratio` is the fraction of requests traced, and unsampled requests skip span tag construction entirely. Lower `sample_ratio` on high-throughput routes to reduce overhead while keeping representative traces.
69+
70+
:::
71+
6672
## Examples
6773

6874
The examples below show different use cases of the `zipkin` Plugin.

t/plugin/zipkin3.t

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,13 @@ plugin_attr:
159159
GET /echo
160160
--- error_log
161161
ngx_var.zipkin_context_traceparent is empty
162+
163+
164+
165+
=== TEST 6: unsampled request still builds span context for set_ngx_var
166+
--- request
167+
GET /echo
168+
--- more_headers
169+
x-b3-sampled: 0
170+
--- error_log eval
171+
qr/ngx_var.zipkin_context_traceparent:00-\w{32}-\w{16}-00/

0 commit comments

Comments
 (0)