Add timeout to statistics_exporter metrics

When statistics_exporter handles getMetrics it may take a long time for
the result of command it sends to be returned if the instance's command
stream is overloaded, this in turn would cause the slave to miss its
window to reply on the getMetrics request and no metrics for the slave
would be available.  Ensure statistics_exporter does not use more than 5
seconds to respond by adding a timeout to the metrics gathering.
This commit is contained in:
Hornwitser committed 2020-11-17 04:19:05 +01:00
1 parent b742291131
commit 10303f979d
1 file changed
+10 -1
+10 -1
View File
@@ -1,6 +1,7 @@
"use strict";
const libPlugin = require("@clusterio/lib/plugin");
const { Gauge } = require("@clusterio/lib/prometheus");
const libHelpers = require("@clusterio/lib/helpers");
const instancePlayerCount = new Gauge(
@@ -28,7 +29,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
}
}
async onMetrics() {
async gatherMetrics() {
let string = await this.instance.server.sendRcon("/sc statistics_exporter.export()");
let stats;
try {
@@ -61,6 +62,14 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
}
}
}
async onMetrics() {
// Wait up to 5 seconds for the metrics to be collected. It may
// take a long time for the command to go through if the command
// stream is overloaded. Should this take more than 5 seconds the
// previous values for the metrics will end up being sent to master.
await libHelpers.timeout(this.gatherMetrics(), 5e3);
}
}
module.exports = {