Comparison

plugins/mod_csi.lua @ 13510:393e265aafba

mod_csi: Count how many sessions are using CSI and their state
author Kim Alvefur <zash@zash.se>
date Sat, 17 Aug 2024 23:13:28 +0200
parent 13075:82980f6890cd
child 13511:453979e16f2d
comparison
equal deleted inserted replaced
13509:f14066ee3239 13510:393e265aafba
1 local statsmanager = require "prosody.core.statsmanager";
1 local st = require "prosody.util.stanza"; 2 local st = require "prosody.util.stanza";
2 local xmlns_csi = "urn:xmpp:csi:0"; 3 local xmlns_csi = "urn:xmpp:csi:0";
3 local csi_feature = st.stanza("csi", { xmlns = xmlns_csi }); 4 local csi_feature = st.stanza("csi", { xmlns = xmlns_csi });
4 5
5 local change = module:metric("counter", "changes", "events", "CSI state changes", {"csi_state"}); 6 local change = module:metric("counter", "changes", "events", "CSI state changes", {"csi_state"});
7 local count = module:metric("gauge", "state", "sessions", "", { "state" });
6 8
7 module:hook("stream-features", function (event) 9 module:hook("stream-features", function (event)
8 if event.origin.username then 10 if event.origin.username then
9 event.features:add_child(csi_feature); 11 event.features:add_child(csi_feature);
10 end 12 end
21 end; 23 end;
22 end 24 end
23 25
24 module:hook("stanza/"..xmlns_csi..":active", refire_event("csi-client-active")); 26 module:hook("stanza/"..xmlns_csi..":active", refire_event("csi-client-active"));
25 module:hook("stanza/"..xmlns_csi..":inactive", refire_event("csi-client-inactive")); 27 module:hook("stanza/"..xmlns_csi..":inactive", refire_event("csi-client-inactive"));
28
29 module:hook_global("stats-update", function()
30 local sessions = prosody.hosts[module.host].sessions;
31 if not sessions then return end
32 statsmanager.cork();
33 -- Can't do :clear() on host-scoped measures?
34 count:with_labels("active"):set(0);
35 count:with_labels("inactive"):set(0);
36 count:with_labels("flushing"):set(0);
37 for user, user_session in pairs(sessions) do
38 for resource, session in pairs(user_session.sessions) do
39 if session.state == "inactive" or session.state == "active" or session.state == "flushing" then
40 count:with_labels(session.state):add(1);
41 end
42 end
43 end
44 statsmanager.uncork();
45 end);