core/statsmanager.lua
author Matthew Wild <mwild1@gmail.com>
Tue, 20 Jan 2015 12:33:20 +0000
changeset 6557 6c22bec3e8d0
child 6558 7b2d16c14659
permissions -rw-r--r--
statsmanager, prosody: New core module and API for gathering statistics about the running server
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 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
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
	local mark_collection_start = measure("duration", "stats.collection_time");
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
	local mark_processing_start = measure("duration", "stats.processing_time");
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 = {}, {};
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
		for name, getter in pairs(stats.get_stats()) do
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 stat_name = name..":"..type;
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
				print(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
				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
    43
				if type == "duration" then
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 rate = extra.rate;
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 rate_name = name..":rate";
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
					latest_stats[rate_name] = rate;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
					changed_stats[rate_name] = rate;
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
			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
		end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
		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
    52
		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
    53
		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
    54
		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
    55
		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
    56
	end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
	timer.add_task(stats_interval, 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
else
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
	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
    61
	-- nop
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
	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
    63
		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
    64
	end
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
	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
    66
	end
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
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
return {
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
	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
    71
	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
    72
	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
    73
		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
    74
	end;
6c22bec3e8d0 statsmanager, prosody: New core module and API for gathering statistics about the running server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
};