Annotate

core/statsmanager.lua @ 6669:7da8b6bc0966

util.xml: Remove unused parameter (thanks, luacheck)
author Matthew Wild <mwild1@gmail.com>
date Wed, 06 May 2015 19:39:28 +0100
parent 6582:71b5de1d830b
child 6910:82765a4ec799
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local stats = require "util.statistics".new();
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local config = require "core.configmanager";
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local log = require "util.logger".init("stats");
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local timer = require "util.timer";
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local fire_event = prosody.events.fire_event;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local stats_config = config.get("*", "statistics_interval");
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local stats_interval = tonumber(stats_config);
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 if stats_config and not stats_interval then
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 log("error", "Invalid 'statistics_interval' setting, statistics will be disabled");
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local measure, collect;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local latest_stats = {};
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local changed_stats = {};
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local stats_extra = {};
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 if stats_interval then
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 log("debug", "Statistics collection is enabled every %d seconds", stats_interval);
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 function measure(type, name)
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local f = assert(stats[type], "unknown stat type: "..type);
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 return f(name);
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6554
diff changeset
26 local mark_collection_start = measure("times", "stats.collection");
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6554
diff changeset
27 local mark_processing_start = measure("times", "stats.processing");
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 function collect()
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local mark_collection_done = mark_collection_start();
6582
71b5de1d830b statsmanager: Fire event at the start of collection to allow for polling
Kim Alvefur <zash@zash.se>
parents: 6577
diff changeset
31 fire_event("stats-update");
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 changed_stats, stats_extra = {}, {};
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6554
diff changeset
33 for stat_name, getter in pairs(stats.get_stats()) do
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local type, value, extra = getter();
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local old_value = latest_stats[stat_name];
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 latest_stats[stat_name] = value;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if value ~= old_value then
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 changed_stats[stat_name] = value;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if extra then
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 stats_extra[stat_name] = extra;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 mark_collection_done();
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local mark_processing_done = mark_processing_start();
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 fire_event("stats-updated", { stats = latest_stats, changed_stats = changed_stats, stats_extra = stats_extra });
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 mark_processing_done();
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return stats_interval;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 timer.add_task(stats_interval, collect);
6577
1e749832ed3b statsmanager: Perform an initial collection right after startup is completed
Kim Alvefur <zash@zash.se>
parents: 6555
diff changeset
52 prosody.events.add_handler("server-started", function () collect() end, -1);
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 else
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 log("debug", "Statistics collection is disabled");
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 -- nop
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 function measure()
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 return measure;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 function collect()
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 return {
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 measure = measure;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 collect = collect;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 get_stats = function ()
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 return latest_stats, changed_stats, stats_extra;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 };