Mercurial > prosody-modules
annotate mod_prometheus/mod_prometheus.lua @ 4542:fb4a50bf60f1
mod_prometheus: Invoke stats collection if in 'manual' mode
Since 10d13e0554f9 a special value for statistics_interval "manual"
exists, where a module is expected to invoke processing in connection to
collection of stats. This makes internal collection and exporting to
Prometheus happens at the same time with no chance of timers getting out
of sync.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 13 Apr 2021 23:53:53 +0200 |
parents | 343dc9dd70dd |
children | 64fa2dd34d43 |
rev | line source |
---|---|
3132
4ef28b6b4e87
mod_prometheus: Add my copyright.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3131
diff
changeset
|
1 -- Log statistics to Prometheus |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 -- |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 -- Copyright (C) 2014 Daurnimator |
3132
4ef28b6b4e87
mod_prometheus: Add my copyright.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3131
diff
changeset
|
4 -- Copyright (C) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 -- |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 -- This module is MIT/X11 licensed. |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 module:set_global(); |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 |
3128
a34e7bd87b39
mod_prometheus: Optimise global lookups.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3127
diff
changeset
|
10 local tostring = tostring; |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 local t_insert = table.insert; |
3128
a34e7bd87b39
mod_prometheus: Optimise global lookups.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3127
diff
changeset
|
12 local t_concat = table.concat; |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 local socket = require "socket"; |
4542
fb4a50bf60f1
mod_prometheus: Invoke stats collection if in 'manual' mode
Kim Alvefur <zash@zash.se>
parents:
3952
diff
changeset
|
14 local statsman = require "core.statsmanager"; |
fb4a50bf60f1
mod_prometheus: Invoke stats collection if in 'manual' mode
Kim Alvefur <zash@zash.se>
parents:
3952
diff
changeset
|
15 local get_stats = statsman.get_stats; |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 local function escape(text) |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 return text:gsub("\\", "\\\\"):gsub("\"", "\\\""):gsub("\n", "\\n"); |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 local function escape_name(name) |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 return name:gsub("[^A-Za-z0-9_]", "_"):gsub("^[^A-Za-z_]", "_%1"); |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 local function get_timestamp() |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 -- Using LuaSocket for that because os.time() only has second precision. |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 return math.floor(socket.gettime() * 1000); |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 local function repr_help(metric, docstring) |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 docstring = docstring:gsub("\\", "\\\\"):gsub("\n", "\\n"); |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 return "# HELP "..escape_name(metric).." "..docstring.."\n"; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 -- local allowed_types = { counter = true, gauge = true, histogram = true, summary = true, untyped = true }; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 -- local allowed_types = { "counter", "gauge", "histogram", "summary", "untyped" }; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 local function repr_type(metric, type_) |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 -- if not allowed_types:contains(type_) then |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 -- return; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 -- end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 return "# TYPE "..escape_name(metric).." "..type_.."\n"; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 local function repr_label(key, value) |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 return key.."=\""..escape(value).."\""; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 local function repr_labels(labels) |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 local values = {} |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 for key, value in pairs(labels) do |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 t_insert(values, repr_label(escape_name(key), escape(value))); |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 if #values == 0 then |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 return ""; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 end |
3128
a34e7bd87b39
mod_prometheus: Optimise global lookups.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3127
diff
changeset
|
56 return "{"..t_concat(values, ", ").."}"; |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 local function repr_sample(metric, labels, value, timestamp) |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 return escape_name(metric)..repr_labels(labels).." "..value.." "..timestamp.."\n"; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
62 |
3149
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
63 local allowed_extras = { min = true, max = true, average = true }; |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
64 local function insert_extras(data, key, name, timestamp, extra) |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
65 if not extra then |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
66 return false; |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
67 end |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
68 local has_extra = false; |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
69 for extra_name in pairs(allowed_extras) do |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
70 if extra[extra_name] then |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
71 local field = { |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
72 value = extra[extra_name], |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
73 labels = { |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
74 ["type"] = name, |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
75 field = extra_name, |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
76 }, |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
77 typ = "gauge"; |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
78 timestamp = timestamp, |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
79 }; |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
80 t_insert(data[key], field); |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
81 has_extra = true; |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
82 end |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
83 end |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
84 return has_extra; |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
85 end |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
86 |
3138
9817e45a79e6
mod_prometheus: Fetch statistics at every request.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3134
diff
changeset
|
87 local function parse_stats() |
3134
99ac6dda9878
mod_prometheus: Move timestamp generation to the stats-update event.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3133
diff
changeset
|
88 local timestamp = tostring(get_timestamp()); |
3138
9817e45a79e6
mod_prometheus: Fetch statistics at every request.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3134
diff
changeset
|
89 local data = {}; |
3149
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
90 local stats, changed_only, extras = get_stats(); |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
91 for stat, value in pairs(stats) do |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 -- module:log("debug", "changed_stats[%q] = %s", stat, tostring(value)); |
3149
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
93 local extra = extras[stat]; |
3138
9817e45a79e6
mod_prometheus: Fetch statistics at every request.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3134
diff
changeset
|
94 local host, sect, name, typ = stat:match("^/([^/]+)/([^/]+)/(.+):(%a+)$"); |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 if host == nil then |
3131
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
96 sect, name, typ = stat:match("^([^.]+)%.(.+):(%a+)$"); |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
97 elseif host == "*" then |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
98 host = nil; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
99 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
100 if sect:find("^mod_measure_.") then |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
101 sect = sect:sub(13); |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
102 elseif sect:find("^mod_statistics_.") then |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
103 sect = sect:sub(16); |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
104 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
105 |
3133
321fd53a3191
mod_prometheus: Put the name as a label, which fits better the usual Prosody model.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3132
diff
changeset
|
106 local key = escape_name("prosody_"..sect); |
3131
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
107 local field = { |
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
108 value = value, |
3133
321fd53a3191
mod_prometheus: Put the name as a label, which fits better the usual Prosody model.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3132
diff
changeset
|
109 labels = { ["type"] = name}, |
3131
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
110 -- TODO: Use the other types where it makes sense. |
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
111 typ = (typ == "rate" and "counter" or "gauge"), |
3134
99ac6dda9878
mod_prometheus: Move timestamp generation to the stats-update event.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3133
diff
changeset
|
112 timestamp = timestamp, |
3131
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
113 }; |
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
114 if host then |
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
115 field.labels.host = host; |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
116 end |
3131
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
117 if data[key] == nil then |
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
118 data[key] = {}; |
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
119 end |
3149
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
120 if not insert_extras(data, key, name, timestamp, extra) then |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
121 t_insert(data[key], field); |
ccbfe7df02dc
mod_prometheus: Expose min, max and average when available.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3138
diff
changeset
|
122 end |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
123 end |
3138
9817e45a79e6
mod_prometheus: Fetch statistics at every request.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3134
diff
changeset
|
124 return data; |
9817e45a79e6
mod_prometheus: Fetch statistics at every request.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3134
diff
changeset
|
125 end |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
126 |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
127 local function get_metrics(event) |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
128 local response = event.response; |
3138
9817e45a79e6
mod_prometheus: Fetch statistics at every request.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3134
diff
changeset
|
129 response.headers.content_type = "text/plain; version=0.0.4"; |
4542
fb4a50bf60f1
mod_prometheus: Invoke stats collection if in 'manual' mode
Kim Alvefur <zash@zash.se>
parents:
3952
diff
changeset
|
130 if statsman.collect then |
fb4a50bf60f1
mod_prometheus: Invoke stats collection if in 'manual' mode
Kim Alvefur <zash@zash.se>
parents:
3952
diff
changeset
|
131 statsman.collect() |
fb4a50bf60f1
mod_prometheus: Invoke stats collection if in 'manual' mode
Kim Alvefur <zash@zash.se>
parents:
3952
diff
changeset
|
132 end |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
133 |
3127
36770ae1148f
mod_prometheus: Don’t shadow variables. [luacheck]
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3126
diff
changeset
|
134 local answer = {}; |
3138
9817e45a79e6
mod_prometheus: Fetch statistics at every request.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3134
diff
changeset
|
135 for key, fields in pairs(parse_stats()) do |
3131
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
136 t_insert(answer, repr_help(key, "TODO: add a description here.")); |
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
137 t_insert(answer, repr_type(key, fields[1].typ)); |
ddd39ca7b953
mod_prometheus: Change the storage model for one which matches Prometheus better.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3128
diff
changeset
|
138 for _, field in pairs(fields) do |
3134
99ac6dda9878
mod_prometheus: Move timestamp generation to the stats-update event.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3133
diff
changeset
|
139 t_insert(answer, repr_sample(key, field.labels, field.value, field.timestamp)); |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
140 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
141 end |
3128
a34e7bd87b39
mod_prometheus: Optimise global lookups.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3127
diff
changeset
|
142 return t_concat(answer, ""); |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
143 end |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
144 |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
145 function module.add_host(module) |
3952
343dc9dd70dd
mod_prometheus: Ensure mod_http is loaded where the http interface is exposed (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
3149
diff
changeset
|
146 module:depends "http"; |
3125
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
147 module:provides("http", { |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
148 default_path = "metrics"; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
149 route = { |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
150 GET = get_metrics; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
151 }; |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
152 }); |
07a2ba55de4d
mod_prometheus: Add a new statistics export module, for Prometheus.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
153 end |