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


##########
apisix/plugins/prometheus/exporter.lua:
##########
@@ -498,10 +530,88 @@ 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)
+        exporter_timer_running = false
+        return
+    end
+
+    local _, err, forcible = shdict_prometheus_cache:set(CACHED_METRICS_KEY, 
res)

Review Comment:
   I think we need to set an expire time
   
   <img width="952" height="254" alt="image" 
src="https://github.com/user-attachments/assets/c21a513f-aa0d-44de-b597-ea0b0f5439b9";
 />
   
   
   if the refresh interval is  15sec, I think the ttl should be about 30sec



##########
apisix/plugins/prometheus/exporter.lua:
##########
@@ -498,10 +530,88 @@ 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)

Review Comment:
   it may fail: 
https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#ngxtimerat
   
   I think we have to use a new way



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