changeset 1680:a9df1f7e273d

mod_log_slow_events: Log events that take a long time to process (including stanzas)
author Matthew Wild <mwild1@gmail.com>
date Tue, 28 Apr 2015 14:26:03 +0100
parents c77e9522dc66
children d20cfc5ba827 752d52d61186
files mod_log_slow_events/mod_log_slow_events.lua
diffstat 1 files changed, 35 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_log_slow_events/mod_log_slow_events.lua	Tue Apr 28 14:26:03 2015 +0100
@@ -0,0 +1,35 @@
+local time = require "socket".gettime;
+
+local max_seconds = module:get_option_number("log_slow_events_threshold", 0.5);
+
+module:wrap_event(false, function (handlers, event_name, event_data)
+	local start = time();
+	local ret = handlers(event_name, event_data);
+	local duration = time()-start;
+	if duration > max_seconds then
+		local data = {};
+		if event_data then
+			local function log_data(name, value)
+				if value then
+					table.insert(data, ("%s=%q"):format(name, value));
+					return true;
+				end
+			end
+			local sess = event_data.origin or event_data.session;
+			if sess then
+				log_data("ip", sess.ip);
+				if not log_data("full_jid", sess.full_jid) then
+					log_data("username", sess.username);
+				end
+				log_data("type", sess.type);
+				log_data("host", sess.host);
+			end
+			local stanza = event_data.stanza;
+			if stanza then
+				log_data("stanza", tostring(stanza));
+			end
+		end
+		module:log("warn", "Slow event '%s' took %0.2f: %s", event_name, duration, next(data) and table.concat(data, ", ") or "no recognised data");
+	end
+	return ret;
+end);