Software /
code /
prosody
Comparison
core/statsmanager.lua @ 7523:3c40b972260e
Merge 0.10 -> trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 27 Jul 2016 14:08:32 +0100 |
parent | 7522:ebf2e77ac8a7 |
child | 7524:b6f32bb3b584 |
comparison
equal
deleted
inserted
replaced
7518:829ebe806e82 | 7523:3c40b972260e |
---|---|
1 | 1 |
2 local stats = require "util.statistics".new(); | |
3 local config = require "core.configmanager"; | 2 local config = require "core.configmanager"; |
4 local log = require "util.logger".init("stats"); | 3 local log = require "util.logger".init("stats"); |
5 local timer = require "util.timer"; | 4 local timer = require "util.timer"; |
6 local fire_event = prosody.events.fire_event; | 5 local fire_event = prosody.events.fire_event; |
7 | 6 |
9 local stats_interval = tonumber(stats_config); | 8 local stats_interval = tonumber(stats_config); |
10 if stats_config and not stats_interval then | 9 if stats_config and not stats_interval then |
11 log("error", "Invalid 'statistics_interval' setting, statistics will be disabled"); | 10 log("error", "Invalid 'statistics_interval' setting, statistics will be disabled"); |
12 end | 11 end |
13 | 12 |
13 local stats_provider_config = config.get("*", "statistics_provider"); | |
14 local stats_provider = stats_provider_config or "internal"; | |
15 | |
16 local builtin_providers = { | |
17 internal = "util.statistics"; | |
18 statsd = "util.statsd"; | |
19 }; | |
20 | |
21 if stats_provider:match("^library:") then | |
22 stats_provider = stats_provider:match(":(.+)$"); | |
23 else | |
24 stats_provider = builtin_providers[stats_provider]; | |
25 if not stats_provider then | |
26 log("error", "Unrecognized built-in statistics provider '%s', using internal instead", stats_provider_config); | |
27 stats_provider = builtin_providers["internal"]; | |
28 end | |
29 end | |
30 | |
31 local have_stats_provider, stats_lib = pcall(require, stats_provider); | |
32 | |
33 local stats, stats_err; | |
34 | |
35 if not have_stats_provider then | |
36 stats, stats_err = nil, stats_lib; | |
37 else | |
38 local stats_config = config.get("*", "statistics_config"); | |
39 stats, stats_err = stats_lib.new(stats_config); | |
40 end | |
41 | |
42 if not stats then | |
43 log("error", "Error loading statistics provider '%s': %s", stats_provider, stats_err); | |
44 end | |
45 | |
14 local measure, collect; | 46 local measure, collect; |
15 local latest_stats = {}; | 47 local latest_stats = {}; |
16 local changed_stats = {}; | 48 local changed_stats = {}; |
17 local stats_extra = {}; | 49 local stats_extra = {}; |
18 | 50 |
19 if stats_interval then | 51 if stats then |
20 log("debug", "Statistics collection is enabled every %d seconds", stats_interval); | |
21 function measure(type, name) | 52 function measure(type, name) |
22 local f = assert(stats[type], "unknown stat type: "..type); | 53 local f = assert(stats[type], "unknown stat type: "..type); |
23 return f(name); | 54 return f(name); |
24 end | 55 end |
56 end | |
25 | 57 |
26 local mark_collection_start = measure("times", "stats.collection"); | 58 if stats_interval then |
27 local mark_processing_start = measure("times", "stats.processing"); | 59 if stats.get_stats then |
60 log("debug", "Statistics collection is enabled every %d seconds", stats_interval); | |
28 | 61 |
29 function collect() | 62 local mark_collection_start = measure("times", "stats.collection"); |
30 local mark_collection_done = mark_collection_start(); | 63 local mark_processing_start = measure("times", "stats.processing"); |
31 fire_event("stats-update"); | 64 |
32 changed_stats, stats_extra = {}, {}; | 65 function collect() |
33 for stat_name, getter in pairs(stats.get_stats()) do | 66 local mark_collection_done = mark_collection_start(); |
34 local type, value, extra = getter(); | 67 fire_event("stats-update"); |
35 local old_value = latest_stats[stat_name]; | 68 changed_stats, stats_extra = {}, {}; |
36 latest_stats[stat_name] = value; | 69 for stat_name, getter in pairs(stats.get_stats()) do |
37 if value ~= old_value then | 70 local type, value, extra = getter(); |
38 changed_stats[stat_name] = value; | 71 local old_value = latest_stats[stat_name]; |
72 latest_stats[stat_name] = value; | |
73 if value ~= old_value then | |
74 changed_stats[stat_name] = value; | |
75 end | |
76 if extra then | |
77 stats_extra[stat_name] = extra; | |
78 end | |
39 end | 79 end |
40 if extra then | 80 mark_collection_done(); |
41 stats_extra[stat_name] = extra; | 81 local mark_processing_done = mark_processing_start(); |
42 end | 82 fire_event("stats-updated", { stats = latest_stats, changed_stats = changed_stats, stats_extra = stats_extra }); |
83 mark_processing_done(); | |
84 return stats_interval; | |
43 end | 85 end |
44 mark_collection_done(); | 86 timer.add_task(stats_interval, collect); |
45 local mark_processing_done = mark_processing_start(); | 87 prosody.events.add_handler("server-started", function () collect() end, -1); |
46 fire_event("stats-updated", { stats = latest_stats, changed_stats = changed_stats, stats_extra = stats_extra }); | 88 else |
47 mark_processing_done(); | 89 log("error", "statistics_interval specified, but the selected statistics_provider (%s) does not support statistics collection", stats_provider_config or "internal"); |
48 return stats_interval; | |
49 end | 90 end |
91 end | |
50 | 92 |
51 timer.add_task(stats_interval, collect); | 93 if not stats_interval and stats_provider == "util.statistics" then |
52 prosody.events.add_handler("server-started", function () collect() end, -1); | |
53 else | |
54 log("debug", "Statistics collection is disabled"); | 94 log("debug", "Statistics collection is disabled"); |
55 -- nop | 95 -- nop |
56 function measure() | 96 function measure() |
57 return measure; | 97 return measure; |
58 end | 98 end |
60 end | 100 end |
61 end | 101 end |
62 | 102 |
63 return { | 103 return { |
64 measure = measure; | 104 measure = measure; |
65 collect = collect; | |
66 get_stats = function () | 105 get_stats = function () |
67 return latest_stats, changed_stats, stats_extra; | 106 return latest_stats, changed_stats, stats_extra; |
68 end; | 107 end; |
69 get = function (name) | 108 get = function (name) |
70 return latest_stats[name], stats_extra[name]; | 109 return latest_stats[name], stats_extra[name]; |