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 () => {
let stats = await this.getStats();
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,
force: force.forceName,
data: force.data,
}, needleOptionsWithTokenAuthHeader, (err, resp) => {
if(!err && resp.body && resp.body.ok == false) console.log(resp.body);
});
forceData: JSON.stringify(force.data),
};
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);
}
@@ -26,18 +26,19 @@ class masterPlugin {
@returns {string} failure
*/
app.post("/api/logStats", authenticate.middleware, function(req,res) {
let data = JSON.parse(req.body.forceData);
// endpointHitCounter.labels(req.route.path).inc();
if(typeof req.body == "object"
&& req.body.instanceID
&& typeof req.body.instanceID == "string"
&& req.body.instanceID.length < 100
&& req.body.data
&& typeof req.body.data == "object"
&& !Array.isArray(req.body.data)){
&& data
&& typeof data == "object"
&& !Array.isArray(data)){
try{
Object.keys(req.body.data).forEach(statisticName => {
Object.keys(req.body.data[statisticName]).forEach(direction => {
let statHash = req.body.data[statisticName][direction];
Object.keys(data).forEach(statisticName => {
Object.keys(data[statisticName]).forEach(direction => {
let statHash = data[statisticName][direction];
Object.keys(statHash).forEach(item => {
let value = Number(statHash[item]);
if(value) prometheusStatisticsGauge.labels(req.body.instanceID, req.body.force, statisticName, direction, item).set(value);
@@ -50,6 +51,8 @@ class masterPlugin {
console.log(e)
};
} else {
console.log(`/api/logStats: Invalid request. instanceID: ${req.body.instanceID}, dataIsArray: `
+ `${Array.isArray(data)}, ${typeof req.body.instanceID}, ${typeof data}`);
res.send({
ok:false,
msg:"Invalid request parameters"