diff --git a/nodes/core/io/21-httpin.js b/nodes/core/io/21-httpin.js index 3d9d6b5c8..7fdeeaa25 100644 --- a/nodes/core/io/21-httpin.js +++ b/nodes/core/io/21-httpin.js @@ -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 { diff --git a/red/nodes/Node.js b/red/nodes/Node.js index cb48479af..ff0ca4626 100644 --- a/red/nodes/Node.js +++ b/red/nodes/Node.js @@ -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 { diff --git a/red/util.js b/red/util.js index 6e4da1339..12bb537e9 100644 --- a/red/util.js +++ b/red/util.js @@ -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 }; diff --git a/test/red/util_spec.js b/test/red/util_spec.js index 2c7878a8c..a512bcf18 100644 --- a/test/red/util_spec.js +++ b/test/red/util_spec.js @@ -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);