Annotate

util/statistics.lua @ 13135:3fd24e1945b0

mod_storage_internal: Lazy-load archive items while iterating Very large list files previously ran into limits of the Lua parser, or just caused Prosody to freeze while parsing. Using the new index we can parse individual items one at a time. This probably won't reduce overall CPU usage, probably the opposite, but it will reduce the number of items in memory at once and allow collection of items after we iterated past them.
author Kim Alvefur <zash@zash.se>
date Wed, 12 May 2021 01:25:44 +0200
parent 12975:d10957394a3c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12975
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12124
diff changeset
1 local time = require "prosody.util.time".now;
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12124
diff changeset
2 local new_metric_registry = require "prosody.util.openmetrics".new_metric_registry;
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12124
diff changeset
3 local render_histogram_le = require "prosody.util.openmetrics".render_histogram_le;
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
5 -- BEGIN of Metric implementations
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
6
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
7 -- Gauges
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
8 local gauge_metric_mt = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
9 gauge_metric_mt.__index = gauge_metric_mt
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
10
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
11 local function new_gauge_metric()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
12 local metric = { value = 0 }
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
13 setmetatable(metric, gauge_metric_mt)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
14 return metric
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
15 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
16
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
17 function gauge_metric_mt:set(value)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
18 self.value = value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
19 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
20
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
21 function gauge_metric_mt:add(delta)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
22 self.value = self.value + delta
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
23 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
24
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
25 function gauge_metric_mt:reset()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
26 self.value = 0
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
27 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
29 function gauge_metric_mt:iter_samples()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
30 local done = false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
31 return function(_s)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
32 if done then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
33 return nil, true
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
34 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
35 done = true
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
36 return "", nil, _s.value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
37 end, self
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
38 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
39
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
40 -- Counters
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
41 local counter_metric_mt = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
42 counter_metric_mt.__index = counter_metric_mt
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
43
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
44 local function new_counter_metric()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
45 local metric = {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
46 _created = time(),
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
47 value = 0,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
48 }
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
49 setmetatable(metric, counter_metric_mt)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
50 return metric
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
51 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
52
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
53 function counter_metric_mt:set(value)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
54 self.value = value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
55 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
56
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
57 function counter_metric_mt:add(value)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
58 self.value = (self.value or 0) + value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
59 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
60
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
61 function counter_metric_mt:iter_samples()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
62 local step = 0
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
63 return function(_s)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
64 step = step + 1
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
65 if step == 1 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
66 return "_created", nil, _s._created
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
67 elseif step == 2 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
68 return "_total", nil, _s.value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
69 else
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
70 return nil, nil, true
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
71 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
72 end, self
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
73 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
74
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
75 function counter_metric_mt:reset()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
76 self.value = 0
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
79 -- Histograms
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
80 local histogram_metric_mt = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
81 histogram_metric_mt.__index = histogram_metric_mt
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
83 local function new_histogram_metric(buckets)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
84 local metric = {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
85 _created = time(),
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
86 _sum = 0,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
87 _count = 0,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
88 }
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
89 -- the order of buckets matters unfortunately, so we cannot directly use
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
90 -- the thresholds as table keys
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
91 for i, threshold in ipairs(buckets) do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
92 metric[i] = {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
93 threshold = threshold,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
94 threshold_s = render_histogram_le(threshold),
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
95 count = 0
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
96 }
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
97 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
98 setmetatable(metric, histogram_metric_mt)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
99 return metric
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
100 end
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
101
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
102 function histogram_metric_mt:sample(value)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
103 -- According to the I-D, values must be part of all buckets
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
104 for i, bucket in pairs(self) do
12124
7d985e5bc1fb openmetrics/histograms: improve code clarity
Kim Alvefur <zash@zash.se>
parents: 12123
diff changeset
105 if "number" == type(i) and value <= bucket.threshold then
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
106 bucket.count = bucket.count + 1
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
107 end
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
108 end
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
109 self._sum = self._sum + value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
110 self._count = self._count + 1
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
111 end
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
112
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
113 function histogram_metric_mt:iter_samples()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
114 local key = nil
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
115 return function (_s)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
116 local data
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
117 key, data = next(_s, key)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
118 if key == "_created" or key == "_sum" or key == "_count" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
119 return key, nil, data
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
120 elseif key ~= nil then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
121 return "_bucket", {["le"] = data.threshold_s}, data.count
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
122 else
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
123 return nil, nil, nil
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
124 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
125 end, self
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
126 end
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
127
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
128 function histogram_metric_mt:reset()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
129 self._created = time()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
130 self._count = 0
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
131 self._sum = 0
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
132 for i, bucket in pairs(self) do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
133 if "number" == type(i) then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
134 bucket.count = 0
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
135 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
136 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
137 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
138
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
139 -- Summary
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
140 local summary_metric_mt = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
141 summary_metric_mt.__index = summary_metric_mt
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
142
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
143 local function new_summary_metric()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
144 -- quantiles are not supported yet
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
145 local metric = {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
146 _created = time(),
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
147 _sum = 0,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
148 _count = 0,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
149 }
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
150 setmetatable(metric, summary_metric_mt)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
151 return metric
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
152 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
154 function summary_metric_mt:sample(value)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
155 self._sum = self._sum + value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
156 self._count = self._count + 1
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
157 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
159 function summary_metric_mt:iter_samples()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
160 local key = nil
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
161 return function (_s)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
162 local data
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
163 key, data = next(_s, key)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
164 return key, nil, data
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
165 end, self
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
166 end
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
167
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
168 function summary_metric_mt:reset()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
169 self._created = time()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
170 self._count = 0
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
171 self._sum = 0
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
172 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
174 local pull_backend = {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
175 gauge = new_gauge_metric,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
176 counter = new_counter_metric,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
177 histogram = new_histogram_metric,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
178 summary = new_summary_metric,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
179 }
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
181 -- END of Metric implementations
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
182
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
183 local function new()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
184 return {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
185 metric_registry = new_metric_registry(pull_backend),
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
186 }
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 end
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 return {
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
190 new = new;
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 }