mod_measure_stanza_counts/mod_measure_stanza_counts.lua
author Kim Alvefur <zash@zash.se>
Wed, 07 Mar 2018 18:15:31 +0100
changeset 2910 d9603b555be2
parent 2792 512405077709
child 4563 33b1b6ff23d8
permissions -rw-r--r--
luacheckrc: Forget about the _M global
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2791
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
module:set_global()
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
local filters = require"util.filters";
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
local stanza_kinds = { message = true, presence = true, iq = true };
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
local function rate(measures, dir)
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
	return function (stanza, session)
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
		measures[dir]();
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
		measures[dir .. "_" .. session.type]();
2792
512405077709 mod_measure_stanza_counts: Fix a crash in mod_bosh.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2791
diff changeset
    11
		if stanza.attr and not stanza.attr.xmlns and stanza_kinds[stanza.name] then
2791
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
			measures[dir .. "_" .. session.type .. "_" .. stanza.name]();
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
		end
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
		return stanza;
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
	end
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
end
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
local measures = setmetatable({}, {
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
	__index = function (t, name)
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
		local m = module:measure(name, "rate");
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
		t[name] = m;
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
		return m;
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
	end
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
});
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
local function measure_stanza_counts(session)
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
	filters.add_filter(session, "stanzas/in",  rate(measures, "incoming"));
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
	filters.add_filter(session, "stanzas/out", rate(measures, "outgoing"));
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
end
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
750572f6f59d mod_measure_stanza_counts: Counts stanzas and reports using 0.10+ statistics API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
filters.add_filter_hook(measure_stanza_counts);