Software /
code /
prosody
Annotate
util/human/units.lua @ 11523:5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
The metric subsystem of Prosody has had some shortcomings from
the perspective of the current state-of-the-art in metric
observability.
The OpenMetrics standard [0] is a formalization of the data
model (and serialization format) of the well-known and
widely-used Prometheus [1] software stack.
The previous stats subsystem of Prosody did not map well to that
format (see e.g. [2] and [3]); the key reason is that it was
trying to do too much math on its own ([2]) while lacking
first-class support for "families" of metrics ([3]) and
structured metric metadata (despite the `extra` argument to
metrics, there was no standard way of representing common things
like "tags" or "labels").
Even though OpenMetrics has grown from the Prometheus world of
monitoring, it maps well to other popular monitoring stacks
such as:
- InfluxDB (labels can be mapped to tags and fields as necessary)
- Carbon/Graphite (labels can be attached to the metric name with
dot-separation)
- StatsD (see graphite when assuming that graphite is used as
backend, which is the default)
The util.statsd module has been ported to use the OpenMetrics
model as a proof of concept. An implementation which exposes
the util.statistics backend data as Prometheus metrics is
ready for publishing in prosody-modules (most likely as
mod_openmetrics_prometheus to avoid breaking existing 0.11
deployments).
At the same time, the previous measure()-based API had one major
advantage: It is really simple and easy to use without requiring
lots of knowledge about OpenMetrics or similar concepts. For that
reason as well as compatibility with existing code, it is preserved
and may even be extended in the future.
However, code relying on the `stats-updated` event as well as
`get_stats` from `statsmanager` will break because the data
model has changed completely; in case of `stats-updated`, the
code will simply not run (as the event was renamed in order
to avoid conflicts); the `get_stats` function has been removed
completely (so it will cause a traceback when it is attempted
to be used).
Note that the measure_*_event methods have been removed from
the module API. I was unable to find any uses or documentation
and thus deemed they should not be ported. Re-implementation is
possible when necessary.
[0]: https://openmetrics.io/
[1]: https://prometheus.io/
[2]: #959
[3]: #960
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Sun, 18 Apr 2021 11:47:41 +0200 |
parent | 10903:c5f26f9adb31 |
child | 12573:0f4feaf9ca64 |
rev | line source |
---|---|
10889
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
1 local math_abs = math.abs; |
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
2 local math_ceil = math.ceil; |
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
3 local math_floor = math.floor; |
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
4 local math_log = math.log; |
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
5 local math_max = math.max; |
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
6 local math_min = math.min; |
10888
42a0d9089de9
util.human.units: Handle location of unpack() in Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10886
diff
changeset
|
7 local unpack = table.unpack or unpack; --luacheck: ignore 113 |
42a0d9089de9
util.human.units: Handle location of unpack() in Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10886
diff
changeset
|
8 |
10890
a451f80d1cea
util.human.units: Handle lack of math.log(n, base) on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10889
diff
changeset
|
9 if math_log(10, 10) ~= 1 then |
a451f80d1cea
util.human.units: Handle lack of math.log(n, base) on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10889
diff
changeset
|
10 -- Lua 5.1 COMPAT |
a451f80d1cea
util.human.units: Handle lack of math.log(n, base) on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10889
diff
changeset
|
11 local log10 = math.log10; |
a451f80d1cea
util.human.units: Handle lack of math.log(n, base) on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10889
diff
changeset
|
12 function math_log(n, base) |
a451f80d1cea
util.human.units: Handle lack of math.log(n, base) on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10889
diff
changeset
|
13 return log10(n) / log10(base); |
a451f80d1cea
util.human.units: Handle lack of math.log(n, base) on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10889
diff
changeset
|
14 end |
a451f80d1cea
util.human.units: Handle lack of math.log(n, base) on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10889
diff
changeset
|
15 end |
a451f80d1cea
util.human.units: Handle lack of math.log(n, base) on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10889
diff
changeset
|
16 |
10886
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local large = { |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 "k", 1000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 "M", 1000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 "G", 1000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 "T", 1000000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 "P", 1000000000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 "E", 1000000000000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 "Z", 1000000000000000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 "Y", 1000000000000000000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 } |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 local small = { |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 "m", 0.001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 "μ", 0.000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 "n", 0.000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 "p", 0.000000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 "f", 0.000000000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 "a", 0.000000000000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 "z", 0.000000000000000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 "y", 0.000000000000000000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 } |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 local binary = { |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 "Ki", 2^10, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 "Mi", 2^20, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 "Gi", 2^30, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 "Ti", 2^40, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 "Pi", 2^50, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 "Ei", 2^60, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 "Zi", 2^70, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 "Yi", 2^80, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 } |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 |
10903
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
49 local function adjusted_unit(n, b) |
10889
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
50 local round = math_floor; |
10886
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 local prefixes = large; |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 local logbase = 1000; |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 if b == 'b' then |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 prefixes = binary; |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 logbase = 1024; |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 elseif n < 1 then |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 prefixes = small; |
10889
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
58 round = math_ceil; |
10886
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 end |
10889
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
60 local m = math_max(0, math_min(8, round(math_abs(math_log(math_abs(n), logbase))))); |
10888
42a0d9089de9
util.human.units: Handle location of unpack() in Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10886
diff
changeset
|
61 local prefix, multiplier = unpack(prefixes, m * 2-1, m*2); |
10903
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
62 return multiplier or 1, prefix; |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
63 end |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
64 |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
65 -- n: number, the number to format |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
66 -- unit: string, the base unit |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
67 -- b: optional enum 'b', thousands base |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
68 local function format(n, unit, b) --> string |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
69 local fmt = "%.3g %s%s"; |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
70 if n == 0 then |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
71 return fmt:format(n, "", unit); |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
72 end |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
73 local multiplier, prefix = adjusted_unit(n, b); |
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
74 return fmt:format(n / multiplier, prefix or "", unit); |
10886
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 return { |
10903
c5f26f9adb31
util.human.units: Factor out function for getting multiplier
Kim Alvefur <zash@zash.se>
parents:
10890
diff
changeset
|
78 adjust = adjusted_unit; |
10886
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 format = format; |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 }; |