bzp2010 commented on code in PR #12383:
URL: https://github.com/apache/apisix/pull/12383#discussion_r2222148829


##########
apisix/plugins/prometheus/exporter.lua:
##########
@@ -498,10 +524,71 @@ local function collect(ctx, stream_only)
         end
     end
 
+    return core.table.concat(prometheus:metric_data())
+end
+
+
+local function exporter_timer(premature, yieldable)
+    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, true)
+
+    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)
+        return
+    end
+    shdict_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
+
+    exporter_timer(false, false)

Review Comment:
   ### NOTE
   
   The initialization of the timer will perform an acquisition task 
synchronously, i.e. the first acquisition will always occur in the init_worker 
phase, which provides initial access to the metrics data.
   
   If at any time , the metrics data (the string in the `prometheus-cache` 
shdict) is not available, the API will report an error and log it. This is by 
design not very likely to happen.



-- 
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]

Reply via email to