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

spacewander 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 4620ceabc feat(promethues): add ngx.shared.DICT status (#7412)
4620ceabc is described below

commit 4620ceabc86eea35d754903577c0073580e198fb
Author: wangchengcheng <[email protected]>
AuthorDate: Tue Jul 19 19:26:27 2022 +0800

    feat(promethues): add ngx.shared.DICT status (#7412)
---
 apisix/plugins/prometheus/exporter.lua | 22 ++++++++++++++++++++++
 docs/en/latest/plugins/prometheus.md   | 19 +++++++++++++++++++
 docs/zh/latest/plugins/prometheus.md   | 21 ++++++++++++++++++++-
 t/plugin/prometheus.t                  | 11 +++++++++++
 4 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/apisix/plugins/prometheus/exporter.lua 
b/apisix/plugins/prometheus/exporter.lua
index c65a39c48..e6b61b26b 100644
--- a/apisix/plugins/prometheus/exporter.lua
+++ b/apisix/plugins/prometheus/exporter.lua
@@ -18,6 +18,7 @@ local base_prometheus = require("prometheus")
 local core      = require("apisix.core")
 local plugin    = require("apisix.plugin")
 local ipairs    = ipairs
+local pairs     = pairs
 local ngx       = ngx
 local re_gmatch = ngx.re.gmatch
 local ffi       = require("ffi")
@@ -122,6 +123,14 @@ function _M.http_init(prometheus_enabled_in_stream)
             "Etcd modify index for APISIX keys",
             {"key"})
 
+    metrics.shared_dict_capacity_bytes = 
prometheus:gauge("shared_dict_capacity_bytes",
+            "The capacity of each nginx shared DICT since APISIX start",
+            {"name"})
+
+    metrics.shared_dict_free_space_bytes = 
prometheus:gauge("shared_dict_free_space_bytes",
+            "The free space of each nginx shared DICT since APISIX start",
+            {"name"})
+
     -- per service
 
     -- The consumer label indicates the name of consumer corresponds to the
@@ -352,6 +361,16 @@ local function etcd_modify_index()
 end
 
 
+local function shared_dict_status()
+    local name = {}
+    for shared_dict_name, shared_dict in pairs(ngx.shared) do
+        name[1] = shared_dict_name
+        metrics.shared_dict_capacity_bytes:set(shared_dict:capacity(), name)
+        metrics.shared_dict_free_space_bytes:set(shared_dict:free_space(), 
name)
+    end
+end
+
+
 local function collect(ctx, stream_only)
     if not prometheus or not metrics then
         core.log.error("prometheus: plugin is not initialized, please make 
sure ",
@@ -359,6 +378,9 @@ local function collect(ctx, stream_only)
         return 500, {message = "An unexpected error occurred"}
     end
 
+    -- collect ngx.shared.DICT status
+    shared_dict_status()
+
     -- across all services
     nginx_status()
 
diff --git a/docs/en/latest/plugins/prometheus.md 
b/docs/en/latest/plugins/prometheus.md
index 0e02aa6bb..8f0b4383e 100644
--- a/docs/en/latest/plugins/prometheus.md
+++ b/docs/en/latest/plugins/prometheus.md
@@ -201,6 +201,7 @@ The following metrics are exported by the `prometheus` 
Plugin:
   | node     | IP address of the Upstream node.                                
                                                                    |
 
 - Info: Information about the APISIX node.
+- Shared dict: The capacity and free space of all nginx.shared.DICT in APISIX.
 
 Here are the original metrics from APISIX:
 
@@ -272,6 +273,24 @@ 
apisix_http_latency_bucket{type="upstream",route="1",service="",consumer="",node
 # HELP apisix_node_info Info of APISIX node
 # TYPE apisix_node_info gauge
 apisix_node_info{hostname="desktop-2022q8f-wsl"} 1
+# HELP apisix_shared_dict_capacity_bytes The capacity of each nginx shared 
DICT since APISIX start
+# TYPE apisix_shared_dict_capacity_bytes gauge
+apisix_shared_dict_capacity_bytes{name="access-tokens"} 1048576
+apisix_shared_dict_capacity_bytes{name="balancer-ewma"} 10485760
+apisix_shared_dict_capacity_bytes{name="balancer-ewma-last-touched-at"} 
10485760
+apisix_shared_dict_capacity_bytes{name="balancer-ewma-locks"} 10485760
+apisix_shared_dict_capacity_bytes{name="discovery"} 1048576
+apisix_shared_dict_capacity_bytes{name="etcd-cluster-health-check"} 10485760
+...
+# HELP apisix_shared_dict_free_space_bytes The free space of each nginx shared 
DICT since APISIX start
+# TYPE apisix_shared_dict_free_space_bytes gauge
+apisix_shared_dict_free_space_bytes{name="access-tokens"} 1032192
+apisix_shared_dict_free_space_bytes{name="balancer-ewma"} 10412032
+apisix_shared_dict_free_space_bytes{name="balancer-ewma-last-touched-at"} 
10412032
+apisix_shared_dict_free_space_bytes{name="balancer-ewma-locks"} 10412032
+apisix_shared_dict_free_space_bytes{name="discovery"} 1032192
+apisix_shared_dict_free_space_bytes{name="etcd-cluster-health-check"} 10412032
+...
 ```
 
 ## Disable Plugin
diff --git a/docs/zh/latest/plugins/prometheus.md 
b/docs/zh/latest/plugins/prometheus.md
index 191ca5387..4595a4808 100644
--- a/docs/zh/latest/plugins/prometheus.md
+++ b/docs/zh/latest/plugins/prometheus.md
@@ -198,6 +198,7 @@ scrape_configs:
     | node         | 上游节点的 IP 地址。                                              
                        |
 
 - Info: 当前 APISIX 节点信息。
+- Shared dict: APISIX 中所有共享内存的容量以及剩余可用空间。
 
 以下是 APISIX 的原始的指标数据集:
 
@@ -205,7 +206,7 @@ scrape_configs:
 curl http://127.0.0.1:9091/apisix/prometheus/metrics
 ```
 
-```
+```shell
 # HELP apisix_bandwidth Total bandwidth in bytes consumed per service in Apisix
 # TYPE apisix_bandwidth counter
 apisix_bandwidth{type="egress",route="",service="",consumer="",node=""} 8417
@@ -269,6 +270,24 @@ 
apisix_http_latency_bucket{type="upstream",route="1",service="",consumer="",node
 # HELP apisix_node_info Info of APISIX node
 # TYPE apisix_node_info gauge
 apisix_node_info{hostname="APISIX"} 1
+# HELP apisix_shared_dict_capacity_bytes The capacity of each nginx shared 
DICT since APISIX start
+# TYPE apisix_shared_dict_capacity_bytes gauge
+apisix_shared_dict_capacity_bytes{name="access-tokens"} 1048576
+apisix_shared_dict_capacity_bytes{name="balancer-ewma"} 10485760
+apisix_shared_dict_capacity_bytes{name="balancer-ewma-last-touched-at"} 
10485760
+apisix_shared_dict_capacity_bytes{name="balancer-ewma-locks"} 10485760
+apisix_shared_dict_capacity_bytes{name="discovery"} 1048576
+apisix_shared_dict_capacity_bytes{name="etcd-cluster-health-check"} 10485760
+...
+# HELP apisix_shared_dict_free_space_bytes The free space of each nginx shared 
DICT since APISIX start
+# TYPE apisix_shared_dict_free_space_bytes gauge
+apisix_shared_dict_free_space_bytes{name="access-tokens"} 1032192
+apisix_shared_dict_free_space_bytes{name="balancer-ewma"} 10412032
+apisix_shared_dict_free_space_bytes{name="balancer-ewma-last-touched-at"} 
10412032
+apisix_shared_dict_free_space_bytes{name="balancer-ewma-locks"} 10412032
+apisix_shared_dict_free_space_bytes{name="discovery"} 1032192
+apisix_shared_dict_free_space_bytes{name="etcd-cluster-health-check"} 10412032
+...
 ```
 
 ## 禁用插件
diff --git a/t/plugin/prometheus.t b/t/plugin/prometheus.t
index 442d1b8b3..8fb2911b7 100644
--- a/t/plugin/prometheus.t
+++ b/t/plugin/prometheus.t
@@ -632,3 +632,14 @@ GET /apisix/prometheus/metrics
 qr/apisix_/
 --- response_body_unlike eval
 qr/etcd/
+
+
+
+=== TEST 42: fetch the prometheus shared dict internal-status data
+--- http_config
+lua_shared_dict test-shared-dict 10m;
+--- request
+GET /apisix/prometheus/metrics
+--- response_body_like
+.*apisix_shared_dict_capacity_bytes{name="test-shared-dict"} 10485760(?:.|\n)*
+apisix_shared_dict_free_space_bytes{name="test-shared-dict"} \d+.*

Reply via email to