This is an automated email from the ASF dual-hosted git repository. membphis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push: new 116deb2 change(zipkin): rearrange the child span (#3877) 116deb2 is described below commit 116deb2f367f3ee68e3e237b9d334b916fd8287a Author: 罗泽轩 <spacewander...@gmail.com> AuthorDate: Tue Mar 23 20:19:16 2021 +0800 change(zipkin): rearrange the child span (#3877) Fix #3827 --- apisix/plugins/zipkin.lua | 68 ++++++++++++++++++++++++++-------------- docs/en/latest/plugins/zipkin.md | 23 ++++++++++++++ docs/zh/latest/plugins/zipkin.md | 23 ++++++++++++++ t/lib/server.lua | 32 ++++++++++++++++--- t/plugin/zipkin.t | 12 +++---- t/plugin/zipkin2.t | 46 +++++++++++++++++++++++++++ 6 files changed, 169 insertions(+), 35 deletions(-) diff --git a/apisix/plugins/zipkin.lua b/apisix/plugins/zipkin.lua index 5872f34..2d74f33 100644 --- a/apisix/plugins/zipkin.lua +++ b/apisix/plugins/zipkin.lua @@ -25,6 +25,8 @@ local pairs = pairs local tonumber = tonumber local plugin_name = "zipkin" +local ZIPKIN_SPAN_VER_1 = 1 +local ZIPKIN_SPAN_VER_2 = 2 local lrucache = core.lrucache.new({ @@ -46,6 +48,10 @@ local schema = { description = "default is $server_addr, you can specify your external ip address", pattern = "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$" }, + span_version = { + enum = {ZIPKIN_SPAN_VER_1, ZIPKIN_SPAN_VER_2}, + default = ZIPKIN_SPAN_VER_2, + }, }, required = {"endpoint", "sample_ratio"} } @@ -182,17 +188,19 @@ function _M.rewrite(plugin_conf, ctx) tracer = tracer, wire_context = wire_context, request_span = request_span, - rewrite_span = nil, - access_span = nil, - proxy_span = nil, } local request_span = ctx.opentracing.request_span - ctx.opentracing.rewrite_span = request_span:start_child_span( - "apisix.rewrite", start_timestamp) + 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) + 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 function _M.access(conf, ctx) @@ -201,17 +209,18 @@ function _M.access(conf, ctx) end local opentracing = ctx.opentracing + local tracer = opentracing.tracer - opentracing.access_span = opentracing.request_span:start_child_span( + if conf.span_version == ZIPKIN_SPAN_VER_1 then + opentracing.access_span = opentracing.request_span:start_child_span( "apisix.access", ctx.REWRITE_END_TIME) - local tracer = opentracing.tracer - - ctx.ACCESS_END_TIME = tracer:time() - opentracing.access_span:finish(ctx.ACCESS_END_TIME) + ctx.ACCESS_END_TIME = tracer:time() + opentracing.access_span:finish(ctx.ACCESS_END_TIME) - opentracing.proxy_span = opentracing.request_span:start_child_span( - "apisix.proxy", ctx.ACCESS_END_TIME) + opentracing.proxy_span = opentracing.request_span:start_child_span( + "apisix.proxy", ctx.ACCESS_END_TIME) + end -- send headers to upstream local outgoing_headers = {} @@ -228,11 +237,18 @@ function _M.header_filter(conf, ctx) end local opentracing = ctx.opentracing + local end_time = opentracing.tracer:time() - ctx.HEADER_FILTER_END_TIME = opentracing.tracer:time() - if opentracing.proxy_span then - opentracing.body_filter_span = opentracing.proxy_span:start_child_span( - "apisix.body_filter", ctx.HEADER_FILTER_END_TIME) + if conf.span_version == ZIPKIN_SPAN_VER_1 then + ctx.HEADER_FILTER_END_TIME = end_time + if opentracing.proxy_span then + opentracing.body_filter_span = opentracing.proxy_span:start_child_span( + "apisix.body_filter", ctx.HEADER_FILTER_END_TIME) + end + else + opentracing.proxy_span:finish(end_time) + opentracing.response_span = opentracing.request_span:start_child_span( + "apisix.response_span", ctx.HEADER_FILTER_END_TIME) end end @@ -245,15 +261,21 @@ function _M.log(conf, ctx) local opentracing = ctx.opentracing local log_end_time = opentracing.tracer:time() - if opentracing.body_filter_span then - opentracing.body_filter_span:finish(log_end_time) + + if conf.span_version == ZIPKIN_SPAN_VER_1 then + if opentracing.body_filter_span then + opentracing.body_filter_span:finish(log_end_time) + end + if opentracing.proxy_span then + opentracing.proxy_span:finish(log_end_time) + end + + else + opentracing.response_span:finish(log_end_time) end local upstream_status = core.response.get_upstream_status(ctx) opentracing.request_span:set_tag("http.status_code", upstream_status) - if opentracing.proxy_span then - opentracing.proxy_span:finish(log_end_time) - end opentracing.request_span:finish(log_end_time) diff --git a/docs/en/latest/plugins/zipkin.md b/docs/en/latest/plugins/zipkin.md index f037de1..df72598 100644 --- a/docs/en/latest/plugins/zipkin.md +++ b/docs/en/latest/plugins/zipkin.md @@ -43,6 +43,29 @@ It's also works with `Apache SkyWalking`, which is support Zipkin v1/v2 format. | sample_ratio | number | required | | [0.00001, 1] | the ratio of sample | | service_name | string | optional | "APISIX" | | service name for zipkin reporter | | server_addr | string | optional | | | IPv4 address for zipkin reporter, default is nginx built-in variables $server_addr, here you can specify your external ip address. | +| span_version | integer| optional | 2 | [1, 2] | the version of span type | + +Currently each traced request will create spans below: + +``` +request +├── proxy: from the beginning of the request to the beginning of header filter +└── response: from the beginning of header filter to the beginning of log +``` + +Previously we created spans below: + +``` +request +├── rewrite +├── access +└── proxy + └── body_filter +``` + +Note: the name of span doesn't represent the corresponding Nginx's phase. + +If you need to be compatible with old style, we can set `span_version` to 1. ## How To Enable diff --git a/docs/zh/latest/plugins/zipkin.md b/docs/zh/latest/plugins/zipkin.md index 42ca9e3..608f5b4 100644 --- a/docs/zh/latest/plugins/zipkin.md +++ b/docs/zh/latest/plugins/zipkin.md @@ -43,6 +43,29 @@ title: zipkin | sample_ratio | number | 必须 | | [0.00001, 1] | 监听的比例 | | service_name | string | 可选 | "APISIX" | | 标记当前服务的名称 | | server_addr | string | 可选 | | | 标记当前 APISIX 实例的IP地址,默认值是 nginx 的内置变量`server_addr` | +| span_version | integer| 可选 | 2 | [1, 2] | span 类型版本 | + +目前每个被跟踪的请求会创建下面的 span: + +``` +request +├── proxy: from the beginning of the request to the beginning of header filter +└── response: from the beginning of header filter to the beginning of log +``` + +之前我们创建的 span 是这样: + +``` +request +├── rewrite +├── access +└── proxy + └── body_filter +``` + +注意上述的 span 的名称跟同名的 Nginx phase 没有关系。 + +如果你需要兼容过去的 span 类型,可以把 `span_version` 设置成 1。 ## 如何启用 diff --git a/t/lib/server.lua b/t/lib/server.lua index 924163d..4e96223 100644 --- a/t/lib/server.lua +++ b/t/lib/server.lua @@ -173,34 +173,56 @@ end function _M.mock_zipkin() ngx.req.read_body() local data = ngx.req.get_body_data() + ngx.log(ngx.NOTICE, data) + local spans = json_decode(data) - if #spans < 5 then - ngx.exit(400) + local ver = ngx.req.get_uri_args()['span_version'] + if ver == "1" then + if #spans ~= 5 then + ngx.log(ngx.ERR, "wrong number of spans: ", #spans) + ngx.exit(400) + end + else + if #spans ~= 3 then + -- request/proxy/response + ngx.log(ngx.ERR, "wrong number of spans: ", #spans) + ngx.exit(400) + end end for _, span in pairs(spans) do - if string.sub(span.name, 1, 6) ~= 'apisix' then + local prefix = string.sub(span.name, 1, 6) + if prefix ~= 'apisix' then + ngx.log(ngx.ERR, "wrong prefix of name", prefix) ngx.exit(400) end if not span.traceId then + ngx.log(ngx.ERR, "missing trace id") ngx.exit(400) end if not span.localEndpoint then + ngx.log(ngx.ERR, "missing local endpoint") ngx.exit(400) end if span.localEndpoint.serviceName ~= 'APISIX' and span.localEndpoint.serviceName ~= 'apisix' then + ngx.log(ngx.ERR, "wrong serviceName: ", span.localEndpoint.serviceName) ngx.exit(400) end if span.localEndpoint.port ~= 1984 then + ngx.log(ngx.ERR, "wrong port: ", span.localEndpoint.port) ngx.exit(400) end - if span.localEndpoint.ipv4 ~= ngx.req.get_uri_args()['server_addr'] then - ngx.exit(400) + local server_addr = ngx.req.get_uri_args()['server_addr'] + if server_addr then + if span.localEndpoint.ipv4 ~= server_addr then + ngx.log(ngx.ERR, "server_addr mismatched") + ngx.exit(400) + end end end diff --git a/t/plugin/zipkin.t b/t/plugin/zipkin.t index 89f6725..5465ac0 100644 --- a/t/plugin/zipkin.t +++ b/t/plugin/zipkin.t @@ -102,7 +102,7 @@ done [[{ "plugins": { "zipkin": { - "endpoint": "http://127.0.0.1:9999/mock_zipkin", + "endpoint": "http://127.0.0.1:1980/mock_zipkin?server_addr=127.0.0.1", "sample_ratio": 1, "service_name": "APISIX" } @@ -120,7 +120,7 @@ done "value": { "plugins": { "zipkin": { - "endpoint": "http://127.0.0.1:9999/mock_zipkin", + "endpoint": "http://127.0.0.1:1980/mock_zipkin?server_addr=127.0.0.1", "sample_ratio": 1, "service_name":"APISIX" } @@ -157,8 +157,7 @@ passed === TEST 5: tiger zipkin --- request GET /opentracing ---- error_log -report zipkin span failed +--- no_error_log [error] --- wait: 10 @@ -306,7 +305,7 @@ opentracing [[{ "plugins": { "zipkin": { - "endpoint": "http://127.0.0.1:9999/mock_zipkin", + "endpoint": "http://127.0.0.1:1980/mock_zipkin?server_addr=1.2.3.4", "sample_ratio": 1, "service_name": "apisix", "server_addr": "1.2.3.4" @@ -340,8 +339,7 @@ passed === TEST 11: tiger zipkin --- request GET /opentracing ---- error_log -report zipkin span failed +--- no_error_log [error] --- wait: 10 diff --git a/t/plugin/zipkin2.t b/t/plugin/zipkin2.t index b443dbe..7b0edef 100644 --- a/t/plugin/zipkin2.t +++ b/t/plugin/zipkin2.t @@ -140,3 +140,49 @@ b3: 80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1 x-b3-sampled: 1 --- error_log new span context: trace id: 80f198ee56343ba864fe8b2a57d3eff7, span id: e457b5a2e4d86bd1, parent span id: nil + + + +=== TEST 9: set plugin with span version 1 +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "zipkin": { + "endpoint": "http://127.0.0.1:1980/mock_zipkin?span_version=1", + "sample_ratio": 1, + "service_name": "apisix", + "span_version": 1 + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/opentracing" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- no_error_log +[error] + + + +=== TEST 10: tiger zipkin +--- request +GET /opentracing +--- wait: 10