mod_measure_stanza_counts/mod_measure_stanza_counts.lua
changeset 4563 33b1b6ff23d8
parent 2792 512405077709
equal deleted inserted replaced
4562:8e58a1b78336 4563:33b1b6ff23d8
     1 module:set_global()
     1 module:set_global()
     2 
     2 
     3 local filters = require"util.filters";
     3 local filters = require"util.filters";
     4 
     4 
       
     5 local stanzas_in = module:metric(
       
     6 	"counter", "received", "",
       
     7 	"Stanzas received by Prosody",
       
     8 	{ "session_type", "stanza_kind" }
       
     9 )
       
    10 local stanzas_out = module:metric(
       
    11 	"counter", "sent", "",
       
    12 	"Stanzas sent by prosody",
       
    13 	{ "session_type", "stanza_kind" }
       
    14 )
       
    15 
     5 local stanza_kinds = { message = true, presence = true, iq = true };
    16 local stanza_kinds = { message = true, presence = true, iq = true };
     6 
    17 
     7 local function rate(measures, dir)
    18 local function rate(metric_family)
     8 	return function (stanza, session)
    19 	return function (stanza, session)
     9 		measures[dir]();
       
    10 		measures[dir .. "_" .. session.type]();
       
    11 		if stanza.attr and not stanza.attr.xmlns and stanza_kinds[stanza.name] then
    20 		if stanza.attr and not stanza.attr.xmlns and stanza_kinds[stanza.name] then
    12 			measures[dir .. "_" .. session.type .. "_" .. stanza.name]();
    21 			metric_family:with_labels(session.type, stanza.name):add(1);
    13 		end
    22 		end
    14 		return stanza;
    23 		return stanza;
    15 	end
    24 	end
    16 end
    25 end
    17 
    26 
    18 local measures = setmetatable({}, {
       
    19 	__index = function (t, name)
       
    20 		local m = module:measure(name, "rate");
       
    21 		t[name] = m;
       
    22 		return m;
       
    23 	end
       
    24 });
       
    25 
       
    26 local function measure_stanza_counts(session)
    27 local function measure_stanza_counts(session)
    27 	filters.add_filter(session, "stanzas/in",  rate(measures, "incoming"));
    28 	filters.add_filter(session, "stanzas/in",  rate(stanzas_in));
    28 	filters.add_filter(session, "stanzas/out", rate(measures, "outgoing"));
    29 	filters.add_filter(session, "stanzas/out", rate(stanzas_out));
    29 end
    30 end
    30 
    31 
    31 filters.add_filter_hook(measure_stanza_counts);
    32 filters.add_filter_hook(measure_stanza_counts);