core/statsmanager.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 27 Jul 2016 14:06:10 +0100
changeset 7525 ebf2e77ac8a7
parent 7524 1c8b63fe6472
child 7527 b6f32bb3b584
permissions -rw-r--r--
statsmanager, util.statsd: Add built-in statsd provider
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6557
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
7524
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
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: 6913
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: 6913
diff changeset
    15
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
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: 6913
diff changeset
    17
	internal = "util.statistics";
7525
ebf2e77ac8a7 statsmanager, util.statsd: Add built-in statsd provider
Matthew Wild <mwild1@gmail.com>
parents: 7524
diff changeset
    18
	statsd = "util.statsd";
7524
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    19
};
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    20
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
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: 6913
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: 6913
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: 6913
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: 6913
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: 6913
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: 6913
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: 6913
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: 6913
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: 6913
diff changeset
    30
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
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: 6913
diff changeset
    32
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
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: 6913
diff changeset
    34
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
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: 6913
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: 6913
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: 6913
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: 6913
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: 6913
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: 6913
diff changeset
    41
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
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: 6913
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: 6913
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: 6913
diff changeset
    45
6557
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
7524
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    51
if stats then
6557
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
7524
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    56
end
6557
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
7524
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    58
if stats_interval then
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    59
	if stats.get_stats then
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    60
		log("debug", "Statistics collection is enabled every %d seconds", stats_interval);
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    61
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    62
		local mark_collection_start = measure("times", "stats.collection");
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    63
		local mark_processing_start = measure("times", "stats.processing");
6557
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
7524
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    65
		function collect()
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    66
			local mark_collection_done = mark_collection_start();
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    67
			fire_event("stats-update");
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    68
			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: 6913
diff changeset
    69
			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: 6913
diff changeset
    70
				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: 6913
diff changeset
    71
				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: 6913
diff changeset
    72
				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: 6913
diff changeset
    73
				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: 6913
diff changeset
    74
					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: 6913
diff changeset
    75
				end
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    76
				if extra then
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    77
					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: 6913
diff changeset
    78
				end
6557
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
			end
7524
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    80
			mark_collection_done();
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    81
			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: 6913
diff changeset
    82
			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: 6913
diff changeset
    83
			mark_processing_done();
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    84
			return stats_interval;
6557
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
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    86
		timer.add_task(stats_interval, collect);
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    87
		prosody.events.add_handler("server-started", function () collect() end, -1);
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    88
	else
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    89
		log("error", "statistics_interval specified, but the selected statistics_provider (%s) does not support statistics collection", stats_provider_config or "internal");
6557
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
	end
7524
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    91
end
6557
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
7524
1c8b63fe6472 statsmanager: Add 'stats_provider' option, to allow selecting alternative API providers to util.statistics
Matthew Wild <mwild1@gmail.com>
parents: 6913
diff changeset
    93
if not stats_interval and stats_provider == "util.statistics" then
6557
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
	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
    95
	-- nop
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
	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
    97
		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
    98
	end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
	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
   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
end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
return {
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
	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
   105
	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
   106
		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
   107
	end;
6913
82765a4ec799 statsmanager: Add get() method
Matthew Wild <mwild1@gmail.com>
parents: 6585
diff changeset
   108
	get = function (name)
82765a4ec799 statsmanager: Add get() method
Matthew Wild <mwild1@gmail.com>
parents: 6585
diff changeset
   109
		return latest_stats[name], stats_extra[name];
82765a4ec799 statsmanager: Add get() method
Matthew Wild <mwild1@gmail.com>
parents: 6585
diff changeset
   110
	end;
6557
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
};