Annotate

util/openmetrics.lua @ 12391:a15647d42880 0.12

mod_invites_register: Push invitee contact entry to inviter Invitee would not show up in the roster of the invite creator unless they fetch their roster afterwards. Fixes #1715 Thanks gerald
author Kim Alvefur <zash@zash.se>
date Tue, 15 Mar 2022 21:59:51 +0100
parent 12387:05c250fa335a
child 12534:3caff1f93520
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
12387
05c250fa335a Spelling: Fix various spelling mistakes (thanks timeless)
Kim Alvefur <zash@zash.se>
parents: 11929
diff changeset
13 It is used by util.statsd and util.statistics to provide the OpenMetrics API.
11523
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;
11929
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
28 local t_concat, t_insert = table.concat, table.insert;
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
29 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
30
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
31 -- BEGIN of Utility: "metric proxy"
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
32 -- 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
33 -- `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
34 -- 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
35 -- `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
36 -- 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
37 local metric_proxy_mt = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
38 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
39
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
40 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
41 return {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
42 _family = metric_family,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
43 with_labels = function(self, ...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
44 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
45 end;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
46 with_partial_label = function(self, label)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
47 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
48 return family:with_labels(label, ...)
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 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
51 }
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
52 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
53
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
54 -- END of Utility: "metric proxy"
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
55
11929
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
56 -- BEGIN Rendering helper functions (internal)
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
57
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
58 local function escape(text)
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
59 return text:gsub("\\", "\\\\"):gsub("\"", "\\\""):gsub("\n", "\\n");
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
60 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
61
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
62 local function escape_name(name)
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
63 return name:gsub("/", "__"):gsub("[^A-Za-z0-9_]", "_"):gsub("^[^A-Za-z_]", "_%1");
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
64 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
65
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
66 local function repr_help(metric, docstring)
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
67 docstring = docstring:gsub("\\", "\\\\"):gsub("\n", "\\n");
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
68 return "# HELP "..escape_name(metric).." "..docstring.."\n";
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
69 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
70
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
71 local function repr_unit(metric, unit)
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
72 if not unit then
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
73 unit = ""
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
74 else
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
75 unit = unit:gsub("\\", "\\\\"):gsub("\n", "\\n");
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
76 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
77 return "# UNIT "..escape_name(metric).." "..unit.."\n";
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
78 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
79
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
80 -- local allowed_types = { counter = true, gauge = true, histogram = true, summary = true, untyped = true };
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
81 -- local allowed_types = { "counter", "gauge", "histogram", "summary", "untyped" };
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
82 local function repr_type(metric, type_)
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
83 -- if not allowed_types:contains(type_) then
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
84 -- return;
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
85 -- end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
86 return "# TYPE "..escape_name(metric).." "..type_.."\n";
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
87 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
88
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
89 local function repr_label(key, value)
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
90 return key.."=\""..escape(value).."\"";
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
91 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
92
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
93 local function repr_labels(labelkeys, labelvalues, extra_labels)
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
94 local values = {}
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
95 if labelkeys then
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
96 for i, key in ipairs(labelkeys) do
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
97 local value = labelvalues[i]
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
98 t_insert(values, repr_label(escape_name(key), escape(value)));
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
99 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
100 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
101 if extra_labels then
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
102 for key, value in pairs(extra_labels) do
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
103 t_insert(values, repr_label(escape_name(key), escape(value)));
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
104 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
105 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
106 if #values == 0 then
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
107 return "";
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
108 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
109 return "{"..t_concat(values, ",").."}";
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
110 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
111
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
112 local function repr_sample(metric, labelkeys, labelvalues, extra_labels, value)
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
113 return escape_name(metric)..repr_labels(labelkeys, labelvalues, extra_labels).." "..string.format("%.17g", value).."\n";
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
114 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
115
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
116 -- END Rendering helper functions (internal)
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
117
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
118 local function render_histogram_le(v)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
119 if v == 1/0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
120 -- 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
121 -- 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
122 return "+Inf"
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
124
11593
0db763f3f3be util.openmetrics: Prettify format of histogram buckets
Kim Alvefur <zash@zash.se>
parents: 11523
diff changeset
125 return string.format("%.14g", v)
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
126 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
127
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
128 -- BEGIN of generic MetricFamily implementation
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
129
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
130 local metric_family_mt = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
131 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
132
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
133 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
134 return function(family_name, labels, extra)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
135 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
136 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 end
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
139 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
140 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
141 local labels = label_keys or {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
142 local user_labels = #labels
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
143 if type_ == "histogram" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
144 local buckets = extra and extra.buckets
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
145 if not buckets then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
146 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
147 end
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
148 buckets = array(buckets)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
149 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
150
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
151 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
152 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
153
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
154 local data
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
155 if #labels == 0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
156 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
157 else
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
158 data = new_multitable()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
159 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
160
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
161 local mf = {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
162 family_name = family_name,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
163 data = data,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
164 type_ = type_,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
165 unit = unit,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
166 description = description,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
167 user_labels = user_labels,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
168 label_keys = labels,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
169 extra = extra,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
170 _metric_ctor = metric_ctor,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
171 }
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
172 setmetatable(mf, metric_family_mt);
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
173 return mf
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 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
177 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
178 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
179
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
180 function metric_family_mt:clear()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
181 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
182 metric:reset()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
183 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
184 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
185
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
186 function metric_family_mt:with_labels(...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
187 local count = select('#', ...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
188 if count ~= self.user_labels then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
189 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
190 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
191 if count == 0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
192 return self.data
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
193 end
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
194 local metric = self.data:get(...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
195 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
196 local values = t_pack(...)
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
197 metric = self:new_metric(values)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
198 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
199 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
200 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
201 return metric
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
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
204 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
205 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
206 return family:with_labels(label, ...)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
207 end)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
208 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
209
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
210 function metric_family_mt:iter_metrics()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
211 if #self.label_keys == 0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
212 local done = false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
213 return function()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
214 if done then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
215 return nil
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
216 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
217 done = true
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
218 return {}, self.data
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
219 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
220 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
221 local searchkeys = {};
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
222 local nlabels = #self.label_keys
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
223 for i=1,nlabels do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
224 searchkeys[i] = nil;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
225 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
226 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
227 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
228 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
229 if label_values.n == 0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
230 return nil, nil
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
231 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
232 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
233 label_values[label_values.n] = nil
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
234 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
235 return label_values, metric
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
236 end, state
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
237 end
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 -- END of generic MetricFamily implementation
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
240
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
241 -- BEGIN of MetricRegistry implementation
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
242
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
243
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
244 -- 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
245 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
246 if mf1.type_ ~= mf2.type_ then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
247 return false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
248 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
249 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
250 return false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
251 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
252 -- 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
253 -- 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
254 -- 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
255 -- anyway.
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 if mf1.unit ~= mf2.unit then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
258 return false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
259 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
260 ]]
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
261 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
262 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
263 return false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
264 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
265 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
266 return true
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
267 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
269 -- 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
270 local function compose_name(name, unit)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
271 local full_name = name
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
272 if unit and unit ~= "" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
273 full_name = full_name .. "_" .. unit
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
274 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
275 -- 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
276 -- conflicts
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
277 return full_name
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
278 end
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
279
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
280 local metric_registry_mt = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
281 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
282
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
283 local function new_metric_registry(backend)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
284 local reg = {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
285 families = {},
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
286 backend = backend,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
287 }
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
288 setmetatable(reg, metric_registry_mt)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
289 return reg
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 end
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
292 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
293 local existing = self.families[name];
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
294 if existing then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
295 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
296 -- 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
297 -- 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
298 -- 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
299 -- 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
300 -- is loaded.
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 -- 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
303 -- 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
304 -- 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
305 -- 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
306 -- 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
307 --
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
308 -- 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
309 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
310 -- 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
311 --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
312 else
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
313 return existing
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 end
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
315 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
316 self.families[name] = metric_family
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
317 return metric_family
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
318 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
319
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
320 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
321 name = compose_name(name, unit)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
322 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
323 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
324 return mf
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
325 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
327 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
328 name = compose_name(name, unit)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
329 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
330 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
331 return mf
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
332 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
333
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
334 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
335 name = compose_name(name, unit)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
336 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
337 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
338 return mf
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
339 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
340
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
341 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
342 name = compose_name(name, unit)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
343 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
344 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
345 return mf
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
346 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
348 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
349 return self.families
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
350 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
351
11929
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
352 function metric_registry_mt:render()
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
353 local answer = {};
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
354 for metric_family_name, metric_family in pairs(self:get_metric_families()) do
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
355 t_insert(answer, repr_help(metric_family_name, metric_family.description))
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
356 t_insert(answer, repr_unit(metric_family_name, metric_family.unit))
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
357 t_insert(answer, repr_type(metric_family_name, metric_family.type_))
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
358 for labelset, metric in metric_family:iter_metrics() do
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
359 for suffix, extra_labels, value in metric:iter_samples() do
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
360 t_insert(answer, repr_sample(metric_family_name..suffix, metric_family.label_keys, labelset, extra_labels, value))
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
361 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
362 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
363 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
364 t_insert(answer, "# EOF\n")
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
365 return t_concat(answer, "");
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
366 end
85d51bfcf56b mod_http_openmetrics: Imported from prosody-modules mod_prometheus @df2246b15075
Matthew Wild <mwild1@gmail.com>
parents: 11600
diff changeset
367
11523
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
368 -- END of MetricRegistry implementation
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
369
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
370 -- 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
371
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
372 local function timed(metric)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
373 local t0 = time()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
374 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
375 return function()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
376 local t1 = time()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
377 submitter(metric, t1-t0)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
378 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
379 end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
380
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
381 -- END of general helpers
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
382
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
383 return {
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
384 new_metric_proxy = new_metric_proxy;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
385 new_metric_registry = new_metric_registry;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
386 render_histogram_le = render_histogram_le;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 10883
diff changeset
387 timed = timed;
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 }