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 9b63fd5 feature: prometheus plugin `apisix_http_status` metric `route` tag Improve recognition (#2497) 9b63fd5 is described below commit 9b63fd5710427820a9002920f8b48cb43bf06cd8 Author: tzssangglass <tzssanggl...@gmail.com> AuthorDate: Wed Oct 28 13:22:25 2020 +0800 feature: prometheus plugin `apisix_http_status` metric `route` tag Improve recognition (#2497) fix #1574 --- apisix/http/router/radixtree_host_uri.lua | 8 ++- apisix/http/router/radixtree_uri.lua | 6 +- apisix/init.lua | 4 ++ apisix/plugins/prometheus/exporter.lua | 12 +++- rockspec/apisix-master-0.rockspec | 2 +- t/plugin/prometheus.t | 109 +++++++++++++++++++++++++++++- 6 files changed, 131 insertions(+), 10 deletions(-) diff --git a/apisix/http/router/radixtree_host_uri.lua b/apisix/http/router/radixtree_host_uri.lua index 25dec65..97bad34 100644 --- a/apisix/http/router/radixtree_host_uri.lua +++ b/apisix/http/router/radixtree_host_uri.lua @@ -61,9 +61,10 @@ local function push_host_router(route, host_routes, only_uri_routes) or route.value.remote_addr, vars = route.value.vars, filter_fun = filter_fun, - handler = function (api_ctx) + handler = function (api_ctx, match_opts) api_ctx.matched_params = nil api_ctx.matched_route = route + api_ctx.curr_req_matched = match_opts.matched end } @@ -130,16 +131,17 @@ function _M.match(api_ctx) match_opts.remote_addr = api_ctx.var.remote_addr match_opts.vars = api_ctx.var match_opts.host = api_ctx.var.host + match_opts.matched = core.tablepool.fetch("matched_route_record", 0, 4) if host_router then local host_uri = api_ctx.var.host - local ok = host_router:dispatch(host_uri:reverse(), match_opts, api_ctx) + local ok = host_router:dispatch(host_uri:reverse(), match_opts, api_ctx, match_opts) if ok then return true end end - local ok = only_uri_router:dispatch(api_ctx.var.uri, match_opts, api_ctx) + local ok = only_uri_router:dispatch(api_ctx.var.uri, match_opts, api_ctx, match_opts) return ok end diff --git a/apisix/http/router/radixtree_uri.lua b/apisix/http/router/radixtree_uri.lua index fe6dfbd..812b84c 100644 --- a/apisix/http/router/radixtree_uri.lua +++ b/apisix/http/router/radixtree_uri.lua @@ -62,9 +62,10 @@ local function create_radixtree_router(routes) or route.value.remote_addr, vars = route.value.vars, filter_fun = filter_fun, - handler = function (api_ctx) + handler = function (api_ctx, match_opts) api_ctx.matched_params = nil api_ctx.matched_route = route + api_ctx.curr_req_matched = match_opts.matched end }) @@ -94,8 +95,9 @@ function _M.match(api_ctx) match_opts.host = api_ctx.var.host match_opts.remote_addr = api_ctx.var.remote_addr match_opts.vars = api_ctx.var + match_opts.matched = core.tablepool.fetch("matched_route_record", 0, 4) - local ok = uri_router:dispatch(api_ctx.var.uri, match_opts, api_ctx) + local ok = uri_router:dispatch(api_ctx.var.uri, match_opts, api_ctx, match_opts) return ok end diff --git a/apisix/init.lua b/apisix/init.lua index 3a51f4b..7324438 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -692,6 +692,10 @@ function _M.http_log_phase() core.tablepool.release("plugins", api_ctx.plugins) end + if api_ctx.curr_req_matched then + core.tablepool.release("matched_route_record", api_ctx.curr_req_matched) + end + core.tablepool.release("api_ctx", api_ctx) end diff --git a/apisix/plugins/prometheus/exporter.lua b/apisix/plugins/prometheus/exporter.lua index 35eeeae..78c2af6 100644 --- a/apisix/plugins/prometheus/exporter.lua +++ b/apisix/plugins/prometheus/exporter.lua @@ -102,7 +102,7 @@ function _M.init() -- no consumer in request. metrics.status = prometheus:counter("http_status", "HTTP status codes per service in APISIX", - {"code", "route", "service", "consumer", "node"}) + {"code", "route", "matched_uri", "matched_host", "service", "consumer", "node"}) metrics.latency = prometheus:histogram("http_latency", "HTTP request latency in milliseconds per service in APISIX", @@ -136,8 +136,16 @@ function _M.log(conf, ctx) service_id = vars.host end + local matched_uri = "" + local matched_host = "" + if ctx.curr_req_matched then + matched_uri = ctx.curr_req_matched._path or "" + matched_host = ctx.curr_req_matched._host or "" + end + metrics.status:inc(1, - gen_arr(vars.status, route_id, service_id, consumer_id, balancer_ip)) + gen_arr(vars.status, route_id, matched_uri, matched_host, + service_id, consumer_id, balancer_ip)) local latency = (ngx.now() - ngx.req.start_time()) * 1000 metrics.latency:observe(latency, diff --git a/rockspec/apisix-master-0.rockspec b/rockspec/apisix-master-0.rockspec index d3685a5..7b7c16b 100644 --- a/rockspec/apisix-master-0.rockspec +++ b/rockspec/apisix-master-0.rockspec @@ -41,7 +41,7 @@ dependencies = { "lua-resty-cookie = 0.1.0", "lua-resty-session = 2.24", "opentracing-openresty = 0.1", - "lua-resty-radixtree = 2.2", + "lua-resty-radixtree = 2.4", "lua-protobuf = 0.3.1", "lua-resty-openidc = 1.7.2-1", "luafilesystem = 1.7.0-2", diff --git a/t/plugin/prometheus.t b/t/plugin/prometheus.t index 451e9c2..3bb73e1 100644 --- a/t/plugin/prometheus.t +++ b/t/plugin/prometheus.t @@ -521,7 +521,7 @@ passed --- request GET /apisix/prometheus/metrics --- response_body eval -qr/apisix_http_status\{code="404",route="3",service="",consumer="",node="127.0.0.1"\} 2/ +qr/apisix_http_status\{code="404",route="3",matched_uri="\/hello3",matched_host="",service="",consumer="",node="127.0.0.1"\} 2/ --- no_error_log [error] @@ -928,6 +928,111 @@ apikey: auth-one --- request GET /apisix/prometheus/metrics --- response_body eval -qr/apisix_http_status\{code="200",route="1",service="",consumer="jack",node="127.0.0.1"\} \d+/ +qr/apisix_http_status\{code="200",route="1",matched_uri="\/hello",matched_host="",service="",consumer="jack",node="127.0.0.1"\} \d+/ +--- no_error_log +[error] + + + +=== TEST 51: set route(id: 9) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/9', + ngx.HTTP_PUT, + [[{ + "methods": ["GET"], + "plugins": { + "prometheus": {} + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "hosts": ["foo.com", "bar.com"], + "uris": ["/foo*", "/bar*"] + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 52: 404 Route Not Found +--- request +GET /not_found +--- error_code: 404 +--- response_body +{"error_msg":"404 Route Not Found"} +--- no_error_log +[error] + + + +=== TEST 53: fetch the prometheus metric data: 404 Route Not Found +--- request +GET /apisix/prometheus/metrics +--- response_body eval +qr/apisix_http_status\{code="404",route="",matched_uri="",matched_host="",service="localhost",consumer="",node=""\} \d+/ +--- no_error_log +[error] + + + +=== TEST 54: hit routes(uri = "/foo*", host = "foo.com") +--- request +GET /foo1 +--- more_headers +Host: foo.com +--- error_code: 404 +--- response_body eval +qr/404 Not Found/ +--- no_error_log +[error] + + + +=== TEST 55: fetch the prometheus metric data: hit routes(uri = "/foo*", host = "foo.com") +--- request +GET /apisix/prometheus/metrics +--- response_body eval +qr/apisix_http_status\{code="404",route="9",matched_uri="\/foo\*",matched_host="foo.com",service="",consumer="",node="127.0.0.1"\} \d+/ +--- no_error_log +[error] + + + +=== TEST 56: hit routes(uri = "/bar*", host = "bar.com") +--- request +GET /bar1 +--- more_headers +Host: bar.com +--- error_code: 404 +--- response_body eval +qr/404 Not Found/ +--- no_error_log +[error] + + + +=== TEST 57: fetch the prometheus metric data: hit routes(uri = "/bar*", host = "bar.com") +--- request +GET /apisix/prometheus/metrics +--- response_body eval +qr/apisix_http_status\{code="404",route="9",matched_uri="\/bar\*",matched_host="bar.com",service="",consumer="",node="127.0.0.1"\} \d+/ --- no_error_log [error]