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