comparison mod_prometheus/mod_prometheus.lua @ 3138:9817e45a79e6

mod_prometheus: Fetch statistics at every request.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 22 Jun 2018 15:19:55 +0200
parents 99ac6dda9878
children ccbfe7df02dc
comparison
equal deleted inserted replaced
3137:178ebea5097c 3138:9817e45a79e6
10 10
11 local tostring = tostring; 11 local tostring = tostring;
12 local t_insert = table.insert; 12 local t_insert = table.insert;
13 local t_concat = table.concat; 13 local t_concat = table.concat;
14 local socket = require "socket"; 14 local socket = require "socket";
15 15 local get_stats = require "core.statsmanager".get_stats;
16 local data = {};
17 16
18 local function escape(text) 17 local function escape(text)
19 return text:gsub("\\", "\\\\"):gsub("\"", "\\\""):gsub("\n", "\\n"); 18 return text:gsub("\\", "\\\\"):gsub("\"", "\\\""):gsub("\n", "\\n");
20 end 19 end
21 20
59 58
60 local function repr_sample(metric, labels, value, timestamp) 59 local function repr_sample(metric, labels, value, timestamp)
61 return escape_name(metric)..repr_labels(labels).." "..value.." "..timestamp.."\n"; 60 return escape_name(metric)..repr_labels(labels).." "..value.." "..timestamp.."\n";
62 end 61 end
63 62
64 module:hook("stats-updated", function (event) 63 local function parse_stats()
65 local all_stats, this = event.stats_extra;
66 local timestamp = tostring(get_timestamp()); 64 local timestamp = tostring(get_timestamp());
67 local host, sect, name, typ; 65 local data = {};
68 data = {}; 66 for stat, value in pairs(get_stats()) do
69 for stat, value in pairs(event.stats) do
70 this = all_stats[stat];
71 -- module:log("debug", "changed_stats[%q] = %s", stat, tostring(value)); 67 -- module:log("debug", "changed_stats[%q] = %s", stat, tostring(value));
72 host, sect, name, typ = stat:match("^/([^/]+)/([^/]+)/(.+):(%a+)$"); 68 local host, sect, name, typ = stat:match("^/([^/]+)/([^/]+)/(.+):(%a+)$");
73 if host == nil then 69 if host == nil then
74 sect, name, typ = stat:match("^([^.]+)%.(.+):(%a+)$"); 70 sect, name, typ = stat:match("^([^.]+)%.(.+):(%a+)$");
75 elseif host == "*" then 71 elseif host == "*" then
76 host = nil; 72 host = nil;
77 end 73 end
95 if data[key] == nil then 91 if data[key] == nil then
96 data[key] = {}; 92 data[key] = {};
97 end 93 end
98 t_insert(data[key], field); 94 t_insert(data[key], field);
99 end 95 end
100 end); 96 return data;
97 end
101 98
102 local function get_metrics(event) 99 local function get_metrics(event)
103 local response = event.response; 100 local response = event.response;
104 response.headers.content_type = "text/plain; version=0.4.4"; 101 response.headers.content_type = "text/plain; version=0.0.4";
105 102
106 local answer = {}; 103 local answer = {};
107 for key, fields in pairs(data) do 104 for key, fields in pairs(parse_stats()) do
108 t_insert(answer, repr_help(key, "TODO: add a description here.")); 105 t_insert(answer, repr_help(key, "TODO: add a description here."));
109 t_insert(answer, repr_type(key, fields[1].typ)); 106 t_insert(answer, repr_type(key, fields[1].typ));
110 for _, field in pairs(fields) do 107 for _, field in pairs(fields) do
111 t_insert(answer, repr_sample(key, field.labels, field.value, field.timestamp)); 108 t_insert(answer, repr_sample(key, field.labels, field.value, field.timestamp));
112 end 109 end