From 123d4c31a2ac3c9b3e8bd2845b13e84794142d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=85=E8=BF=9B=E8=B6=85?= Date: Fri, 11 Mar 2022 15:34:46 +0800 Subject: [PATCH 1/7] fix(prometheus): conflict between global rule and route configure --- apisix/plugins/prometheus.lua | 1 + apisix/plugins/prometheus/exporter.lua | 2 + t/plugin/prometheus3.t | 63 ++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/apisix/plugins/prometheus.lua b/apisix/plugins/prometheus.lua index 27c94f519acd..bce99625f1ac 100644 --- a/apisix/plugins/prometheus.lua +++ b/apisix/plugins/prometheus.lua @@ -39,6 +39,7 @@ local _M = { name = plugin_name, log = exporter.log, schema = schema, + run_policy = "prefer_route", } diff --git a/apisix/plugins/prometheus/exporter.lua b/apisix/plugins/prometheus/exporter.lua index 30534d567fd1..a2bdae62233e 100644 --- a/apisix/plugins/prometheus/exporter.lua +++ b/apisix/plugins/prometheus/exporter.lua @@ -171,6 +171,8 @@ function _M.log(conf, ctx) metrics.bandwidth:inc(vars.bytes_sent, gen_arr("egress", route_id, service_id, consumer_name, balancer_ip)) + + core.log.info("prometheus run report") end diff --git a/t/plugin/prometheus3.t b/t/plugin/prometheus3.t index 0d2db21d509a..e0196dc2afff 100644 --- a/t/plugin/prometheus3.t +++ b/t/plugin/prometheus3.t @@ -203,3 +203,66 @@ plugins: } --- response_body_like eval qr/apisix_batch_process_entries\{name="http logger",route_id="1",server_addr="127.0.0.1"\} \d+/ + + + +=== TEST 5: set prometheus plugin at both global rule and route +--- 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": { + "prometheus": {} + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/opentracing" + }]] + ) + if code >= 300 then + ngx.status = code + return + end + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/global_rules/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "prometheus": {} + } + }]] + ) + if code >= 300 then + ngx.status = code + return + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 6: test prometheus plugin at both global rule and route +--- request +GET /opentracing +--- response_body +opentracing +--- no_error_log +[error] +--- grep_error_log eval +qr/prometheus run \w+/ +--- grep_error_log_out +prometheus run report From 3670b1a6a21d5c550d6282e8372859d281810cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=85=E8=BF=9B=E8=B6=85?= Date: Fri, 11 Mar 2022 16:15:08 +0800 Subject: [PATCH 2/7] fix(prometheus): update test case --- t/plugin/prometheus3.t | 6 ------ 1 file changed, 6 deletions(-) diff --git a/t/plugin/prometheus3.t b/t/plugin/prometheus3.t index e0196dc2afff..551f86975468 100644 --- a/t/plugin/prometheus3.t +++ b/t/plugin/prometheus3.t @@ -246,12 +246,8 @@ qr/apisix_batch_process_entries\{name="http logger",route_id="1",server_addr="12 ngx.say(body) } } ---- request -GET /t --- response_body passed ---- no_error_log -[error] @@ -260,8 +256,6 @@ passed GET /opentracing --- response_body opentracing ---- no_error_log -[error] --- grep_error_log eval qr/prometheus run \w+/ --- grep_error_log_out From 1fec699ede31f87737c5cc970a140624dd5d4d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=85=E8=BF=9B=E8=B6=85?= Date: Fri, 11 Mar 2022 21:46:01 +0800 Subject: [PATCH 3/7] fix(prometheus): update verify case --- t/plugin/prometheus2.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/plugin/prometheus2.t b/t/plugin/prometheus2.t index 72400e7d7792..9626f5896404 100644 --- a/t/plugin/prometheus2.t +++ b/t/plugin/prometheus2.t @@ -924,4 +924,4 @@ GET /hello --- request GET /apisix/prometheus/metrics --- response_body eval -qr/apisix_bandwidth\{type="egress",route="1",service="1",consumer="",node="127.0.0.1"\} \d+/ +qr/apisix_bandwidth\{type="egress",route="1",service="service_name",consumer="",node="127.0.0.1"\} \d+/ From 88b1bb3970a40fe505fa2d1b24936afe338dcb63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=85=E8=BF=9B=E8=B6=85?= Date: Mon, 14 Mar 2022 10:50:52 +0800 Subject: [PATCH 4/7] fix(prometheus): update test case --- apisix/plugins/prometheus/exporter.lua | 2 -- t/plugin/prometheus3.t | 13 +++++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/apisix/plugins/prometheus/exporter.lua b/apisix/plugins/prometheus/exporter.lua index a2bdae62233e..30534d567fd1 100644 --- a/apisix/plugins/prometheus/exporter.lua +++ b/apisix/plugins/prometheus/exporter.lua @@ -171,8 +171,6 @@ function _M.log(conf, ctx) metrics.bandwidth:inc(vars.bytes_sent, gen_arr("egress", route_id, service_id, consumer_name, balancer_ip)) - - core.log.info("prometheus run report") end diff --git a/t/plugin/prometheus3.t b/t/plugin/prometheus3.t index 551f86975468..dfff067a44a9 100644 --- a/t/plugin/prometheus3.t +++ b/t/plugin/prometheus3.t @@ -256,7 +256,12 @@ passed GET /opentracing --- response_body opentracing ---- grep_error_log eval -qr/prometheus run \w+/ ---- grep_error_log_out -prometheus run report +--- error_code: 200 + + + +=== TEST 7: fetch prometheus plugin at both global rule and route data +--- request +GET /apisix/prometheus/metrics +--- response_body eval +qr/apisix_http_status\{code="200",route="1",matched_uri="\/opentracing",matched_host="",service="",consumer="",node="127.0.0.1\"} \d+/ From 8af7fd5bbdce2a04c92366755076b82ee3957e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=85=E8=BF=9B=E8=B6=85?= Date: Mon, 14 Mar 2022 11:05:29 +0800 Subject: [PATCH 5/7] fix(prometheus): update test case check value --- t/plugin/prometheus3.t | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/t/plugin/prometheus3.t b/t/plugin/prometheus3.t index dfff067a44a9..24fb75670dc6 100644 --- a/t/plugin/prometheus3.t +++ b/t/plugin/prometheus3.t @@ -94,6 +94,7 @@ __DATA__ } --- response_body eval "201passed\n" x 3 +--- SKIP @@ -102,6 +103,7 @@ __DATA__ ["GET /hello", "GET /apisix/prometheus/metrics"] --- error_code eval [200, 200] +--- SKIP @@ -167,6 +169,7 @@ __DATA__ } --- response_body passed +--- SKIP @@ -203,6 +206,7 @@ plugins: } --- response_body_like eval qr/apisix_batch_process_entries\{name="http logger",route_id="1",server_addr="127.0.0.1"\} \d+/ +--- SKIP @@ -264,4 +268,4 @@ opentracing --- request GET /apisix/prometheus/metrics --- response_body eval -qr/apisix_http_status\{code="200",route="1",matched_uri="\/opentracing",matched_host="",service="",consumer="",node="127.0.0.1\"} \d+/ +qr/apisix_http_status\{code="200",route="1",matched_uri="\/opentracing",matched_host="",service="",consumer="",node="127.0.0.1\"} 1/ From 854f77762ed4672d7795dfb11309d7e07a7787f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=85=E8=BF=9B=E8=B6=85?= Date: Mon, 14 Mar 2022 11:13:46 +0800 Subject: [PATCH 6/7] fix(prometheus): remove test case debug tag --- t/plugin/prometheus3.t | 4 ---- 1 file changed, 4 deletions(-) diff --git a/t/plugin/prometheus3.t b/t/plugin/prometheus3.t index 24fb75670dc6..cf4d95eaba85 100644 --- a/t/plugin/prometheus3.t +++ b/t/plugin/prometheus3.t @@ -94,7 +94,6 @@ __DATA__ } --- response_body eval "201passed\n" x 3 ---- SKIP @@ -103,7 +102,6 @@ __DATA__ ["GET /hello", "GET /apisix/prometheus/metrics"] --- error_code eval [200, 200] ---- SKIP @@ -169,7 +167,6 @@ __DATA__ } --- response_body passed ---- SKIP @@ -206,7 +203,6 @@ plugins: } --- response_body_like eval qr/apisix_batch_process_entries\{name="http logger",route_id="1",server_addr="127.0.0.1"\} \d+/ ---- SKIP From b95a927d7dac834b02959287abb89119565c970e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=85=E8=BF=9B=E8=B6=85?= Date: Mon, 14 Mar 2022 14:01:59 +0800 Subject: [PATCH 7/7] test(prometheus): remove redundant verify --- t/plugin/prometheus3.t | 1 - 1 file changed, 1 deletion(-) diff --git a/t/plugin/prometheus3.t b/t/plugin/prometheus3.t index cf4d95eaba85..bb57041b7042 100644 --- a/t/plugin/prometheus3.t +++ b/t/plugin/prometheus3.t @@ -256,7 +256,6 @@ passed GET /opentracing --- response_body opentracing ---- error_code: 200