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 0920d1d feat(prometheus): Add metric prefix attr (#5960)
0920d1d is described below
commit 0920d1d21fba36947b4ce13438040874c5d37610
Author: Hao Xin <[email protected]>
AuthorDate: Fri Dec 31 09:26:44 2021 +0800
feat(prometheus): Add metric prefix attr (#5960)
---
apisix/plugins/prometheus/exporter.lua | 9 ++++++++-
conf/config-default.yaml | 1 +
t/cli/test_prometheus.sh | 20 ++++++++++++++++++++
3 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/apisix/plugins/prometheus/exporter.lua
b/apisix/plugins/prometheus/exporter.lua
index d565b4c..a3cc86d 100644
--- a/apisix/plugins/prometheus/exporter.lua
+++ b/apisix/plugins/prometheus/exporter.lua
@@ -16,6 +16,7 @@
--
local base_prometheus = require("prometheus")
local core = require("apisix.core")
+local plugin = require("apisix.plugin")
local ipairs = ipairs
local ngx = ngx
local ngx_capture = ngx.location.capture
@@ -75,7 +76,13 @@ function _M.init()
-- We keep the old metric names for the compatibility.
-- across all services
- prometheus = base_prometheus.init("prometheus-metrics", "apisix_")
+ local metric_prefix = "apisix_"
+ local attr = plugin.plugin_attr("prometheus")
+ if attr and attr.metric_prefix then
+ metric_prefix = attr.metric_prefix
+ end
+
+ prometheus = base_prometheus.init("prometheus-metrics", metric_prefix)
metrics.connections = prometheus:gauge("nginx_http_current_connections",
"Number of HTTP connections",
{"state"})
diff --git a/conf/config-default.yaml b/conf/config-default.yaml
index ca923ba..cbf97dd 100644
--- a/conf/config-default.yaml
+++ b/conf/config-default.yaml
@@ -408,6 +408,7 @@ plugin_attr:
endpoint_addr: http://127.0.0.1:12800
prometheus:
export_uri: /apisix/prometheus/metrics
+ metric_prefix: apisix_
enable_export_server: true
export_addr:
ip: 127.0.0.1
diff --git a/t/cli/test_prometheus.sh b/t/cli/test_prometheus.sh
index 7130044..c06cd61 100755
--- a/t/cli/test_prometheus.sh
+++ b/t/cli/test_prometheus.sh
@@ -123,3 +123,23 @@ if ! echo "$out" | grep "http listen port 9092 conflicts
with prometheus"; then
fi
echo "passed: should detect port conflicts"
+
+echo '
+plugin_attr:
+ prometheus:
+ metric_prefix: apisix_ci_prefix_
+ export_addr:
+ ip: ${{IP}}
+ port: ${{PORT}}
+' > conf/config.yaml
+
+IP=127.0.0.1 PORT=9092 make run
+
+if ! curl -s http://127.0.0.1:9092/apisix/prometheus/metrics | grep
"apisix_ci_prefix_" | wc -l; then
+ echo "failed: should use custom metric prefix"
+ exit 1
+fi
+
+make stop
+
+echo "passed: should use custom metric prefix"