Software /
code /
prosody-modules
Annotate
mod_log_slow_events/mod_log_slow_events.lua @ 1695:78b3b3add19c
mod_log_slow_events: Minor improvement to log message
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 05 May 2015 14:20:53 +0100 |
parent | 1680:a9df1f7e273d |
child | 1696:efc1d674eac0 |
rev | line source |
---|---|
1680
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local time = require "socket".gettime; |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local max_seconds = module:get_option_number("log_slow_events_threshold", 0.5); |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 module:wrap_event(false, function (handlers, event_name, event_data) |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local start = time(); |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local ret = handlers(event_name, event_data); |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local duration = time()-start; |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 if duration > max_seconds then |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local data = {}; |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 if event_data then |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local function log_data(name, value) |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 if value then |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 table.insert(data, ("%s=%q"):format(name, value)); |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 return true; |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 end |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 end |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local sess = event_data.origin or event_data.session; |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 if sess then |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 log_data("ip", sess.ip); |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 if not log_data("full_jid", sess.full_jid) then |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 log_data("username", sess.username); |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 end |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 log_data("type", sess.type); |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 log_data("host", sess.host); |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 end |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local stanza = event_data.stanza; |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 if stanza then |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 log_data("stanza", tostring(stanza)); |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 end |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 end |
1695
78b3b3add19c
mod_log_slow_events: Minor improvement to log message
Matthew Wild <mwild1@gmail.com>
parents:
1680
diff
changeset
|
32 module:log("warn", "Slow event '%s' took %0.2fs: %s", event_name, duration, next(data) and table.concat(data, ", ") or "no recognised data"); |
1680
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 end |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 return ret; |
a9df1f7e273d
mod_log_slow_events: Log events that take a long time to process (including stanzas)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end); |