Add RED.util.generateId and ensure HTTP node adds proper id

This commit is contained in:
Nick O'Leary 2015-06-02 15:54:37 +01:00
parent 5cda08e7b0
commit 4248d20f39
4 changed files with 26 additions and 18 deletions

View File

@ -60,12 +60,14 @@ module.exports = function(RED) {
};
this.callback = function(req,res) {
var msgid = RED.util.generateId();
res._msgid = msgid;
if (node.method.match(/(^post$|^delete$|^put$|^options$)/)) {
node.send({req:req,res:res,payload:req.body});
node.send({_msgid:msgid,req:req,res:res,payload:req.body});
} else if (node.method == "get") {
node.send({req:req,res:res,payload:req.query});
node.send({_msgid:msgid,req:req,res:res,payload:req.query});
} else {
node.send({req:req,res:res});
node.send({_msgid:msgid,req:req,res:res});
}
};
@ -90,14 +92,14 @@ module.exports = function(RED) {
metricsHandler = function(req, res, next) {
var startAt = process.hrtime();
onHeaders(res, function() {
if (res._msgId) {
if (res._msgid) {
var diff = process.hrtime(startAt);
var ms = diff[0] * 1e3 + diff[1] * 1e-6;
var metricResponseTime = ms.toFixed(3);
var metricContentLength = res._headers["content-length"];
//assuming that _id has been set for res._metrics in HttpOut node!
node.metric("response.time.millis", {_id:res._msgId} , metricResponseTime);
node.metric("response.content-length.bytes", {_id:res._msgId} , metricContentLength);
node.metric("response.time.millis", {_msgid:res._msgid} , metricResponseTime);
node.metric("response.content-length.bytes", {_msgid:res._msgid} , metricContentLength);
}
});
next();
@ -167,7 +169,6 @@ module.exports = function(RED) {
msg.res.set('content-length', len);
}
msg.res._msgId = msg._id;
msg.res.send(statusCode,msg.payload);
}
} else {

View File

@ -96,10 +96,6 @@ Node.prototype.close = function() {
}
};
function constructUniqueIdentifier() {
return (1+Math.random()*4294967295).toString(16);
}
Node.prototype.send = function(msg) {
var msgSent = false;
var node;
@ -112,7 +108,7 @@ Node.prototype.send = function(msg) {
// TODO: pre-load flows.get calls - cannot do in constructor
// as not all nodes are defined at that point
if (!msg._msgid) {
msg._msgid = constructUniqueIdentifier();
msg._msgid = redUtil.generateId();
}
this.metric("send",msg);
node = flows.get(this._wire);
@ -171,7 +167,7 @@ Node.prototype.send = function(msg) {
}
/* istanbul ignore else */
if (!sentMessageId) {
sentMessageId = constructUniqueIdentifier();
sentMessageId = redUtil.generateId();
}
this.metric("send",{_msgid:sentMessageId});
@ -190,7 +186,7 @@ Node.prototype.receive = function(msg) {
msg = {};
}
if (!msg._msgid) {
msg._msgid = constructUniqueIdentifier();
msg._msgid = redUtil.generateId();
}
this.metric("receive",msg);
try {

View File

@ -1,5 +1,5 @@
/**
* Copyright 2014 IBM Corp.
* Copyright 2014, 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,6 +16,10 @@
var clone = require("clone");
function generateId() {
return (1+Math.random()*4294967295).toString(16);
}
function ensureString(o) {
if (Buffer.isBuffer(o)) {
return o.toString();
@ -103,5 +107,6 @@ module.exports = {
ensureString: ensureString,
ensureBuffer: ensureBuffer,
cloneMessage: cloneMessage,
compareObjects: compareObjects
compareObjects: compareObjects,
generateId: generateId
};

View File

@ -1,5 +1,5 @@
/**
* Copyright 2014 IBM Corp.
* Copyright 2014, 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,7 +17,13 @@ var should = require("should");
var util = require("../../red/util");
describe("red/util", function() {
// only test for things not tested by overall grunt
describe('generateId', function() {
it('generates an id', function() {
var id = util.generateId();
var id2 = util.generateId();
id.should.not.eql(id2);
});
});
describe('compareObjects', function() {
it('unequal arrays are unequal', function() {
util.compareObjects(["a"],"a").should.equal(false);