Software /
code /
prosody
Annotate
core/statsmanager.lua @ 6581:f2a7ad099e01
Merge 0.10->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 24 Feb 2015 15:22:34 +0000 |
parent | 6577:1e749832ed3b |
child | 6582:71b5de1d830b |
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(); |
6c22bec3e8d0
statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 changed_stats, stats_extra = {}, {}; |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6554
diff
changeset
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 end |
6c22bec3e8d0
statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 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
|
40 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
|
41 end |
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 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 end |
6c22bec3e8d0
statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 |
6c22bec3e8d0
statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 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
|
51 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
|
52 else |
6c22bec3e8d0
statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 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
|
54 -- nop |
6c22bec3e8d0
statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 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
|
56 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
|
57 end |
6c22bec3e8d0
statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 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
|
59 end |
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 |
6c22bec3e8d0
statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 return { |
6c22bec3e8d0
statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 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
|
67 end; |
6c22bec3e8d0
statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 }; |