feat(prometheus-exporter): add metrics-pole for power metrics
This commit is contained in:
75
prometheus-exporter/utils/metrics.lua
Normal file
75
prometheus-exporter/utils/metrics.lua
Normal 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
|
||||
17
prometheus-exporter/utils/prometheus.lua
Normal file
17
prometheus-exporter/utils/prometheus.lua
Normal 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
|
||||
Reference in New Issue
Block a user