view mod_log_events_by_cpu_usage/mod_log_events_by_cpu_usage.lua @ 4974:807007913f67

mod_log_json: Prefer native Lua table.pack over Prosody util.table one Prosody is removing support for Lua 5.1, which was the reason for util.table.pack to exist in the first place, since Lua 5.2+ provides table.pack. In prosody rev 5eaf77114fdb everything was switched over to use table.pack, opening the door for removing util.table.pack at some point. This change here is to prepare for that future eventuality.
author Kim Alvefur <zash@zash.se>
date Mon, 11 Jul 2022 20:08:41 +0200
parents c0bc97c0ba61
children
line wrap: on
line source

module:set_global();

local treshold = module:get_option_number("log_cpu_threshold", 0.01);

function event_wrapper(handlers, event_name, event_data)
	local cpu_before = os.clock();
	local ret = handlers(event_name, event_data);
	local cpu_after = os.clock();
	if (cpu_after - cpu_before) > treshold then
		module:log("warn", "%g seconds of CPU usage while processing event '%s'", (cpu_after - cpu_before), event_name);
	end
	return ret;
end

local http_events = require "net.http.server"._events;
module:wrap_object_event(http_events, false, event_wrapper);

module:wrap_event(false, event_wrapper);
function module.add_host(module)
	module:wrap_event(false, event_wrapper);
end