comparison mod_prometheus/mod_prometheus.lua @ 3134:99ac6dda9878

mod_prometheus: Move timestamp generation to the stats-update event.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 22 Jun 2018 01:03:47 +0200
parents 321fd53a3191
children 9817e45a79e6
comparison
equal deleted inserted replaced
3133:321fd53a3191 3134:99ac6dda9878
61 return escape_name(metric)..repr_labels(labels).." "..value.." "..timestamp.."\n"; 61 return escape_name(metric)..repr_labels(labels).." "..value.." "..timestamp.."\n";
62 end 62 end
63 63
64 module:hook("stats-updated", function (event) 64 module:hook("stats-updated", function (event)
65 local all_stats, this = event.stats_extra; 65 local all_stats, this = event.stats_extra;
66 local timestamp = tostring(get_timestamp());
66 local host, sect, name, typ; 67 local host, sect, name, typ;
67 data = {}; 68 data = {};
68 for stat, value in pairs(event.stats) do 69 for stat, value in pairs(event.stats) do
69 this = all_stats[stat]; 70 this = all_stats[stat];
70 -- module:log("debug", "changed_stats[%q] = %s", stat, tostring(value)); 71 -- module:log("debug", "changed_stats[%q] = %s", stat, tostring(value));
84 local field = { 85 local field = {
85 value = value, 86 value = value,
86 labels = { ["type"] = name}, 87 labels = { ["type"] = name},
87 -- TODO: Use the other types where it makes sense. 88 -- TODO: Use the other types where it makes sense.
88 typ = (typ == "rate" and "counter" or "gauge"), 89 typ = (typ == "rate" and "counter" or "gauge"),
90 timestamp = timestamp,
89 }; 91 };
90 if host then 92 if host then
91 field.labels.host = host; 93 field.labels.host = host;
92 end 94 end
93 if data[key] == nil then 95 if data[key] == nil then
100 local function get_metrics(event) 102 local function get_metrics(event)
101 local response = event.response; 103 local response = event.response;
102 response.headers.content_type = "text/plain; version=0.4.4"; 104 response.headers.content_type = "text/plain; version=0.4.4";
103 105
104 local answer = {}; 106 local answer = {};
105 local timestamp = tostring(get_timestamp());
106 for key, fields in pairs(data) do 107 for key, fields in pairs(data) do
107 t_insert(answer, repr_help(key, "TODO: add a description here.")); 108 t_insert(answer, repr_help(key, "TODO: add a description here."));
108 t_insert(answer, repr_type(key, fields[1].typ)); 109 t_insert(answer, repr_type(key, fields[1].typ));
109 for _, field in pairs(fields) do 110 for _, field in pairs(fields) do
110 t_insert(answer, repr_sample(key, field.labels, field.value, timestamp)); 111 t_insert(answer, repr_sample(key, field.labels, field.value, field.timestamp));
111 end 112 end
112 end 113 end
113 return t_concat(answer, ""); 114 return t_concat(answer, "");
114 end 115 end
115 116