feat(prometheus-exporter): add metrics-pole for power metrics

This commit is contained in:
2025-09-19 21:52:53 +02:00
parent ac04fa2942
commit 25026b2e73
8 changed files with 205 additions and 65 deletions

View File

@@ -0,0 +1,75 @@
local prometheus = require("utils.prometheus")
local metrics_pole = require('prototypes.metrics-pole')
local M = {}
--- @param metrics_table table<number, string>
--- @param surface LuaSurface
--- @param force LuaForce
--- @return fun()
function M.calc_item_production_statistics(metrics_table, surface, force)
return function()
local item_stats = force.get_item_production_statistics(surface.name)
for item, count in pairs(item_stats.input_counts) do
table.insert(metrics_table,
prometheus.counter("production", "",
{ type = "item", surface = surface.name, force = force.name, name = item }, count))
end
for item, count in pairs(item_stats.output_counts) do
table.insert(metrics_table,
prometheus.counter("consumption", "",
{ type = "item", surface = surface.name, force = force.name, name = item }, count))
end
end
end
--- @param metrics_table table<number, string>
--- @param surface LuaSurface
--- @param force LuaForce
--- @return fun()
function M.calc_fluid_production_statistics(metrics_table, surface, force)
return function()
local fluid_stats = force.get_fluid_production_statistics(surface.name)
for item, count in pairs(fluid_stats.input_counts) do
table.insert(metrics_table,
prometheus.counter("production", "",
{ type = "fluid", surface = surface.name, force = force.name, name = item }, count))
end
for item, count in pairs(fluid_stats.output_counts) do
table.insert(metrics_table,
prometheus.counter("consumption", "",
{ type = "fluid", surface = surface.name, force = force.name, name = item }, count))
end
end
end
--- @param metrics_table table<number, string>
--- @param surface LuaSurface
--- @return fun()
function M.calc_power_statistics(metrics_table, surface)
return function()
local seen = {}
for _, pole in pairs(surface.find_entities_filtered { name = metrics_pole.name }) do
if table[pole.electric_network_id] then -- deduplication
goto continue
end
table.insert(seen, pole.electric_network_id)
local stats = pole.electric_network_statistics
for name, count in pairs(stats.input_counts) do
table.insert(metrics_table,
prometheus.counter("energy_consumption", "_joules",
{ surface = surface.name, network_id = pole.electric_network_id, name = name }, count))
end
for name, count in pairs(stats.output_counts) do
table.insert(metrics_table,
prometheus.counter("energy_production", "_joules",
{ surface = surface.name, network_id = pole.electric_network_id, name = name }, count))
end
::continue::
end
end
end
return M

View File

@@ -0,0 +1,17 @@
local M = {}
--- Formats a name + table of labels as a Prometheus metric
--- @param name string
--- @param suffix string
--- @param labels table
--- @param value number
function M.counter(name, suffix, labels, value)
local label_parts = {}
for k, v in pairs(labels) do
table.insert(label_parts, string.format('%s="%s"', k, v))
end
local label_str = table.concat(label_parts, ",")
return string.format("factorio_%s_total%s{%s} %d", name, suffix, label_str, value)
end
return M