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/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+/ diff --git a/t/plugin/prometheus3.t b/t/plugin/prometheus3.t index 0d2db21d509a..bb57041b7042 100644 --- a/t/plugin/prometheus3.t +++ b/t/plugin/prometheus3.t @@ -203,3 +203,64 @@ 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) + } + } +--- response_body +passed + + + +=== TEST 6: test prometheus plugin at both global rule and route +--- request +GET /opentracing +--- response_body +opentracing + + + +=== 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\"} 1/