comparison mod_log_slow_events/mod_log_slow_events.lua @ 1696:efc1d674eac0

mod_log_slow_events: Log slow HTTP requests
author Matthew Wild <mwild1@gmail.com>
date Tue, 05 May 2015 14:21:13 +0100
parents 78b3b3add19c
children 8c0bf3151e37
comparison
equal deleted inserted replaced
1695:78b3b3add19c 1696:efc1d674eac0
1 local time = require "socket".gettime; 1 local time = require "socket".gettime;
2 local base64_decode = require "util.encodings".base64.decode;
2 3
3 local max_seconds = module:get_option_number("log_slow_events_threshold", 0.5); 4 local max_seconds = module:get_option_number("log_slow_events_threshold", 0.5);
4 5
5 module:wrap_event(false, function (handlers, event_name, event_data) 6 function event_wrapper(handlers, event_name, event_data)
6 local start = time(); 7 local start = time();
7 local ret = handlers(event_name, event_data); 8 local ret = handlers(event_name, event_data);
8 local duration = time()-start; 9 local duration = time()-start;
9 if duration > max_seconds then 10 if duration > max_seconds then
10 local data = {}; 11 local data = {};
25 log_data("host", sess.host); 26 log_data("host", sess.host);
26 end 27 end
27 local stanza = event_data.stanza; 28 local stanza = event_data.stanza;
28 if stanza then 29 if stanza then
29 log_data("stanza", tostring(stanza)); 30 log_data("stanza", tostring(stanza));
31 else
32 local request = event_data.request;
33 if request then
34 log_data("http_method", request.method);
35 log_data("http_path", request.path);
36 local auth = request.headers.authorization;
37 if auth then
38 local creds = auth:match("^Basic +(.+)$");
39 if creds then
40 local user = string.match(base64_decode(creds) or "", "^([^:]+):");
41 log_data("http_user", user);
42 end
43 end
44 end
30 end 45 end
31 end 46 end
32 module:log("warn", "Slow event '%s' took %0.2fs: %s", event_name, duration, next(data) and table.concat(data, ", ") or "no recognised data"); 47 module:log("warn", "Slow event '%s' took %0.2fs: %s", event_name, duration, next(data) and table.concat(data, ", ") or "no recognised data");
33 end 48 end
34 return ret; 49 return ret;
35 end); 50 end
51
52 module:wrap_event(false, event_wrapper);
53 local http_events = require "net.http.server"._events;
54 module:wrap_object_event(http_events, false, event_wrapper);
55