Software /
code /
prosody
Annotate
spec/util_dbuffer_spec.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 | 11158:3a72cb126d6c |
child | 11636:11e0a0a08da3 |
rev | line source |
---|---|
11105 | 1 local dbuffer = require "util.dbuffer"; |
2 describe("util.dbuffer", function () | |
3 describe("#new", function () | |
4 it("has a constructor", function () | |
5 assert.Function(dbuffer.new); | |
6 end); | |
7 it("can be created", function () | |
8 assert.truthy(dbuffer.new()); | |
9 end); | |
10 it("won't create an empty buffer", function () | |
11 assert.falsy(dbuffer.new(0)); | |
12 end); | |
13 it("won't create a negatively sized buffer", function () | |
14 assert.falsy(dbuffer.new(-1)); | |
15 end); | |
16 end); | |
17 describe(":write", function () | |
18 local b = dbuffer.new(); | |
19 it("works", function () | |
20 assert.truthy(b:write("hi")); | |
21 end); | |
22 end); | |
23 | |
24 describe(":read", function () | |
25 it("supports optional bytes parameter", function () | |
26 -- should return the frontmost chunk | |
27 local b = dbuffer.new(); | |
28 assert.truthy(b:write("hello")); | |
29 assert.truthy(b:write(" ")); | |
30 assert.truthy(b:write("world")); | |
31 assert.equal("h", b:read(1)); | |
32 | |
33 assert.equal("ello", b:read()); | |
34 assert.equal(" ", b:read()); | |
35 assert.equal("world", b:read()); | |
36 end); | |
37 end); | |
38 | |
39 describe(":discard", function () | |
40 local b = dbuffer.new(); | |
41 it("works", function () | |
42 assert.truthy(b:write("hello world")); | |
43 assert.truthy(b:discard(6)); | |
44 assert.equal(5, b:length()); | |
11156
a8ef69f7fc35
util.dbuffer: Expose length as :len() method, like strings
Kim Alvefur <zash@zash.se>
parents:
11105
diff
changeset
|
45 assert.equal(5, b:len()); |
11105 | 46 assert.equal("world", b:read(5)); |
47 end); | |
48 end); | |
49 | |
50 describe(":collapse()", function () | |
51 it("works on an empty buffer", function () | |
52 local b = dbuffer.new(); | |
53 b:collapse(); | |
54 end); | |
55 end); | |
56 | |
57 describe(":sub", function () | |
58 -- Helper function to compare buffer:sub() with string:sub() | |
59 local s = "hello world"; | |
60 local function test_sub(b, x, y) | |
61 local string_result, buffer_result = s:sub(x, y), b:sub(x, y); | |
62 assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil")); | |
63 end | |
64 | |
65 it("works", function () | |
66 local b = dbuffer.new(); | |
67 assert.truthy(b:write("hello world")); | |
68 assert.equals("hello", b:sub(1, 5)); | |
69 end); | |
70 | |
71 it("works after discard", function () | |
72 local b = dbuffer.new(256); | |
73 assert.truthy(b:write("foobar")); | |
74 assert.equals("foobar", b:sub(1, 6)); | |
75 assert.truthy(b:discard(3)); -- consume "foo" | |
76 assert.equals("bar", b:sub(1, 3)); | |
77 end); | |
78 | |
79 it("supports optional end parameter", function () | |
80 local b = dbuffer.new(); | |
81 assert.truthy(b:write("hello world")); | |
82 assert.equals("hello world", b:sub(1)); | |
83 assert.equals("world", b:sub(-5)); | |
84 end); | |
85 | |
86 it("is equivalent to string:sub", function () | |
87 local b = dbuffer.new(11); | |
88 assert.truthy(b:write(s)); | |
89 for i = -13, 13 do | |
90 for j = -13, 13 do | |
91 test_sub(b, i, j); | |
92 end | |
93 end | |
94 end); | |
95 end); | |
96 | |
97 describe(":byte", function () | |
98 -- Helper function to compare buffer:byte() with string:byte() | |
99 local s = "hello world" | |
100 local function test_byte(b, x, y) | |
101 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)}; | |
102 assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil")); | |
103 end | |
104 | |
105 it("is equivalent to string:byte", function () | |
106 local b = dbuffer.new(11); | |
107 assert.truthy(b:write(s)); | |
108 test_byte(b, 1); | |
109 test_byte(b, 3); | |
110 test_byte(b, -1); | |
111 test_byte(b, -3); | |
112 for i = -13, 13 do | |
113 for j = -13, 13 do | |
114 test_byte(b, i, j); | |
115 end | |
116 end | |
117 end); | |
118 | |
119 it("works with characters > 127", function () | |
120 local b = dbuffer.new(); | |
121 b:write(string.char(0, 140)); | |
122 local r = { b:byte(1, 2) }; | |
123 assert.same({ 0, 140 }, r); | |
124 end); | |
125 | |
126 it("works on an empty buffer", function () | |
127 local b = dbuffer.new(); | |
128 assert.equal("", b:sub(1,1)); | |
129 end); | |
130 end); | |
131 end); |