From 10303f979d439e10dc7ec42ef3705af795fd9a67 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sun, 15 Nov 2020 21:18:12 +0100 Subject: [PATCH] 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. --- plugins/statistics_exporter/instance.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/plugins/statistics_exporter/instance.js b/plugins/statistics_exporter/instance.js index 8a3d63be..d95dd9c7 100644 --- a/plugins/statistics_exporter/instance.js +++ b/plugins/statistics_exporter/instance.js @@ -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 = {