From dca21b3a3d62ec68759d9065a685e51b54514b1f Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 28 Apr 2015 13:52:07 +0100 Subject: moduleapi: Experimental API for modules to measure the rate+duration of events --- core/moduleapi.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'core') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index e32c116a..71e19e01 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -375,6 +375,25 @@ function api:measure(name, type) return measure(type, "/"..self.host.."/mod_"..self.name.."/"..name); end +function api:measure_object_event(events_object, event_name, stat_name) + local m = self:measure(stat_name or event_name, "duration"); + local function handler(handlers, event_name, event_data) + local finished = m(); + local ret = handlers(event_name, event_data); + finished(); + return ret; + end + return self:hook_object_event(events_object, event_name, handler); +end + +function api:measure_event(event_name, stat_name) + return self:hook_object_event((hosts[self.host] or prosody).events.wrappers, event_name, handler); +end + +function api:measure_global_event(event_name, stat_name) + return self:hook_object_event(prosody.events.wrappers, event_name, handler); +end + function api.init(mm) modulemanager = mm; return api; -- cgit v1.2.3 From 5b4edd3877c2cb0e52320b98031f4096f30d4068 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 28 Apr 2015 14:18:43 +0100 Subject: moduleapi: New methods for modules to conveniently wrap events --- core/moduleapi.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'core') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 71e19e01..f3326295 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -120,6 +120,18 @@ function api:unhook(event, handler) return self:unhook_object_event((hosts[self.host] or prosody).events, event, handler); end +function api:wrap_object_event(events_object, event, handler) + return self:hook_object_event(assert(events_object.wrappers, "no wrappers"), event, handler); +end + +function api:wrap_event(event, handler) + return self:wrap_object_event((hosts[self.host] or prosody).events, event, handler); +end + +function api:wrap_global(event, handler) + return self:hook_object_event(prosody.events, event, handler, priority); +end + function api:require(lib) local f, n = pluginloader.load_code(self.name, lib..".lib.lua", self.environment); if not f then -- cgit v1.2.3