membphis commented on code in PR #12383: URL: https://github.com/apache/apisix/pull/12383#discussion_r2224139651
########## apisix/plugins/prometheus/exporter.lua: ########## @@ -498,10 +530,83 @@ 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) + + if err then + core.log.error("Failed to save metrics to shdict: ", err) + 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 + + 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) Review Comment: just confirm, no "err" is returned? ########## apisix/plugins/prometheus/exporter.lua: ########## @@ -498,10 +530,83 @@ 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) + + if err then + core.log.error("Failed to save metrics to shdict: ", err) Review Comment: if the value is to large, will fail when we call `set` method ########## apisix/plugins/prometheus/exporter.lua: ########## @@ -498,10 +530,83 @@ 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) + + if err then + core.log.error("Failed to save metrics to shdict: ", err) Review Comment: need more information, eg: the name of `shdict` and the size of value -- 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: notifications-unsubscr...@apisix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org