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 per-player stats #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions Metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ Please read the warning regarding the cardinality of train metrics!

### Players

| Name | Labels | Description |
|-----------------------------------|--------|----------------------------------------------|
| `factorio_connected_player_count` | | Count of currently connected players |
| `factorio_total_player_count` | | Count of all players who were ever connected |
Per-player metrics (those with `name` label) are disabled by default and can be enabled in the settings (uncheck "Disable per player stats").

| Name | Labels | Description |
|------------------------------------|---------------|---------------------------------------------------|
| `factorio_connected_player_count` | | Count of currently connected players |
| `factorio_total_player_count` | | Count of all players who were ever connected |
| `factorio_player_connected` | name | 1 if the player is currently connected |
| `factorio_player_last_online` | name | Last tick the player was connected |
| `factorio_player_time_online` | name | Time (in seconds) the player spent online |
| `factorio_player_position_x` | name | The x coordinate of the player's (last) position |
| `factorio_player_position_y` | name | The y coordinate of the player's (last) position |
| `factorio_player_position_surface` | name, surface | 1 for the surface of the player's (last) position |

### Production

Expand Down
14 changes: 14 additions & 0 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ bucket_settings = train_buckets(settings.startup["graftorio2-train-histogram-buc
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
disable_per_player_stats = settings.startup["graftorio2-disable-per-player-stats"].value

gauge_tick = prometheus.gauge("factorio_tick", "game tick")
gauge_connected_player_count = prometheus.gauge("factorio_connected_player_count", "connected players")
gauge_total_player_count = prometheus.gauge("factorio_total_player_count", "total registered players")

gauge_player_connected =
prometheus.gauge("factorio_player_connected", "connected players by name", { "name" })
gauge_player_last_online =
prometheus.gauge("factorio_player_last_online", "last tick the player was online", { "name" })
gauge_player_time_online =
prometheus.gauge("factorio_player_time_online", "total seconds player spent online", { "name" })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe it should be a counter and not a gauge, the value can only go up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I was just matching the other calls - as far as I could tell (although I did not look at every file), all other currently added metrics are either gauges or histograms (even gauge_tick, of which these two are kind of 'subsets', so to speak).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also on that note, gauge_player_time_online and gauge_tick should be actively reset to 0 on start if they are turned into counters, no? Otherwise if the server crashes without saving, but still exported its metrics at least once since the last save, the observed value could appear to decrease on the real-world timescale, even if it is monotonically increasing in relation to game-ticks.

Another interpretation (aside from just "this case is irrelevant") could be that rate(factorio_tick) should not be used, because the value can, exactly as stated by its gauge nature, go down?

I will wait for further confirmation on this before changing the metrics to counters. If you want I can also go over the existing metrics and change their type where appropriate, although I dont know how well prometheus handles existing metrics changing their type.

gauge_player_position_x =
prometheus.gauge("factorio_player_position_x", "x position of the player", { "name" })
gauge_player_position_y =
prometheus.gauge("factorio_player_position_y", "y position of the player", { "name" })
gauge_player_position_surface =
prometheus.gauge("factorio_player_position_surface", "player on surface", { "name", "surface" })

gauge_seed = prometheus.gauge("factorio_seed", "seed", { "surface" })
gauge_mods = prometheus.gauge("factorio_mods", "mods", { "name", "version" })

Expand Down
18 changes: 18 additions & 0 deletions events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function register_events(event)
gauge_mods:set(1, { name, version })
end

gauge_player_position_x:reset()
gauge_player_position_y:reset()
gauge_player_position_surface:reset()

for _, player in pairs(game.players) do
stats = {
{ player.force.item_production_statistics, gauge_item_production_input, gauge_item_production_output },
Expand Down Expand Up @@ -84,6 +88,14 @@ function register_events(event)
end
end

if not disable_per_player_stats then
gauge_player_last_online:set(player.last_online, { player.name })
gauge_player_time_online:set(player.online_time / 60, { player.name })
gauge_player_position_x:set(player.position.x, { player.name })
gauge_player_position_y:set(player.position.y, { player.name })
gauge_player_position_surface:set(1, { player.name, player.surface.name })
end

-- research tick handler
on_research_tick(player, event)
end
Expand All @@ -101,4 +113,10 @@ end
function register_events_players(event)
gauge_connected_player_count:set(#game.connected_players)
gauge_total_player_count:set(#game.players)
if not disable_per_player_stats then
gauge_player_connected:reset()
for _, player in pairs(game.connected_players) do
gauge_player_connected:set(player.connected and 1 or 0, { player.name })
end
end
end
2 changes: 2 additions & 0 deletions locale/en/locale.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ 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-disable-per-player-stats=Disable per player stats

[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-disable-per-player-stats=Stats for each player: connected, last time online, position, etc.
7 changes: 7 additions & 0 deletions settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ data:extend({
default_value = false,
allow_blank = false,
},
{
type = "bool-setting",
name = "graftorio2-disable-per-player-stats",
setting_type = "startup",
default_value = true,
allow_blank = false,
},
})