Annotate

core/statsmanager.lua @ 7524:b6f32bb3b584

statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
author Matthew Wild <mwild1@gmail.com>
date Wed, 27 Jul 2016 15:01:28 +0100
parent 7522:ebf2e77ac8a7
child 7533:4ef37ac69562
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 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
3 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
4 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
5 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
6
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 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
8 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
9 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
10 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
11 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
7521
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
13 local stats_provider_config = config.get("*", "statistics_provider");
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
14 local stats_provider = stats_provider_config or "internal";
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
15
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
16 local builtin_providers = {
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
17 internal = "util.statistics";
7522
ebf2e77ac8a7 statsmanager, util.statsd: Add built-in statsd provider
Matthew Wild <mwild1@gmail.com>
parents: 7521
diff changeset
18 statsd = "util.statsd";
7521
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
19 };
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
20
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
21 if stats_provider:match("^library:") then
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
22 stats_provider = stats_provider:match(":(.+)$");
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
23 else
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
24 stats_provider = builtin_providers[stats_provider];
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
25 if not stats_provider then
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
26 log("error", "Unrecognized built-in statistics provider '%s', using internal instead", stats_provider_config);
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
27 stats_provider = builtin_providers["internal"];
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
28 end
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
29 end
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
30
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
31 local have_stats_provider, stats_lib = pcall(require, stats_provider);
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
32
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
33 local stats, stats_err;
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
34
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
35 if not have_stats_provider then
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
36 stats, stats_err = nil, stats_lib;
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
37 else
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
38 local stats_config = config.get("*", "statistics_config");
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
39 stats, stats_err = stats_lib.new(stats_config);
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
40 end
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
41
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
42 if not stats then
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
43 log("error", "Error loading statistics provider '%s': %s", stats_provider, stats_err);
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
44 end
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
45
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 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
47 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
48 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
49 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
50
7521
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
51 if stats then
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 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
53 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
54 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
55 end
7521
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
56 end
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
7521
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
58 if stats_interval then
7524
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
59 log("debug", "Statistics collection is enabled every %d seconds", stats_interval);
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
60
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
61 local mark_collection_start = measure("times", "stats.collection");
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
62 local mark_processing_start = measure("times", "stats.processing");
7521
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
63
7524
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
64 function collect()
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
65 local mark_collection_done = mark_collection_start();
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
66 fire_event("stats-update");
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
67 mark_collection_done();
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
7524
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
69 if stats.get_stats then
7521
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
70 changed_stats, stats_extra = {}, {};
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
71 for stat_name, getter in pairs(stats.get_stats()) do
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
72 local type, value, extra = getter();
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
73 local old_value = latest_stats[stat_name];
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
74 latest_stats[stat_name] = value;
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
75 if value ~= old_value then
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
76 changed_stats[stat_name] = value;
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
77 end
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
78 if extra then
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
79 stats_extra[stat_name] = extra;
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
80 end
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
7521
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
82 local mark_processing_done = mark_processing_start();
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
83 fire_event("stats-updated", { stats = latest_stats, changed_stats = changed_stats, stats_extra = stats_extra });
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
84 mark_processing_done();
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
7524
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
86 return stats_interval;
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
7524
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
88 timer.add_task(stats_interval, collect);
b6f32bb3b584 statsmanager: If provider doesn't offer a get_stats method, still fire the stats-update event if stats_interval is specified (however stats-updated will not fire)
Matthew Wild <mwild1@gmail.com>
parents: 7522
diff changeset
89 prosody.events.add_handler("server-started", function () collect() end, -1);
7521
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
90 end
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
7521
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6910
diff changeset
92 if not stats_interval and stats_provider == "util.statistics" then
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 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
94 -- nop
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 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
96 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
97 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 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
99 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 return {
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 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
104 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
105 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
106 end;
6910
82765a4ec799 statsmanager: Add get() method
Matthew Wild <mwild1@gmail.com>
parents: 6582
diff changeset
107 get = function (name)
82765a4ec799 statsmanager: Add get() method
Matthew Wild <mwild1@gmail.com>
parents: 6582
diff changeset
108 return latest_stats[name], stats_extra[name];
82765a4ec799 statsmanager: Add get() method
Matthew Wild <mwild1@gmail.com>
parents: 6582
diff changeset
109 end;
6554
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 };