Skip to content

Commit 6afa841

Browse files
NoctivagusObitusMarkus Hube
authored andcommitted
prometheus-node-exporter-lua remove zero values
depending on the configuration there may be multiple interfaces creating multiple time series always reporting 0 value. omiting them from the export saves resources. most notably cpu. - remove time series with value of zero - make 'omit_zero_values' configurable - formating accoding to shfmt and stylua Signed-off-by: Markus Hube <[email protected]>
1 parent e35b928 commit 6afa841

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

utils/prometheus-node-exporter-lua/files/etc/init.d/prometheus-node-exporter-lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,27 @@ _log() {
1111
start_service() {
1212
. /lib/functions/network.sh
1313

14-
local interface port bind4 bind6
14+
local interface port bind4 bind6 omit_zero_values
1515

1616
config_load prometheus-node-exporter-lua.main
1717
config_get keepalive "main" http_keepalive 70
1818
config_get interface "main" listen_interface "loopback"
1919
config_get port "main" listen_port 9100
20+
config_get omit_zero_values "main" omit_zero_values 0
2021

2122
[ "$interface" = "*" ] || {
22-
network_get_ipaddr bind4 "$interface"
23+
network_get_ipaddr bind4 "$interface"
2324
network_get_ipaddr6 bind6 "$interface"
2425
[ -n "$bind4$bind6" ] || {
2526
_log "defering start until listen interface $interface becomes ready"
2627
return 0
2728
}
28-
}
29+
}
2930

3031
procd_open_instance
3132

33+
[ "$omit_zero_values" -eq 1 ] && procd_set_param env OMIT_ZERO_VALUES=1
34+
3235
procd_set_param command /usr/sbin/uhttpd -f -c /dev/null -l / -L /usr/bin/prometheus-node-exporter-lua
3336
[ $keepalive -gt 0 ] && procd_append_param command -k $keepalive
3437

@@ -46,8 +49,7 @@ start_service() {
4649
procd_close_instance
4750
}
4851

49-
service_triggers()
50-
{
52+
service_triggers() {
5153
local interface
5254

5355
procd_add_reload_trigger "prometheus-node-exporter-lua"

utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
socket = require("socket")
1010

11+
-- get configs
12+
13+
local omit_zero_values = os.getenv("OMIT_ZERO_VALUES") == "1"
14+
1115
-- Parsing
1216

1317
function space_split(s)
@@ -22,7 +26,7 @@ function get_contents(filename)
2226
local f = io.open(filename, "rb")
2327
local contents = ""
2428
if f then
25-
contents = f:read "*a"
29+
contents = f:read("*a")
2630
f:close()
2731
end
2832

@@ -34,8 +38,8 @@ end
3438
function print_metric(metric, labels, value)
3539
local label_string = ""
3640
if labels then
37-
for label,value in pairs(labels) do
38-
label_string = label_string .. label .. '="' .. value .. '",'
41+
for label, value in pairs(labels) do
42+
label_string = label_string .. label .. '="' .. value .. '",'
3943
end
4044
label_string = "{" .. string.sub(label_string, 1, -2) .. "}"
4145
end
@@ -45,7 +49,9 @@ end
4549
function metric(name, mtype, labels, value)
4650
output("# TYPE " .. name .. " " .. mtype)
4751
local outputter = function(labels, value)
48-
print_metric(name, labels, value)
52+
if not (omit_zero_values and tonumber(value) == 0) then
53+
print_metric(name, labels, value)
54+
end
4955
end
5056
if value then
5157
outputter(labels, value)
@@ -67,10 +73,10 @@ end
6773
function run_all_collectors(collectors)
6874
local metric_duration = metric("node_scrape_collector_duration_seconds", "gauge")
6975
local metric_success = metric("node_scrape_collector_success", "gauge")
70-
for _,cname in pairs(collectors) do
76+
for _, cname in pairs(collectors) do
7177
if col_mods[cname] ~= nil then
7278
local duration, success = timed_scrape(col_mods[cname])
73-
local labels = {collector=cname}
79+
local labels = { collector = cname }
7480
metric_duration(labels, duration)
7581
metric_success(labels, success)
7682
end
@@ -80,7 +86,7 @@ end
8086
-- Web server-specific functions
8187

8288
function handle_request(env)
83-
if env.PATH_INFO ~= '/metrics' then
89+
if env.PATH_INFO ~= "/metrics" then
8490
uhttpd.send("Status: 404 Not Found\r\n")
8591
uhttpd.send("Server: lua-metrics\r\n")
8692
uhttpd.send("Content-Type: text/plain\r\n\r\n")
@@ -91,7 +97,7 @@ function handle_request(env)
9197
uhttpd.send("Content-Type: text/plain; version=0.0.4\r\n\r\n")
9298
local cols = {}
9399
for c in env.QUERY_STRING:gmatch("collect[^=]*=([^&]+)") do
94-
cols[#cols+1] = c
100+
cols[#cols + 1] = c
95101
end
96102
if #cols == 0 then
97103
cols = col_names
@@ -107,12 +113,14 @@ col_names = {}
107113
ls_fd = io.popen("ls -1 /usr/lib/lua/prometheus-collectors/*.lua")
108114
for c in ls_fd:lines() do
109115
c = c:match("([^/]+)%.lua$")
110-
col_mods[c] = require('prometheus-collectors.'..c)
111-
col_names[#col_names+1] = c
116+
col_mods[c] = require("prometheus-collectors." .. c)
117+
col_names[#col_names + 1] = c
112118
end
113119
ls_fd:close()
114120

115-
output = function (str) uhttpd.send(str.."\n") end
121+
output = function(str)
122+
uhttpd.send(str .. "\n")
123+
end
116124

117125
if arg ~= nil then
118126
output = print

0 commit comments

Comments
 (0)