diff options
author | Matthew Wild <mwild1@gmail.com> | 2016-07-29 14:58:42 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2016-07-29 14:58:42 +0100 |
commit | ac5a68baa5d74a79b96013ee2660419026a43b32 (patch) | |
tree | 47793a1698f0b6d69fa8610ec0fc20b708e96442 /core | |
parent | bf39a5e45f3a3f988aadea1dc968f66a23721767 (diff) | |
parent | 1eb4ec4922c25d0446cbdca54353085b094585e6 (diff) | |
download | prosody-ac5a68baa5d74a79b96013ee2660419026a43b32.tar.gz prosody-ac5a68baa5d74a79b96013ee2660419026a43b32.zip |
Merge 0.10 -> trunk
Diffstat (limited to 'core')
-rw-r--r-- | core/statsmanager.lua | 125 |
1 files changed, 66 insertions, 59 deletions
diff --git a/core/statsmanager.lua b/core/statsmanager.lua index 19d5860e..67702dd9 100644 --- a/core/statsmanager.lua +++ b/core/statsmanager.lua @@ -4,42 +4,53 @@ local log = require "util.logger".init("stats"); local timer = require "util.timer"; local fire_event = prosody.events.fire_event; -local stats_config = config.get("*", "statistics_interval"); -local stats_interval = tonumber(stats_config); +local stats_interval_config = config.get("*", "statistics_interval"); +local stats_interval = tonumber(stats_interval_config); if stats_config and not stats_interval then log("error", "Invalid 'statistics_interval' setting, statistics will be disabled"); end -local stats_provider_config = config.get("*", "statistics_provider"); -local stats_provider = stats_provider_config or "internal"; +local stats_provider_name; +local stats_provider_config = config.get("*", "statistics"); +local stats_provider = stats_provider_config; + +if not stats_provider and stats_interval then + stats_provider = "internal"; +elseif stats_provider and not stats_interval then + stats_interval = 60; +end local builtin_providers = { internal = "util.statistics"; statsd = "util.statsd"; }; -if stats_provider:match("^library:") then - stats_provider = stats_provider:match(":(.+)$"); -else - stats_provider = builtin_providers[stats_provider]; - if not stats_provider then - log("error", "Unrecognized built-in statistics provider '%s', using internal instead", stats_provider_config); - stats_provider = builtin_providers["internal"]; - end -end -local have_stats_provider, stats_lib = pcall(require, stats_provider); +local stats, stats_err = false, nil; -local stats, stats_err; +if stats_provider then + if stats_provider:sub(1,1) == ":" then + stats_provider = stats_provider:sub(2); + stats_provider_name = "external "..stats_provider; + elseif stats_provider then + stats_provider_name = "built-in "..stats_provider; + stats_provider = builtin_providers[stats_provider]; + if not stats_provider then + log("error", "Unrecognized statistics provider '%s', statistics will be disabled", stats_provider_config); + end + end -if not have_stats_provider then - stats, stats_err = nil, stats_lib; -else - local stats_config = config.get("*", "statistics_config"); - stats, stats_err = stats_lib.new(stats_config); + local have_stats_provider, stats_lib = pcall(require, stats_provider); + if not have_stats_provider then + stats, stats_err = nil, stats_lib; + else + local stats_config = config.get("*", "statistics_config"); + stats, stats_err = stats_lib.new(stats_config); + stats_provider_name = stats_lib._NAME or stats_provider_name; + end end -if not stats then +if stats == nil then log("error", "Error loading statistics provider '%s': %s", stats_provider, stats_err); end @@ -53,51 +64,47 @@ if stats then local f = assert(stats[type], "unknown stat type: "..type); return f(name); end -end -if stats_interval then - log("debug", "Statistics collection is enabled every %d seconds", stats_interval); - - local mark_collection_start = measure("times", "stats.collection"); - local mark_processing_start = measure("times", "stats.processing"); - - function collect() - local mark_collection_done = mark_collection_start(); - fire_event("stats-update"); - mark_collection_done(); - - if stats.get_stats then - changed_stats, stats_extra = {}, {}; - for stat_name, getter in pairs(stats.get_stats()) do - local type, value, extra = getter(); - local old_value = latest_stats[stat_name]; - latest_stats[stat_name] = value; - if value ~= old_value then - changed_stats[stat_name] = value; - end - if extra then - stats_extra[stat_name] = extra; + if stats_interval then + log("debug", "Statistics enabled using %s provider, collecting every %d seconds", stats_provider_name, stats_interval); + + local mark_collection_start = measure("times", "stats.collection"); + local mark_processing_start = measure("times", "stats.processing"); + + function collect() + local mark_collection_done = mark_collection_start(); + fire_event("stats-update"); + mark_collection_done(); + + if stats.get_stats then + changed_stats, stats_extra = {}, {}; + for stat_name, getter in pairs(stats.get_stats()) do + local type, value, extra = getter(); + local old_value = latest_stats[stat_name]; + latest_stats[stat_name] = value; + if value ~= old_value then + changed_stats[stat_name] = value; + end + if extra then + stats_extra[stat_name] = extra; + end end + local mark_processing_done = mark_processing_start(); + fire_event("stats-updated", { stats = latest_stats, changed_stats = changed_stats, stats_extra = stats_extra }); + mark_processing_done(); end - local mark_processing_done = mark_processing_start(); - fire_event("stats-updated", { stats = latest_stats, changed_stats = changed_stats, stats_extra = stats_extra }); - mark_processing_done(); + return stats_interval; end - return stats_interval; + timer.add_task(stats_interval, collect); + prosody.events.add_handler("server-started", function () collect() end, -1); + else + log("debug", "Statistics enabled using %s provider, collection is disabled", stats_provider_name); end - timer.add_task(stats_interval, collect); - prosody.events.add_handler("server-started", function () collect() end, -1); +else + log("debug", "Statistics disabled"); + function measure() return measure; end end -if not stats_interval and stats_provider == "util.statistics" then - log("debug", "Statistics collection is disabled"); - -- nop - function measure() - return measure; - end - function collect() - end -end return { measure = measure; |