Diff

mod_http_admin_api/mod_http_admin_api.lua @ 4776:13e913471b75

mod_http_admin_api: Ensure freshness of metrics when in manual mode When in manual collection mode, as recommended for Prometheus, collection needs to be triggered manually, or they would be stale, possibly dating from the start of the server. This might vary per metric depending on how and when the metrics are gathered.
author Kim Alvefur <zash@zash.se>
date Thu, 18 Nov 2021 19:26:07 +0100
parent 4714:bd83c9ada037
child 4777:883ad8b0a7c0
line wrap: on
line diff
--- a/mod_http_admin_api/mod_http_admin_api.lua	Thu Nov 18 09:07:40 2021 +0000
+++ b/mod_http_admin_api/mod_http_admin_api.lua	Thu Nov 18 19:26:07 2021 +0100
@@ -17,6 +17,8 @@
 
 local site_name = module:get_option_string("site_name", module.host);
 
+local manual_stats_collection = module:context("*"):get_option("statistics_interval") == "manual";
+
 local json_content_type = "application/json";
 
 local www_authenticate_header = ("Bearer realm=%q"):format(module.host.."/"..module.name);
@@ -612,6 +614,9 @@
 local function get_server_metrics(event)
 	event.response.headers["Content-Type"] = json_content_type;
 	local result = {};
+	if manual_stats_collection then
+		statsmanager.collect();
+	end
 	local families = statsmanager.get_metric_registry():get_metric_families();
 	result.memory = maybe_export_plain_gauge(families.process_resident_memory_bytes);
 	result.cpu = maybe_export_plain_counter(families.process_cpu_seconds);