Update logging/metric system

This commit is contained in:
Nick O'Leary
2015-02-03 22:02:26 +00:00
parent 7d6ce1ec12
commit 0aaea1ec40
24 changed files with 245 additions and 209 deletions

View File

@@ -70,11 +70,11 @@ describe('debug node', function() {
try {
helper.log().called.should.be.true;
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].level == "info";
return evt[0].type == "debug";
});
logEvents.should.have.length(1);
var tstmp = logEvents[0][0].timestamp;
logEvents[0][0].should.eql({level:'info',id:'n1',type:'debug',msg:'test', timestamp:tstmp});
logEvents[0][0].should.eql({level:helper.log().INFO, id:'n1',type:'debug',msg:'test', timestamp:tstmp});
done();
} catch(err) {

View File

@@ -139,11 +139,11 @@ describe('function node', function() {
try {
helper.log().called.should.be.true;
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].level == "error";
return evt[0].type == "function";
});
logEvents.should.have.length(1);
var msg = logEvents[0][0];
msg.should.have.property('level', 'error');
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
msg.should.have.property('type', 'function');
msg.should.have.property('msg', 'ReferenceError: retunr is not defined');

View File

@@ -129,14 +129,13 @@ describe('html node', function() {
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
try {
helper.log().called.should.be.false;
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n1.receive({payload:null,topic: "bar"});
helper.log().called.should.be.true;
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].level == "info";
return evt[0].type == "html";
});
logEvents.should.have.length(1);
// Each logEvent is the array of args passed to the function.

View File

@@ -76,13 +76,11 @@ describe('JSON node', function() {
{id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() {
try {
helper.log().called.should.be.false;
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
jn1.receive({payload:'foo',topic: "bar"});
helper.log().called.should.be.true;
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].level == "info";
return evt[0].type == "json";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg',"SyntaxError: Unexpected token o"+ "\nfoo");
@@ -98,13 +96,11 @@ describe('JSON node', function() {
{id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() {
try {
helper.log().called.should.be.false;
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
jn1.receive({payload:1,topic: "bar"});
helper.log().called.should.be.true;
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].level == "info";
return evt[0].type == "json";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg',"dropped: 1");

View File

@@ -84,9 +84,8 @@ describe('XML node', function() {
n1.receive({payload:'<not valid xml>',topic: "bar"});
setTimeout(function() {
try {
helper.log().called.should.be.true;
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].level == "error";
return evt[0].type == "xml";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
@@ -109,9 +108,8 @@ describe('XML node', function() {
n1.receive({payload:1,topic: "bar"});
setTimeout(function() {
try {
helper.log().called.should.be.true;
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].level == "info";
return evt[0].type == "xml";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg',"This node only handles xml strings or js objects.");

View File

@@ -53,6 +53,13 @@ function helperNode(n) {
module.exports = {
load: function(testNode, testFlows, testCredentials, cb) {
logSpy = sinon.spy(log,"log");
logSpy.FATAL = log.FATAL;
logSpy.ERROR = log.ERROR;
logSpy.WARN = log.WARN;
logSpy.INFO = log.INFO;
logSpy.DEBUG = log.DEBUG;
logSpy.TRACE = log.TRACE;
logSpy.METRIC = log.METRIC;
if (typeof testCredentials === 'function') {
cb = testCredentials;

View File

@@ -91,7 +91,7 @@ describe('Node', function() {
var n = new RedNode({id:'123',type:'abc'});
n.on('input',function(msg) {
(typeof msg).should.not.be.equal("undefined");
(typeof msg._messageUuid).should.not.be.equal("undefined");
(typeof msg._id).should.not.be.equal("undefined");
done();
});
n.receive();
@@ -101,7 +101,7 @@ describe('Node', function() {
var n = new RedNode({id:'123',type:'abc'});
n.on('input',function(msg) {
(typeof msg).should.not.be.equal("undefined");
(typeof msg._messageUuid).should.not.be.equal("undefined");
(typeof msg._id).should.not.be.equal("undefined");
done();
});
n.receive(null);
@@ -308,6 +308,31 @@ describe('Node', function() {
n1.send(message);
});
it("logs the uuid for all messages sent", function(done) {
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':sender,'n2':receiver1,'n3':receiver2}[id];
});
var logHandler = {
messagesSent: 0,
emit: function(event, msg) {
if (msg.event == "node.abc.send" && msg.level == Log.METRIC) {
this.messagesSent++;
(typeof msg.msgid).should.not.be.equal("undefined");
flowGet.restore();
done();
}
}
};
Log.addHandler(logHandler);
var sender = new RedNode({id:'n1',type:'abc', wires:[['n2', 'n3']]});
var receiver1 = new RedNode({id:'n2',type:'abc'});
var receiver2 = new RedNode({id:'n3',type:'abc'});
sender.send({"some": "message"});
})
});
@@ -319,14 +344,11 @@ describe('Node', function() {
loginfo = msg;
});
n.log("a log message");
should.deepEqual({level:"info", id:n.id,
should.deepEqual({level:Log.INFO, id:n.id,
type:n.type, msg:"a log message", }, loginfo);
Log.log.restore();
done();
});
});
describe('#log', function() {
it('produces a log message with a name', function(done) {
var n = new RedNode({id:'123', type:'abc', name:"barney"});
var loginfo = {};
@@ -334,7 +356,7 @@ describe('Node', function() {
loginfo = msg;
});
n.log("a log message");
should.deepEqual({level:"info", id:n.id, name: "barney",
should.deepEqual({level:Log.INFO, id:n.id, name: "barney",
type:n.type, msg:"a log message"}, loginfo);
Log.log.restore();
done();
@@ -349,7 +371,7 @@ describe('Node', function() {
loginfo = msg;
});
n.warn("a warning");
should.deepEqual({level:"warn", id:n.id,
should.deepEqual({level:Log.WARN, id:n.id,
type:n.type, msg:"a warning"}, loginfo);
Log.log.restore();
done();
@@ -364,7 +386,7 @@ describe('Node', function() {
loginfo = msg;
});
n.error("an error message");
should.deepEqual({level:"error", id:n.id,
should.deepEqual({level:Log.ERROR, id:n.id,
type:n.type, msg:"an error message"}, loginfo);
Log.log.restore();
done();
@@ -378,10 +400,10 @@ describe('Node', function() {
sinon.stub(Log, 'log', function(msg) {
loginfo = msg;
});
var msg = {payload:"foo", _messageUuid:"987654321"};
var msg = {payload:"foo", _id:"987654321"};
n.metric("test.metric",msg,"15mb");
should.deepEqual({level:"metric", nodeid:n.id,
event:"test.metric",msguuid:"987654321", metric:"15mb"}, loginfo);
should.deepEqual({metric:"15mb", level:Log.METRIC, nodeid:n.id,
event:"node.abc.test.metric",msgid:"987654321"}, loginfo);
Log.log.restore();
done();
});

View File

@@ -21,6 +21,7 @@ var util = require("util");
var index = require("../../../red/nodes/index");
var credentials = require("../../../red/nodes/credentials");
var log = require("../../../red/log");
describe('Credentials', function() {
@@ -131,18 +132,18 @@ describe('Credentials', function() {
return when(true);
}
};
var logmsg = 'no errors yet';
sinon.stub(util, 'log', function(msg) {
var logmsg = 'nothing logged yet';
sinon.stub(log, 'warn', function(msg) {
logmsg = msg;
});
credentials.init(storage);
credentials.load().then(function() {
should.equal('[red] Error loading credentials : test forcing failure', logmsg);
util.log.restore();
logmsg.should.equal("Error loading credentials : test forcing failure");
log.warn.restore();
done();
}).otherwise(function(err){
util.log.restore();
log.warn.restore();
done(err);
});
});
@@ -189,7 +190,7 @@ describe('Credentials', function() {
});
}
var logmsg = 'nothing logged yet';
sinon.stub(util, 'log', function(msg) {
sinon.stub(log, 'warn', function(msg) {
logmsg = msg;
});
var settings = {
@@ -201,10 +202,10 @@ describe('Credentials', function() {
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
credentials.extract(testnode);
should.equal(logmsg, 'Credential Type test is not registered.');
util.log.restore();
log.warn.restore();
done();
}).otherwise(function(err){
util.log.restore();
log.warn.restore();
done(err);
});
});