comparison plugins/mod_csi.lua @ 13512:0b742bbbb819

mod_csi: Optimize metrics collection (prematurely) I did not measure, but this should be way fewer function calls.
author Kim Alvefur <zash@zash.se>
date Sun, 18 Aug 2024 16:56:36 +0200
parents 453979e16f2d
children 7fcae9da8934
comparison
equal deleted inserted replaced
13511:453979e16f2d 13512:0b742bbbb819
1 local statsmanager = require "prosody.core.statsmanager";
2 local st = require "prosody.util.stanza"; 1 local st = require "prosody.util.stanza";
3 local xmlns_csi = "urn:xmpp:csi:0"; 2 local xmlns_csi = "urn:xmpp:csi:0";
4 local csi_feature = st.stanza("csi", { xmlns = xmlns_csi }); 3 local csi_feature = st.stanza("csi", { xmlns = xmlns_csi });
5 4
6 local change = module:metric("counter", "changes", "events", "CSI state changes", {"csi_state"}); 5 local change = module:metric("counter", "changes", "events", "CSI state changes", {"csi_state"});
27 module:hook("stanza/"..xmlns_csi..":inactive", refire_event("csi-client-inactive")); 26 module:hook("stanza/"..xmlns_csi..":inactive", refire_event("csi-client-inactive"));
28 27
29 module:hook_global("stats-update", function() 28 module:hook_global("stats-update", function()
30 local sessions = prosody.hosts[module.host].sessions; 29 local sessions = prosody.hosts[module.host].sessions;
31 if not sessions then return end 30 if not sessions then return end
32 statsmanager.cork(); 31 local active, inactive, flushing = 0, 0, 0;
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_session in pairs(sessions) do 32 for _, user_session in pairs(sessions) do
38 for _, session in pairs(user_session.sessions) do 33 for _, session in pairs(user_session.sessions) do
39 if session.state == "inactive" or session.state == "active" or session.state == "flushing" then 34 if session.state == "inactive" then
40 count:with_labels(session.state):add(1); 35 inactive = inactive + 1;
36 elseif session.state == "active" then
37 inactive = inactive + 1;
38 elseif session.state == "flushing" then
39 inactive = inactive + 1;
41 end 40 end
42 end 41 end
43 end 42 end
44 statsmanager.uncork(); 43 count:with_labels("active"):set(active);
44 count:with_labels("inactive"):set(inactive);
45 count:with_labels("flushing"):set(flushing);
45 end); 46 end);