Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

1.0.0 (hapi 2.0)

  • Loading branch information...
commit cb088b3c104af715e99d6087a5fb4a1d0a9363d0 1 parent b731439
@hueniverse hueniverse authored
View
2  lib/index.js
@@ -10,7 +10,7 @@ var internals = {};
exports.register = function (plugin, options, next) {
- plugin.api('monitor', new Monitor(plugin, options));
+ plugin.expose('monitor', new Monitor(plugin, options));
return next();
};
View
11 lib/monitor.js
@@ -4,12 +4,12 @@ var Os = require('os');
var Events = require('events');
var Path = require('path');
var Fs = require('fs');
-var NodeUtil = require('util');
+var Dgram = require('dgram');
+var Url = require('url');
var Async = require('async');
var Hoek = require('hoek');
+var Nipple = require('nipple');
var SafeStringify = require('json-stringify-safe');
-var Dgram = require('dgram');
-var Url = require('url');
var System = require('./system');
var Process = require('./process');
var Network = require('./network');
@@ -188,7 +188,7 @@ module.exports = internals.Monitor = function (plugin, options) {
return this;
};
-NodeUtil.inherits(internals.Monitor, Events.EventEmitter);
+Hoek.inherits(internals.Monitor, Events.EventEmitter);
internals.Monitor.prototype.stop = function () {
@@ -246,7 +246,6 @@ internals.Monitor.prototype._broadcastRemotes = function () {
internals.Monitor.prototype._broadcastHttp = function () {
var self = this;
- var request = this.plugin.hapi.client.request;
Object.keys(self._subscriberQueues.http).forEach(function (uri) {
@@ -265,7 +264,7 @@ internals.Monitor.prototype._broadcastHttp = function () {
subscriberQueue.length = 0; // Empty queue (must not set to [] or queue reference will change)
- request('post', uri, { headers: { 'content-type': 'application/json' }, payload: JSON.stringify(envelope), timeout: self.settings.requestTimeout });
+ Nipple.request('post', uri, { headers: { 'content-type': 'application/json' }, payload: JSON.stringify(envelope), timeout: self.settings.requestTimeout });
});
};
View
13 lib/network.js 100644 → 100755
@@ -87,8 +87,9 @@ internals.NetworkMonitor.prototype._onResponse = function (request) {
var msec = Date.now() - request.info.received;
var port = (request.server && request.server.info) ? request.server.info.port : 0;
- var statusCode = request.raw && request.raw.res ? request.raw.res.statusCode : 0;
- statusCode = statusCode || request._response && request._response._code ? request._response._code : 500; // When res.statusCode isn't set yet
+
+ var response = request.response;
+ var statusCode = response.statusCode;
this._responseTimes[port] = this._responseTimes[port] || { count: 0, total: 0, max: 0 };
this._responseTimes[port].count++;
@@ -101,12 +102,8 @@ internals.NetworkMonitor.prototype._onResponse = function (request) {
this._requests[port].statusCodes[statusCode] = this._requests[port].statusCodes[statusCode] || 0;
this._requests[port].statusCodes[statusCode]++;
- if (statusCode === 500 &&
- request._response &&
- request._response._err &&
- request._response._err.message &&
- request._response._err.message.indexOf('response closed') > -1) {
-
+ var log = request.getLog('aborted');
+ if (log.length) {
this._requests[port].disconnects++;
}
};
View
9 package.json
@@ -1,7 +1,7 @@
{
"name": "good",
"description": "Server and process monitoring plugin",
- "version": "0.12.4",
+ "version": "1.0.0",
"repository": "git://github.com/spumko/good",
"main": "index",
"keywords": [
@@ -17,16 +17,17 @@
},
"dependencies": {
"hoek": "1.x.x",
+ "nipple": "2.x.x",
"async": "0.2.x",
"memwatch": "0.2.x",
- "optimist": "0.x.x",
+ "optimist": "0.6.x",
"json-stringify-safe": "5.0.x"
},
"peerDependencies": {
- "hapi": "1.x.x"
+ "hapi": "2.x.x"
},
"devDependencies": {
- "hapi": "1.x.x",
+ "hapi": "2.x.x",
"lab": "1.x.x"
},
"scripts": {
View
28 test/monitor.js
@@ -99,7 +99,7 @@ describe('Monitor', function () {
});
});
- it('doesn\'t throw an error when opsInterval is more than 100', function (done) {
+ it('does not throw an error when opsInterval is more than 100', function (done) {
var options = {
subscribers: {},
@@ -253,9 +253,9 @@ describe('Monitor', function () {
};
var server = new Hapi.Server(0);
- server.route({ method: 'GET', path: '/err', handler: function (request) {
+ server.route({ method: 'GET', path: '/err', handler: function (request, reply) {
- request.reply(new Hapi.error.internal('my error'))
+ reply(new Hapi.error.internal('my error'))
}});
var plugin = {
@@ -285,7 +285,7 @@ describe('Monitor', function () {
describe('#_broadcastHttp', function () {
- it('doesn\'t do anything if there are no subscribers', function (done) {
+ it('does not do anything if there are no subscribers', function (done) {
var options = {
subscribers: {}
@@ -332,7 +332,7 @@ describe('Monitor', function () {
var remoteServer = new Hapi.Server(0);
remoteServer.route({
- method: 'POST', path: '/', handler: function (request) {
+ method: 'POST', path: '/', handler: function (request, reply) {
expect(request.payload.appVer).to.exist;
expect(request.payload.appVer).to.not.equal('unknown');
@@ -366,18 +366,18 @@ describe('Monitor', function () {
var server = new Hapi.Server(0);
remoteServer.route({
- method: 'POST', path: '/', handler: function (request) {
+ method: 'POST', path: '/', handler: function (request, reply) {
expect(request.payload.events[0].statusCode).to.equal(200);
- request.reply('Success');
+ reply('Success');
done();
}
});
server.route({
- method: 'GET', path: '/', handler: function (request) {
+ method: 'GET', path: '/', handler: function (request, reply) {
- request.reply('Success');
+ reply('Success');
}
});
@@ -407,7 +407,7 @@ describe('Monitor', function () {
});
});
- it('doesn\'t fail when a remote subscriber is unavailable', function (done) {
+ it('does not fail when a remote subscriber is unavailable', function (done) {
var options = {
subscribers: {
@@ -434,7 +434,7 @@ describe('Monitor', function () {
describe('#_broadcastUdp', function () {
- it('doesn\'t do anything if there are no subscribers', function (done) {
+ it('does not do anything if there are no subscribers', function (done) {
var options = {
subscribers: {}
@@ -477,7 +477,7 @@ describe('Monitor', function () {
});
});
- it('doesn\'t fail when a remote subscriber is unavailable', function (done) {
+ it('does not fail when a remote subscriber is unavailable', function (done) {
var options = {
subscribers: {
@@ -1006,7 +1006,7 @@ describe('Monitor', function () {
});
});
- it('doesn\'t overwrite log file with several initial log events', function (done) {
+ it('does not overwrite log file with several initial log events', function (done) {
var folderPath = Path.join(__dirname, 'logs');
@@ -1185,7 +1185,7 @@ describe('Monitor', function () {
});
});
- it('handles the error when directory doesn\'t exist', function (done) {
+ it('handles the error when directory does not exist', function (done) {
var folderPath = Path.join(__dirname, 'logs');
var options = {
View
85 test/network.js 100644 → 100755
@@ -1,10 +1,11 @@
// Load modules
+var Http = require('http');
+var Events = require('events');
+var Stream = require('stream');
var Lab = require('lab');
var Hapi = require('hapi');
var Hoek = require('hoek');
-var Http = require('http');
-var Events = require('events');
var NetworkMonitor = require('../lib/network');
@@ -76,8 +77,8 @@ describe('Network Monitor', function () {
var tags = ['hapi', 'received'];
var tagsMap = Hoek.mapToObject(tags);
- var request1 = { server: server1, info: { received: Date.now() - 1 }, url: { pathname: '/' }};
- var request2 = { server: server2, info: { received: Date.now() - 2 }, url: { pathname: '/test' } };
+ var request1 = { server: server1, info: { received: Date.now() - 1 }, url: { pathname: '/' }, response: { statusCode: 200 }, getLog: function () { return []; } };
+ var request2 = { server: server2, info: { received: Date.now() - 2 }, url: { pathname: '/test' }, response: { statusCode: 200 }, getLog: function () { return []; } };
emitter.emit('request', request1, { tags: tags }, tagsMap);
emitter.emit('request', request1, { tags: tags }, tagsMap);
emitter.emit('request', request2, { tags: tags }, tagsMap);
@@ -113,54 +114,70 @@ describe('Network Monitor', function () {
it('tracks server disconnects', function (done) {
- var dataServer = new Hapi.Server(0);
- var proxiedServer = new Hapi.Server(0);
+ var goodServer = new Hapi.Server(0);
var server = new Hapi.Server(0);
- proxiedServer.route({ method: '*', path: '/{p*}', handler: function (request) {
+ goodServer.route({
+ method: '*', path: '/{p*}', handler: function (request, reply) {
- }});
+ expect(request.payload.events[0].load.requests[server.info.port].disconnects).to.equal(1);
+ server.stop();
+ goodServer.stop();
- dataServer.route({ method: '*', path: '/{p*}', handler: function (request) {
+ done();
+ }
+ });
- expect(request.payload.events[0].load.requests[server.info.port].disconnects).to.equal(1);
- server.stop();
- proxiedServer.stop();
- dataServer.stop();
+ var TestStream = function () {
- done();
- }});
+ Stream.Readable.call(this);
+ };
- dataServer.start(function () {
+ Hapi.utils.inherits(TestStream, Stream.Readable);
- proxiedServer.start(function () {
+ TestStream.prototype._read = function (size) {
- server.route({ method: '*', path: '/{p*}', handler: { proxy: { mapUri: function (request, next) {
+ var self = this;
- next(null, 'http://127.0.0.1:' + proxiedServer.info.port + request.path);
- }}}});
+ if (this.isDone) {
+ return;
+ }
+
+ this.isDone = true;
+
+ setTimeout(function () { self.push('Hello'); }, 10);
+ setTimeout(function () { self.push(null); }, 20);
+ };
+
+ var handler = function (request, reply) {
- var options = {
- subscribers: {},
- opsInterval: 100
- };
- options.subscribers['http://127.0.0.1:' + dataServer.info.port] = ['ops'];
+ reply(new TestStream());
+ };
- server.pack.require('../', options, function () {
+ goodServer.start(function () {
- server.start(function () {
+ server.route({ method: 'POST', path: '/', handler: handler });
- var req = Http.get('http://127.0.0.1:' + server.info.port, function () {
+ var options = {
+ subscribers: {},
+ opsInterval: 100
+ };
+ options.subscribers['http://127.0.0.1:' + goodServer.info.port] = ['ops'];
- });
+ server.pack.require('../', options, function () {
- req.on('error', function () {});
+ server.start(function () {
- setTimeout(function () {
+ var options = {
+ hostname: '127.0.0.1',
+ port: server.info.port,
+ path: '/',
+ method: 'POST'
+ };
- req.destroy();
- }, 5);
- });
+ var req = Http.request(options, function (res) { req.destroy(); });
+ req.end('{}');
+ req.on('error', function () { });
});
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.