Add timeout to slave metric request

When the slave metrics request takes more than 10 seconds the response
to the /metrics endpoint ends up being discarded by Prometheus which
defaults to a timeout of 10 seconds.  Fix by ignoring slaves that take
longer than 8 seconds to respond to the metrics request.
This commit is contained in:
Hornwitser committed 2020-11-17 04:19:05 +01:00
1 parent a06781ebc3
commit b742291131
3 files changed
+69 -2

No files matched your search

+27
View File
@@ -1,5 +1,6 @@
"use strict";
const assert = require("assert").strict;
const hrtime = process.hrtime.bigint;
const libHelpers = require("@clusterio/lib/helpers");
@@ -15,4 +16,30 @@ describe("lib/helpers", function() {
assert.equal(libHelpers.basicType({ a: 1 }), "object");
});
});
describe("wait", function() {
it("should wait the approximate amount of time given", async function() {
let startNs = hrtime();
await libHelpers.wait(100);
let duration = Number(hrtime() - startNs) / 1e6;
assert(duration > 90 && duration < 110, `duration waited (${duration}ms) 10% of expected (100ms)`);
});
});
describe("timeout", function() {
it("should return the result of an already resolved promise", async function() {
assert.equal(await libHelpers.timeout(Promise.resolve("value"), 10, "timeout"), "value");
});
it("should return the result of the promise if received before timeout", async function() {
assert.equal(await libHelpers.timeout(
(async () => { await libHelpers.wait(10); return "value"; })(), 20, "timeout"
), "value");
});
it("should return the timeoutResult if not received before timeout", async function() {
assert.equal(await libHelpers.timeout(
(async () => { await libHelpers.wait(20); return "value"; })(), 10, "timeout"
), "timeout");
});
});
});