Merge pull request #479 from Hornwitser/small-things-9

Small things 9
This commit is contained in:
Hornwitser authored and GitHub committed 2023-02-16 20:15:26 +01:00
commit b7b7aa737a
33 files changed
+754 -193

No files matched your search

+3 -1
View File
@@ -112,7 +112,8 @@ class Control extends libLink.Link {
} }
async debugWsMessageEventHandler(message) { async debugWsMessageEventHandler(message) {
print("WS", message.data.direction, message.data.content); // eslint-disable-next-line no-console
console.log("WS", message.data.direction, message.data.content);
} }
async shutdown() { async shutdown() {
@@ -186,6 +187,7 @@ async function startControl() {
level: args.logLevel, level: args.logLevel,
format: new libLoggingUtils.TerminalFormat(), format: new libLoggingUtils.TerminalFormat(),
})); }));
libLoggingUtils.handleUnhandledErrors(logger);
logger.verbose(`Loading available plugins from ${args.pluginList}`); logger.verbose(`Loading available plugins from ${args.pluginList}`);
let pluginList = new Map(); let pluginList = new Map();
+3 -3
View File
@@ -142,7 +142,7 @@ class CommandTree {
* *
* @param {module:lib/link.Link} client - link to master server to query slave on. * @param {module:lib/link.Link} client - link to master server to query slave on.
* @param {string} slaveName - string with name or id of slave. * @param {string} slaveName - string with name or id of slave.
* @returns {number} slave ID. * @returns {Promise<number>} slave ID.
* @static * @static
*/ */
async function resolveSlave(client, slaveName) { async function resolveSlave(client, slaveName) {
@@ -174,7 +174,7 @@ async function resolveSlave(client, slaveName) {
* *
* @param {module:lib/link.Link} client - link to master server to query instance on. * @param {module:lib/link.Link} client - link to master server to query instance on.
* @param {string} instanceName - string with name or id of instance. * @param {string} instanceName - string with name or id of instance.
* @returns {number} instance ID. * @returns {Promise<number>} instance ID.
* @static * @static
*/ */
async function resolveInstance(client, instanceName) { async function resolveInstance(client, instanceName) {
@@ -239,7 +239,7 @@ async function resolveModPack(client, modPackName) {
* *
* @param {module:lib/link.Link} client - link to master server to query role on. * @param {module:lib/link.Link} client - link to master server to query role on.
* @param {string} roleName - string with name or id of role. * @param {string} roleName - string with name or id of role.
* @returns {Object} Role info. * @returns {Promise<object>} Role info.
* @static * @static
*/ */
async function retrieveRole(client, roleName) { async function retrieveRole(client, roleName) {
+4 -4
View File
@@ -67,7 +67,7 @@ async function loadZip(server, modVersions, mod) {
* The server to load the file from. * The server to load the file from.
* @param {Map<string, string>} modVersions - Mapping of mod to version used. * @param {Map<string, string>} modVersions - Mapping of mod to version used.
* @param {string} modPath - Factorio style path to the file to load. * @param {string} modPath - Factorio style path to the file to load.
* @returns {?Buffer} The content of the file or null if not found. * @returns {Promise<?Buffer>} The content of the file or null if not found.
* @memberof module:lib/factorio * @memberof module:lib/factorio
* @private * @private
* @inner * @inner
@@ -255,7 +255,7 @@ function filterItems(prototypes) {
* @param {Map<string, string>} modVersions - * @param {Map<string, string>} modVersions -
* Mapping of mod name to versions to get icons from. * Mapping of mod name to versions to get icons from.
* @param {Array<Object>} items - Array of item prototypes. * @param {Array<Object>} items - Array of item prototypes.
* @return {{itemSheet: Jimp, itemData: Map<string,Object>}} * @return {Promise<{itemSheet: Jimp, itemData: Map<string,Object>}>}
* Item spritesheet and metadata. * Item spritesheet and metadata.
* @memberof module:lib/factorio * @memberof module:lib/factorio
* @private * @private
@@ -327,7 +327,7 @@ async function exportItems(server, modVersions, items) {
* Mapping of mod name to version to export locale from. * Mapping of mod name to version to export locale from.
* @param {Array<string>} modOrder - Load order of the mods. * @param {Array<string>} modOrder - Load order of the mods.
* @param {string} languageCode - Language to export locale for. * @param {string} languageCode - Language to export locale for.
* @returns {Map<string, string>} merged locale information * @returns {Promise<Map<string, string>>} merged locale information
* @memberof module:lib/factorio * @memberof module:lib/factorio
* @private * @private
* @inner * @inner
@@ -379,7 +379,7 @@ async function exportLocale(server, modVersions, modOrder, languageCode) {
* *
* @param {module:lib/factorio.FactorioServer} server - * @param {module:lib/factorio.FactorioServer} server -
* The server to export the data from. * The server to export the data from.
* @returns {JSZip} zip file with exported data. * @returns {Promise<JSZip>} zip file with exported data.
* @memberof module:lib/factorio * @memberof module:lib/factorio
*/ */
async function exportData(server) { async function exportData(server) {
+34 -13
View File
@@ -86,7 +86,7 @@ function versionOrder(a, b) {
* @param {string} targetVersion - * @param {string} targetVersion -
* Version to look for, supports the special value "latest" for the * Version to look for, supports the special value "latest" for the
* latest version available. * latest version available.
* @returns {Array} Array with path to data dir and version found. * @returns {Promise<Array>} Array with path to data dir and version found.
* @memberof module:lib/factorio * @memberof module:lib/factorio
* @private * @private
* @inner * @inner
@@ -163,7 +163,7 @@ function randomDynamicPort() {
* the given length. * the given length.
* *
* @param {number} length - the length of the password to generate. * @param {number} length - the length of the password to generate.
* @return {string} password of the given length * @return {Promise<string>} password of the given length
* @memberof module:lib/factorio * @memberof module:lib/factorio
* @private * @private
* @inner * @inner
@@ -699,6 +699,16 @@ class FactorioServer extends events.EventEmitter {
this._server.stderr.pipe(stderr); this._server.stderr.pipe(stderr);
} }
_resetState() {
this._state = "init";
this._server = null;
this._rconClient = null;
this._rconReady = false;
this._gameReady = false;
this._unexpected = [];
this._runningAutosave = null;
}
_watchExit() { _watchExit() {
this._server.on("exit", (code, signal) => { this._server.on("exit", (code, signal) => {
if (this._state !== "stopping") { if (this._state !== "stopping") {
@@ -736,15 +746,21 @@ class FactorioServer extends events.EventEmitter {
this._rconClient.end().catch(() => {}); this._rconClient.end().catch(() => {});
} }
// Reset server state this._resetState();
this._state = "init"; this.emit("exit");
this._server = null; });
this._rconClient = null; this._server.on("error", err => {
this._rconReady = false; if (err.code === "EACCES") {
this._gameReady = false; this.emit("error", new libErrors.EnvironmentError("Unable to run server: Permission denied"));
this._unexpected = []; } else {
this._runningAutosave = null; this.emit("error", new libErrors.EnvironmentError(`Unexpected error:\n${err.stack}`));
}
if (this._rconClient) {
this._rconClient.end().catch(() => {});
}
this._resetState();
this.emit("exit"); this.emit("exit");
}); });
} }
@@ -805,6 +821,11 @@ class FactorioServer extends events.EventEmitter {
if (code !== 0) { if (code !== 0) {
throw new Error(`Factorio exited with status ${code}`); throw new Error(`Factorio exited with status ${code}`);
} }
} catch (err) {
if (err.code === "EACCES") {
throw new libErrors.EnvironmentError("Unable to run server: Permission denied");
}
throw err;
} finally { } finally {
this._state = "init"; this._state = "init";
} }
@@ -902,7 +923,7 @@ class FactorioServer extends events.EventEmitter {
* @param {boolean} expectEmpty - * @param {boolean} expectEmpty -
* if true throw if the response is not empty. Useful for detecting * if true throw if the response is not empty. Useful for detecting
* errors that might have been sent in response. * errors that might have been sent in response.
* @returns {string} response from server. * @returns {Promise<string>} response from server.
*/ */
async sendRcon(message, expectEmpty) { async sendRcon(message, expectEmpty) {
this._check(["running", "stopping"]); this._check(["running", "stopping"]);
@@ -1036,7 +1057,7 @@ class FactorioServer extends events.EventEmitter {
* Ensures achievements are disabled on the save that's running. This is * Ensures achievements are disabled on the save that's running. This is
* necessary in order to run any commands at all. * necessary in order to run any commands at all.
* *
* @returns {boolean} * @returns {Promise<boolean>}
* True if acheivements got disabled and false if they already where * True if acheivements got disabled and false if they already where
* disabled. * disabled.
*/ */
@@ -1104,7 +1125,7 @@ class FactorioServer extends events.EventEmitter {
* *
* Loads server-settings.example.json from the data dir. * Loads server-settings.example.json from the data dir.
* *
* @returns {object} the parsed server-settings. * @returns {Promise<object>} the parsed server-settings.
*/ */
async exampleSettings() { async exampleSettings() {
return JSON.parse(await fs.readFile(this.dataPath("server-settings.example.json"), "utf-8")); return JSON.parse(await fs.readFile(this.dataPath("server-settings.example.json"), "utf-8"));
+49 -4
View File
@@ -5,6 +5,7 @@
const fs = require("fs-extra"); const fs = require("fs-extra");
const path = require("path"); const path = require("path");
const crypto = require("crypto"); // needed for getTempFile const crypto = require("crypto"); // needed for getTempFile
/** /**
* Returns the newest file in a directory * Returns the newest file in a directory
* *
@@ -12,7 +13,7 @@ const crypto = require("crypto"); // needed for getTempFile
* @param {function(string): boolean} filter - * @param {function(string): boolean} filter -
* Optional function to filter out files to ignore. Should return true * Optional function to filter out files to ignore. Should return true
* if the file is to be considered. * if the file is to be considered.
* @returns {?string} * @returns {Promise<?string>}
* Name of the file with the newest timestamp or null if the * Name of the file with the newest timestamp or null if the
* directory contains no files. * directory contains no files.
*/ */
@@ -32,6 +33,38 @@ async function getNewestFile(directory, filter = (name) => true) {
return newestFile; return newestFile;
} }
/**
* Returns the total size of all files in a directory
*
* Sums up the file size of all files in the given directory if it exists.
* Error reading the size of files are ignored, and error reading the
* directory will result in 0 being returned.
*
* @param {string} directory - The directory to sum files in.
* @returns {Promise<number>} The size in bytes of all files in the directory.
*/
async function directorySize(directory) {
let dirEntries;
try {
dirEntries = await fs.readdir(directory, { withFileTypes: true });
} catch (err) {
if (err.code === "ENOENT") {
return 0;
}
throw err;
}
let statTasks = [];
for (let entry of dirEntries) {
if (entry.isFile()) {
statTasks.push(fs
.stat(path.join(directory, entry.name))
.then(stat => stat.size, _ => 0)
);
}
}
return (await Promise.all(statTasks)).reduce((a, v) => a + v, 0);
}
/** /**
* Modifies name in case it already exisist at the given directory * Modifies name in case it already exisist at the given directory
* *
@@ -47,8 +80,8 @@ async function getNewestFile(directory, filter = (name) => true) {
* @param {string} directory - directory to check in. * @param {string} directory - directory to check in.
* @param {string} name - file name to check for, may have extension. * @param {string} name - file name to check for, may have extension.
* @param {string} extension - dot extension used for the file name. * @param {string} extension - dot extension used for the file name.
* @returns {string} modified name with extension that likely does not exist * @returns {Promise<string>} modified name with extension that likely does
* in the folder * not exist in the folder
*/ */
async function findUnusedName(directory, name, extension = "") { async function findUnusedName(directory, name, extension = "") {
if (extension && name.endsWith(extension)) { if (extension && name.endsWith(extension)) {
@@ -103,8 +136,19 @@ async function getTempFile(prefix, suffix, tmpdir) {
* @param {object|string} options - see fs.writeFile, `flag` must not be set. * @param {object|string} options - see fs.writeFile, `flag` must not be set.
*/ */
async function safeOutputFile(file, data, options={}) { async function safeOutputFile(file, data, options={}) {
let directory = path.dirname(file);
if (!await fs.pathExists(directory)) {
await fs.mkdirs(directory);
}
let temporary = `${file}.tmp`; let temporary = `${file}.tmp`;
await fs.outputFile(temporary, data, options); let fd = await fs.open(temporary, "w");
try {
await fs.writeFile(fd, data, options);
await fs.fsync(fd);
} finally {
await fs.close(fd);
}
await fs.rename(temporary, file); await fs.rename(temporary, file);
} }
@@ -193,6 +237,7 @@ function cleanFilename(name) {
module.exports = { module.exports = {
getNewestFile, getNewestFile,
directorySize,
findUnusedName, findUnusedName,
getTempFile, getTempFile,
safeOutputFile, safeOutputFile,
+1 -1
View File
@@ -63,7 +63,7 @@ async function timeout(promise, time, timeoutResult) {
* returns all the data which was read from the stream. * returns all the data which was read from the stream.
* *
* @param {Readable} stream - byte stream to read to the end. * @param {Readable} stream - byte stream to read to the end.
* @returns {Buffer} content of the stream. * @returns {Promise<Buffer>} content of the stream.
* @static * @static
*/ */
async function readStream(stream) { async function readStream(stream) {
+1 -1
View File
@@ -255,7 +255,7 @@ class Request extends Message {
* *
* @param {module:lib/link.Link} link - Link to send request over. * @param {module:lib/link.Link} link - Link to send request over.
* @param {Object} data - Data to send with the request. * @param {Object} data - Data to send with the request.
* @returns {object} response data * @returns {Promise<object>} response data
*/ */
async send(link, data = {}) { async send(link, data = {}) {
// XXX validate link target/source? // XXX validate link target/source?
+64 -7
View File
@@ -32,9 +32,14 @@ class ConsoleTransport extends Transport {
super(options); super(options);
this.errorLevels = new Set(options.errorLevels || ["fatal", "error"]); this.errorLevels = new Set(options.errorLevels || ["fatal", "error"]);
this.warnLevels = new Set(options.warnLevels || ["warn"]); this.warnLevels = new Set(options.warnLevels || ["warn"]);
this.filter = options.filter || (() => true);
} }
log(info, callback) { log(info, callback) {
if (!this.filter(info)) {
return callback();
}
/* eslint-disable no-console */ /* eslint-disable no-console */
if (this.errorLevels.has(info[LEVEL])) { if (this.errorLevels.has(info[LEVEL])) {
console.error(info[MESSAGE]); console.error(info[MESSAGE]);
@@ -44,7 +49,7 @@ class ConsoleTransport extends Transport {
console.log(info[MESSAGE]); console.log(info[MESSAGE]);
} }
/* eslint-enable no-console */ /* eslint-enable no-console */
callback(); return callback();
} }
} }
@@ -58,13 +63,11 @@ class WebConsoleFormat {
transform(info, options) { transform(info, options) {
let src = " "; let src = " ";
if (info.slave_id) { if (info.slave_id !== undefined) {
let slaveName = info.slave_id; src += `s:${info.slave_name} - `;
src += `s:${slaveName} - `;
} }
if (info.instance_id) { if (info.instance_id !== undefined) {
let instanceName = info.instance_id; src += `i:${info.instance_name}`;
src += `i:${instanceName}`;
} }
if (info.plugin) { if (info.plugin) {
src += `${info.plugin}: `; src += `${info.plugin}: `;
@@ -79,6 +82,59 @@ class WebConsoleFormat {
} }
} }
/**
* Filter object for logs
* @typedef {Object} module:lib/logging~LogFilter
* @property {string} [max_level] -
* Maximum log level to include. Higher levels are more verbose.
* @property {boolean} [all] -
* Include log entries from master, all slaves and all instances.
* @property {boolean} [master] -
* Include log entries from the master server.
* @property {Array<number>} [slave_ids] -
* Include log entries for the given slaves and instances of those
* slaves by id.
* @property {Array<number>} [instance_ids] -
* Include log entries for the given instances by id.
*/
/**
* Create log filter by level and source.
*
* @param {module:lib/logging~LogFilter} filter -
* Filter to filter log entries by.
* @returns {function(object): boolean}
* filter returning true for log entries that match it.
* @static
*/
function logFilter({ all, master, slave_ids, instance_ids, max_level }) {
return info => {
// Note: reversed to filter out undefined levels
if (max_level && !(levels[info.level] <= levels[max_level])) {
return false;
}
if (all) {
return true;
}
if (master && info.slave_id === undefined) {
return true;
}
if (
slave_ids
&& info.slave_id !== undefined
&& info.instance_id === undefined
&& slave_ids.includes(info.slave_id)
) {
return true;
}
if (instance_ids && info.instance_id !== undefined && instance_ids.includes(info.instance_id)) {
return true;
}
return false;
};
}
const logger = winston.createLogger({ const logger = winston.createLogger({
level: "verbose", level: "verbose",
levels, levels,
@@ -90,5 +146,6 @@ module.exports = {
WebConsoleFormat, WebConsoleFormat,
colors, colors,
levels, levels,
logFilter,
logger, logger,
}; };
+18 -57
View File
@@ -18,7 +18,7 @@ const chalk = require("chalk");
const libLink = require("./link"); const libLink = require("./link");
const libErrors = require("./errors"); const libErrors = require("./errors");
const libFileOps = require("./file_ops"); const libFileOps = require("./file_ops");
const { levels, logger } = require("./logging"); const { levels, logFilter, logger } = require("./logging");
const libStream = require("./stream"); const libStream = require("./stream");
const finished = util.promisify(stream.finished); const finished = util.promisify(stream.finished);
@@ -140,21 +140,6 @@ class LinkTransport extends Transport {
} }
const logFileGlob = /^[a-z]+-(\d{4}-\d{2}-\d{2})\.log$/; const logFileGlob = /^[a-z]+-(\d{4}-\d{2}-\d{2})\.log$/;
/**
* Filter object for logs
* @typedef {Object} module:lib/logging_utils~LogFilter
* @property {string} [max_level] -
* Maximum log level to include. Higher levels are more verbose.
* @property {boolean} [all] -
* Include log entries from master, all slaves and all instances.
* @property {boolean} [master] -
* Include log entries from the master server.
* @property {Array<number>} [slave_ids] -
* Include log entries for the given slaves and instances of those
* slaves by id.
* @property {Array<number>} [instance_ids] -
* Include log entries for the given instances by id.
*/
/** /**
* Keeps an index over a log directory to speed up queries to it * Keeps an index over a log directory to speed up queries to it
@@ -205,7 +190,7 @@ class LogIndex {
* module:lib/logging_utils.LogIndex#save}. * module:lib/logging_utils.LogIndex#save}.
* *
* @param {string} logDirectory - Path to directory to load from. * @param {string} logDirectory - Path to directory to load from.
* @returns {module:lib/logging_utils.LogIndex} loaded or created log index. * @returns {Promise<module:lib/logging_utils.LogIndex>} loaded or created log index.
*/ */
static async load(logDirectory) { static async load(logDirectory) {
try { try {
@@ -289,7 +274,7 @@ class LogIndex {
* *
* @param {string} file - * @param {string} file -
* Name of file in the log directory to check for. * Name of file in the log directory to check for.
* @param {module:lib/logging_utils~LogFilter} filter - * @param {module:lib/logging~LogFilter} filter -
* Filter to check index if file contains entries for. * Filter to check index if file contains entries for.
* @returns {boolean} * @returns {boolean}
* true if the file may contain entries included by the filter. * true if the file may contain entries included by the filter.
@@ -326,43 +311,6 @@ class LogIndex {
} }
} }
/**
* Create log filter by level and source.
*
* @param {module:lib/logging_utils~LogFilter} filter -
* Filter to filter log entries by.
* @returns {function(object): boolean}
* filter returning true for log entries that match it.
* @static
*/
function logFilter({ all, master, slave_ids, instance_ids, max_level }) {
return info => {
// Note: reversed to filter out undefined levels
if (max_level && !(levels[info.level] <= levels[max_level])) {
return false;
}
if (all) {
return true;
}
if (master && info.slave_id === undefined) {
return true;
}
if (
slave_ids
&& info.slave_id !== undefined
&& info.instance_id === undefined
&& slave_ids.includes(info.slave_id)
) {
return true;
}
if (instance_ids && info.instance_id !== undefined && instance_ids.includes(info.instance_id)) {
return true;
}
return false;
};
}
/** /**
* Filter object for querying logs * Filter object for querying logs
* @typedef {Object} module:lib/logging_utils~QueryLogFilter * @typedef {Object} module:lib/logging_utils~QueryLogFilter
@@ -391,7 +339,7 @@ function logFilter({ all, master, slave_ids, instance_ids, max_level }) {
* Filter to limit logs by. * Filter to limit logs by.
* @param {module:lib/logging_utils.LogIndex=} index - * @param {module:lib/logging_utils.LogIndex=} index -
* Index to speed up query with. * Index to speed up query with.
* @returns {Array<Object>} log entries matching the filter * @returns {Promise<Array<Object>>} log entries matching the filter
*/ */
async function queryLog(logDirectory, filter, index) { async function queryLog(logDirectory, filter, index) {
let files = (await fs.readdir(logDirectory)).filter(entry => logFileGlob.test(entry)); let files = (await fs.readdir(logDirectory)).filter(entry => logFileGlob.test(entry));
@@ -510,12 +458,25 @@ async function migrateLogs(log, directory, pattern) {
} }
} }
function handleUnhandledErrors() {
/* eslint-disable node/no-process-exit */
process.on("uncaughtException", err => {
logger.fatal(`Uncaught exception:\n${err.stack}`);
process.exit(1);
});
process.on("unhandledRejection", (reason, promise) => {
logger.fatal(`Unhandled rejection:\n${reason.stack ? reason.stack : reason}`);
process.exit(1);
});
/* eslint-enable node/no-process-exit */
}
module.exports = { module.exports = {
TerminalFormat, TerminalFormat,
LinkTransport, LinkTransport,
LogIndex, LogIndex,
logFilter, handleUnhandledErrors,
queryLog, queryLog,
migrateLogs, migrateLogs,
+1 -1
View File
@@ -812,7 +812,7 @@ function attachPluginMessages(link, plugin) {
* Mapping of plugin names to plugins to invoke the hook on. * Mapping of plugin names to plugins to invoke the hook on.
* @param {string} hook - Name of hook to invoke. * @param {string} hook - Name of hook to invoke.
* @param {...*} args - Arguments to pass on to the hook. * @param {...*} args - Arguments to pass on to the hook.
* @returns {Array} Non-empty return values from the hooks. * @returns {Promise<Array>} Non-empty return values from the hooks.
*/ */
async function invokeHook(plugins, hook, ...args) { async function invokeHook(plugins, hook, ...args) {
let results = []; let results = [];
+24 -10
View File
@@ -6,6 +6,7 @@
"use strict"; "use strict";
const libErrors = require("./errors"); const libErrors = require("./errors");
const libPlugin = require("./plugin");
const path = require("path"); const path = require("path");
@@ -18,7 +19,7 @@ const path = require("path");
* *
* @param {Map<string, string>} pluginList - * @param {Map<string, string>} pluginList -
* Mapping of plugin name to require path for the plugins to load. * Mapping of plugin name to require path for the plugins to load.
* @returns {Array<Object>} Array of plugin info modules. * @returns {Promise<Array<Object>>} Array of plugin info modules.
* @static * @static
*/ */
async function loadPluginInfos(pluginList) { async function loadPluginInfos(pluginList) {
@@ -48,18 +49,33 @@ async function loadPluginInfos(pluginList) {
return plugins; return plugins;
} }
function loadPluginClass(entrypointName, className, pluginClass, pluginInfo) {
let resolvedPath = path.posix.join(pluginInfo.requirePath, pluginInfo[entrypointName]);
let entrypoint = require(resolvedPath);
if (!entrypoint[className]) {
throw new libErrors.PluginError(pluginInfo.name,
new Error(`Expected ${resolvedPath} to export a class named ${className}`)
);
}
if (!(entrypoint[className].prototype instanceof pluginClass)) {
throw new libErrors.PluginError(pluginInfo.name,
new Error(`Expected ${className} exported from ${resolvedPath} to be a subclass of ${pluginClass.name}`)
);
}
return entrypoint[className];
}
/** /**
* Load master plugin class of a plugin * Load master plugin class of a plugin
* *
* @param {Object} pluginInfo - * @param {Object} pluginInfo -
* Plugin info object returned from {@link * Plugin info object returned from {@link
* module:lib/plugin_loader.loadPluginInfos} to load class from. * module:lib/plugin_loader.loadPluginInfos} to load class from.
* @returns {function} plugin class * @returns {Promise<function>} plugin class
* @static * @static
*/ */
async function loadMasterPluginClass(pluginInfo) { async function loadMasterPluginClass(pluginInfo) {
let entrypoint = require(path.posix.join(pluginInfo.requirePath, pluginInfo.masterEntrypoint)); return loadPluginClass("masterEntrypoint", "MasterPlugin", libPlugin.BaseMasterPlugin, pluginInfo);
return entrypoint.MasterPlugin;
} }
/** /**
@@ -68,12 +84,11 @@ async function loadMasterPluginClass(pluginInfo) {
* @param {Object} pluginInfo - * @param {Object} pluginInfo -
* Plugin info object returned from {@link * Plugin info object returned from {@link
* module:lib/plugin_loader.loadPluginInfos} to load class from. * module:lib/plugin_loader.loadPluginInfos} to load class from.
* @returns {function} plugin class * @returns {Promise<function>} plugin class
* @static * @static
*/ */
async function loadInstancePluginClass(pluginInfo) { async function loadInstancePluginClass(pluginInfo) {
let entrypoint = require(path.posix.join(pluginInfo.requirePath, pluginInfo.instanceEntrypoint)); return loadPluginClass("instanceEntrypoint", "InstancePlugin", libPlugin.BaseInstancePlugin, pluginInfo);
return entrypoint.InstancePlugin;
} }
/** /**
@@ -82,12 +97,11 @@ async function loadInstancePluginClass(pluginInfo) {
* @param {Object} pluginInfo - * @param {Object} pluginInfo -
* Plugin info object returned from {@link * Plugin info object returned from {@link
* module:lib/plugin_loader.loadPluginInfos} to load class from. * module:lib/plugin_loader.loadPluginInfos} to load class from.
* @returns {function} plugin class * @returns {Promise<function>} plugin class
* @static * @static
*/ */
async function loadControlPluginClass(pluginInfo) { async function loadControlPluginClass(pluginInfo) {
let entrypoint = require(path.posix.join(pluginInfo.requirePath, pluginInfo.controlEntrypoint)); return loadPluginClass("controlEntrypoint", "ControlPlugin", libPlugin.BaseControlPlugin, pluginInfo);
return entrypoint.ControlPlugin;
} }
module.exports = { module.exports = {
+231 -5
View File
@@ -9,9 +9,8 @@
* The ordinary use case of instrumenting a code base should be covered by * The ordinary use case of instrumenting a code base should be covered by
* {@link module:lib/prometheus.Counter}, {@link * {@link module:lib/prometheus.Counter}, {@link
* module:lib/prometheus.Gauge}, {@link module:lib/prometheus.Histogram} and * module:lib/prometheus.Gauge}, {@link module:lib/prometheus.Histogram} and
* {@link module:lib/prometheus.exposition}. Note that the summary * {@link module:lib/prometheus.exposition}. See documentation for each of
* collector is not implemented. See documentation for each of the listed * the listed interfaces for more information.
* interfaces for more information.
* *
* @example * @example
* const { Counter, exposition } = require("@clusterio/lib/prometheus"); * const { Counter, exposition } = require("@clusterio/lib/prometheus");
@@ -802,7 +801,7 @@ class GaugeChild extends ValueCollectorChild {
* to avoid having the time of many collectors add up to too much. * to avoid having the time of many collectors add up to too much.
* *
* Like with the Counter the Gauge also supports labels. When using labels * Like with the Counter the Gauge also supports labels. When using labels
* the methods for changing the value of the counter is not longer usable * the methods for changing the value of the counter is no longer usable
* directly on the counter itself, instead a child counter with label values * directly on the counter itself, instead a child counter with label values
* set has to be retrieved with the `.labels()` method. For example: * set has to be retrieved with the `.labels()` method. For example:
* *
@@ -919,6 +918,232 @@ class Gauge extends ValueCollector {
} }
} }
/**
* Child Summary holding the sum and count for a single label set.
*/
class SummaryChild {
constructor(collector, key) {
this._sumValues = collector._sumValues;
this._countValues = collector._countValues;
this._key = key;
this._sumValues.set(key, 0);
this._countValues.set(key, 0);
}
/**
* Sum of all observations for label set
* @type {number}
*/
get sum() {
return this._sumValues.get(this._key);
}
/**
* Count of observations for label set
* @type {number}
*/
get count() {
return this._countValues.get(this._key);
}
/**
* Observe a given value and increment matching buckets for label set
*
* @param {number} value - number to count into summary.
*/
observe(value) {
this._sumValues.set(this._key, this._sumValues.get(this._key) + value);
this._countValues.set(this._key, this._countValues.get(this._key) + 1);
}
/**
* Start a timer for observing a duration for label set
*
* @returns {function()}
* function that when called will store the duration in seconds from
* when the timer was started into the metric.
*/
startTimer() {
let start = process.hrtime.bigint();
return () => {
let end = process.hrtime.bigint();
this.observe(Number(end - start) / 1e9);
};
}
}
/**
* Summary metric
*
* Sample observations into a count and sum. This is useful when you have
* an operation that reports a metric that you want insight into the rate
* and average size of values for. A common case for this is request
* duration, for example:
*
* ```js
* const requestDuration = new Summary(
* "app_request_duration_seconds",
* "Time to process app requests",
* );
*
* async function handleRequest(...) {
* const observeDuration = requestDuration.startTimer();
* try {
* // Do async stuff.
* } finally {
* observeDuration();
* }
* }
* ```
*
* Note that reporting quantiles is note supported. This makes the Summary
* less insightful than the Histogram while using less resources.
*
* The `.startTimer()` method provides a convenient interface for adding
* observed durations to the summary. Other types of values can be added to
* a summary with the `.observe()` method.
*
* Like with the Counter and Gauge the Summary collector also supports
* labels. When using labels the methods for observing values into the
* summary is no longer usable directly on the counter itself, instead a
* child summary with label values set has to be retrieved with the
* `.labels()` method. For example:
*
* ```js
* const requestDuration = new Summary(
* "app_request_duration_seconds",
* "Time to process app requests",
* { labels: ["endpoint"] }
* );
*
* async function handleRequest(endpoint, ...) {
* const observeDuration = requestDuration.labels(endpoint).startTimer();
* try {
* // Do async stuff.
* } finally {
* observeDuration();
* }
* }
* ```
*
* When using labels the summary is no longer initalized with a default
* value, and this may cause issues with aggregating and querying the
* summary in Prometheus. When dynamically setting labels it is recommended
* where possible to initialize all possible combinations of label values
* that will be used. This is done by calling `.labels()` for each
* combination, for example:
*
* ```js
* for (let endpoint of ["/status", "/api", ...) {
* requestDuration.labels(endpoint);
* }
* ```
*
* Note that every combination of label values used creates two new time
* series that need to be stored and processed. You should carefully
* evaluate which labels you actually need as resource usage for a metric
* increases exponentially with the number of labels used.
*
* @static
*/
class Summary extends LabeledCollector {
/**
* Create optionally labeled summary
*
* @param {string} name - Name of the metric.
* @param {string} help - Help text for metric.
* @param {Object=} options - options for collector.
* @param {Array<string>=} options.labels -
* Labels for this metric, defaults to no labels.
* @param {boolean=} options.register -
* If true registers this collector with the default registry.
* Defaults to true.
* @param {function()=} options.callback -
* Possibly async function that is called when the metric is
* collected. The collector being collected is passed as the
* argument.
*/
constructor(name, help, options = {}) {
super("summary", name, help, options, SummaryChild);
this._sumValues = new Map();
this._countValues = new Map();
this._defaultChild = this.metric.labels.length ? null : this.labels({});
}
async* collect() {
if (this.callback) { await this.callback(this); }
yield {
metric: this.metric,
samples: new Map([
["_sum", this._sumValues],
["_count", this._countValues],
]),
};
}
/**
* Sum of all observations for label set
*
* Note: Only available if this is an unlabeled collector.
* @type {number}
*/
get sum() {
return this._defaultChild.sum;
}
/**
* Count of observations for label set
*
* Note: Only available if this is an unlabeled collector.
* @type {number}
*/
get count() {
return this._defaultChild.count;
}
/**
* Observe a given value and increment matching buckets
*
* Note: Only works if this is an unlabeled collector.
* @param {number} value - number to count into histogram buckets.
*/
observe(value) {
this._defaultChild.observe(value);
}
/**
* Start a timer for observing a duration
*
* Note: Only works if this is an unlabeled collector.
* @returns {function()}
* function that when called will store the duration in seconds from
* when the timer was started into the metric.
*/
startTimer() {
return this._defaultChild.startTimer();
}
remove(...labels) {
let key = super.remove(...labels);
this._sumValues.delete(key);
this._countValues.delete(key);
}
removeAll(labels) {
super.removeAll(labels);
removeMatchingLabels(this._sumValues, labels);
removeMatchingLabels(this._countValues, labels);
}
clear() {
super.clear();
this._sumValues = new Map();
this._countValues = new Map();
}
}
function formatValue(value) { function formatValue(value) {
if (value === Infinity) { if (value === Infinity) {
return "+Inf"; return "+Inf";
@@ -1047,7 +1272,7 @@ class HistogramChild {
* *
* Like with the Counter and Gauge the Histogram collector also supports * Like with the Counter and Gauge the Histogram collector also supports
* labels. When using labels the methods for observing values into the * labels. When using labels the methods for observing values into the
* histogram is not longer usable directly on the counter itself, instead a * histogram is no longer usable directly on the counter itself, instead a
* child histogram with label values set has to be retrieved with the * child histogram with label values set has to be retrieved with the
* `.labels()` method. For example: * `.labels()` method. For example:
* *
@@ -1567,6 +1792,7 @@ defaultCollectors.processStartTimeSeconds.setToCurrentTime();
module.exports = { module.exports = {
Counter, Counter,
Gauge, Gauge,
Summary,
Histogram, Histogram,
CollectorRegistry, CollectorRegistry,
Collector, Collector,
+3
View File
@@ -618,16 +618,19 @@ definePermission({
name: "core.user.get", name: "core.user.get",
title: "Get user", title: "Get user",
description: "Get the details of a user in the cluster.", description: "Get the details of a user in the cluster.",
grantByDefault: true,
}); });
definePermission({ definePermission({
name: "core.user.list", name: "core.user.list",
title: "List users", title: "List users",
description: "Get the full list of users in the cluster.", description: "Get the full list of users in the cluster.",
grantByDefault: true,
}); });
definePermission({ definePermission({
name: "core.user.subscribe", name: "core.user.subscribe",
title: "Subscribe to user updates", title: "Subscribe to user updates",
description: "Subscribe to be notified on updates on the details and status of users.", description: "Subscribe to be notified on updates on the details and status of users.",
grantByDefault: true,
}); });
definePermission({ definePermission({
name: "core.user.create", name: "core.user.create",
+8 -8
View File
@@ -38,6 +38,7 @@ const libLoggingUtils = require("@clusterio/lib/logging_utils");
const Master = require("./src/Master"); const Master = require("./src/Master");
const UserManager = require("./src/UserManager"); const UserManager = require("./src/UserManager");
const version = require("./package").version;
// globals // globals
let master; let master;
@@ -283,6 +284,13 @@ async function initialize() {
format: new libLoggingUtils.TerminalFormat(), format: new libLoggingUtils.TerminalFormat(),
})); }));
} }
libLoggingUtils.handleUnhandledErrors(logger);
let command = parameters.args._[0];
if (command === "run") {
logger.info(`Starting Clusterio master ${version}`);
parameters.shouldRun = true;
}
logger.info(`Loading available plugins from ${parameters.args.pluginList}`); logger.info(`Loading available plugins from ${parameters.args.pluginList}`);
let pluginList = new Map(); let pluginList = new Map();
@@ -295,7 +303,6 @@ async function initialize() {
} }
// If the command is plugin management we don't try to load plugins // If the command is plugin management we don't try to load plugins
let command = parameters.args._[0];
if (command === "plugin") { if (command === "plugin") {
await libSharedCommands.handlePluginCommand(parameters.args, pluginList, parameters.args.pluginList); await libSharedCommands.handlePluginCommand(parameters.args, pluginList, parameters.args.pluginList);
return parameters; return parameters;
@@ -336,15 +343,11 @@ async function initialize() {
await libSharedCommands.handleConfigCommand( await libSharedCommands.handleConfigCommand(
parameters.args, parameters.masterConfig, parameters.masterConfigPath parameters.args, parameters.masterConfig, parameters.masterConfigPath
); );
return parameters;
} else if (command === "bootstrap") { } else if (command === "bootstrap") {
await handleBootstrapCommand(parameters.args, parameters.masterConfig); await handleBootstrapCommand(parameters.args, parameters.masterConfig);
return parameters;
} }
// If we get here the command was run
parameters.shouldRun = true;
return parameters; return parameters;
} }
@@ -353,9 +356,6 @@ async function startup() {
// and as the process name in ps/top on linux. // and as the process name in ps/top on linux.
process.title = "clusterioMaster"; process.title = "clusterioMaster";
// add better stack traces on promise rejection
process.on("unhandledRejection", err => logger.error(`Unhandled rejection:\n${err.stack}`));
let { args, shouldRun, clusterLogger, pluginInfos, masterConfigPath, masterConfig } = await initialize(); let { args, shouldRun, clusterLogger, pluginInfos, masterConfigPath, masterConfig } = await initialize();
if (!shouldRun) { if (!shouldRun) {
return; return;
+6 -3
View File
@@ -9,7 +9,7 @@ const libData = require("@clusterio/lib/data");
const libErrors = require("@clusterio/lib/errors"); const libErrors = require("@clusterio/lib/errors");
const libHelpers = require("@clusterio/lib/helpers"); const libHelpers = require("@clusterio/lib/helpers");
const libLink = require("@clusterio/lib/link"); const libLink = require("@clusterio/lib/link");
const { logger } = require("@clusterio/lib/logging"); const { logFilter, logger } = require("@clusterio/lib/logging");
const libLoggingUtils = require("@clusterio/lib/logging_utils"); const libLoggingUtils = require("@clusterio/lib/logging_utils");
const libPlugin = require("@clusterio/lib/plugin"); const libPlugin = require("@clusterio/lib/plugin");
const libPrometheus = require("@clusterio/lib/prometheus"); const libPrometheus = require("@clusterio/lib/prometheus");
@@ -289,7 +289,7 @@ class ControlConnection extends BaseConnection {
async updateInstanceConfig(instance) { async updateInstanceConfig(instance) {
let slaveId = instance.config.get("instance.assigned_slave"); let slaveId = instance.config.get("instance.assigned_slave");
if (slaveId) { if (slaveId !== null) {
let connection = this._master.wsServer.slaveConnections.get(slaveId); let connection = this._master.wsServer.slaveConnections.get(slaveId);
if (connection) { if (connection) {
await libLink.messages.assignInstance.send(connection, { await libLink.messages.assignInstance.send(connection, {
@@ -356,6 +356,9 @@ class ControlConnection extends BaseConnection {
instance_id, instance_id,
serialized_config: instance.config.serialize("slave"), serialized_config: instance.config.serialize("slave"),
}); });
} else {
instance.status = "unassigned";
this._master.instanceUpdated(instance);
} }
} }
@@ -671,7 +674,7 @@ class ControlConnection extends BaseConnection {
this.logTransport = new libLoggingUtils.LinkTransport({ link: this }); this.logTransport = new libLoggingUtils.LinkTransport({ link: this });
this._master.clusterLogger.add(this.logTransport); this._master.clusterLogger.add(this.logTransport);
} }
this.logTransport.filter = libLoggingUtils.logFilter(this.logSubscriptions); this.logTransport.filter = logFilter(this.logSubscriptions);
} else if (this.logTransport) { } else if (this.logTransport) {
this._master.clusterLogger.remove(this.logTransport); this._master.clusterLogger.remove(this.logTransport);
+36 -8
View File
@@ -6,7 +6,7 @@ const fs = require("fs-extra");
const http = require("http"); const http = require("http");
const https = require("https"); const https = require("https");
const path = require("path"); const path = require("path");
const stream = require("stream");
const libConfig = require("@clusterio/lib/config"); const libConfig = require("@clusterio/lib/config");
const libData = require("@clusterio/lib/data"); const libData = require("@clusterio/lib/data");
@@ -26,9 +26,15 @@ const UserManager = require("./UserManager");
const WsServer = require("./WsServer"); const WsServer = require("./WsServer");
const hitCounter = new libPrometheus.Counter( const endpointDurationSummary = new libPrometheus.Summary(
"clusterio_master_http_hits_total", "clusterio_master_http_endpoint_duration_seconds",
"How many HTTP requests in total have been received" "Time it took to respond to a an HTTP request",
{ labels: ["route"] }
);
const logSizeGauge = new libPrometheus.Gauge(
"clusterio_master_log_bytes",
"Size of all log files currently stored on the master."
); );
/** /**
@@ -130,6 +136,19 @@ class Master {
} }
} }
/**
* Get the total size of the logs stored
*
* @returns {Promise<number>} size in bytes of stored log files.
*/
async logSize() {
return (await Promise.all([
libFileOps.directorySize(path.join(this.logDirectory, "cluster")),
libFileOps.directorySize(path.join(this.logDirectory, "master")),
libFileOps.directorySize(path.join(this.logDirectory, "slave")),
])).reduce((a, v) => a + v, 0);
}
async _startInternal(args) { async _startInternal(args) {
this.logDirectory = args.logDirectory; this.logDirectory = args.logDirectory;
this.clusterLogIndex = await libLoggingUtils.LogIndex.load(path.join(this.logDirectory, "cluster")); this.clusterLogIndex = await libLoggingUtils.LogIndex.load(path.join(this.logDirectory, "cluster"));
@@ -138,6 +157,7 @@ class Master {
err => logger.error(`Error building cluster log index:\n${err.stack}`) err => logger.error(`Error building cluster log index:\n${err.stack}`)
); );
}, 600e3); }, 600e3);
logSizeGauge.callback = async () => { logSizeGauge.set(await this.logSize()); };
// Start webpack development server if enabled // Start webpack development server if enabled
if (args.dev || args.devPlugin) { if (args.dev || args.devPlugin) {
@@ -464,7 +484,7 @@ class Master {
* @param {module:lib/logging_utils~QueryLogFilter} filter - * @param {module:lib/logging_utils~QueryLogFilter} filter -
* Filter to limit entries with. Note that only the master log can * Filter to limit entries with. Note that only the master log can
* be queried from this function. * be queried from this function.
* @returns {Array<Object>} log entries matching the filter * @returns {Promise<Array<Object>>} log entries matching the filter
*/ */
async queryMasterLog(filter) { async queryMasterLog(filter) {
return await libLoggingUtils.queryLog( return await libLoggingUtils.queryLog(
@@ -477,7 +497,7 @@ class Master {
* *
* @param {module:lib/logging_utils~QueryLogFilter} filter - * @param {module:lib/logging_utils~QueryLogFilter} filter -
* Filter to limit entries with. * Filter to limit entries with.
* @returns {Array<Object>} log entries matching the filter * @returns {Promise<Array<Object>>} log entries matching the filter
*/ */
async queryClusterLog(filter) { async queryClusterLog(filter) {
return await libLoggingUtils.queryLog( return await libLoggingUtils.queryLog(
@@ -487,7 +507,15 @@ class Master {
static addAppRoutes(app, pluginInfos) { static addAppRoutes(app, pluginInfos) {
app.use((req, res, next) => { app.use((req, res, next) => {
hitCounter.inc(); let start = process.hrtime.bigint();
stream.finished(res, () => {
let routePath = "static";
if (req.route && req.route.path) {
routePath = req.route.path;
}
let end = process.hrtime.bigint();
endpointDurationSummary.labels(routePath).observe(Number(end - start) / 1e9);
});
next(); next();
}); });
app.use(compression()); app.use(compression());
@@ -783,7 +811,7 @@ class Master {
* @param {object} data - Data to pass with the request. * @param {object} data - Data to pass with the request.
* @param {number} data.instance_id - * @param {number} data.instance_id -
* ID of instance to send this request towards * ID of instance to send this request towards
* @returns {object} data returned from the request. * @returns {Promise<object>} data returned from the request.
*/ */
async forwardRequestToInstance(request, data) { async forwardRequestToInstance(request, data) {
let instance = this.getRequestInstance(data.instance_id); let instance = this.getRequestInstance(data.instance_id);
+6 -1
View File
@@ -1,9 +1,14 @@
// Shared metrics // Shared metrics
"use strict"; "use strict";
const libPrometheus = require("@clusterio/lib/prometheus"); const libPrometheus = require("@clusterio/lib/prometheus");
const util = require("util");
exports.endpointHitCounter = new libPrometheus.Counter( exports.endpointHitCounter = new libPrometheus.Counter(
"clusterio_master_http_endpoint_hits_total", "clusterio_master_http_endpoint_hits_total",
"How many requests a particular HTTP endpoint has gotten", "How many requests a particular HTTP endpoint has gotten",
{ labels: ["route"] } { labels: ["route"], register: false }
);
exports.endpointHitCounter.labels = util.deprecate(
exports.endpointHitCounter.labels, "incrementing endpointHitCounter is no longer needed and has no effect"
); );
-7
View File
@@ -19,8 +19,6 @@ const libPlugin = require("@clusterio/lib/plugin");
const libPrometheus = require("@clusterio/lib/prometheus"); const libPrometheus = require("@clusterio/lib/prometheus");
const { logger } = require("@clusterio/lib/logging"); const { logger } = require("@clusterio/lib/logging");
const { endpointHitCounter } = require("./metrics");
const finished = util.promisify(nodeStream.finished); const finished = util.promisify(nodeStream.finished);
@@ -41,8 +39,6 @@ function mergeSamples(destinationResult, sourceResult) {
// Prometheus polling endpoint // Prometheus polling endpoint
async function getMetrics(req, res, next) { async function getMetrics(req, res, next) {
endpointHitCounter.labels(req.route.path).inc();
let results = []; let results = [];
let pluginResults = await libPlugin.invokeHook(req.app.locals.master.plugins, "onMetrics"); let pluginResults = await libPlugin.invokeHook(req.app.locals.master.plugins, "onMetrics");
for (let metricIterator of pluginResults) { for (let metricIterator of pluginResults) {
@@ -181,7 +177,6 @@ function validateUserToken(req, res, next) {
// Handle an uploaded export package. // Handle an uploaded export package.
async function uploadExport(req, res) { async function uploadExport(req, res) {
endpointHitCounter.labels(req.route.path).inc();
if (req.get("Content-Type") !== "application/zip") { if (req.get("Content-Type") !== "application/zip") {
res.sendStatus(415); res.sendStatus(415);
return; return;
@@ -340,7 +335,6 @@ async function uploadSave(req, res) {
res.status(403).json({ request_errors: [err.message] }); res.status(403).json({ request_errors: [err.message] });
return; return;
} }
endpointHitCounter.labels(req.route.path).inc();
let contentType = req.get("Content-Type"); let contentType = req.get("Content-Type");
let match = contentTypeRegExp.exec(contentType); let match = contentTypeRegExp.exec(contentType);
@@ -479,7 +473,6 @@ async function uploadMod(req, res) {
res.status(403).json({ request_errors: [err.message] }); res.status(403).json({ request_errors: [err.message] });
return; return;
} }
endpointHitCounter.labels(req.route.path).inc();
let contentType = req.get("Content-Type"); let contentType = req.get("Content-Type");
let match = contentTypeRegExp.exec(contentType); let match = contentTypeRegExp.exec(contentType);
+8 -2
View File
@@ -27,7 +27,7 @@ const libPluginLoader = require("@clusterio/lib/plugin_loader");
const libErrors = require("@clusterio/lib/errors"); const libErrors = require("@clusterio/lib/errors");
const libConfig = require("@clusterio/lib/config"); const libConfig = require("@clusterio/lib/config");
const libSharedCommands = require("@clusterio/lib/shared_commands"); const libSharedCommands = require("@clusterio/lib/shared_commands");
const { ConsoleTransport, levels, logger } = require("@clusterio/lib/logging"); const { ConsoleTransport, FilteredTransport, levels, logger } = require("@clusterio/lib/logging");
const libLoggingUtils = require("@clusterio/lib/logging_utils"); const libLoggingUtils = require("@clusterio/lib/logging_utils");
const Slave = require("./src/Slave"); const Slave = require("./src/Slave");
@@ -126,8 +126,15 @@ async function startSlave() {
logger.add(new ConsoleTransport({ logger.add(new ConsoleTransport({
level: args.logLevel, level: args.logLevel,
format: new libLoggingUtils.TerminalFormat(), format: new libLoggingUtils.TerminalFormat(),
filter: info => info.instance_id === undefined,
})); }));
} }
libLoggingUtils.handleUnhandledErrors(logger);
let command = args._[0];
if (command === "run") {
logger.info(`Starting Clusterio slave ${version}`);
}
// add better stack traces on promise rejection // add better stack traces on promise rejection
process.on("unhandledRejection", err => logger.error(`Unhandled rejection:\n${err.stack}`)); process.on("unhandledRejection", err => logger.error(`Unhandled rejection:\n${err.stack}`));
@@ -143,7 +150,6 @@ async function startSlave() {
} }
// If the command is plugin management we don't try to load plugins // If the command is plugin management we don't try to load plugins
let command = args._[0];
if (command === "plugin") { if (command === "plugin") {
await libSharedCommands.handlePluginCommand(args, pluginList, args.pluginList); await libSharedCommands.handlePluginCommand(args, pluginList, args.pluginList);
return; return;
+16 -12
View File
@@ -16,7 +16,7 @@ const libPluginLoader = require("@clusterio/lib/plugin_loader");
const libErrors = require("@clusterio/lib/errors"); const libErrors = require("@clusterio/lib/errors");
const libPrometheus = require("@clusterio/lib/prometheus"); const libPrometheus = require("@clusterio/lib/prometheus");
const libLuaTools = require("@clusterio/lib/lua_tools"); const libLuaTools = require("@clusterio/lib/lua_tools");
const { logger } = require("@clusterio/lib/logging"); const libLogging = require("@clusterio/lib/logging");
const instanceRconCommandDuration = new libPrometheus.Histogram( const instanceRconCommandDuration = new libPrometheus.Histogram(
@@ -53,7 +53,7 @@ const instanceFactorioAutosaveSize = new libPrometheus.Gauge(
); );
function applyAsConfig(name) { function applyAsConfig(name) {
return async function action(instance, value) { return async function action(instance, value, logger) {
if (name === "tags" && value instanceof Array) { if (name === "tags" && value instanceof Array) {
// Replace spaces with non-break spaces and delimit by spaces. // Replace spaces with non-break spaces and delimit by spaces.
// This does change the defined tags, but there doesn't seem to // This does change the defined tags, but there doesn't seem to
@@ -83,7 +83,7 @@ const serverSettingsActions = {
"game_password": applyAsConfig("password"), "game_password": applyAsConfig("password"),
"require_user_verification": applyAsConfig("require-user-verification"), "require_user_verification": applyAsConfig("require-user-verification"),
"tags": applyAsConfig("tags"), "tags": applyAsConfig("tags"),
"visibility": async (instance, value) => { "visibility": async (instance, value, logger) => {
for (let scope of ["lan", "public", "steam"]) { for (let scope of ["lan", "public", "steam"]) {
try { try {
let enabled = Boolean(value[scope]); let enabled = Boolean(value[scope]);
@@ -116,7 +116,7 @@ class Instance extends libLink.Link {
*/ */
this.id = this.config.get("instance.id"); this.id = this.config.get("instance.id");
this.logger = logger.child({ this.logger = libLogging.logger.child({
instance_id: this.id, instance_id: this.id,
instance_name: this.name, instance_name: this.name,
}); });
@@ -193,7 +193,7 @@ class Instance extends libLink.Link {
/** /**
* Mod pack currently running on this instance * Mod pack currently running on this instance
* @type {module:lib/data.ModPack>} * @type {module:lib/data.ModPack}
*/ */
this.activeModPack = undefined; this.activeModPack = undefined;
@@ -538,7 +538,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
* entries from the given settings object. * entries from the given settings object.
* *
* @param {Object} overrides - Server settings to override. * @param {Object} overrides - Server settings to override.
* @returns {Object} * @returns {Promise<Object>}
* server example settings with the given settings applied over it. * server example settings with the given settings applied over it.
*/ */
async resolveServerSettings(overrides) { async resolveServerSettings(overrides) {
@@ -580,7 +580,6 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
* @param {String} factorioDir - Path to factorio installation. * @param {String} factorioDir - Path to factorio installation.
*/ */
static async create(instanceDir, factorioDir) { static async create(instanceDir, factorioDir) {
logger.info(`Creating ${instanceDir}`);
await fs.ensureDir(path.join(instanceDir, "script-output")); await fs.ensureDir(path.join(instanceDir, "script-output"));
await fs.ensureDir(path.join(instanceDir, "saves")); await fs.ensureDir(path.join(instanceDir, "saves"));
} }
@@ -649,7 +648,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
// Write mod-list.json // Write mod-list.json
await fs.outputFile(this.path("mods", "mod-list.json"), JSON.stringify({ await fs.outputFile(this.path("mods", "mod-list.json"), JSON.stringify({
mods: [{ name: "base" }, ...this.activeModPack.mods.values()].map(m => ({ name: m.name, enabled: true })), mods: [...this.activeModPack.mods.values()],
}, null, 2)); }, null, 2));
// Write mod-settings.dat // Write mod-settings.dat
@@ -699,10 +698,10 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
* *
* Creates a new save if no save is passed and patches it with modules. * Creates a new save if no save is passed and patches it with modules.
* *
* @param {String|null} saveName - * @param {string|null} saveName -
* Save to prepare from the instance saves directory. Creates a new * Save to prepare from the instance saves directory. Creates a new
* save if null. * save if null.
* @returns {String} Name of the save prepared. * @returns {Promise<string>} Name of the save prepared.
*/ */
async prepareSave(saveName) { async prepareSave(saveName) {
// Use latest save if no save was specified // Use latest save if no save was specified
@@ -757,7 +756,12 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
throw new Error(`Module for plugin ${pluginName} is missing module.json`); throw new Error(`Module for plugin ${pluginName} is missing module.json`);
} }
let module = JSON.parse(await fs.readFile(moduleJsonPath)); let module;
try {
module = JSON.parse(await fs.readFile(moduleJsonPath));
} catch (err) {
throw new Error(`Loading module/module.json in plugin ${pluginName} failed: ${err.message}`);
}
if (module.name !== pluginName) { if (module.name !== pluginName) {
throw new Error(`Expected name of module for plugin ${pluginName} to match the plugin name`); throw new Error(`Expected name of module for plugin ${pluginName} to match the plugin name`);
} }
@@ -875,7 +879,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
for (let [key, action] of Object.entries(serverSettingsActions)) { for (let [key, action] of Object.entries(serverSettingsActions)) {
if (current[key] !== undefined && !util.isDeepStrictEqual(current[key], previous[key])) { if (current[key] !== undefined && !util.isDeepStrictEqual(current[key], previous[key])) {
await action(this, current[key]); await action(this, current[key], this.logger);
} }
} }
} }
+55 -13
View File
@@ -41,7 +41,7 @@ function checkRequestSaveName(name) {
* instanceInfo objects. * instanceInfo objects.
* *
* @param {string} instancesDir - Directory containing instances * @param {string} instancesDir - Directory containing instances
* @returns {Map<integer, Object>} * @returns {Promise<Map<integer, Object>>}
* mapping between instance id and information about this instance. * mapping between instance id and information about this instance.
* @private * @private
*/ */
@@ -307,7 +307,7 @@ class Slave extends libLink.Link {
let instanceInfo = this.instanceInfos.get(instance_id); let instanceInfo = this.instanceInfos.get(instance_id);
if (instanceInfo) { if (instanceInfo) {
instanceInfo.config.update(serialized_config, true, "master"); instanceInfo.config.update(serialized_config, true, "master");
logger.verbose(`Updated config for ${instanceInfo.path}`); logger.verbose(`Updated config for ${instanceInfo.path}`, this.instanceLogMeta(instance_id, instanceInfo));
} else { } else {
instanceInfo = this.discoveredInstanceInfos.get(instance_id); instanceInfo = this.discoveredInstanceInfos.get(instance_id);
@@ -320,6 +320,7 @@ class Slave extends libLink.Link {
let instanceDir = await this._createNewInstanceDir(instanceConfig.get("instance.name")); let instanceDir = await this._createNewInstanceDir(instanceConfig.get("instance.name"));
logger.info(`Creating ${instanceDir}`);
await Instance.create(instanceDir, this.config.get("slave.factorio_directory")); await Instance.create(instanceDir, this.config.get("slave.factorio_directory"));
instanceInfo = { instanceInfo = {
path: instanceDir, path: instanceDir,
@@ -367,6 +368,14 @@ class Slave extends libLink.Link {
} }
} }
instanceLogMeta(instanceId, instanceInfo) {
instanceInfo = instanceInfo || this.instanceInfos.get(instanceId);
if (!instanceInfo) {
return { instance_id: instanceId, instance_name: String(instanceId) };
}
return { instance_id: instanceId, instance_name: instanceInfo.config.get("instance.name") };
}
getRequestInstanceInfo(instanceId) { getRequestInstanceInfo(instanceId) {
let instanceInfo = this.instanceInfos.get(instanceId); let instanceInfo = this.instanceInfos.get(instanceId);
if (!instanceInfo) { if (!instanceInfo) {
@@ -379,7 +388,7 @@ class Slave extends libLink.Link {
* Initialize and connect an unloaded instance * Initialize and connect an unloaded instance
* *
* @param {number} instanceId - ID of instance to initialize. * @param {number} instanceId - ID of instance to initialize.
* @returns {module:slave/slave~InstanceConnection} connection to instance. * @returns {Promise<module:slave/slave~InstanceConnection>} connection to instance.
*/ */
async _connectInstance(instanceId) { async _connectInstance(instanceId) {
let instanceInfo = this.getRequestInstanceInfo(instanceId); let instanceInfo = this.getRequestInstanceInfo(instanceId);
@@ -427,14 +436,30 @@ class Slave extends libLink.Link {
async startInstanceRequestHandler(message, request) { async startInstanceRequestHandler(message, request) {
let instanceId = message.data.instance_id; let instanceId = message.data.instance_id;
let instanceConnection = await this._connectInstance(instanceId); try {
return await request.send(instanceConnection, message.data); let instanceConnection = await this._connectInstance(instanceId);
return await request.send(instanceConnection, message.data);
} catch (err) {
if (!(err instanceof libErrors.RequestError)) {
logger.error(`Error starting instance:\n${err.stack}`, this.instanceLogMeta(instanceId));
throw new libErrors.RequestError(err.message);
}
throw err;
}
} }
async loadScenarioRequestHandler(message, request) { async loadScenarioRequestHandler(message, request) {
let instanceId = message.data.instance_id; let instanceId = message.data.instance_id;
let instanceConnection = await this._connectInstance(instanceId); try {
return await request.send(instanceConnection, message.data); let instanceConnection = await this._connectInstance(instanceId);
return await request.send(instanceConnection, message.data);
} catch (err) {
if (!(err instanceof libErrors.RequestError)) {
logger.error(`Error starting instance:\n${err.stack}`, this.instanceLogMeta(instanceId));
throw new libErrors.RequestError(err.message);
}
throw err;
}
} }
async listSavesRequestHandler(message, request) { async listSavesRequestHandler(message, request) {
@@ -452,8 +477,16 @@ class Slave extends libLink.Link {
async createSaveRequestHandler(message, request) { async createSaveRequestHandler(message, request) {
let instanceId = message.data.instance_id; let instanceId = message.data.instance_id;
let instanceConnection = await this._connectInstance(instanceId); try {
await request.send(instanceConnection, message.data); let instanceConnection = await this._connectInstance(instanceId);
await request.send(instanceConnection, message.data);
} catch (err) {
if (!(err instanceof libErrors.RequestError)) {
logger.error(`Error creating save:\n${err.stack}`, this.instanceLogMeta(instanceId));
throw new libErrors.RequestError(err.message);
}
throw err;
}
} }
async renameSaveRequestHandler(message) { async renameSaveRequestHandler(message) {
@@ -629,13 +662,21 @@ class Slave extends libLink.Link {
url, method: "PUT", url, method: "PUT",
core: { ca: this.tlsCa }, core: { ca: this.tlsCa },
data: content, data: content,
}).catch(err => logger.error(`Error pushing save to master:\n${err.stack}`)); }).catch(err => logger.error(`Error pushing save to master:\n${err.stack}`, this.instanceLogMeta(instance_id)));
} }
async exportDataRequestHandler(message, request) { async exportDataRequestHandler(message, request) {
let instanceId = message.data.instance_id; let instanceId = message.data.instance_id;
let instanceConnection = await this._connectInstance(instanceId); try {
await request.send(instanceConnection, message.data); let instanceConnection = await this._connectInstance(instanceId);
await request.send(instanceConnection, message.data);
} catch (err) {
if (!(err instanceof libErrors.RequestError)) {
logger.error(`Error exporting data:\n${err.stack}`, this.instanceLogMeta(instanceId));
throw new libErrors.RequestError(err.message);
}
throw err;
}
} }
async stopInstance(instanceId) { async stopInstance(instanceId) {
@@ -691,7 +732,8 @@ class Slave extends libLink.Link {
}); });
} catch (err) { } catch (err) {
logger.error( logger.error(
`Error during auto startup for ${instanceInfo.config.get("instance.name")}:\n${err.stack}` `Error during auto startup for ${instanceInfo.config.get("instance.name")}:\n${err.stack}`,
this.instanceLogMeta(instanceId, instanceInfo)
); );
} }
} }
@@ -28,6 +28,10 @@ export default function AssignInstanceModal(props) {
return; return;
} }
if (slaveId === "null") {
slaveId = null;
}
setApplying(true); setApplying(true);
libLink.messages.assignInstanceCommand.send(control, { libLink.messages.assignInstanceCommand.send(control, {
instance_id: props.id, instance_id: props.id,
@@ -67,9 +71,12 @@ export default function AssignInstanceModal(props) {
the instance. Note that reassigning an instance from the instance. Note that reassigning an instance from
one slave to another will not move the server save over. one slave to another will not move the server save over.
</Paragraph> </Paragraph>
<Form form={form} initialValues={{ slave: props.slaveId }}> <Form form={form} initialValues={{ slave: props.slaveId === null ? "null" : props.slaveId }}>
<Form.Item name="slave" label="Slave"> <Form.Item name="slave" label="Slave">
<Select> <Select>
<Select.Option value={"null"}>
<Typography.Text italic>Unassigned</Typography.Text>
</Select.Option>
{slaveList.map((slave) => <Select.Option {slaveList.map((slave) => <Select.Option
key={slave["id"]} key={slave["id"]}
value={slave["id"]} value={slave["id"]}
@@ -16,6 +16,9 @@ export default function InstanceList(props) {
let [slaveList] = useSlaveList(); let [slaveList] = useSlaveList();
function slaveName(slaveId) { function slaveName(slaveId) {
if (slaveId === null) {
return "";
}
let slave = slaveList.find(s => s.id === slaveId); let slave = slaveList.find(s => s.id === slaveId);
if (slave) { if (slave) {
return slave.name; return slave.name;
+1 -1
View File
@@ -13,7 +13,7 @@ function TokenAuth(props) {
<Paragraph>If you have generated an authentication token from the master server, use that here.</Paragraph> <Paragraph>If you have generated an authentication token from the master server, use that here.</Paragraph>
<Form <Form
name="login" name="login"
onFinish={(values) => { props.setToken(values.token); }} onFinish={(values) => { props.setToken(values.token.trim()); }}
layout="inline" layout="inline"
> >
<Form.Item <Form.Item
+1 -1
View File
@@ -111,7 +111,7 @@ export default function UsersPage() {
title: "Last seen", title: "Last seen",
key: "lastSeen", key: "lastSeen",
render: user => formatLastSeen(user), render: user => formatLastSeen(user),
sorter: sortLastSeen, sorter: (a, b) => sortLastSeen(a, b),
responsive: ["lg"], responsive: ["lg"],
}, },
]} ]}
+3 -18
View File
@@ -1,5 +1,5 @@
import { libData, libErrors, libLink, libLogging, libPlugin } from "@clusterio/lib"; import { libData, libErrors, libLink, libLogging, libPlugin } from "@clusterio/lib";
const { logger } = libLogging; const { logFilter, logger } = libLogging;
import packageJson from "../../package.json"; import packageJson from "../../package.json";
@@ -27,22 +27,6 @@ export class ControlConnector extends libLink.WebSocketClientConnector {
} }
} }
function logFilter({ all, master, slave_ids, instance_ids }, info) {
if (all) {
return true;
}
if (master && info.slave_id === undefined) {
return true;
}
if (info.slave_id && slave_ids.includes(info.slave_id)) {
return true;
}
if (info.instance_id && instance_ids.includes(info.instance_id)) {
return true;
}
return false;
}
/** /**
* Handles running the control * Handles running the control
* *
@@ -504,7 +488,7 @@ export class Control extends libLink.Link {
let info = message.data.info; let info = message.data.info;
for (let [filter, handlers] of this.logHandlers) { for (let [filter, handlers] of this.logHandlers) {
if (logFilter(filter, info)) { if (logFilter(filter)(info)) {
for (let handler of handlers) { for (let handler of handlers) {
handler(info); handler(info);
} }
@@ -539,6 +523,7 @@ export class Control extends libLink.Link {
handlers.splice(index, 1); handlers.splice(index, 1);
if (!handlers.length) { if (!handlers.length) {
this.logHandlers.delete(filter);
await this.updateLogSubscriptions(); await this.updateLogSubscriptions();
} }
} }
-4
View File
@@ -44,8 +44,6 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
}, 60e3).unref(); }, 60e3).unref();
this.master.app.get("/api/player_auth/servers", (req, res) => { this.master.app.get("/api/player_auth/servers", (req, res) => {
this.metrics.endpointHitCounter.labels(req.route.path).inc();
let servers = []; let servers = [];
for (let instance of this.master.instances.values()) { for (let instance of this.master.instances.values()) {
if (instance.status === "running" && instance.config.get("player_auth.load_plugin")) { if (instance.status === "running" && instance.config.get("player_auth.load_plugin")) {
@@ -56,12 +54,10 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
}); });
this.master.app.post("/api/player_auth/player_code", express.json(), (req, res, next) => { this.master.app.post("/api/player_auth/player_code", express.json(), (req, res, next) => {
this.metrics.endpointHitCounter.labels(req.route.path).inc();
this.handlePlayerCode(req, res).catch(next); this.handlePlayerCode(req, res).catch(next);
}); });
this.master.app.post("/api/player_auth/verify", express.json(), (req, res, next) => { this.master.app.post("/api/player_auth/verify", express.json(), (req, res, next) => {
this.metrics.endpointHitCounter.labels(req.route.path).inc();
this.handleVerify(req, res).catch(next); this.handleVerify(req, res).catch(next);
}); });
} }
+1 -1
View File
@@ -86,7 +86,7 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
this.subscribedControlLinks = new Set(); this.subscribedControlLinks = new Set();
routes.addApiRoutes(this.master.app, this.items, this.metrics.endpointHitCounter); routes.addApiRoutes(this.master.app, this.items);
} }
updateStorage() { updateStorage() {
+1 -3
View File
@@ -1,5 +1,5 @@
"use strict"; "use strict";
function addApiRoutes(app, items, endpointHitCounter) { function addApiRoutes(app, items) {
/** /**
* GET endpoint to read the masters current inventory of items. * GET endpoint to read the masters current inventory of items.
@@ -10,7 +10,6 @@ function addApiRoutes(app, items, endpointHitCounter) {
* @returns {object[]} JSON [{name:"iron-plate", count:100},{name:"copper-plate",count:5}] * @returns {object[]} JSON [{name:"iron-plate", count:100},{name:"copper-plate",count:5}]
*/ */
app.get("/api/inventory", (req, res) => { app.get("/api/inventory", (req, res) => {
endpointHitCounter.labels(req.route.path).inc();
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// Check it and send it // Check it and send it
@@ -31,7 +30,6 @@ function addApiRoutes(app, items, endpointHitCounter) {
* @returns {object} JSON {"iron-plate":100, "copper-plate":5} * @returns {object} JSON {"iron-plate":100, "copper-plate":5}
*/ */
app.get("/api/inventoryAsObject", (req, res) => { app.get("/api/inventoryAsObject", (req, res) => {
endpointHitCounter.labels(req.route.path).inc();
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// Check it and send it // Check it and send it
+15
View File
@@ -41,6 +41,21 @@ describe("lib/file_ops", function() {
}); });
}); });
describe("getNewestFile()", function() {
it("returns 0 if directory does not exist", async function() {
let size = await libFileOps.directorySize(path.join(baseDir, "invalid"));
assert.equal(size, 0);
});
it("returns 0 if directory is empty", async function() {
let size = await libFileOps.directorySize(path.join(baseDir, "test", "folder"));
assert.equal(size, 0);
});
it("returns size of files in directory", async function() {
let size = await libFileOps.directorySize(path.join(baseDir, "test"));
assert.equal(size, 21);
});
});
describe("findUnusedName()", function() { describe("findUnusedName()", function() {
it("should return named unchanged if it does not exist", async function() { it("should return named unchanged if it does not exist", async function() {
let cases = [ let cases = [
+41
View File
@@ -64,4 +64,45 @@ describe("lib/plugin_loader", function() {
); );
}); });
}); });
describe("loadMasterPluginClass()", function() {
let baseDir = path.join("temp", "test", "plugin");
let missingClass = path.join(baseDir, "missing_class_plugin");
let wrongParentClass = path.join(baseDir, "wrong_parent_class_plugin");
before(async function() {
async function writeEntrypoint(pluginPath, content) {
await fs.outputFile(path.join(pluginPath, "master.js"), content);
}
await writeEntrypoint(missingClass, "");
await writeEntrypoint(wrongParentClass, "class MasterPlugin {}\n module.exports = { MasterPlugin };\n");
});
it("should throw if class is missing from entrypoint", async function() {
await assert.rejects(
libPluginLoader.loadMasterPluginClass({
name: "test",
masterEntrypoint: "master",
requirePath: path.resolve(missingClass),
}),
{
message:
`PluginError: Expected ${path.resolve(missingClass, "master")} ` +
"to export a class named MasterPlugin",
}
);
});
it("should throw if class is not a subclass of BaseMasterPlugin", async function() {
await assert.rejects(
libPluginLoader.loadMasterPluginClass({
name: "test",
masterEntrypoint: "master",
requirePath: path.resolve(wrongParentClass),
}),
{
message:
"PluginError: Expected MasterPlugin exported from " +
`${path.resolve(wrongParentClass, "master")} to be a subclass of BaseMasterPlugin`,
}
);
});
});
}); });
+108
View File
@@ -210,6 +210,114 @@ describe("lib/prometheus", function() {
}); });
}); });
describe("class Summary", function() {
let summary;
let labeledSummary;
beforeEach(function() {
summary = new prometheus.Summary(
"test", "Help", { register: false }
);
labeledSummary = new prometheus.Summary(
"test", "Help", { register: false, labels: ["a", "b"] }
);
});
describe("constructor", function() {
it("should have sum and count initialized to 0", function() {
assert.equal(summary.sum, 0);
assert.equal(summary.count, 0);
});
it("should throw on unrecoginzed option", function() {
assert.throws(
() => new prometheus.Summary("test", "help", { invalid: 1 }),
new Error("Unrecognized option 'invalid'")
);
});
});
describe(".observe()", function() {
it("should increment sum and count", function() {
summary.observe(2);
assert.equal(summary.sum, 2);
assert.equal(summary.count, 1);
summary.observe(4);
assert.equal(summary.sum, 6);
assert.equal(summary.count, 2);
});
});
describe(".startTimer()", function() {
it("should add observations when closure is called", function() {
let observeDuration = summary.startTimer();
assert.equal(summary.count, 0);
observeDuration();
assert.equal(summary.count, 1);
observeDuration();
assert.equal(summary.count, 2);
});
});
describe(".labels()", function() {
it("should initialize a value for the label set", function() {
labeledSummary.labels({ a: "1", b: "2" });
assert(labeledSummary._sumValues.has('a="1",b="2"'), "sum value not set");
assert(labeledSummary._countValues.has('a="1",b="2"'), "count value not set");
labeledSummary.labels("4", "5");
assert(labeledSummary._sumValues.has('a="4",b="5"'), "sum value not set");
assert(labeledSummary._countValues.has('a="4",b="5"'), "count value not set");
});
it("should return a child supporting the summary methods", function() {
let child = labeledSummary.labels({ a: "1", b: "2" });
void child.sum;
void child.count;
child.observe(2);
let observeDuration = child.startTimer();
observeDuration();
child = labeledSummary.labels("7", "8");
void child.sum;
void child.count;
child.observe(2);
observeDuration = child.startTimer();
observeDuration();
});
it("should return the same child for the same labels", function() {
let child = labeledSummary.labels("1", "2");
assert.equal(labeledSummary.labels("1", "2"), child);
assert.equal(labeledSummary.labels({ a: "1", b: "2" }), child);
});
});
describe(".remove()", function() {
it("should remove a label set created with .labels()", function() {
labeledSummary.labels("1", "2");
labeledSummary.remove("1", "2");
assert(!labeledSummary._sumValues.has('a="1",b="2"'), "sum value not removed");
assert(!labeledSummary._countValues.has('a="1",b="2"'), "count value not removed");
});
});
describe(".removeAll()", function() {
it("should remove a label set created with .labels()", function() {
labeledSummary.labels("1", "2");
labeledSummary.labels("1", "3");
labeledSummary.labels("4", "6");
labeledSummary.removeAll({ a: "1" });
assert.deepEqual(labeledSummary._sumValues, new Map([['a="4",b="6"', 0]]));
assert.deepEqual(labeledSummary._countValues, new Map([['a="4",b="6"', 0]]));
});
});
describe(".clear()", function() {
it("should remove all label sets created with .labels()", function() {
labeledSummary.labels("1", "2");
labeledSummary.labels("1", "3");
labeledSummary.labels("4", "6");
labeledSummary.clear();
assert(labeledSummary._sumValues.size === 0, "sums were not cleared");
assert(labeledSummary._countValues.size === 0, "counts were not cleared");
});
});
});
describe("class Histogram", function() { describe("class Histogram", function() {
let histogram; let histogram;
let labeledHistogram; let labeledHistogram;
+1 -3
View File
@@ -219,9 +219,7 @@ class MockMaster {
async function createMasterPlugin(MasterPluginClass, info) { async function createMasterPlugin(MasterPluginClass, info) {
let master = new MockMaster(); let master = new MockMaster();
let metrics = { let metrics = {};
endpointHitCounter: new libPrometheus.Counter("hit_counter", "Hit Counter", { labels: ["route"] }),
};
let logger = new MockLogger(); let logger = new MockLogger();
let plugin = new MasterPluginClass(info, master, metrics, logger); let plugin = new MasterPluginClass(info, master, metrics, logger);
await plugin.init(); await plugin.init();