SkyeYoung commented on code in PR #12383:
URL: https://github.com/apache/apisix/pull/12383#discussion_r2224100594
##########
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)
+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 = shdict_prometheus_cache:get(CACHED_METRICS_KEY)
+ if not cached_metrics_text then
+ core.log.error("Failed to retrieve cached metrics: data is nil")
+ return 500, "Failed to retrieve metrics: no data available"
Review Comment:
done
--
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]