Annotate

util/openmetrics.lua @ 11749:83d6d6a70edf

net.http: fail open if surrounding code does not configure TLS Previously, if surrounding code was not configuring the TLS context used default in net.http, it would not validate certificates at all. This is not a security issue with prosody, because prosody updates the context with `verify = "peer"` as well as paths to CA certificates in util.startup.init_http_client. Nevertheless... Let's not leave this pitfall out there in the open.
author Jonas Schäfer <jonas@wielicki.name>
date Sun, 29 Aug 2021 15:04:47 +0200
parent 11600:a02c277eb97a
child 11929:85d51bfcf56b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
1 --[[
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
2 This module implements a subset of the OpenMetrics Internet Draft version 00.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
3
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
4 URL: https://tools.ietf.org/html/draft-richih-opsawg-openmetrics-00
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
5
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
6 The following metric types are supported:
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
7
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
8 - Counter
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
9 - Gauge
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
10 - Histogram
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
11 - Summary
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
12
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
13 It is used by util.statsd and util.statistics to provite the OpenMetrics API.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
14
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
15 To understand what this module is about, it is useful to familiarize oneself
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
16 with the terms MetricFamily, Metric, LabelSet, Label and MetricPoint as
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
17 defined in the I-D linked above.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
18 --]]
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
19 -- metric constructor interface:
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
20 -- metric_ctor(..., family_name, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
21
7988
dc758422d896 util.statistics,statsd,throttle,timer: Replace dependency on LuaSockect with util.time for precision time
Kim Alvefur <zash@zash.se>
parents: 6649
diff changeset
22 local time = require "util.time".now;
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
23 local select = select;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
24 local array = require "util.array";
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
25 local log = require "util.logger".init("util.openmetrics");
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
26 local new_multitable = require "util.multitable".new;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
27 local iter_multitable = require "util.multitable".iter;
11600
a02c277eb97a util.openmetrics: Use pack from util.table, detect appropriate unpack for Lua 5.1 (thanks sups)
Matthew Wild <mwild1@gmail.com>
parents: 11593
diff changeset
28 local t_pack, t_unpack = require "util.table".pack, table.unpack or unpack; --luacheck: ignore 113/unpack
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
30 -- BEGIN of Utility: "metric proxy"
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
31 -- This allows to wrap a MetricFamily in a proxy which only provides the
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
32 -- `with_labels` and `with_partial_label` methods. This allows to pre-set one
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
33 -- or more labels on a metric family. This is used in particular via
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
34 -- `with_partial_label` by the moduleapi in order to pre-set the `host` label
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
35 -- on metrics created in non-global modules.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
36 local metric_proxy_mt = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
37 metric_proxy_mt.__index = metric_proxy_mt
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
38
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
39 local function new_metric_proxy(metric_family, with_labels_proxy_fun)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
40 return {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
41 _family = metric_family,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
42 with_labels = function(self, ...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
43 return with_labels_proxy_fun(self._family, ...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
44 end;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
45 with_partial_label = function(self, label)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
46 return new_metric_proxy(self._family, function(family, ...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
47 return family:with_labels(label, ...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
48 end)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
49 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
50 }
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 -- END of Utility: "metric proxy"
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
54
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
55 local function render_histogram_le(v)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
56 if v == 1/0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
57 -- I-D-00: 4.1.2.2.1:
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
58 -- Exposers MUST produce output for positive infinity as +Inf.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
59 return "+Inf"
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
61
11593
0db763f3f3be util.openmetrics: Prettify format of histogram buckets
Kim Alvefur <zash@zash.se>
parents: 11523
diff changeset
62 return string.format("%.14g", v)
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
63 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
64
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
65 -- BEGIN of generic MetricFamily implementation
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
66
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
67 local metric_family_mt = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
68 metric_family_mt.__index = metric_family_mt
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
69
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
70 local function histogram_metric_ctor(orig_ctor, buckets)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
71 return function(family_name, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
72 return orig_ctor(buckets, family_name, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
73 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
76 local function new_metric_family(backend, type_, family_name, unit, description, label_keys, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
77 local metric_ctor = assert(backend[type_], "statistics backend does not support "..type_.." metrics families")
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
78 local labels = label_keys or {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
79 local user_labels = #labels
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
80 if type_ == "histogram" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
81 local buckets = extra and extra.buckets
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
82 if not buckets then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
83 error("no buckets given for histogram metric")
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
84 end
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
85 buckets = array(buckets)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
86 buckets:push(1/0) -- must have +inf bucket
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
87
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
88 metric_ctor = histogram_metric_ctor(metric_ctor, buckets)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
89 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
90
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
91 local data
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
92 if #labels == 0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
93 data = metric_ctor(family_name, nil, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
94 else
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
95 data = new_multitable()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
96 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
97
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
98 local mf = {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
99 family_name = family_name,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
100 data = data,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
101 type_ = type_,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
102 unit = unit,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
103 description = description,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
104 user_labels = user_labels,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
105 label_keys = labels,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
106 extra = extra,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
107 _metric_ctor = metric_ctor,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
108 }
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
109 setmetatable(mf, metric_family_mt);
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
110 return mf
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
111 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
112
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
113 function metric_family_mt:new_metric(labels)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
114 return self._metric_ctor(self.family_name, labels, self.extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
115 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
116
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
117 function metric_family_mt:clear()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
118 for _, metric in self:iter_metrics() do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
119 metric:reset()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
120 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
121 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
122
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
123 function metric_family_mt:with_labels(...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
124 local count = select('#', ...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
125 if count ~= self.user_labels then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
126 error("number of labels passed to with_labels does not match number of label keys")
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
127 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
128 if count == 0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
129 return self.data
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
130 end
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
131 local metric = self.data:get(...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
132 if not metric then
11600
a02c277eb97a util.openmetrics: Use pack from util.table, detect appropriate unpack for Lua 5.1 (thanks sups)
Matthew Wild <mwild1@gmail.com>
parents: 11593
diff changeset
133 local values = t_pack(...)
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
134 metric = self:new_metric(values)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
135 values[values.n+1] = metric
11600
a02c277eb97a util.openmetrics: Use pack from util.table, detect appropriate unpack for Lua 5.1 (thanks sups)
Matthew Wild <mwild1@gmail.com>
parents: 11593
diff changeset
136 self.data:set(t_unpack(values, 1, values.n+1))
11523
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 return metric
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
139 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
140
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
141 function metric_family_mt:with_partial_label(label)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
142 return new_metric_proxy(self, function (family, ...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
143 return family:with_labels(label, ...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
144 end)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
145 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
146
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
147 function metric_family_mt:iter_metrics()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
148 if #self.label_keys == 0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
149 local done = false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
150 return function()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
151 if done then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
152 return nil
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
153 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
154 done = true
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
155 return {}, self.data
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
156 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
157 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
158 local searchkeys = {};
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
159 local nlabels = #self.label_keys
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
160 for i=1,nlabels do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
161 searchkeys[i] = nil;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
162 end
11600
a02c277eb97a util.openmetrics: Use pack from util.table, detect appropriate unpack for Lua 5.1 (thanks sups)
Matthew Wild <mwild1@gmail.com>
parents: 11593
diff changeset
163 local it, state = iter_multitable(self.data, t_unpack(searchkeys, 1, nlabels))
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
164 return function(_s)
11600
a02c277eb97a util.openmetrics: Use pack from util.table, detect appropriate unpack for Lua 5.1 (thanks sups)
Matthew Wild <mwild1@gmail.com>
parents: 11593
diff changeset
165 local label_values = t_pack(it(_s))
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
166 if label_values.n == 0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
167 return nil, nil
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
168 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
169 local metric = label_values[label_values.n]
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
170 label_values[label_values.n] = nil
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
171 label_values.n = label_values.n - 1
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
172 return label_values, metric
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
173 end, state
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
174 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
175
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
176 -- END of generic MetricFamily implementation
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
177
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
178 -- BEGIN of MetricRegistry implementation
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
179
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
180
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
181 -- Helper to test whether two metrics are "equal".
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
182 local function equal_metric_family(mf1, mf2)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
183 if mf1.type_ ~= mf2.type_ then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
184 return false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
185 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
186 if #mf1.label_keys ~= #mf2.label_keys then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
187 return false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
188 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
189 -- Ignoring unit here because in general it'll be part of the name anyway
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
190 -- So either the unit was moved into/out of the name (which is a valid)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
191 -- thing to do on an upgrade or we would expect not to see any conflicts
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
192 -- anyway.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
193 --[[
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
194 if mf1.unit ~= mf2.unit then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
195 return false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
196 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
197 ]]
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
198 for i, key in ipairs(mf1.label_keys) do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
199 if key ~= mf2.label_keys[i] then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
200 return false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
201 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
202 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
203 return true
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
204 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
206 -- If the unit is not empty, add it to the full name as per the I-D spec.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
207 local function compose_name(name, unit)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
208 local full_name = name
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
209 if unit and unit ~= "" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
210 full_name = full_name .. "_" .. unit
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
211 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
212 -- TODO: prohibit certain suffixes used by metrics if where they may cause
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
213 -- conflicts
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
214 return full_name
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
215 end
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
216
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
217 local metric_registry_mt = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
218 metric_registry_mt.__index = metric_registry_mt
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
220 local function new_metric_registry(backend)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
221 local reg = {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
222 families = {},
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
223 backend = backend,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
224 }
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
225 setmetatable(reg, metric_registry_mt)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
226 return reg
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 end
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
229 function metric_registry_mt:register_metric_family(name, metric_family)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
230 local existing = self.families[name];
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
231 if existing then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
232 if not equal_metric_family(metric_family, existing) then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
233 -- We could either be strict about this, or replace the
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
234 -- existing metric family with the new one.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
235 -- Being strict is nice to avoid programming errors /
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
236 -- conflicts, but causes issues when a new version of a module
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
237 -- is loaded.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
238 --
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
239 -- We will thus assume that the new metric is the correct one;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
240 -- That is probably OK because unless you're reaching down into
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
241 -- the util.openmetrics or core.statsmanager API, your metric
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
242 -- name is going to be scoped to `prosody_mod_$modulename`
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
243 -- anyway and the damage is thus controlled.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
244 --
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
245 -- To make debugging such issues easier, we still log.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
246 log("debug", "replacing incompatible existing metric family %s", name)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
247 -- Below is the code to be strict.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
248 --error("conflicting declarations for metric family "..name)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
249 else
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
250 return existing
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 end
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
252 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
253 self.families[name] = metric_family
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
254 return metric_family
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
255 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
256
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
257 function metric_registry_mt:gauge(name, unit, description, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
258 name = compose_name(name, unit)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
259 local mf = new_metric_family(self.backend, "gauge", name, unit, description, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
260 mf = self:register_metric_family(name, mf)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
261 return mf
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
262 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
264 function metric_registry_mt:counter(name, unit, description, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
265 name = compose_name(name, unit)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
266 local mf = new_metric_family(self.backend, "counter", name, unit, description, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
267 mf = self:register_metric_family(name, mf)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
268 return mf
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
269 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
270
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
271 function metric_registry_mt:histogram(name, unit, description, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
272 name = compose_name(name, unit)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
273 local mf = new_metric_family(self.backend, "histogram", name, unit, description, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
274 mf = self:register_metric_family(name, mf)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
275 return mf
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
276 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
277
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
278 function metric_registry_mt:summary(name, unit, description, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
279 name = compose_name(name, unit)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
280 local mf = new_metric_family(self.backend, "summary", name, unit, description, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
281 mf = self:register_metric_family(name, mf)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
282 return mf
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
283 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
285 function metric_registry_mt:get_metric_families()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
286 return self.families
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
287 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
288
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
289 -- END of MetricRegistry implementation
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
290
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
291 -- BEGIN of general helpers for implementing high-level APIs on top of OpenMetrics
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
292
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
293 local function timed(metric)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
294 local t0 = time()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
295 local submitter = assert(metric.sample or metric.set, "metric type cannot be used with timed()")
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
296 return function()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
297 local t1 = time()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
298 submitter(metric, t1-t0)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
299 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
300 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
301
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
302 -- END of general helpers
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
303
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
304 return {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
305 new_metric_proxy = new_metric_proxy;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
306 new_metric_registry = new_metric_registry;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
307 render_histogram_le = render_histogram_le;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
308 timed = timed;
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 }