changeset 5668:ecfd7aece33b

mod_measure_modules: Report module statuses via OpenMetrics Someone in the chat asked about a health check endpoint, which reminded me of mod_http_status, which provides access to module statuses with full details. After that, this idea came about, which seems natural. As noted in the README, it could be used to monitor that critical modules are in fact loaded correctly. As more modules use the status API, the more useful this module and mod_http_status becomes.
author Kim Alvefur <zash@zash.se>
date Fri, 06 Oct 2023 18:34:39 +0200
parents 9bcd257dea4e
children 30f91daa40b4
files mod_measure_modules/README.md mod_measure_modules/mod_measure_modules.lua
diffstat 2 files changed, 89 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_measure_modules/README.md	Fri Oct 06 18:34:39 2023 +0200
@@ -0,0 +1,56 @@
+# Introduction
+
+This module reports [module status priorities][doc:developers:moduleapi#logging-and-status] as metrics, which are a kind of persistent log messages
+indicating whether the module is functioning properly.
+
+This concept was introduced in [Prosody 0.12.0][doc:release:0.12.0#api] and is not used extensively yet, primarily for reporting failure to load
+modules or e.g. [mod_component] not being connected to its external component yet.
+
+Besides using this to report problems, this metric could also be used to count how many modules are loaded or monitor for when critical modules aren't
+loaded at all.
+
+# Configuration
+
+After installing, enable by adding to [`modules_enabled`][doc:modules_enabled] like many other modules:
+
+``` lua
+-- in the global section
+modules_enabled = {
+    -- Other globally enabled modules here...
+    "http_openmetrics";
+    "measure_modules"; -- add
+}
+```
+
+# Example OpenMetrics
+
+``` openmetrics
+# HELP prosody_module_status Prosody module status
+# UNIT prosody_module_status
+# TYPE prosody_module_status gauge
+prosody_module_status{host="example.org",module="message"} 0
+prosody_module_status{host="example.org",module="presence"} 0
+prosody_module_status{host="groups.example.org",module="muc"} 0
+```
+
+# Details
+
+The priorities are reported as the following values:
+
+0
+:   `core` - no problem, nothing to report
+
+1
+:   `info` - no problem, but a module had something important to say
+
+2
+:   `warn` - something is not right
+
+3
+:   `error` - something has gone wrong
+
+Status changes are generally also reported in Prosodys logs, so look there for details.
+
+# See also
+
+- [mod_http_status] provides all module status details as JSON via HTTP
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_measure_modules/mod_measure_modules.lua	Fri Oct 06 18:34:39 2023 +0200
@@ -0,0 +1,33 @@
+module:set_global();
+
+local mm = require "core.modulemanager";
+local sm = require "core.statsmanager";
+
+local measure_status = sm.metric("gauge", "prosody_module_status", "", "Prosody module status", { "host"; "module" });
+
+local status_priorities = { error = 3; warn = 2; info = 1; core = 0 };
+
+function module.add_host(module)
+	local measure = measure_status:with_partial_label(module.host);
+
+	if module.global then
+		measure = measure_status:with_partial_label(":global");
+	end
+
+	-- Already loaded modules
+	local modules = mm.get_modules(module.host);
+	for name, mod in pairs(modules) do
+		measure:with_labels(name):set(status_priorities[mod.module.status_type] or 0);
+	end
+
+	-- TODO hook module load and unload
+
+	-- Future changes
+	module:hook("module-status/updated", function(event)
+		local mod = mm.get_module(event.name);
+		measure:with_labels(event.name):set(status_priorities[mod and mod.module.status_type] or 0);
+	end);
+
+end
+
+module:add_host(); -- Initialize global context