fix(prometheus): collect nginx_status and return the body (#4014)

Signed-off-by: spacewander <spacewanderlzx@gmail.com>
Co-authored-by: John Bampton <jbampton@users.noreply.github.com>
This commit is contained in:
罗泽轩 2021-04-10 12:20:24 +08:00 committed by GitHub
parent d6f3f7ae02
commit e9fedee192
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 9 deletions

View File

@ -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 %}

View File

@ -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)

View File

@ -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"