Software /
code /
prosody
Comparison
util/statistics.lua @ 10883:d75d805c852f
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.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 04 Jan 2019 06:56:45 +0100 |
parent | 10314:bbc879eab1bf |
child | 11523:5f15ab7c6ae5 |
comparison
equal
deleted
inserted
replaced
10882:1999bb052d49 | 10883:d75d805c852f |
---|---|
42 | 42 |
43 | 43 |
44 local registry = {}; | 44 local registry = {}; |
45 local methods; | 45 local methods; |
46 methods = { | 46 methods = { |
47 amount = function (name, initial) | 47 amount = function (name, conf) |
48 local v = initial or 0; | 48 local v = conf and conf.initial or 0; |
49 registry[name..":amount"] = function () return "amount", v; end | 49 registry[name..":amount"] = function () |
50 return "amount", v, conf; | |
51 end | |
50 return function (new_v) v = new_v; end | 52 return function (new_v) v = new_v; end |
51 end; | 53 end; |
52 counter = function (name, initial) | 54 counter = function (name, conf) |
53 local v = initial or 0; | 55 local v = conf and conf.initial or 0; |
54 registry[name..":amount"] = function () return "amount", v; end | 56 registry[name..":amount"] = function () |
57 return "amount", v, conf; | |
58 end | |
55 return function (delta) | 59 return function (delta) |
56 v = v + delta; | 60 v = v + delta; |
57 end; | 61 end; |
58 end; | 62 end; |
59 rate = function (name) | 63 rate = function (name, conf) |
60 local since, n, total = time(), 0, 0; | 64 local since, n, total = time(), 0, 0; |
61 registry[name..":rate"] = function () | 65 registry[name..":rate"] = function () |
62 total = total + n; | 66 total = total + n; |
63 local t = time(); | 67 local t = time(); |
64 local stats = { | 68 local stats = { |
65 rate = n/(t-since); | 69 rate = n/(t-since); |
66 count = n; | 70 count = n; |
67 total = total; | 71 total = total; |
72 units = conf and conf.units; | |
73 type = conf and conf.type; | |
68 }; | 74 }; |
69 since, n = t, 0; | 75 since, n = t, 0; |
70 return "rate", stats.rate, stats; | 76 return "rate", stats.rate, stats; |
71 end; | 77 end; |
72 return function () | 78 return function () |
73 n = n + 1; | 79 n = n + 1; |
74 end; | 80 end; |
75 end; | 81 end; |
76 distribution = function (name, unit, type) | 82 distribution = function (name, conf) |
77 type = type or "distribution"; | 83 local units = conf and conf.units; |
84 local type = conf and conf.type or "distribution"; | |
78 local events, last_event = {}, 0; | 85 local events, last_event = {}, 0; |
79 local n_actual_events = 0; | 86 local n_actual_events = 0; |
80 local since = time(); | 87 local since = time(); |
81 | 88 |
82 registry[name..":"..type] = function () | 89 registry[name..":"..type] = function () |
83 local new_time = time(); | 90 local new_time = time(); |
84 local stats = get_distribution_stats(events, n_actual_events, since, new_time, unit); | 91 local stats = get_distribution_stats(events, n_actual_events, since, new_time, units); |
85 events, last_event = {}, 0; | 92 events, last_event = {}, 0; |
86 n_actual_events = 0; | 93 n_actual_events = 0; |
87 since = new_time; | 94 since = new_time; |
88 return type, stats.average, stats; | 95 return type, stats.average, stats; |
89 end; | 96 end; |
94 last_event = (last_event%duration_max_samples) + 1; | 101 last_event = (last_event%duration_max_samples) + 1; |
95 events[last_event] = value; | 102 events[last_event] = value; |
96 end | 103 end |
97 end; | 104 end; |
98 end; | 105 end; |
99 sizes = function (name) | 106 sizes = function (name, conf) |
100 return methods.distribution(name, "bytes", "size"); | 107 conf = conf or { units = "bytes", type = "size" } |
108 return methods.distribution(name, conf); | |
101 end; | 109 end; |
102 times = function (name) | 110 times = function (name, conf) |
111 local units = conf and conf.units or "seconds"; | |
103 local events, last_event = {}, 0; | 112 local events, last_event = {}, 0; |
104 local n_actual_events = 0; | 113 local n_actual_events = 0; |
105 local since = time(); | 114 local since = time(); |
106 | 115 |
107 registry[name..":duration"] = function () | 116 registry[name..":duration"] = function () |
108 local new_time = time(); | 117 local new_time = time(); |
109 local stats = get_distribution_stats(events, n_actual_events, since, new_time, "seconds"); | 118 local stats = get_distribution_stats(events, n_actual_events, since, new_time, units); |
110 events, last_event = {}, 0; | 119 events, last_event = {}, 0; |
111 n_actual_events = 0; | 120 n_actual_events = 0; |
112 since = new_time; | 121 since = new_time; |
113 return "duration", stats.average, stats; | 122 return "duration", stats.average, stats; |
114 end; | 123 end; |