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 7525f1f  feat(prometheus): add prometheus metrics 
`node_info`(hostname)  (#2063)
7525f1f is described below

commit 7525f1fcc93de1d9dd4767124359198eb053177a
Author: nic-chen <33000667+nic-c...@users.noreply.github.com>
AuthorDate: Sun Aug 16 09:59:55 2020 +0800

    feat(prometheus): add prometheus metrics `node_info`(hostname)  (#2063)
    
    * using a new metric `node_info` to store the hostname
---
 apisix/plugins/prometheus/exporter.lua | 10 ++++++++++
 t/plugin/prometheus.t                  | 10 ++++++++++
 2 files changed, 20 insertions(+)

diff --git a/apisix/plugins/prometheus/exporter.lua 
b/apisix/plugins/prometheus/exporter.lua
index 8e6d74d..82ccf45 100644
--- a/apisix/plugins/prometheus/exporter.lua
+++ b/apisix/plugins/prometheus/exporter.lua
@@ -78,6 +78,11 @@ function _M.init()
     metrics.etcd_reachable = prometheus:gauge("etcd_reachable",
             "Config server etcd reachable from APISIX, 0 is unreachable")
 
+
+    metrics.node_info = prometheus:gauge("node_info",
+            "Info of APISIX node",
+            {"hostname"})
+
     metrics.etcd_modify_indexes = prometheus:gauge("etcd_modify_indexes",
             "Etcd modify index for APISIX keys",
             {"key"})
@@ -98,6 +103,7 @@ function _M.init()
     metrics.bandwidth = prometheus:counter("bandwidth",
             "Total bandwidth in bytes consumed per service in APISIX",
             {"type", "route", "service", "node"})
+
 end
 
 
@@ -265,6 +271,8 @@ function _M.collect()
     etcd_modify_index()
 
     -- config server status
+    local vars = ngx.var or {}
+    local hostname = vars.hostname or ""
     local config = core.config.new()
     local version, err = config:server_version()
     if version then
@@ -276,6 +284,8 @@ function _M.collect()
                        "processing metrics endpoint: ", err)
     end
 
+    metrics.node_info:set(1, gen_arr(hostname))
+
     local res, _ = config:getkey("/routes")
     if res and res.headers then
         clear_tab(key_values)
diff --git a/t/plugin/prometheus.t b/t/plugin/prometheus.t
index 3a7d5c7..9a43def 100644
--- a/t/plugin/prometheus.t
+++ b/t/plugin/prometheus.t
@@ -773,3 +773,13 @@ GET /apisix/prometheus/metrics
 qr/apisix_etcd_modify_indexes\{key="x_etcd_index"\} \d+/
 --- no_error_log
 [error]
+
+
+
+=== TEST 43: fetch the prometheus metric data -- hostname
+--- request
+GET /apisix/prometheus/metrics
+--- response_body eval
+qr/apisix_node_info\{hostname=".*"\} 1/
+--- no_error_log
+[error]

Reply via email to