From 3b6ebeb653efe3f50eb74b11c9f9ffcd5559ea28 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 28 Sep 2021 21:32:29 +0200 Subject: [PATCH] Handle errors gathering metrics from slaves If the session to a slave is lost while gathering metrics the error would propagate out to the metrics route handler and break collection of statistics. Fix by handling errors thrown from sending the getMetrics request to slaves. --- packages/master/src/routes.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/master/src/routes.js b/packages/master/src/routes.js index 0310d001..1ab57577 100644 --- a/packages/master/src/routes.js +++ b/packages/master/src/routes.js @@ -8,6 +8,7 @@ const jwt = require("jsonwebtoken"); const path = require("path"); const util = require("util"); +const libErrors = require("@clusterio/lib/errors"); const libHelpers = require("@clusterio/lib/helpers"); const libLink = require("@clusterio/lib/link"); const libPlugin = require("@clusterio/lib/plugin"); @@ -47,7 +48,15 @@ async function getMetrics(req, res, next) { let requests = []; let timeout = req.app.locals.master.config.get("master.metrics_timeout") * 1000; for (let slaveConnection of req.app.locals.master.wsServer.slaveConnections.values()) { - requests.push(libHelpers.timeout(libLink.messages.getMetrics.send(slaveConnection), timeout, null)); + requests.push(libHelpers.timeout( + libLink.messages.getMetrics.send(slaveConnection).catch(err => { + if (!(err instanceof libErrors.SessionLost)) { + logger.error(`Unexpected error gathering metrics from slave:\n${err.stack}`); + } + return null; + }), + timeout, null + )); } for await (let result of await libPrometheus.defaultRegistry.collect()) {