Comparison

plugins/mod_c2s.lua @ 11524:6de302b53a3e

mod_c2s: Port to new OpenMetrics API
author Jonas Schäfer <jonas@wielicki.name>
date Sun, 18 Apr 2021 12:34:17 +0200
parent 11517:f7275c2c58fa
child 11560:3bbb1af92514
comparison
equal deleted inserted replaced
11523:5f15ab7c6ae5 11524:6de302b53a3e
10 10
11 local add_task = require "util.timer".add_task; 11 local add_task = require "util.timer".add_task;
12 local new_xmpp_stream = require "util.xmppstream".new; 12 local new_xmpp_stream = require "util.xmppstream".new;
13 local nameprep = require "util.encodings".stringprep.nameprep; 13 local nameprep = require "util.encodings".stringprep.nameprep;
14 local sessionmanager = require "core.sessionmanager"; 14 local sessionmanager = require "core.sessionmanager";
15 local statsmanager = require "core.statsmanager";
15 local st = require "util.stanza"; 16 local st = require "util.stanza";
16 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; 17 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
17 local uuid_generate = require "util.uuid".generate; 18 local uuid_generate = require "util.uuid".generate;
18 local runner = require "util.async".runner; 19 local runner = require "util.async".runner;
19 20
26 local c2s_timeout = module:get_option_number("c2s_timeout", 300); 27 local c2s_timeout = module:get_option_number("c2s_timeout", 300);
27 local stream_close_timeout = module:get_option_number("c2s_close_timeout", 5); 28 local stream_close_timeout = module:get_option_number("c2s_close_timeout", 5);
28 local opt_keepalives = module:get_option_boolean("c2s_tcp_keepalives", module:get_option_boolean("tcp_keepalives", true)); 29 local opt_keepalives = module:get_option_boolean("c2s_tcp_keepalives", module:get_option_boolean("tcp_keepalives", true));
29 local stanza_size_limit = module:get_option_number("c2s_stanza_size_limit"); -- TODO come up with a sensible default (util.xmppstream defaults to 10M) 30 local stanza_size_limit = module:get_option_number("c2s_stanza_size_limit"); -- TODO come up with a sensible default (util.xmppstream defaults to 10M)
30 31
31 local measure_connections = module:measure("connections", "amount"); 32 local measure_connections = module:metric("gauge", "connections", "", "Established c2s connections", {"host", "type", "ip_family"});
32 local measure_ipv6 = module:measure("ipv6", "amount");
33 33
34 local sessions = module:shared("sessions"); 34 local sessions = module:shared("sessions");
35 local core_process_stanza = prosody.core_process_stanza; 35 local core_process_stanza = prosody.core_process_stanza;
36 local hosts = prosody.hosts; 36 local hosts = prosody.hosts;
37 37
38 local stream_callbacks = { default_ns = "jabber:client" }; 38 local stream_callbacks = { default_ns = "jabber:client" };
39 local listener = {}; 39 local listener = {};
40 local runner_callbacks = {}; 40 local runner_callbacks = {};
41 41
42 module:hook("stats-update", function () 42 module:hook("stats-update", function ()
43 local count = 0; 43 -- for push backends, avoid sending out updates for each increment of
44 local ipv6 = 0; 44 -- the metric below.
45 statsmanager.cork()
46 measure_connections:clear()
45 for _, session in pairs(sessions) do 47 for _, session in pairs(sessions) do
46 count = count + 1; 48 local host = session.host or ""
47 if session.ip and session.ip:match(":") then 49 local type_ = session.type or "other"
48 ipv6 = ipv6 + 1; 50
49 end 51 -- we want to expose both v4 and v6 counters in all cases to make
50 end 52 -- queries smoother
51 measure_connections(count); 53 local is_ipv6 = session.ip and session.ip:match(":") and 1 or 0
52 measure_ipv6(ipv6); 54 local is_ipv4 = 1 - is_ipv6
55 measure_connections:with_labels(host, type_, "ipv4"):add(is_ipv4)
56 measure_connections:with_labels(host, type_, "ipv6"):add(is_ipv6)
57 end
58 statsmanager.uncork()
53 end); 59 end);
54 60
55 --- Stream events handlers 61 --- Stream events handlers
56 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; 62 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
57 63