changeset 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
parents fd90925dc239
children 883ad8b0a7c0
files mod_http_admin_api/mod_http_admin_api.lua
diffstat 1 files changed, 5 insertions(+), 0 deletions(-) [+]
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);