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

young 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 30ae5dfc6 feat: decoupled prometheus exporter's calculation and output 
(#12383)
30ae5dfc6 is described below

commit 30ae5dfc6f07153f6c782bf9d0fe09e597e61f64
Author: YYYoung <isk...@outlook.com>
AuthorDate: Fri Jul 25 09:30:07 2025 +0800

    feat: decoupled prometheus exporter's calculation and output (#12383)
---
 apisix/cli/config.lua                      |   4 +-
 apisix/cli/ngx_tpl.lua                     |  21 +--
 apisix/core/config_etcd.lua                |   2 +-
 apisix/init.lua                            |   4 +
 apisix/plugin.lua                          |  30 ++--
 apisix/plugins/prometheus.lua              |   9 +-
 apisix/plugins/prometheus/exporter.lua     | 259 +++++++++++++++++++++--------
 apisix/stream/plugins/prometheus.lua       |   2 +
 conf/config.yaml.example                   |   4 +
 t/APISIX.pm                                |   1 +
 t/cli/test_prometheus.sh                   |   6 +
 t/cli/test_prometheus_run_in_privileged.sh | 113 -------------
 t/cli/test_prometheus_stream.sh            |   6 +
 t/plugin/prometheus.t                      |   8 +
 t/plugin/prometheus2.t                     |   8 +
 t/plugin/prometheus3.t                     |  11 ++
 t/plugin/prometheus4.t                     |  13 ++
 17 files changed, 289 insertions(+), 212 deletions(-)

diff --git a/apisix/cli/config.lua b/apisix/cli/config.lua
index 20f0e0486..e1baea904 100644
--- a/apisix/cli/config.lua
+++ b/apisix/cli/config.lua
@@ -95,6 +95,7 @@ local _M = {
     meta = {
       lua_shared_dict = {
         ["prometheus-metrics"] = "15m",
+        ["prometheus-cache"] = "10m",
         ["standalone-config"] = "10m",
         ["status-report"] = "1m",
       }
@@ -323,7 +324,8 @@ local _M = {
       export_addr = {
         ip = "127.0.0.1",
         port = 9091
-      }
+      },
+      refresh_interval = 15
     },
     ["server-info"] = {
       report_ttl = 60
diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index 34576aadc..feca0581d 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -67,6 +67,9 @@ lua {
     {% if enabled_stream_plugins["prometheus"] then %}
     lua_shared_dict prometheus-metrics {* 
meta.lua_shared_dict["prometheus-metrics"] *};
     {% end %}
+    {% if enabled_plugins["prometheus"] or 
enabled_stream_plugins["prometheus"] then %}
+    lua_shared_dict prometheus-cache {* 
meta.lua_shared_dict["prometheus-cache"] *};
+    {% end %}
     {% if standalone_with_admin_api then %}
     lua_shared_dict standalone-config {* 
meta.lua_shared_dict["standalone-config"] *};
     {% end %}
@@ -96,22 +99,20 @@ http {
     }
 
     init_worker_by_lua_block {
-        require("apisix.plugins.prometheus.exporter").http_init(true)
+        local prometheus = require("apisix.plugins.prometheus.exporter")
+        prometheus.http_init(true)
+        prometheus.init_exporter_timer()
     }
 
     server {
-        {% if use_apisix_base then %}
-            listen {* prometheus_server_addr *} 
enable_process=privileged_agent;
-        {% else %}
-            listen {* prometheus_server_addr *};
-        {% end %}
+        listen {* prometheus_server_addr *};
 
         access_log off;
 
         location / {
             content_by_lua_block {
                 local prometheus = 
require("apisix.plugins.prometheus.exporter")
-                prometheus.export_metrics(true)
+                prometheus.export_metrics()
             }
         }
 
@@ -577,11 +578,7 @@ http {
 
     {% if enabled_plugins["prometheus"] and prometheus_server_addr then %}
     server {
-        {% if use_apisix_base then %}
-            listen {* prometheus_server_addr *} 
enable_process=privileged_agent;
-        {% else %}
-            listen {* prometheus_server_addr *};
-        {% end %}
+        listen {* prometheus_server_addr *};
 
         access_log off;
 
diff --git a/apisix/core/config_etcd.lua b/apisix/core/config_etcd.lua
index d47694149..93e97a93b 100644
--- a/apisix/core/config_etcd.lua
+++ b/apisix/core/config_etcd.lua
@@ -1001,7 +1001,7 @@ function _M.new(key, opts)
         sync_times = 0,
         running = true,
         conf_version = 0,
-        values = nil,
+        values = {},
         need_reload = true,
         watching_stream = nil,
         routes_hash = nil,
diff --git a/apisix/init.lua b/apisix/init.lua
index b5ee018ba..f518f2028 100644
--- a/apisix/init.lua
+++ b/apisix/init.lua
@@ -155,6 +155,10 @@ function _M.http_init_worker()
     if local_conf.apisix and local_conf.apisix.enable_server_tokens == false 
then
         ver_header = "APISIX"
     end
+
+    -- To ensure that all workers related to Prometheus metrics are 
initialized,
+    -- we need to put the initialization of the Prometheus plugin here.
+    plugin.init_prometheus()
 end
 
 
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index 6cb876b4c..de5421a93 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -341,8 +341,6 @@ function _M.load(config)
         return local_plugins
     end
 
-    local exporter = require("apisix.plugins.prometheus.exporter")
-
     if ngx.config.subsystem == "http" then
         if not http_plugin_names then
             core.log.error("failed to read plugin list from local file")
@@ -356,15 +354,6 @@ function _M.load(config)
             if not ok then
                 core.log.error("failed to load plugins: ", err)
             end
-
-            local enabled = core.table.array_find(http_plugin_names, 
"prometheus") ~= nil
-            local active  = exporter.get_prometheus() ~= nil
-            if not enabled then
-                exporter.destroy()
-            end
-            if enabled and not active then
-                exporter.http_init()
-            end
         end
     end
 
@@ -808,18 +797,21 @@ do
 end
 
 
-function _M.init_worker()
+function _M.init_prometheus()
     local _, http_plugin_names, stream_plugin_names = get_plugin_names()
+    local enabled_in_http = core.table.array_find(http_plugin_names, 
"prometheus")
+    local enabled_in_stream = core.table.array_find(stream_plugin_names, 
"prometheus")
 
-    -- some plugins need to be initialized in init* phases
-    if is_http and core.table.array_find(http_plugin_names, "prometheus") then
-        local prometheus_enabled_in_stream =
-            core.table.array_find(stream_plugin_names, "prometheus")
-        
require("apisix.plugins.prometheus.exporter").http_init(prometheus_enabled_in_stream)
-    elseif not is_http and core.table.array_find(stream_plugin_names, 
"prometheus") then
-        require("apisix.plugins.prometheus.exporter").stream_init()
+    -- For stream-only mode, there are separate calls in ngx_tpl.lua.
+    -- And for other modes, whether in stream or http plugins,
+    -- the prometheus exporter needs to be initialized.
+    if is_http and (enabled_in_http or enabled_in_stream) then
+        require("apisix.plugins.prometheus.exporter").init_exporter_timer()
     end
+end
 
+
+function _M.init_worker()
     -- someone's plugin needs to be initialized after prometheus
     -- see https://github.com/apache/apisix/issues/3286
     _M.load()
diff --git a/apisix/plugins/prometheus.lua b/apisix/plugins/prometheus.lua
index b15469754..592c12ab5 100644
--- a/apisix/plugins/prometheus.lua
+++ b/apisix/plugins/prometheus.lua
@@ -17,7 +17,6 @@
 local core = require("apisix.core")
 local exporter = require("apisix.plugins.prometheus.exporter")
 
-
 local plugin_name = "prometheus"
 local schema = {
     type = "object",
@@ -35,6 +34,7 @@ local _M = {
     priority = 500,
     name = plugin_name,
     log  = exporter.http_log,
+    destroy = exporter.destroy,
     schema = schema,
     run_policy = "prefer_route",
 }
@@ -55,4 +55,11 @@ function _M.api()
 end
 
 
+function _M.init()
+    local local_conf = core.config.local_conf()
+    local enabled_in_stream = core.table.array_find(local_conf.stream_plugins, 
"prometheus")
+    exporter.http_init(enabled_in_stream)
+end
+
+
 return _M
diff --git a/apisix/plugins/prometheus/exporter.lua 
b/apisix/plugins/prometheus/exporter.lua
index d34ab8739..278e3784b 100644
--- a/apisix/plugins/prometheus/exporter.lua
+++ b/apisix/plugins/prometheus/exporter.lua
@@ -21,7 +21,6 @@ local control   = require("apisix.control.v1")
 local ipairs    = ipairs
 local pairs     = pairs
 local ngx       = ngx
-local re_gmatch = ngx.re.gmatch
 local ffi       = require("ffi")
 local C         = ffi.C
 local pcall = pcall
@@ -45,23 +44,33 @@ local latency_details = 
require("apisix.utils.log-util").latency_details_in_ms
 local xrpc = require("apisix.stream.xrpc")
 local unpack = unpack
 local next = next
+local process = require("ngx.process")
+local tonumber = tonumber
+local shdict_prometheus_cache = ngx.shared["prometheus-cache"]
+local ngx_timer_every = ngx.timer.every
 
-
-local ngx_capture
-if ngx.config.subsystem == "http" then
-    ngx_capture = ngx.location.capture
+if not shdict_prometheus_cache then
+    error("lua_shared_dict \"prometheus-cache\" not configured")
 end
 
-
 local plugin_name = "prometheus"
 local default_export_uri = "/apisix/prometheus/metrics"
 -- Default set of latency buckets, 1ms to 60s:
 local DEFAULT_BUCKETS = {1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 
10000, 30000, 60000}
+-- Default refresh interval
+local DEFAULT_REFRESH_INTERVAL = 15
+
+local CACHED_METRICS_KEY = "cached_metrics_text"
 
 local metrics = {}
 
 local inner_tab_arr = {}
 
+local exporter_timer_running = false
+
+local exporter_timer_created = false
+
+
 local function gen_arr(...)
     clear_tab(inner_tab_arr)
     for i = 1, select('#', ...) do
@@ -110,6 +119,15 @@ end
 
 
 function _M.http_init(prometheus_enabled_in_stream)
+    -- FIXME:
+    -- Now the HTTP subsystem loads the stream plugin unintentionally, which 
shouldn't happen.
+    -- But this part did not show any issues during testing,
+    -- it's just to prevent the same problem from happening again.
+    if ngx.config.subsystem ~= "http" then
+        core.log.warn("prometheus plugin http_init should not be called in 
stream subsystem")
+        return
+    end
+
     -- todo: support hot reload, we may need to update the lua-prometheus
     -- library
     if ngx.get_phase() ~= "init" and ngx.get_phase() ~= "init_worker"  then
@@ -211,6 +229,15 @@ end
 
 
 function _M.stream_init()
+    -- FIXME:
+    -- Now the HTTP subsystem loads the stream plugin unintentionally, which 
shouldn't happen.
+    -- It breaks the initialization logic of the plugin,
+    -- here it is temporarily fixed using a workaround.
+    if ngx.config.subsystem ~= "stream" then
+        core.log.warn("prometheus plugin stream_init should not be called in 
http subsystem")
+        return
+    end
+
     if ngx.get_phase() ~= "init" and ngx.get_phase() ~= "init_worker"  then
         return
     end
@@ -310,39 +337,50 @@ function _M.stream_log(conf, ctx)
 end
 
 
-local ngx_status_items = {"active", "accepted", "handled", "total",
-                         "reading", "writing", "waiting"}
+-- FFI definitions for nginx connection status
+-- Based on 
https://github.com/nginx/nginx/blob/master/src/event/ngx_event.c#L61-L78
+ffi.cdef[[
+    typedef uint64_t ngx_atomic_uint_t;
+    extern ngx_atomic_uint_t  *ngx_stat_accepted;
+    extern ngx_atomic_uint_t  *ngx_stat_handled;
+    extern ngx_atomic_uint_t  *ngx_stat_requests;
+    extern ngx_atomic_uint_t  *ngx_stat_active;
+    extern ngx_atomic_uint_t  *ngx_stat_reading;
+    extern ngx_atomic_uint_t  *ngx_stat_writing;
+    extern ngx_atomic_uint_t  *ngx_stat_waiting;
+]]
+
 local label_values = {}
 
+-- Mapping of status names to FFI global variables and metrics
+local status_mapping = {
+    {name = "active", var = "ngx_stat_active"},
+    {name = "accepted", var = "ngx_stat_accepted"},
+    {name = "handled", var = "ngx_stat_handled"},
+    {name = "total", var = "ngx_stat_requests"},
+    {name = "reading", var = "ngx_stat_reading"},
+    {name = "writing", var = "ngx_stat_writing"},
+    {name = "waiting", var = "ngx_stat_waiting"},
+}
+
+-- Use FFI to get nginx status directly from global variables
 local function nginx_status()
-    local res = ngx_capture("/apisix/nginx_status")
-    if not res or res.status ~= 200 then
-        core.log.error("failed to fetch Nginx status")
-        return
-    end
-
-    -- Active connections: 2
-    -- server accepts handled requests
-    --   26 26 84
-    -- Reading: 0 Writing: 1 Waiting: 1
-
-    local iterator, err = re_gmatch(res.body, [[(\d+)]], "jmo")
-    if not iterator then
-        core.log.error("failed to re.gmatch Nginx status: ", err)
-        return
-    end
-
-    for _, name in ipairs(ngx_status_items) do
-        local val = iterator()
-        if not val then
-            break
+    for _, item in ipairs(status_mapping) do
+        local ok, value = pcall(function()
+            local stat_ptr = C[item.var]
+            return stat_ptr and tonumber(stat_ptr[0]) or 0
+        end)
+
+        if not ok then
+            core.log.error("failed to read ", item.name, " via FFI")
+            return
         end
 
-        if name == "total" then
-            metrics.requests:set(val[0])
+        if item.name == "total" then
+            metrics.requests:set(value)
         else
-            label_values[1] = name
-            metrics.connections:set(val[0], label_values)
+            label_values[1] = item.name
+            metrics.connections:set(value, label_values)
         end
     end
 end
@@ -438,13 +476,7 @@ local function shared_dict_status()
 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 ",
-                     " 'prometheus_metrics' shared dict is present in nginx 
template")
-        return 500, {message = "An unexpected error occurred"}
-    end
-
+local function collect(yieldable)
     -- collect ngx.shared.DICT status
     shared_dict_status()
 
@@ -454,34 +486,37 @@ local function collect(ctx, stream_only)
     local config = core.config.new()
 
     -- config server status
-    local vars = ngx.var or {}
-    local hostname = vars.hostname or ""
+    local hostname = core.utils.gethostname() or ""
     local version = core.version.VERSION or ""
 
+    local local_conf = core.config.local_conf()
+    local stream_only = local_conf.apisix.proxy_mode == "stream"
     -- we can't get etcd index in metric server if only stream subsystem is 
enabled
     if config.type == "etcd" and not stream_only then
         -- etcd modify index
         etcd_modify_index()
 
-        local version, err = config:server_version()
-        if version then
-            metrics.etcd_reachable:set(1)
+        if yieldable then
+            local version, err = config:server_version()
+            if version then
+                metrics.etcd_reachable:set(1)
 
-        else
-            metrics.etcd_reachable:set(0)
-            core.log.error("prometheus: failed to reach config server while ",
-                           "processing metrics endpoint: ", err)
-        end
+            else
+                metrics.etcd_reachable:set(0)
+                core.log.error("prometheus: failed to reach config server 
while ",
+                            "processing metrics endpoint: ", err)
+            end
 
-        -- Because request any key from etcd will return the "X-Etcd-Index".
-        -- A non-existed key is preferred because it doesn't return too much 
data.
-        -- So use phantom key to get etcd index.
-        local res, _ = config:getkey("/phantomkey")
-        if res and res.headers then
-            clear_tab(key_values)
-            -- global max
-            key_values[1] = "x_etcd_index"
-            metrics.etcd_modify_indexes:set(res.headers["X-Etcd-Index"], 
key_values)
+            -- Because request any key from etcd will return the 
"X-Etcd-Index".
+            -- A non-existed key is preferred because it doesn't return too 
much data.
+            -- So use phantom key to get etcd index.
+            local res, _ = config:getkey("/phantomkey")
+            if res and res.headers then
+                clear_tab(key_values)
+                -- global max
+                key_values[1] = "x_etcd_index"
+                metrics.etcd_modify_indexes:set(res.headers["X-Etcd-Index"], 
key_values)
+            end
         end
     end
 
@@ -498,10 +533,104 @@ local function collect(ctx, stream_only)
         end
     end
 
+    return core.table.concat(prometheus:metric_data())
+end
+
+
+local function exporter_timer(premature, yieldable, cache_exptime)
+    if premature then
+        return
+    end
+
+    if not prometheus then
+        return
+    end
+
+    if exporter_timer_running then
+        core.log.warn("Previous calculation still running, skipping")
+        return
+    end
+
+    exporter_timer_running = true
+
+    local ok, res = pcall(collect, yieldable)
+    if not ok then
+        core.log.error("Failed to collect metrics: ", res)
+        exporter_timer_running = false
+        return
+    end
+
+    -- Clear the cached data after cache_exptime to prevent stale data in case 
of an error.
+    local _, err, forcible = shdict_prometheus_cache:set(CACHED_METRICS_KEY, 
res, cache_exptime)
+    if err then
+        core.log.error("Failed to save metrics to the `prometheus-cache` 
shared dict: ", err,
+                    ". The size of the value being attempted to be saved is: 
", #res)
+    end
+
+    if forcible then
+        core.log.error("Shared dictionary used for prometheus cache " ..
+  "is full. REPORTED METRIC DATA MIGHT BE INCOMPLETE. Please increase the " ..
+  "size of the `prometheus-cache` shared dict or decrease metric cardinality.")
+    end
+
+    exporter_timer_running = false
+end
+
+
+local function init_exporter_timer()
+    if process.type() ~= "privileged agent" then
+        return
+    end
+
+    local refresh_interval = DEFAULT_REFRESH_INTERVAL
+    local attr = plugin.plugin_attr("prometheus")
+    if attr and attr.refresh_interval then
+        refresh_interval = attr.refresh_interval
+    end
+
+    local cache_exptime = refresh_interval * 2
+
+    exporter_timer(false, false, cache_exptime)
+
+    if exporter_timer_created then
+        core.log.info("Exporter timer already created, skipping")
+        return
+    end
+
+    -- When the APISIX configuration `refresh_interval` is updated,
+    -- the timer will not restart, and the new `refresh_interval` will not be 
applied.
+    -- APISIX needs to be restarted to apply the changes.
+    local ok, err = ngx_timer_every(refresh_interval, exporter_timer, true, 
cache_exptime)
+
+    if ok then
+        exporter_timer_created = true
+    else
+        core.log.error("Failed to start the exporter timer: ", err)
+    end
+end
+_M.init_exporter_timer = init_exporter_timer
+
+
+local function get_cached_metrics()
+    if not prometheus or not metrics then
+        core.log.error("prometheus: plugin is not initialized, please make 
sure ",
+                     " 'prometheus_metrics' shared dict is present in nginx 
template")
+        return 500, {message = "An unexpected error occurred"}
+    end
+
     core.response.set_header("content_type", "text/plain")
-    return 200, core.table.concat(prometheus:metric_data())
+    local cached_metrics_text, err = 
shdict_prometheus_cache:get(CACHED_METRICS_KEY)
+    if err then
+        core.log.error("Failed to retrieve cached metrics: err: ", err)
+        return 500, {message = "Failed to retrieve metrics. err: " .. err}
+    end
+    if not cached_metrics_text then
+        core.log.error("Failed to retrieve cached metrics: data is nil")
+        return 500, {message = "Failed to retrieve metrics: no data available"}
+    end
+
+    return 200, cached_metrics_text
 end
-_M.collect = collect
 
 
 local function get_api(called_by_api_router)
@@ -514,7 +643,7 @@ local function get_api(called_by_api_router)
     local api = {
         methods = {"GET"},
         uri = export_uri,
-        handler = collect
+        handler = get_cached_metrics
     }
 
     if not called_by_api_router then
@@ -530,16 +659,16 @@ end
 _M.get_api = get_api
 
 
-function _M.export_metrics(stream_only)
+function _M.export_metrics()
     if not prometheus then
-        core.response.exit(200, "{}")
+        return core.response.exit(200, "{}")
     end
     local api = get_api(false)
     local uri = ngx.var.uri
     local method = ngx.req.get_method()
 
     if uri == api.uri and method == api.methods[1] then
-        local code, body = api.handler(nil, stream_only)
+        local code, body = api.handler()
         if code or body then
             core.response.exit(code, body)
         end
diff --git a/apisix/stream/plugins/prometheus.lua 
b/apisix/stream/plugins/prometheus.lua
index 46222eca2..6b30ca252 100644
--- a/apisix/stream/plugins/prometheus.lua
+++ b/apisix/stream/plugins/prometheus.lua
@@ -35,6 +35,8 @@ local _M = {
     priority = 500,
     name = plugin_name,
     log  = exporter.stream_log,
+    destroy = exporter.destroy,
+    init = exporter.stream_init,
     schema = schema,
     run_policy = "prefer_route",
 }
diff --git a/conf/config.yaml.example b/conf/config.yaml.example
index 1d52f6e41..48b8696e3 100644
--- a/conf/config.yaml.example
+++ b/conf/config.yaml.example
@@ -170,6 +170,9 @@ nginx_config:                     # Config for render the 
template to generate n
   meta:
     lua_shared_dict:              # Nginx Lua shared memory zone. Size units 
are m or k.
       prometheus-metrics: 15m
+      prometheus-cache: 10m       # Cache the calculated metrics data text.
+                                  # Please resize when the `error.log` prompts 
that the data is full.
+                                  # NOTE: Restart APISIX to take effect.
       standalone-config: 10m
 
   stream:
@@ -605,6 +608,7 @@ plugin_attr:          # Plugin attributes
     export_addr:                            # Set the address for the 
Prometheus export server.
       ip: 127.0.0.1                         # Set the IP.
       port: 9091                            # Set the port.
+    refresh_interval: 15                    # Set the interval for refreshing 
cached metric data. unit: second.
     # metrics:    # Create extra labels from nginx variables: 
https://nginx.org/en/docs/varindex.html
     #  http_status:
     #    expire: 0 # The expiration time after which metrics are removed. 
unit: second.
diff --git a/t/APISIX.pm b/t/APISIX.pm
index d8480c47d..28c88733d 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -290,6 +290,7 @@ thread_pool grpc-client-nginx-module threads=1;
 
 lua {
     lua_shared_dict prometheus-metrics 15m;
+    lua_shared_dict prometheus-cache 10m;
     lua_shared_dict standalone-config 10m;
     lua_shared_dict status-report 1m;
     lua_shared_dict nacos 10m;
diff --git a/t/cli/test_prometheus.sh b/t/cli/test_prometheus.sh
index a613993d1..3f0931c18 100755
--- a/t/cli/test_prometheus.sh
+++ b/t/cli/test_prometheus.sh
@@ -25,6 +25,10 @@ sleep 1
 
 make run
 
+# The privileged agent may be ready later than the normal workers,
+# so we need to wait for a while.
+sleep 1
+
 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 at default prometheus address"
@@ -56,6 +60,8 @@ plugin_attr:
 
 IP=127.0.0.1 PORT=9092 make run
 
+sleep 1
+
 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 at configured prometheus address"
diff --git a/t/cli/test_prometheus_run_in_privileged.sh 
b/t/cli/test_prometheus_run_in_privileged.sh
deleted file mode 100755
index 08d019353..000000000
--- a/t/cli/test_prometheus_run_in_privileged.sh
+++ /dev/null
@@ -1,113 +0,0 @@
-#!/usr/bin/env bash
-
-#
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.
-# The ASF licenses this file to You under the Apache License, Version 2.0
-# (the "License"); you may not use this file except in compliance with
-# the License.  You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-. ./t/cli/common.sh
-
-git checkout conf/config.yaml
-
-exit_if_not_customed_nginx
-
-# prometheus run in privileged works when only http is enabled
-sleep 0.5
-rm logs/error.log || true
-
-echo '
-apisix:
-    extra_lua_path: "$prefix/t/lib/?.lua"
-nginx_config:
-    error_log_level: info
-' > conf/config.yaml
-
-make run
-sleep 0.1
-
-curl -s -o /dev/null http://127.0.0.1:9091/apisix/prometheus/metrics
-
-if ! grep -E "process type: privileged agent" logs/error.log; then
-    echo "failed: prometheus run in privileged can't work when only http is 
enabled"
-    exit 1
-fi
-
-make stop
-
-echo "prometheus run in privileged agent successfully when only http is 
enabled"
-
-
-# prometheus run in privileged works when both http & stream are enabled
-sleep 0.5
-rm logs/error.log || true
-
-echo '
-apisix:
-    proxy_mode: "http&stream"
-    extra_lua_path: "$prefix/t/lib/?.lua"
-    enable_admin: true
-    stream_proxy:
-        tcp:
-            - addr: 9100
-stream_plugins:
-    - prometheus
-nginx_config:
-    error_log_level: info
-' > conf/config.yaml
-
-make run
-sleep 0.1
-
-curl -s -o /dev/null http://127.0.0.1:9091/apisix/prometheus/metrics
-
-if ! grep -E " process type: privileged agent" logs/error.log; then
-    echo "failed: prometheus run in privileged can't work when both http & 
stream are enabled"
-    exit 1
-fi
-
-echo "passed: prometheus run in privileged agent successfully when both http & 
stream are enabled"
-
-make stop
-
-
-# prometheus run in privileged works when only stream is enabled
-sleep 0.5
-rm logs/error.log || true
-
-echo '
-apisix:
-    proxy_mode: "http&stream"
-    extra_lua_path: "$prefix/t/lib/?.lua"
-    enable_admin: false
-    stream_proxy:
-        tcp:
-            - addr: 9100
-stream_plugins:
-    - prometheus
-nginx_config:
-    error_log_level: info
-' > conf/config.yaml
-
-make run
-sleep 0.1
-
-curl -s -o /dev/null http://127.0.0.1:9091/apisix/prometheus/metrics
-
-if ! grep -E " process type: privileged agent" logs/error.log; then
-    echo "failed: prometheus run in privileged can't work when only stream is 
enabled"
-    exit 1
-fi
-
-echo "passed: prometheus run in privileged agent successfully when only stream 
is enabled"
diff --git a/t/cli/test_prometheus_stream.sh b/t/cli/test_prometheus_stream.sh
index c326315c5..d9cca3264 100755
--- a/t/cli/test_prometheus_stream.sh
+++ b/t/cli/test_prometheus_stream.sh
@@ -30,6 +30,9 @@ apisix:
             - addr: 9100
 stream_plugins:
     - prometheus
+plugin_attr:
+    prometheus:
+        refresh_interval: 1
 " > conf/config.yaml
 
 make run
@@ -74,6 +77,9 @@ apisix:
             - addr: 9100
 stream_plugins:
     - prometheus
+plugin_attr:
+    prometheus:
+        refresh_interval: 1
 " > conf/config.yaml
 
 make run
diff --git a/t/plugin/prometheus.t b/t/plugin/prometheus.t
index 9254de532..dd879a054 100644
--- a/t/plugin/prometheus.t
+++ b/t/plugin/prometheus.t
@@ -37,6 +37,14 @@ add_block_preprocessor(sub {
     if (!defined $block->request) {
         $block->set_value("request", "GET /t");
     }
+
+    if (!defined $block->yaml_config) {
+        $block->set_value("yaml_config", <<'EOF');
+plugin_attr:
+    prometheus:
+        refresh_interval: 0.1
+EOF
+    }
 });
 
 run_tests;
diff --git a/t/plugin/prometheus2.t b/t/plugin/prometheus2.t
index 40b2cdc8b..e2b26eb0e 100644
--- a/t/plugin/prometheus2.t
+++ b/t/plugin/prometheus2.t
@@ -37,6 +37,14 @@ add_block_preprocessor(sub {
     if (!defined $block->request) {
         $block->set_value("request", "GET /t");
     }
+
+    if (!defined $block->yaml_config) {
+        $block->set_value("yaml_config", <<'EOF');
+plugin_attr:
+    prometheus:
+        refresh_interval: 0.1
+EOF
+    }
 });
 
 run_tests;
diff --git a/t/plugin/prometheus3.t b/t/plugin/prometheus3.t
index b80986a34..4d0103436 100644
--- a/t/plugin/prometheus3.t
+++ b/t/plugin/prometheus3.t
@@ -37,6 +37,14 @@ add_block_preprocessor(sub {
     if (!$block->request) {
         $block->set_value("request", "GET /t");
     }
+
+    if (!defined $block->yaml_config) {
+        $block->set_value("yaml_config", <<'EOF');
+plugin_attr:
+    prometheus:
+        refresh_interval: 0.1
+EOF
+    }
 });
 
 run_tests;
@@ -168,6 +176,9 @@ passed
 
 === TEST 4: check metrics
 --- yaml_config
+plugin_attr:
+    prometheus:
+        refresh_interval: 0.1
 plugins:
   - public-api
   - error-log-logger
diff --git a/t/plugin/prometheus4.t b/t/plugin/prometheus4.t
index eac1f6066..6a890f131 100644
--- a/t/plugin/prometheus4.t
+++ b/t/plugin/prometheus4.t
@@ -32,6 +32,14 @@ add_block_preprocessor(sub {
     if (!defined $block->request) {
         $block->set_value("request", "GET /t");
     }
+
+    if (!defined $block->yaml_config) {
+        $block->set_value("yaml_config", <<'EOF');
+plugin_attr:
+    prometheus:
+        refresh_interval: 0.1
+EOF
+    }
 });
 
 run_tests;
@@ -98,6 +106,7 @@ passed
 --- yaml_config
 plugin_attr:
     prometheus:
+        refresh_interval: 0.1
         metrics:
             bandwidth:
                 extra_labels:
@@ -120,6 +129,7 @@ 
qr/apisix_bandwidth\{type="egress",route="10",service="",consumer="",node="127.0
 --- yaml_config
 plugin_attr:
     prometheus:
+        refresh_interval: 0.1
         metrics:
             http_status:
                 extra_labels:
@@ -141,6 +151,7 @@ 
qr/apisix_http_status\{code="200",route="10",matched_uri="\/hello",matched_host=
 --- yaml_config
 plugin_attr:
     prometheus:
+        refresh_interval: 0.1
         default_buckets:
             - 15
             - 55
@@ -257,6 +268,8 @@ apisix:
 plugins:
   - example-plugin
 plugin_attr:
+  prometheus:
+    refresh_interval: 0.1
   example-plugin:
     val: 1
 --- config

Reply via email to