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

shuyangw 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 e9fedee  fix(prometheus): collect nginx_status and return the body 
(#4014)
e9fedee is described below

commit e9fedee192ee5d545318b38b5c32718df54c85bc
Author: 罗泽轩 <[email protected]>
AuthorDate: Sat Apr 10 12:20:24 2021 +0800

    fix(prometheus): collect nginx_status and return the body (#4014)
    
    Signed-off-by: spacewander <[email protected]>
    Co-authored-by: John Bampton <[email protected]>
---
 apisix/cli/ngx_tpl.lua        |  8 ++++++++
 apisix/plugins/prometheus.lua |  5 ++++-
 t/cli/test_prometheus.sh      | 21 +++++++++++++--------
 3 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index eeafc5e..7173a3a 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -300,6 +300,14 @@ http {
                 prometheus.export_metrics()
             }
         }
+
+        {% if with_module_status then %}
+        location = /apisix/nginx_status {
+            allow 127.0.0.0/24;
+            deny all;
+            stub_status;
+        }
+        {% end %}
     }
     {% end %}
 
diff --git a/apisix/plugins/prometheus.lua b/apisix/plugins/prometheus.lua
index d89ffb6..3aebf4e 100644
--- a/apisix/plugins/prometheus.lua
+++ b/apisix/plugins/prometheus.lua
@@ -90,7 +90,10 @@ function _M.export_metrics()
     local method = ngx.req.get_method()
 
     if uri == api.uri and method == api.methods[1] then
-        return api.handler()
+        local code, body = api.handler()
+        if code or body then
+            core.response.exit(code, body)
+        end
     end
 
     return core.response.exit(404)
diff --git a/t/cli/test_prometheus.sh b/t/cli/test_prometheus.sh
index eac55c7..96287ed 100755
--- a/t/cli/test_prometheus.sh
+++ b/t/cli/test_prometheus.sh
@@ -25,19 +25,24 @@ make run
 
 code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} 
http://127.0.0.1:9080/apisix/prometheus/metrics)
 if [ ! $code -eq 404 ]; then
-    echo "failed: should listen default prometheus address"
+    echo "failed: should listen at default prometheus address"
     exit 1
 fi
 
 code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} 
http://127.0.0.1:9091/apisix/prometheus/metrics)
 if [ ! $code -eq 200 ]; then
-    echo "failed: should listen default prometheus address"
+    echo "failed: should listen at default prometheus address"
+    exit 1
+fi
+
+if ! curl -i http://127.0.0.1:9091/apisix/prometheus/metrics | grep 
"apisix_nginx_http_current_connections" > /dev/null; then
+    echo "failed: should listen at default prometheus address"
     exit 1
 fi
 
 make stop
 
-echo "passed: should listen default prometheus address"
+echo "passed: should listen at default prometheus address"
 
 echo '
 plugin_attr:
@@ -51,13 +56,13 @@ IP=127.0.0.1 PORT=9092 make run
 
 code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} 
http://127.0.0.1:9092/apisix/prometheus/metrics)
 if [ ! $code -eq 200 ]; then
-    echo "failed: should listen configured prometheus address"
+    echo "failed: should listen at configured prometheus address"
     exit 1
 fi
 
 make stop
 
-echo "passed: should listen configured prometheus address"
+echo "passed: should listen at configured prometheus address"
 
 echo '
 plugin_attr:
@@ -73,16 +78,16 @@ IP=127.0.0.1 PORT=9092 make run
 
 code=$(curl -v -k -i -m 20 -o /dev/null -s 
http://127.0.0.1:9092/prometheus/metrics || echo 'ouch')
 if [ "$code" != "ouch" ]; then
-    echo "failed: should listen previous prometheus address"
+    echo "failed: should listen at previous prometheus address"
     exit 1
 fi
 
 code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} 
http://127.0.0.1:9080/prometheus/metrics)
 if [ ! $code -eq 200 ]; then
-    echo "failed: should listen previous prometheus address"
+    echo "failed: should listen at previous prometheus address"
     exit 1
 fi
 
 make stop
 
-echo "passed: should listen previous prometheus address"
+echo "passed: should listen at previous prometheus address"

Reply via email to