aboutsummaryrefslogtreecommitdiffstats
path: root/util/statistics.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2019-01-04 06:56:45 +0100
committerKim Alvefur <zash@zash.se>2019-01-04 06:56:45 +0100
commitf10b1ff16550191f7b2be04073bec6fccfbf741b (patch)
tree70a90dcfd68ca314b29918526dd35196c8b05ce9 /util/statistics.lua
parent1deae0e925edc5b83e1ca509be2d72bc1d686130 (diff)
downloadprosody-f10b1ff16550191f7b2be04073bec6fccfbf741b.tar.gz
prosody-f10b1ff16550191f7b2be04073bec6fccfbf741b.zip
util.statistics: Unify API of methods to include a config table
The primary goal here is to allow specifying an unit that each statistic is measured in.
Diffstat (limited to 'util/statistics.lua')
-rw-r--r--util/statistics.lua37
1 files changed, 23 insertions, 14 deletions
diff --git a/util/statistics.lua b/util/statistics.lua
index 0ec88e21..db608217 100644
--- a/util/statistics.lua
+++ b/util/statistics.lua
@@ -44,19 +44,23 @@ local function new_registry(config)
local registry = {};
local methods;
methods = {
- amount = function (name, initial)
- local v = initial or 0;
- registry[name..":amount"] = function () return "amount", v; end
+ amount = function (name, conf)
+ local v = conf and conf.initial or 0;
+ registry[name..":amount"] = function ()
+ return "amount", v, conf;
+ end
return function (new_v) v = new_v; end
end;
- counter = function (name, initial)
- local v = initial or 0;
- registry[name..":amount"] = function () return "amount", v; end
+ counter = function (name, conf)
+ local v = conf and conf.initial or 0;
+ registry[name..":amount"] = function ()
+ return "amount", v, conf;
+ end
return function (delta)
v = v + delta;
end;
end;
- rate = function (name)
+ rate = function (name, conf)
local since, n, total = time(), 0, 0;
registry[name..":rate"] = function ()
total = total + n;
@@ -65,6 +69,8 @@ local function new_registry(config)
rate = n/(t-since);
count = n;
total = total;
+ units = conf and conf.units;
+ type = conf and conf.type;
};
since, n = t, 0;
return "rate", stats.rate, stats;
@@ -73,15 +79,16 @@ local function new_registry(config)
n = n + 1;
end;
end;
- distribution = function (name, unit, type)
- type = type or "distribution";
+ distribution = function (name, conf)
+ local units = conf and conf.units;
+ local type = conf and conf.type or "distribution";
local events, last_event = {}, 0;
local n_actual_events = 0;
local since = time();
registry[name..":"..type] = function ()
local new_time = time();
- local stats = get_distribution_stats(events, n_actual_events, since, new_time, unit);
+ local stats = get_distribution_stats(events, n_actual_events, since, new_time, units);
events, last_event = {}, 0;
n_actual_events = 0;
since = new_time;
@@ -96,17 +103,19 @@ local function new_registry(config)
end
end;
end;
- sizes = function (name)
- return methods.distribution(name, "bytes", "size");
+ sizes = function (name, conf)
+ conf = conf or { units = "bytes", type = "size" }
+ return methods.distribution(name, conf);
end;
- times = function (name)
+ times = function (name, conf)
+ local units = conf and conf.units or "seconds";
local events, last_event = {}, 0;
local n_actual_events = 0;
local since = time();
registry[name..":duration"] = function ()
local new_time = time();
- local stats = get_distribution_stats(events, n_actual_events, since, new_time, "seconds");
+ local stats = get_distribution_stats(events, n_actual_events, since, new_time, units);
events, last_event = {}, 0;
n_actual_events = 0;
since = new_time;