in apisix/plugins/zipkin.lua [127:244]
function _M.rewrite(plugin_conf, ctx)
local conf = core.table.clone(plugin_conf)
conf.server_port = tonumber(ctx.var['server_port'])
if not conf.server_addr or conf.server_addr == '' then
conf.server_addr = ctx.var["server_addr"]
end
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
local debug = headers["x-b3-flags"]
if debug == "1" then
per_req_sample_ratio = 1
end
local trace_id, request_span_id, sampled, parent_span_id
local b3 = headers["b3"]
if b3 then
core.request.set_header(ctx, "b3", nil)
local err
err, trace_id, request_span_id, sampled, parent_span_id = parse_b3(b3)
if err then
core.log.error("invalid b3 header: ", b3, ", ignored: ", err)
return 400
end
if sampled == "d" then
core.request.set_header(ctx, "x-b3-flags", "1")
sampled = "1"
end
else
sampled = headers["x-b3-sampled"]
trace_id = headers["x-b3-traceid"]
parent_span_id = headers["x-b3-parentspanid"]
request_span_id = headers["x-b3-spanid"]
end
local zipkin_ctx = core.tablepool.fetch("zipkin_ctx", 0, 3)
zipkin_ctx.trace_id = trace_id
zipkin_ctx.parent_span_id = parent_span_id
zipkin_ctx.request_span_id = request_span_id
ctx.zipkin = zipkin_ctx
local wire_context = tracer:extract("http_headers", ctx)
local start_timestamp = ngx.req.start_time()
local request_span = tracer:start_span("apisix.request", {
child_of = wire_context,
start_timestamp = start_timestamp,
tags = {
component = "apisix",
["span.kind"] = "server",
["http.method"] = ctx.var.request_method,
["http.url"] = ctx.var.request_uri,
["peer.ipv4"] = core.request.get_remote_client_ip(ctx),
["peer.port"] = core.request.get_remote_client_port(ctx),
}
})
ctx.opentracing = {
tracer = tracer,
wire_context = wire_context,
request_span = request_span,
}
if sampled == "1" or sampled == "true" then
per_req_sample_ratio = 1
elseif sampled == "0" or sampled == "false" then
per_req_sample_ratio = 0
end
ctx.opentracing_sample = tracer.sampler:sample(per_req_sample_ratio or conf.sample_ratio)
if not ctx.opentracing_sample then
request_span:set_baggage_item("x-b3-sampled","0")
else
request_span:set_baggage_item("x-b3-sampled","1")
end
if plugin_info.set_ngx_var then
local span_context = request_span:context()
ngx_var.zipkin_context_traceparent = string_format("00-%s-%s-%02x",
to_hex(span_context.trace_id),
to_hex(span_context.span_id),
span_context:get_baggage_item("x-b3-sampled"))
ngx_var.zipkin_trace_id = span_context.trace_id
ngx_var.zipkin_span_id = span_context.span_id
end
if not ctx.opentracing_sample then
return
end
local request_span = ctx.opentracing.request_span
if conf.span_version == ZIPKIN_SPAN_VER_1 then
ctx.opentracing.rewrite_span = request_span:start_child_span("apisix.rewrite",
start_timestamp)
ctx.REWRITE_END_TIME = tracer:time()
ctx.opentracing.rewrite_span:finish(ctx.REWRITE_END_TIME)
else
ctx.opentracing.proxy_span = request_span:start_child_span("apisix.proxy",
start_timestamp)
end
end