SkyeYoung commented on code in PR #12383:
URL: https://github.com/apache/apisix/pull/12383#discussion_r2220706155
##########
apisix/plugins/prometheus/exporter.lua:
##########
@@ -498,10 +506,70 @@ local function collect(ctx, stream_only)
end
end
+ return core.table.concat(prometheus:metric_data())
+end
+
+
+local function exporter_timer(premature)
+ if premature then
+ return
+ end
+
+ if not prometheus 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
+
+ ngx.timer.at(refresh_interval, exporter_timer)
+
+ if exporter_timer_running then
+ core.log.warn("Previous calculation still running, skipping")
+ return
+ end
+
+ exporter_timer_running = true
+
+ local ok, res = pcall(collect)
+ if not ok then
+ core.log.error("Failed to collect metrics: ", res)
+ return
+ end
+ ngx.shared["prometheus-cache"]:set(CACHED_METRICS_KEY, res)
+
+ exporter_timer_running = false
+end
+
+local function init_exporter_timer()
+ if process.type() ~= "privileged agent" then
+ return
+ end
+
+ ngx.timer.at(0, exporter_timer)
Review Comment:
After the modification, this place can still only be asynchronous at the
moment.
If synchronization is needed, we need to modify the following parts:
https://github.com/apache/apisix/blob/6fb9bf94281525c1fca397f681b4890b69440369/apisix/plugins/prometheus/exporter.lua#L467-L494
The reason is that this part requests the API, which will lead to the
following error when directly using `exporter_timer()`:
```txt
2025/07/21 14:41:12 [error] 464992#464992: init_worker_by_lua error:
/home/xxx/apisix//deps/share/lua/5.1/resty/http.lua:74: API disabled in the
context of init_worker_by_lua*
stack traceback:
[C]: in function 'co_create'
/home/xxx/apisix//deps/share/lua/5.1/resty/http.lua:74: in function
'_body_reader'
/home/xxx/apisix//deps/share/lua/5.1/resty/http.lua:821: in function
'request'
/home/xxx/apisix//deps/share/lua/5.1/resty/etcd/v3.lua:120: in function
'request_uri_via_unix_socket'
/home/xxx/apisix//deps/share/lua/5.1/resty/etcd/v3.lua:160: in function
'http_request_uri'
/home/xxx/apisix//deps/share/lua/5.1/resty/etcd/v3.lua:251: in function
'server_version'
/home/xxx/apisix/apisix/core/config_etcd.lua:1066: in function
'server_version'
/home/xxx/apisix/apisix/plugins/prometheus/exporter.lua:474: in function
'collect'
/home/xxx/apisix/apisix/plugins/prometheus/exporter.lua:537: in function
'exporter_timer'
/home/xxx/apisix/apisix/plugins/prometheus/exporter.lua:553: in function
'init_exporter_timer'
/home/xxx/apisix/apisix/plugin.lua:808: in function 'init_prometheus'
/home/xxx/apisix/apisix/init.lua:161: in function 'http_init_worker'
init_worker_by_lua:2: in main chunk
```
So, if we need to ensure synchronized initialization, we need to continue
the discussion: whether to remove or move the collection of this part of the
indicators.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]