fix for stats exporter console errors (#166)

This commit is contained in:
veladon authored and Daniel committed 2019-07-15 13:49:49 +02:00
1 parent 3b814eeea4
commit 233410add7
2 files changed
+20 -11

No files matched your search

+11 -5
View File
@@ -20,13 +20,19 @@ module.exports = class remoteCommands {
setInterval(async () => { setInterval(async () => {
let stats = await this.getStats(); let stats = await this.getStats();
stats.forEach(force => { stats.forEach(force => {
needle.post(`${mergedConfig.masterIP}:${mergedConfig.masterPort}/api/logStats`, { // I have no idea why I need to do JSON.stringify before passing it to needle.post,
// but if you do forceData: force.data, it doesn't work...
let request = {
instanceID: this.config.unique, instanceID: this.config.unique,
force: force.forceName, force: force.forceName,
data: force.data, forceData: JSON.stringify(force.data),
}, needleOptionsWithTokenAuthHeader, (err, resp) => { };
if(!err && resp.body && resp.body.ok == false) console.log(resp.body); needle.post(`${mergedConfig.masterIP}:${mergedConfig.masterPort}/api/logStats`,
}); request,
needleOptionsWithTokenAuthHeader, (err, resp) => {
if(!err && resp.body && resp.body.ok == false) console.log("Error calling /api/logStats: " + resp.body);
}
);
}); });
}, 15000); }, 15000);
} }
@@ -26,18 +26,19 @@ class masterPlugin {
@returns {string} failure @returns {string} failure
*/ */
app.post("/api/logStats", authenticate.middleware, function(req,res) { app.post("/api/logStats", authenticate.middleware, function(req,res) {
let data = JSON.parse(req.body.forceData);
// endpointHitCounter.labels(req.route.path).inc(); // endpointHitCounter.labels(req.route.path).inc();
if(typeof req.body == "object" if(typeof req.body == "object"
&& req.body.instanceID && req.body.instanceID
&& typeof req.body.instanceID == "string" && typeof req.body.instanceID == "string"
&& req.body.instanceID.length < 100 && req.body.instanceID.length < 100
&& req.body.data && data
&& typeof req.body.data == "object" && typeof data == "object"
&& !Array.isArray(req.body.data)){ && !Array.isArray(data)){
try{ try{
Object.keys(req.body.data).forEach(statisticName => { Object.keys(data).forEach(statisticName => {
Object.keys(req.body.data[statisticName]).forEach(direction => { Object.keys(data[statisticName]).forEach(direction => {
let statHash = req.body.data[statisticName][direction]; let statHash = data[statisticName][direction];
Object.keys(statHash).forEach(item => { Object.keys(statHash).forEach(item => {
let value = Number(statHash[item]); let value = Number(statHash[item]);
if(value) prometheusStatisticsGauge.labels(req.body.instanceID, req.body.force, statisticName, direction, item).set(value); if(value) prometheusStatisticsGauge.labels(req.body.instanceID, req.body.force, statisticName, direction, item).set(value);
@@ -50,6 +51,8 @@ class masterPlugin {
console.log(e) console.log(e)
}; };
} else { } else {
console.log(`/api/logStats: Invalid request. instanceID: ${req.body.instanceID}, dataIsArray: `
+ `${Array.isArray(data)}, ${typeof req.body.instanceID}, ${typeof data}`);
res.send({ res.send({
ok:false, ok:false,
msg:"Invalid request parameters" msg:"Invalid request parameters"