You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by we...@apache.org on 2019/11/25 08:53:15 UTC

[incubator-apisix] branch master updated: change: used `lua-resty-prometheus` which the API is simpler. (#806)

This is an automated email from the ASF dual-hosted git repository.

wenming pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 670655d  change: used `lua-resty-prometheus` which the API is simpler. (#806)
670655d is described below

commit 670655d62df3b215e86ad1ea1f81abde2627580b
Author: YuanSheng Wang <me...@gmail.com>
AuthorDate: Mon Nov 25 16:53:08 2019 +0800

    change: used `lua-resty-prometheus` which the API is simpler. (#806)
---
 lua/apisix/plugins/prometheus/exporter.lua | 32 +++++++++++++-----------------
 rockspec/apisix-master-0.rockspec          |  2 +-
 2 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/lua/apisix/plugins/prometheus/exporter.lua b/lua/apisix/plugins/prometheus/exporter.lua
index 97dbcb4..6331d82 100644
--- a/lua/apisix/plugins/prometheus/exporter.lua
+++ b/lua/apisix/plugins/prometheus/exporter.lua
@@ -14,7 +14,7 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 --
-local base_prometheus = require("nginx.prometheus")
+local base_prometheus = require("resty.prometheus")
 local core      = require("apisix.core")
 local ipairs    = ipairs
 local ngx_capture = ngx.location.capture
@@ -27,10 +27,9 @@ local DEFAULT_BUCKETS = { 1, 2, 5, 7, 10, 15, 20, 25, 30, 40, 50, 60, 70,
     2000, 5000, 10000, 30000, 60000 }
 
 local metrics = {}
-local tmp_tab = {}
 
 
-local _M = {version = 0.2}
+local _M = {version = 0.3}
 
 
 function _M.init()
@@ -61,27 +60,27 @@ end
 
 
 function _M.log(conf, ctx)
-    core.table.clear(tmp_tab)
+    local vars = ctx.var
 
     local service_name
     if ctx.matched_route and ctx.matched_route.value then
-        service_name = ctx.matched_route.value.desc or ctx.matched_route.value.id
+        service_name = ctx.matched_route.value.desc or
+                       ctx.matched_route.value.id
+    else
+        service_name = vars.host
     end
 
     local balancer_ip = ctx.balancer_ip
-    core.table.set(tmp_tab, ctx.var.status, service_name, balancer_ip)
-    metrics.status:inc(1, tmp_tab)
+    metrics.status:inc(1, vars.status, service_name, balancer_ip)
 
     local latency = (ngx.now() - ngx.req.start_time()) * 1000
-    tmp_tab[1] = "request"
-    metrics.latency:observe(latency, tmp_tab)
-
-    tmp_tab[1] = "ingress"
-    metrics.bandwidth:inc(ctx.var.request_length, tmp_tab)
+    metrics.latency:observe(latency, "request", service_name, balancer_ip)
 
-    tmp_tab[1] = "egress"
-    metrics.bandwidth:inc(ctx.var.bytes_sent, tmp_tab)
+    metrics.bandwidth:inc(vars.request_length, "ingress", service_name,
+                          balancer_ip)
 
+    metrics.bandwidth:inc(vars.bytes_sent, "egress", service_name,
+                          balancer_ip)
 end
 
 
@@ -105,15 +104,13 @@ local function nginx_status()
         return
     end
 
-    core.table.clear(tmp_tab)
     for _, name in ipairs(ngx_statu_items) do
         local val = iterator()
         if not val then
             break
         end
 
-        tmp_tab[1] = name
-        metrics.connections:set(val[0], tmp_tab)
+        metrics.connections:set(val[0], name)
     end
 end
 
@@ -141,7 +138,6 @@ function _M.collect()
     end
 
     core.response.set_header("content_type", "text/plain")
-
     return 200, core.table.concat(prometheus:metric_data())
 end
 
diff --git a/rockspec/apisix-master-0.rockspec b/rockspec/apisix-master-0.rockspec
index a4f5a0c..4ed756e 100644
--- a/rockspec/apisix-master-0.rockspec
+++ b/rockspec/apisix-master-0.rockspec
@@ -45,7 +45,7 @@ dependencies = {
     "lua-resty-openidc = 1.7.2-1",
     "luafilesystem = 1.7.0-2",
     "lua-tinyyaml = 0.1",
-    "iresty-nginx-lua-prometheus = 0.20190917",
+    "lua-resty-prometheus = 1.0",
     "jsonschema = 0.4",
     "lua-resty-ipmatcher = 0.3",
 }