diff --git a/COPYRIGHT b/COPYRIGHT index 98408cde0b6b..ad8a171d9dd2 100644 --- a/COPYRIGHT +++ b/COPYRIGHT @@ -239,3 +239,44 @@ Redistributions in binary form must reproduce the above copyright notice, this l THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +%%%%%%%%% + +opentracing-openresty + +https://github.com/iresty/opentracing-openresty + +Apache License 2 + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +%%%%%%%%% + +kong-plugin-zipkin + +https://github.com/Kong/kong-plugin-zipkin + +Copyright 2018-2019 Kong Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +%%%%%%%%% diff --git a/conf/config.yaml b/conf/config.yaml index 8821017a918e..c860d9347b12 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -19,8 +19,9 @@ plugins: # plugin list - example-plugin - limit-req - limit-count + - limit-conn - key-auth - prometheus - - limit-conn - node-status - jwt-auth + - zipkin diff --git a/doc/images/plugin/zipkin-1.jpg b/doc/images/plugin/zipkin-1.jpg new file mode 100644 index 000000000000..935146bacd33 Binary files /dev/null and b/doc/images/plugin/zipkin-1.jpg differ diff --git a/doc/images/plugin/zipkin-2.jpg b/doc/images/plugin/zipkin-2.jpg new file mode 100644 index 000000000000..291cd4b9b05a Binary files /dev/null and b/doc/images/plugin/zipkin-2.jpg differ diff --git a/doc/plugins/zipkin.md b/doc/plugins/zipkin.md new file mode 100644 index 000000000000..a4b7e31362a2 --- /dev/null +++ b/doc/plugins/zipkin.md @@ -0,0 +1,95 @@ +# Summary +- [**Name**](#name) +- [**Attributes**](#attributes) +- [**How To Enable**](#how-to-enable) +- [**Test Plugin**](#test-plugin) +- [**Disable Plugin**](#disable-plugin) + + +## Name + +[Zipkin](https://github.com/openzipkin/zipkin) is a OpenTracing plugin. + +It's also works with `Apache SkyWalking`, which is support Zipkin v1/v2 format. + +## Attributes + +* `endpoint`: the http endpoint of Ziplin, for example: `http://127.0.0.1:9411/api/v2/spans`. + +* `sample_ratio`: the ratio of sample, the minimum is 0.00001, the maximum is 1. + +## How To Enable + +Here's an example, enable the zipkin plugin on the specified route: + +```shell +curl http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -d ' +{ + "methods": ["GET"], + "uri": "/index.html", + "plugins": { + "zipkin": { + "endpoint": "http://127.0.0.1:9411/api/v2/spans", + "sample_ratio": 1 + } + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "39.97.63.215:80": 1 + } + } +}' +``` + +## Test Plugin + +### run the Zipkin instance + +e.g. using docker: + +``` +sudo docker run -d -p 9411:9411 openzipkin/zipkin +``` + +Here is a test example: + +```shell +$ curl http://127.0.0.1:9080/index.html +HTTP/1.1 200 OK +... +``` + +Then you can use a browser to access the webUI of Zipkin: + +``` +http://127.0.0.1:9411/zipkin +``` + +![](../../doc/images/plugin/zipkin-1.png) + +![](../../doc/images/plugin/zipkin-2.png) + +## Disable Plugin + +When you want to disable the zipkin plugin, it is very simple, + you can delete the corresponding json configuration in the plugin configuration, + no need to restart the service, it will take effect immediately: + +```shell +$ curl http://127.0.0.1:2379/v2/keys/apisix/routes/1 -X PUT -d value=' +{ + "methods": ["GET"], + "uri": "/index.html", + "plugins": { + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "39.97.63.215:80": 1 + } + } +}' +``` + +The zipkin plugin has been disabled now. It works for other plugins. diff --git a/lua/apisix/core/request.lua b/lua/apisix/core/request.lua index cfaac65e5784..9ca6c541ab2b 100644 --- a/lua/apisix/core/request.lua +++ b/lua/apisix/core/request.lua @@ -2,12 +2,15 @@ local ngx = ngx local get_headers = ngx.req.get_headers - +local tonumber = tonumber local _M = {version = 0.1} local function _headers(ctx) + if not ctx then + ctx = ngx.ctx.api_ctx + end local headers = ctx.headers if not headers then headers = get_headers() @@ -20,6 +23,9 @@ _M.headers = _headers function _M.header(ctx, name) + if not ctx then + ctx = ngx.ctx.api_ctx + end return _headers(ctx)[name] end @@ -28,6 +34,9 @@ end -- so if there is a load balancer between downstream client and APISIX, -- this function will return the ip of load balancer. function _M.get_ip(ctx) + if not ctx then + ctx = ngx.ctx.api_ctx + end return ctx.var.realip_remote_addr or ctx.var.remote_addr or '' end @@ -35,8 +44,19 @@ end -- get remote address of downstream client, -- in cases there is a load balancer between downstream client and APISIX. function _M.get_remote_client_ip(ctx) + if not ctx then + ctx = ngx.ctx.api_ctx + end return ctx.var.remote_addr or '' end +function _M.get_remote_client_port(ctx) + if not ctx then + ctx = ngx.ctx.api_ctx + end + return tonumber(ctx.var.remote_port) + end + + return _M diff --git a/lua/apisix/core/response.lua b/lua/apisix/core/response.lua index 24db61781ca8..ed61ea39c91c 100644 --- a/lua/apisix/core/response.lua +++ b/lua/apisix/core/response.lua @@ -10,7 +10,8 @@ local type = type local ngx_exit = ngx.exit local insert_tab = table.insert local concat_tab = table.concat - +local str_sub = string.sub +local tonumber = tonumber local _M = {version = 0.1} @@ -78,4 +79,9 @@ function _M.set_header(...) end +function _M.get_upstream_status(ctx) + -- $upstream_status maybe including mutiple status, only need the last one + return tonumber(str_sub(ctx.var.upstream_status or "", -3)) +end + return _M diff --git a/lua/apisix/plugins/zipkin.lua b/lua/apisix/plugins/zipkin.lua new file mode 100644 index 000000000000..8e8f706e15a4 --- /dev/null +++ b/lua/apisix/plugins/zipkin.lua @@ -0,0 +1,165 @@ +local core = require("apisix.core") +local new_tracer = require("opentracing.tracer").new +local zipkin_codec = require("apisix.plugins.zipkin.codec") +local new_random_sampler = require("apisix.plugins.zipkin.random_sampler").new +local new_reporter = require("apisix.plugins.zipkin.reporter").new +local ngx = ngx +local pairs = pairs + +local plugin_name = "zipkin" + + +-- You can follow this document to write schema: +-- https://github.com/Tencent/rapidjson/blob/master/bin/draft-04/schema +-- rapidjson not supported `format` in draft-04 yet +local schema = { + type = "object", + properties = { + endpoint = {type = "string"}, + sample_ratio = {type = "number", minimum = 0.00001, maximum = 1} + }, + required = {"endpoint", "sample_ratio"} +} + + +local _M = { + version = 0.1, + priority = -1000, -- last running plugin + name = plugin_name, + schema = schema, +} + + +function _M.check_schema(conf) + return core.schema.check(schema, conf) +end + + +local function create_tracer(conf) + local tracer = new_tracer(new_reporter(conf), new_random_sampler(conf)) + tracer:register_injector("http_headers", zipkin_codec.new_injector()) + tracer:register_extractor("http_headers", zipkin_codec.new_extractor()) + return tracer +end + +local function report2endpoint(premature, reporter) + if premature then + return + end + + local ok, err = reporter:flush() + if not ok then + core.log.error("reporter flush ", err) + return + end + + core.log.info("report2endpoint ok") +end + + +function _M.rewrite(conf, ctx) + local tracer = core.lrucache.plugin_ctx(plugin_name, ctx, + create_tracer, conf) + + ctx.opentracing_sample = tracer.sampler:sample() + if not ctx.opentracing_sample then + return + end + + local wire_context = tracer:extract("http_headers", + core.request.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.method, + ["http.url"] = ctx.var.request_uri, + -- TODO: support ipv6 + ["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, + 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) + ctx.REWRITE_END_TIME = tracer:time() + ctx.opentracing.rewrite_span:finish(ctx.REWRITE_END_TIME) +end + +function _M.access(conf, ctx) + if not ctx.opentracing_sample then + return + end + + local opentracing = ctx.opentracing + + 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) + + opentracing.proxy_span = opentracing.request_span:start_child_span( + "apisix.proxy", ctx.ACCESS_END_TIME) + + -- send headers to upstream + local outgoing_headers = {} + tracer:inject(opentracing.proxy_span, "http_headers", outgoing_headers) + for k, v in pairs(outgoing_headers) do + core.response.set_header(k, v) + end +end + + +function _M.header_filter(conf, ctx) + if not ctx.opentracing_sample then + return + end + + local opentracing = ctx.opentracing + + ctx.HEADER_FILTER_END_TIME = opentracing.tracer:time() + opentracing.body_filter_span = opentracing.proxy_span:start_child_span( + "apisix.body_filter", ctx.HEADER_FILTER_END_TIME) +end + + +function _M.log(conf, ctx) + if not ctx.opentracing_sample then + return + end + + local opentracing = ctx.opentracing + + local log_end_time = opentracing.tracer:time() + opentracing.body_filter_span:finish(log_end_time) + + local upstream_status = core.response.get_upstream_status(ctx) + opentracing.request_span:set_tag("http.status_code", upstream_status) + opentracing.proxy_span:finish(log_end_time) + opentracing.request_span:finish(log_end_time) + + local reporter = opentracing.tracer.reporter + local ok, err = ngx.timer.at(0, report2endpoint, reporter) + if not ok then + core.log.error("failed to create timer: ", err) + end +end + +return _M diff --git a/lua/apisix/plugins/zipkin/codec.lua b/lua/apisix/plugins/zipkin/codec.lua new file mode 100644 index 000000000000..7a083235f10a --- /dev/null +++ b/lua/apisix/plugins/zipkin/codec.lua @@ -0,0 +1,106 @@ +local core = require("apisix.core") +local to_hex = require "resty.string".to_hex +local new_span_context = require("opentracing.span_context").new + +local function hex_to_char(c) + return string.char(tonumber(c, 16)) +end + +local function from_hex(str) + if str ~= nil then -- allow nil to pass through + str = str:gsub("%x%x", hex_to_char) + end + return str +end + +local function new_extractor() + return function(headers) +-- X-B3-Sampled: if an upstream decided to sample this request, we do too. + local sample = headers["x-b3-sampled"] + if sample == "1" or sample == "true" then + sample = true + elseif sample == "0" or sample == "false" then + sample = false + elseif sample ~= nil then + core.log.warn("x-b3-sampled header invalid; ignoring.") + sample = nil + end + +-- X-B3-Flags: if it equals '1' then it overrides sampling policy +-- We still want to warn on invalid sample header, so do this after the above + local debug = headers["x-b3-flags"] + if debug == "1" then + sample = true + elseif debug ~= nil then + core.log.warn("x-b3-flags header invalid; ignoring.") + end + + local had_invalid_id = false + + local trace_id = headers["x-b3-traceid"] + -- Validate trace id + if trace_id and + ((#trace_id ~= 16 and #trace_id ~= 32) or trace_id:match("%X")) then + core.log.warn("x-b3-traceid header invalid; ignoring.") + had_invalid_id = true + end + + local parent_span_id = headers["x-b3-parentspanid"] + -- Validate parent_span_id + if parent_span_id and + (#parent_span_id ~= 16 or parent_span_id:match("%X")) then + core.log.warn("x-b3-parentspanid header invalid; ignoring.") + had_invalid_id = true + end + + local request_span_id = headers["x-b3-spanid"] + -- Validate request_span_id + if request_span_id and + (#request_span_id ~= 16 or request_span_id:match("%X")) then + core.log.warn("x-b3-spanid header invalid; ignoring.") + had_invalid_id = true + end + + if trace_id == nil or had_invalid_id then + return nil + end + + -- Process jaegar baggage header + local baggage = {} + for k, v in pairs(headers) do + local baggage_key = k:match("^uberctx%-(.*)$") + if baggage_key then + baggage[baggage_key] = ngx.unescape_uri(v) + end + end + + trace_id = from_hex(trace_id) + parent_span_id = from_hex(parent_span_id) + request_span_id = from_hex(request_span_id) + + return new_span_context(trace_id, request_span_id, parent_span_id, + sample, baggage) + end +end + +local function new_injector() + return function(span_context, headers) + -- We want to remove headers if already present + headers["x-b3-traceid"] = to_hex(span_context.trace_id) + headers["x-b3-parentspanid"] = span_context.parent_id + and to_hex(span_context.parent_id) or nil + headers["x-b3-spanid"] = to_hex(span_context.span_id) + local flags = core.request.header(nil, "x-b3-flags") + headers["x-b3-flags"] = flags + headers["x-b3-sampled"] = (not flags) + for key, value in span_context:each_baggage_item() do + -- XXX: https://github.com/opentracing/specification/issues/117 + headers["uberctx-"..key] = ngx.escape_uri(value) + end + end +end + +return { + new_extractor = new_extractor, + new_injector = new_injector, +} diff --git a/lua/apisix/plugins/zipkin/random_sampler.lua b/lua/apisix/plugins/zipkin/random_sampler.lua new file mode 100644 index 000000000000..afd1bfe36e38 --- /dev/null +++ b/lua/apisix/plugins/zipkin/random_sampler.lua @@ -0,0 +1,18 @@ +local _M = {} +local mt = { __index = _M } + +function _M.new(conf) + local sample_ratio = conf.sample_ratio + assert(type(sample_ratio) == "number" and + sample_ratio >= 0 and sample_ratio <= 1, "invalid sample_ratio") + return setmetatable({ + sample_ratio = sample_ratio + }, mt) +end + +function _M.sample(self) + return math.random() < self.sample_ratio +end + + +return _M diff --git a/lua/apisix/plugins/zipkin/reporter.lua b/lua/apisix/plugins/zipkin/reporter.lua new file mode 100644 index 000000000000..e1416694e080 --- /dev/null +++ b/lua/apisix/plugins/zipkin/reporter.lua @@ -0,0 +1,123 @@ +local resty_http = require "resty.http" +local to_hex = require "resty.string".to_hex +local cjson = require "cjson".new() +cjson.encode_number_precision(16) + + +local _M = {} +local mt = { __index = _M } + + +local span_kind_map = { + client = "CLIENT", + server = "SERVER", + producer = "PRODUCER", + consumer = "CONSUMER", +} + + +function _M.new(conf) + local endpoint = conf.endpoint + assert(type(endpoint) == "string", "invalid http endpoint") + return setmetatable({ + endpoint = endpoint, + pending_spans = {}, + pending_spans_n = 0, + }, mt) +end + + +function _M.report(self, span) + local span_context = span:context() + + local zipkin_tags = {} + for k, v in span:each_tag() do + -- Zipkin tag values should be strings + zipkin_tags[k] = tostring(v) + end + + local span_kind = zipkin_tags["span.kind"] + zipkin_tags["span.kind"] = nil + + local localEndpoint do + local serviceName = zipkin_tags["peer.service"] + if serviceName then + zipkin_tags["peer.service"] = nil + localEndpoint = { + serviceName = serviceName, + -- TODO: ip/port from ngx.var.server_name/ngx.var.server_port? + } + else + -- needs to be null, not the empty object + localEndpoint = cjson.null + end + end + + local remoteEndpoint do + local peer_port = span:get_tag "peer.port" -- get as number + if peer_port then + zipkin_tags["peer.port"] = nil + remoteEndpoint = { + ipv4 = zipkin_tags["peer.ipv4"], + -- ipv6 = zipkin_tags["peer.ipv6"], + port = peer_port, -- port is *not* optional + } + zipkin_tags["peer.ipv4"] = nil + zipkin_tags["peer.ipv6"] = nil + else + remoteEndpoint = cjson.null + end + end + + local zipkin_span = { + traceId = to_hex(span_context.trace_id), + name = span.name, + parentId = span_context.parent_id and + to_hex(span_context.parent_id) or nil, + id = to_hex(span_context.span_id), + kind = span_kind_map[span_kind], + timestamp = span.timestamp * 1000000, + duration = math.floor(span.duration * 1000000), -- zipkin wants integer + -- TODO: debug? + localEndpoint = localEndpoint, + remoteEndpoint = remoteEndpoint, + tags = zipkin_tags, + annotations = span.logs + } + + local i = self.pending_spans_n + 1 + self.pending_spans[i] = zipkin_span + self.pending_spans_n = i +end + +function _M.flush(self) + if self.pending_spans_n == 0 then + + return true + end + + local pending_spans = cjson.encode(self.pending_spans) + self.pending_spans = {} + self.pending_spans_n = 0 + + local httpc = resty_http.new() + local res, err = httpc:request_uri(self.endpoint, { + method = "POST", + headers = { + ["content-type"] = "application/json", + }, + body = pending_spans, + }) + + -- TODO: on failure, retry? + if not res then + return nil, "failed to request: " .. err + elseif res.status < 200 or res.status >= 300 then + return nil, "failed: " .. res.status .. " " .. res.reason + end + + return true +end + + +return _M diff --git a/rockspec/apisix-0.5-0.rockspec b/rockspec/apisix-0.5-0.rockspec index aac25e9c700e..20f8bf4c63fd 100644 --- a/rockspec/apisix-0.5-0.rockspec +++ b/rockspec/apisix-0.5-0.rockspec @@ -22,6 +22,10 @@ dependencies = { "lua-resty-ngxvar = 0.3", "lua-resty-jit-uuid = 0.0.7", "rapidjson = 0.6.0-1", + "lua-resty-healthcheck-iresty = 1.0.0", + "lua-resty-jwt = 0.2.0", + "lua-resty-cookie = 0.1.0", + "opentracing-openresty = 0.1", } build = { diff --git a/rockspec/apisix-dev-1.0-0.rockspec b/rockspec/apisix-dev-1.0-0.rockspec index b5c18ee4d4fc..7123c71892bf 100644 --- a/rockspec/apisix-dev-1.0-0.rockspec +++ b/rockspec/apisix-dev-1.0-0.rockspec @@ -27,6 +27,7 @@ dependencies = { "lua-resty-cookie", "lua-resty-session", "lua-resty-openidc", + "opentracing-openresty", } build = { diff --git a/t/admin/plugins.t b/t/admin/plugins.t index 158bba6ddb9f..68ac848d3a57 100644 --- a/t/admin/plugins.t +++ b/t/admin/plugins.t @@ -14,6 +14,6 @@ __DATA__ --- request GET /apisix/admin/plugins/list --- response_body_like eval -qr/example-plugin","limit-req","limit-count","key-auth","prometheus","limit-conn","node-status"/ +qr/\["example-plugin","limit-req","limit-count","limit-conn","key-auth","prometheus","node-status","jwt-auth","zipkin"\]/ --- no_error_log [error] diff --git a/t/lib/server.lua b/t/lib/server.lua index a536f904eabb..53e01bec6c87 100644 --- a/t/lib/server.lua +++ b/t/lib/server.lua @@ -1,3 +1,5 @@ +local json_decode = require("cjson").decode + local _M = {} @@ -31,6 +33,30 @@ function _M.sleep1() end +function _M.opentracing() + ngx.say("opentracing") +end + + +function _M.mock_zipkin() + ngx.req.read_body() + local data = ngx.req.get_body_data() + local spans = json_decode(data) + if #spans < 5 then + ngx.exit(400) + end + + for _, span in pairs(spans) do + if string.sub(span.name, 1, 6) ~= 'apisix' then + ngx.exit(400) + end + if not span.traceId then + ngx.exit(400) + end + end +end + + function _M.go() local action = string.sub(ngx.var.uri, 2) if not _M[action] then diff --git a/t/plugin/zipkin.t b/t/plugin/zipkin.t new file mode 100644 index 000000000000..5c4c67c7842f --- /dev/null +++ b/t/plugin/zipkin.t @@ -0,0 +1,302 @@ +use t::APISix 'no_plan'; + +repeat_each(2); +no_long_string(); +no_root_location(); +log_level("info"); +run_tests; + +__DATA__ + +=== TEST 1: sanity +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.zipkin") + local ok, err = plugin.check_schema({endpoint = 'http://127.0.0.1', sample_ratio = 0.001}) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- request +GET /t +--- response_body +done +--- no_error_log +[error] + + + +=== TEST 2: default value of sample_ratio +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.zipkin") + local ok, err = plugin.check_schema({endpoint = 'http://127.0.0.1'}) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- request +GET /t +--- response_body +done +--- no_error_log +[error] +--- SKIP + + + +=== TEST 3: wrong value of ratio +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.zipkin") + local ok, err = plugin.check_schema({endpoint = 'http://127.0.0.1', sample_ratio = -0.1}) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- request +GET /t +--- response_body +invalid "minimum" in docuement at pointer "#/sample_ratio" +done +--- no_error_log +[error] + + + +=== TEST 4: wrong value of ratio +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.zipkin") + local ok, err = plugin.check_schema({endpoint = 'http://127.0.0.1', sample_ratio = 2}) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- request +GET /t +--- response_body +invalid "maximum" in docuement at pointer "#/sample_ratio" +done +--- no_error_log +[error] + + + +=== TEST 5: add plugin +--- 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:1982/mock_zipkin", + "sample_ratio": 1 + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/opentracing" + }]], + [[{ + "node": { + "value": { + "plugins": { + "zipkin": { + "endpoint": "http://127.0.0.1:1982/mock_zipkin", + "sample_ratio": 1 + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/opentracing" + }, + "key": "/apisix/routes/1" + }, + "action": "set" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 6: tiger zipkin +--- request +GET /opentracing +--- response_body +opentracing +--- grep_error_log eval +qr/\[info\].*/ +--- grep_error_log_out eval +qr{report2endpoint ok} + + + +=== TEST 7: change sample ratio +--- 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:1982/mock_zipkin", + "sample_ratio": 0.00001 + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/opentracing" + }]], + [[{ + "node": { + "value": { + "plugins": { + "zipkin": { + "endpoint": "http://127.0.0.1:1982/mock_zipkin", + "sample_ratio": 0.00001 + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/opentracing" + }, + "key": "/apisix/routes/1" + }, + "action": "set" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 8: not tiger zipkin +--- request +GET /opentracing +--- response_body +opentracing +--- no_error_log +report2endpoint ok + + + +=== TEST 9: disabled +--- 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": { + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/opentracing" + }]], + [[{ + "node": { + "value": { + "plugins": { + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/opentracing" + }, + "key": "/apisix/routes/1" + }, + "action": "set" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 10: not tiger zipkin +--- request +GET /opentracing +--- response_body +opentracing +--- no_error_log +report2endpoint ok