From 482c4e9c5ef4def082ea63e354bfeb31920ba48d Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Wed, 4 Feb 2015 22:28:17 +0000 Subject: [PATCH] Add memory metric reporting --- red/log.js | 3 +-- red/nodes/Node.js | 2 +- red/red.js | 2 +- red/server.js | 33 +++++++++++++++++++++++++++++++++ test/red/nodes/Node_spec.js | 2 +- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/red/log.js b/red/log.js index faa93aacd..5baa3e580 100644 --- a/red/log.js +++ b/red/log.js @@ -19,7 +19,7 @@ var EventEmitter = require("events").EventEmitter; var levels = { - off: 0, + off: 1, fatal: 10, error: 20, warn: 30, @@ -46,7 +46,6 @@ var ConsoleLogHandler = function(settings) { this.logLevel = levels[settings.level]||levels.info; this.metricsOn = settings.metrics||false; metricsEnabled = this.metricsOn; - this.on("log",function(msg) { if (this.shouldReportMessage(msg.level)) { if (msg.level == log.METRIC) { diff --git a/red/nodes/Node.js b/red/nodes/Node.js index af9f31578..49bc3de90 100644 --- a/red/nodes/Node.js +++ b/red/nodes/Node.js @@ -213,7 +213,7 @@ Node.prototype.metric = function(eventname, msg, metricValue) { metrics.nodeid = this.id; metrics.event = "node."+this.type+"."+eventname; metrics.msgid = msg._id; - metrics.metric = metricValue; + metrics.value = metricValue; Log.log(metrics); } diff --git a/red/red.js b/red/red.js index 75527f865..c292df4dd 100644 --- a/red/red.js +++ b/red/red.js @@ -34,9 +34,9 @@ var RED = { init: function(httpServer,userSettings) { userSettings.version = this.version(); + log.init(userSettings); settings.init(userSettings); server.init(httpServer,settings); - log.init(userSettings); return server.app; }, diff --git a/red/server.js b/red/server.js index e8725d63d..c1ae15909 100644 --- a/red/server.js +++ b/red/server.js @@ -28,6 +28,9 @@ var nodeApp = null; var server = null; var settings = null; +var runtimeMetricInterval = null; + + function init(_server,_settings) { server = _server; settings = _settings; @@ -47,6 +50,11 @@ function start() { storage.init(settings).then(function() { settings.load(storage).then(function() { + if (log.metric()) { + runtimeMetricInterval = setInterval(function() { + reportMetrics(); + }, 15000); + } console.log("\nWelcome to Node-RED\n===================\n"); if (settings.version) { log.info("Version: "+settings.version); @@ -193,9 +201,34 @@ function uninstallModule(module) { }); } +function reportMetrics() { + var memUsage = process.memoryUsage(); + + // only need to init these once per report + var metrics = {}; + metrics.level = log.METRIC; + + //report it + metrics.event = "runtime.memory.rss" + metrics.value = memUsage.rss; + log.log(metrics); + + metrics.event = "runtime.memory.heapTotal" + metrics.value = memUsage.heapTotal; + log.log(metrics); + + metrics.event = "runtime.memory.heapUsed" + metrics.value = memUsage.heapUsed; + log.log(metrics); +} + function stop() { + if (runtimeMetricInterval) { + clearInterval(runtimeMetricInterval); + runtimeMetricInterval = null; + } redNodes.stopFlows(); comms.stop(); } diff --git a/test/red/nodes/Node_spec.js b/test/red/nodes/Node_spec.js index eae8b6a60..4c1beb5ee 100644 --- a/test/red/nodes/Node_spec.js +++ b/test/red/nodes/Node_spec.js @@ -402,7 +402,7 @@ describe('Node', function() { }); var msg = {payload:"foo", _id:"987654321"}; n.metric("test.metric",msg,"15mb"); - should.deepEqual({metric:"15mb", level:Log.METRIC, nodeid:n.id, + should.deepEqual({value:"15mb", level:Log.METRIC, nodeid:n.id, event:"node.abc.test.metric",msgid:"987654321"}, loginfo); Log.log.restore(); done();