Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prometheus combinator #36

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions control.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
prometheus = require("prometheus/prometheus")
local logging = require("logging")
require("train")
require("yarm")
require("events")
require("power")
require("research")
require("signals")
require("signals-ui")
require("signals-debug-ui")

bucket_settings = train_buckets(settings.startup["graftorio2-train-histogram-buckets"].value)
nth_tick = settings.startup["graftorio2-nth-tick"].value
server_save = settings.startup["graftorio2-server-save"].value
disable_train_stats = settings.startup["graftorio2-disable-train-stats"].value
enable_signal_groups = settings.startup["graftorio2-enable-signal-groups"].value

gauge_tick = prometheus.gauge("factorio_tick", "game tick")
gauge_connected_player_count = prometheus.gauge("factorio_connected_player_count", "connected players")
Expand Down Expand Up @@ -125,13 +130,17 @@ gauge_power_production_output =
prometheus.gauge("factorio_power_production_output", "power consumed", { "force", "name", "network", "surface" })

script.on_init(function()
logging.reload_settings()

if game.active_mods["YARM"] then
global.yarm_on_site_update_event_id = remote.call("YARM", "get_on_site_updated_event_id")
script.on_event(global.yarm_on_site_update_event_id, handleYARM)
end

on_power_init()

on_signals_init()

script.on_nth_tick(nth_tick, register_events)

script.on_event(defines.events.on_player_joined_game, register_events_players)
Expand All @@ -156,9 +165,34 @@ script.on_init(function()

-- research events
script.on_event(defines.events.on_research_finished, on_research_finished)

-- ui events
script.on_event(defines.events.on_gui_closed, function(event)
on_signals_gui_closed(event)
on_signals_debug_gui_closed(event)
end)
script.on_event(defines.events.on_gui_click, function (event)
on_signals_gui_click(event)
end)
script.on_event(defines.events.on_gui_selection_state_changed, function (event)
on_signals_gui_selection_state_changed(event)
end)
script.on_event(defines.events.on_gui_confirmed, function (event)
on_signals_gui_confirmed(event)
end)
script.on_event(defines.events.on_gui_text_changed, function (event)
on_signals_gui_text_changed(event)
end)
script.on_event(defines.events.on_gui_opened, function (event)
on_signals_gui_opened(event)
end)
script.on_event(defines.events.on_runtime_mod_setting_changed, function (event)
logging.reload_settings()
end)
end)

script.on_load(function()
logging.reload_settings()
if global.yarm_on_site_update_event_id then
if script.get_event_handler(global.yarm_on_site_update_event_id) then
script.on_event(global.yarm_on_site_update_event_id, handleYARM)
Expand All @@ -167,6 +201,8 @@ script.on_load(function()

on_power_load()

on_signals_load()

script.on_nth_tick(nth_tick, register_events)

script.on_event(defines.events.on_player_joined_game, register_events_players)
Expand All @@ -191,11 +227,38 @@ script.on_load(function()

-- research events
script.on_event(defines.events.on_research_finished, on_research_finished)

-- ui events
script.on_event(defines.events.on_gui_closed, function(event)
on_signals_gui_closed(event)
on_signals_debug_gui_closed(event)
end)
script.on_event(defines.events.on_gui_click, function (event)
on_signals_gui_click(event)
end)
script.on_event(defines.events.on_gui_selection_state_changed, function (event)
on_signals_gui_selection_state_changed(event)
end)
script.on_event(defines.events.on_gui_confirmed, function (event)
on_signals_gui_confirmed(event)
end)
script.on_event(defines.events.on_gui_text_changed, function (event)
on_signals_gui_text_changed(event)
end)
script.on_event(defines.events.on_gui_opened, function (event)
on_signals_gui_opened(event)
end)
script.on_event(defines.events.on_runtime_mod_setting_changed, function (event)
logging.reload_settings()
end)
end)

script.on_configuration_changed(function(event)
logging.reload_settings()
if game.active_mods["YARM"] then
global.yarm_on_site_update_event_id = remote.call("YARM", "get_on_site_updated_event_id")
script.on_event(global.yarm_on_site_update_event_id, handleYARM)
end
end)

add_signals_debug_ui_command()
62 changes: 62 additions & 0 deletions data.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
local flib_table = require("__flib__.table")

local prometheusCombinatorItem = flib_table.deep_copy(data.raw["item"]["constant-combinator"])
local prometheusCombinatorEntity = flib_table.deep_copy(data.raw["constant-combinator"]["constant-combinator"])

local prometheusCombinatorTint = { r = 0.5, g = 1, b = 0.7, a = 1 }

prometheusCombinatorItem.name = "prometheus-combinator"
prometheusCombinatorItem.place_result = "prometheus-combinator"
prometheusCombinatorItem.icons = {
{
icon = prometheusCombinatorItem.icon,
icon_size = prometheusCombinatorItem.icon_size,
tint = prometheusCombinatorTint
}
}

prometheusCombinatorEntity.name = "prometheus-combinator"
prometheusCombinatorEntity.minable.result = "prometheus-combinator"
prometheusCombinatorEntity.item_slot_count = 1
prometheusCombinatorEntity.sprites.sheets = {
{
frames = 4,
size = { 57, 52 },
filename = "__base__/graphics/entity/combinator/constant-combinator.png",
tint = prometheusCombinatorTint,
hr_version = {
frames = 4,
size = { 114, 102 },
shift = { 0, 0.125 },
filename = "__base__/graphics/entity/combinator/hr-constant-combinator.png",
tint = prometheusCombinatorTint,
scale = 0.5
}
},
{
frames = 4,
size = { 50, 34 },
filename = "__base__/graphics/entity/combinator/constant-combinator-shadow.png",
draw_as_shadow = true,
hr_version = {
frames = 4,
size = { 98, 66 },
shift = { 0.234375, 0.109375 },
filename = "__base__/graphics/entity/combinator/hr-constant-combinator-shadow.png",
scale = 0.5,
draw_as_shadow = true
}
}
}

local prometheusCombinatorRecipe = {
type = "recipe",
name = "prometheus-combinator",
enabled = false,
energy_required = 0.5,
ingredients = { { "constant-combinator", 1 }, { "electronic-circuit", 1 } },
result = "prometheus-combinator"
}

table.insert(data.raw["technology"]["circuit-network"].effects, { type = "unlock-recipe", recipe = "prometheus-combinator" })
data:extend { prometheusCombinatorItem, prometheusCombinatorEntity, prometheusCombinatorRecipe }
3 changes: 3 additions & 0 deletions events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ function register_events(event)
-- power tick handler
on_power_tick(event)

-- signal tick handler
on_signals_tick(event)

if server_save then
game.write_file("graftorio2/game.prom", prometheus.collect(), false, 0)
else
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"author": "remijouannet",
"description": "visualize metrics from your factorio game in grafana",
"homepage": "https://github.com/remijouannet/graftorio2",
"dependencies": ["? YARM >= 0.8.207"],
"dependencies": ["? YARM >= 0.8.207", "flib >= 0.14.1"],
"factorio_version": "1.1"
}
34 changes: 34 additions & 0 deletions locale/en/gui.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[graftorio2-signals-gui]

error-configuration-metric-name-empty=Metric name must not be empty
error-configuration-metric-name-invalid=Invalid metric name. Only alphanumeric characters and underscores are allowed.
configuration-title=Prometheus Combinator
configuration-content-caption=Configure metric
configuration-metric-name-caption=Metric name
configuration-metric-name-prefix-note=Actual metric name will be "factorio_custom_{name}"
configuration-metric-name-toggle-type=Right-click to toggle between free input and drop-down
configuration-signal-empty-all-exported=If no filter is specified, ALL signals will be exported
configuration-group-name-caption=Group name
configuration-group-setting-disabled=Disabled in mod settings
configuration-apply=Apply
configuration-cancel=Cancel

[graftorio2-signals-debug-gui]
debug-caption=Prometheus Combinator Debug
debug-tab-active-metrics=Active Metrics
debug-tab-global-metrics=Global Metrics
debug-tab-global-combinators=Global Combinators
debug-metric-caption=Metric: __1__
debug-metric-properties-caption=Properties:
debug-metric-prometheus-present=Prometheus gauge present:
debug-metric-prometheus-present-true=True
debug-metric-prometheus-present-false=False
debug-groups-general-caption=Groups
debug-groups-specific-caption=Group: __1__
debug-combinator-entity-caption=Entity:
debug-combinator-entity-nil=nil
debug-combinator-entity-invalid=invalid
debug-combinator-entity-mismatch=stored under __1__!
debug-combinator-path-caption=Path:
debug-combinator-path-text=__1__/__2__/__3__
debug-combinator-filter-caption=Filter:
15 changes: 15 additions & 0 deletions locale/en/locale.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ graftorio2-train-histogram-buckets=Train histogram buckets
graftorio2-nth-tick=Scheduler timer
graftorio2-server-save=Save prom file on server only
graftorio2-disable-train-stats=Disable train stats
graftorio2-enable-signal-groups=Enable groups for Prometheus Combinators
graftorio2-log-level=Logging level

[mod-setting-description]
graftorio2-train-histogram-buckets=Train trip times (in seconds) to categorize into groups.
graftorio2-nth-tick=How often prom file is written
graftorio2-enable-signal-groups=Allows using an additional label called "group" to separate signals inside the same metric (eg. item throughput at location X). If false, label will still exist but always have an empty value.

[item-name]
prometheus-combinator=Prometheus Combinator

[item-description]
prometheus-combinator=A combinator that allows you to export signals to prometheus

[entity-name]
prometheus-combinator=Prometheus Combinator

[entity-description]
prometheus-combinator=A combinator that allows you to export signals to prometheus
46 changes: 46 additions & 0 deletions logging.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
local logging_groups = {
global = 30,
signals = 30,
}

local levels = {
info = 30,
debug = 20,
verbose = 10,
trace = 0
}

local function debug_log(text, logging_level, logging_group)
if type(logging_level) == "string" then
logging_level = levels[logging_level]
elseif logging_level == nil then
logging_level = levels.debug
end
if logging_group == nil then
logging_group = "global"
end
local stored_level = logging_groups[logging_group]
if stored_level == nil then
stored_level = logging_groups.global
end
if logging_level >= stored_level then
log(text)
end
end

local function reload_settings()
local settings_level = levels[settings.global["graftorio2-log-level"].value]
if type(settings_level) ~= "number" then
error("Unexpected settings_level: " .. tostring(settings_level) .. " (from " .. tostring(settings.global["graftorio2-log-level"].value) .. ")")
end
for k, v in pairs(logging_groups) do
logging_groups[k] = settings_level
end
end

return {
debug_log = debug_log,
logging_groups = logging_groups,
levels = levels,
reload_settings = reload_settings,
}
9 changes: 9 additions & 0 deletions migrations/50__add_global_signal_data.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if global["signal-data"] == nil then
global["signal-data"] = {}
end
if global["signal-data"]["metrics"] == nil then
global["signal-data"]["metrics"] = {}
end
if global["signal-data"]["combinators"] == nil then
global["signal-data"]["combinators"] = {}
end
7 changes: 7 additions & 0 deletions migrations/50__unlock_prometheus_combinator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
for _, force in pairs(game.forces) do
local technologies = force.technologies
local recipes = force.recipes
if technologies["circuit-network"].researched then
recipes["prometheus-combinator"].enabled = true
end
end
10 changes: 9 additions & 1 deletion prometheus/prometheus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end

function Registry:unregister(collector)
if self.collectors[collector.name] ~= nil then
table.remove(self.collectors, collector.name)
self.collectors[collector.name] = nil
end
end

Expand Down Expand Up @@ -73,6 +73,13 @@ local function register(collector)
return collector
end

local function unregister(collector)
local registry = get_registry()
registry:unregister(collector)

return collector
end

local function register_callback(callback)
local registry = get_registry()
registry:register_callback(callback)
Expand Down Expand Up @@ -379,5 +386,6 @@ return {
collect = collect,
collect_http = collect_http,
clear = clear,
unregister = unregister,
init = init,
}
15 changes: 15 additions & 0 deletions settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ data:extend({
default_value = false,
allow_blank = false,
},
{
type = "bool-setting",
name = "graftorio2-enable-signal-groups",
setting_type = "startup",
default_value = false,
allow_blank = false,
},
{
type = "string-setting",
name = "graftorio2-log-level",
setting_type = "runtime-global",
default_value = "info",
allow_blank = false,
allowed_values = { "info", "debug", "verbose", "trace" }
},
})
Loading