Software /
code /
prosody
Comparison
core/statsmanager.lua @ 6554:6c22bec3e8d0
statsmanager, prosody: New core module and API for gathering statistics about the running server
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 20 Jan 2015 12:33:20 +0000 |
child | 6555:7b2d16c14659 |
comparison
equal
deleted
inserted
replaced
6553:0e29c05c3503 | 6554:6c22bec3e8d0 |
---|---|
1 | |
2 local stats = require "util.statistics".new(); | |
3 local config = require "core.configmanager"; | |
4 local log = require "util.logger".init("stats"); | |
5 local timer = require "util.timer"; | |
6 local fire_event = prosody.events.fire_event; | |
7 | |
8 local stats_config = config.get("*", "statistics_interval"); | |
9 local stats_interval = tonumber(stats_config); | |
10 if stats_config and not stats_interval then | |
11 log("error", "Invalid 'statistics_interval' setting, statistics will be disabled"); | |
12 end | |
13 | |
14 local measure, collect; | |
15 local latest_stats = {}; | |
16 local changed_stats = {}; | |
17 local stats_extra = {}; | |
18 | |
19 if stats_interval then | |
20 log("debug", "Statistics collection is enabled every %d seconds", stats_interval); | |
21 function measure(type, name) | |
22 local f = assert(stats[type], "unknown stat type: "..type); | |
23 return f(name); | |
24 end | |
25 | |
26 local mark_collection_start = measure("duration", "stats.collection_time"); | |
27 local mark_processing_start = measure("duration", "stats.processing_time"); | |
28 | |
29 function collect() | |
30 local mark_collection_done = mark_collection_start(); | |
31 changed_stats, stats_extra = {}, {}; | |
32 for name, getter in pairs(stats.get_stats()) do | |
33 local type, value, extra = getter(); | |
34 local stat_name = name..":"..type; | |
35 local old_value = latest_stats[stat_name]; | |
36 latest_stats[stat_name] = value; | |
37 if value ~= old_value then | |
38 changed_stats[stat_name] = value; | |
39 end | |
40 if extra then | |
41 print(stat_name, extra) | |
42 stats_extra[stat_name] = extra; | |
43 if type == "duration" then | |
44 local rate = extra.rate; | |
45 local rate_name = name..":rate"; | |
46 latest_stats[rate_name] = rate; | |
47 changed_stats[rate_name] = rate; | |
48 end | |
49 end | |
50 end | |
51 mark_collection_done(); | |
52 local mark_processing_done = mark_processing_start(); | |
53 fire_event("stats-updated", { stats = latest_stats, changed_stats = changed_stats, stats_extra = stats_extra }); | |
54 mark_processing_done(); | |
55 return stats_interval; | |
56 end | |
57 | |
58 timer.add_task(stats_interval, collect); | |
59 else | |
60 log("debug", "Statistics collection is disabled"); | |
61 -- nop | |
62 function measure() | |
63 return measure; | |
64 end | |
65 function collect() | |
66 end | |
67 end | |
68 | |
69 return { | |
70 measure = measure; | |
71 collect = collect; | |
72 get_stats = function () | |
73 return latest_stats, changed_stats, stats_extra; | |
74 end; | |
75 }; |