Rewrite statistics export

Add automatic export of metrics from slaves to the master server.  And
rewrite the statistics export plugin to use this new system.
This commit is contained in:
Hornwitser committed 2019-12-20 21:26:29 +01:00
1 parent 8229d1955c
commit 26eefb0476
19 files changed
+334 -226

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
let link = require("lib/link");
module.exports = {
name: "statistics_exporter",
title: "Prometheus Statistics Export",
description: "Exports statistics to Prometheus",
version: "2.0.0-alpha",
instanceEntrypoint: "instance",
}
+62
View File
@@ -0,0 +1,62 @@
const plugin = require('lib/plugin');
const { Gauge } = require('lib/prometheus');
const instancePlayerCount = new Gauge(
'clusterio_instance_player_count', "Amount of players connected to this cluster",
{ labels: ['instance_id'] }
);
const instanceGameTicksTotal = new Gauge(
'clusterio_instance_game_ticks_total', "Game tick an instance has progressed to",
{ labels: ['instance_id'] }
);
const instanceForceFlowStatistics = new Gauge(
'clusterio_instance_force_flow_statistics', "Items/fluids/enemies/buildings produced/built/killed by a force",
{ labels: ['instance_id', 'force', 'statistic', 'direction', 'name'] },
);
const instanceGameFlowStatistics = new Gauge(
'clusterio_instance_game_flow_statistics', "Pollution produced/consumed in the game",
{ labels: ['instance_id', 'statistic', 'direction', 'name'] },
);
class InstancePlugin extends plugin.BaseInstancePlugin {
async onMetrics() {
let string = await this.instance.server.sendRcon('/sc remote.call("statistics_exporter", "export")');
let stats = JSON.parse(string);
let instanceId = this.instance.config.id;
instanceGameTicksTotal.labels(String(instanceId)).set(stats.game_tick);
instancePlayerCount.labels(String(instanceId)).set(stats.player_count);
for (let [forceName, flowStatistics] of Object.entries(stats.force_flow_statistics)) {
for (let [statisticName, statistic] of Object.entries(flowStatistics)) {
for (let [direction, counts] of Object.entries(statistic)) {
for (let [item, value] of Object.entries(counts)) {
instanceForceFlowStatistics.labels(
String(instanceId), forceName, statisticName, direction, item
).set(value);
}
}
}
}
for (let [direction, counts] of Object.entries(stats.game_flow_statistics.pollution_statistics)) {
for (let [item, value] of Object.entries(counts)) {
instanceGameFlowStatistics.labels(
String(instanceId), "pollution_statistics", direction, item
).set(value);
}
}
}
}
module.exports = {
InstancePlugin,
// For testing only
_instancePlayerCount: instancePlayerCount,
_instanceGameTicksTotal: instanceGameTicksTotal,
_instanceForceFlowStatistics: instanceForceFlowStatistics,
_instanceGameFlowStatistics: instanceGameFlowStatistics,
}
@@ -0,0 +1,39 @@
local statistics = {
"item_production_statistics",
"fluid_production_statistics",
"kill_count_statistics",
"entity_build_count_statistics",
}
local function export()
local stats = {
game_tick = game.tick,
player_count = #game.connected_players,
game_flow_statistics = {
pollution_statistics = {
input = game.pollution_statistics.input_counts,
output = game.pollution_statistics.output_counts,
},
},
force_flow_statistics = {}
}
for _, force in pairs(game.forces) do
local flow_statistics = {}
for _, statName in pairs(statistics) do
flow_statistics[statName] = {
input = force[statName].input_counts,
output = force[statName].output_counts,
}
end
stats.force_flow_statistics[force.name] = flow_statistics
end
rcon.print(game.table_to_json(stats))
end
return {
add_remote_interface = function()
remote.add_interface("statistics_exporter", { export = export })
end
}
Whitespace-only changes.