Skip to content

Commit

Permalink
Allow ops collecting to be disabled. Closes #411.
Browse files Browse the repository at this point in the history
  • Loading branch information
arb committed Dec 23, 2015
1 parent e16798a commit 5582343
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 71 deletions.
60 changes: 20 additions & 40 deletions lib/index.js
@@ -1,63 +1,43 @@
'use strict';

// Load modules
const Hoek = require('hoek');
const Joi = require('joi');
const Schema = require('./schema');

const internals = {
defaults: {
responseEvent: 'tail',
requestHeaders: false,
requestPayload: false,
responsePayload: false,
extensions: [],
reporters: [],
ops: {
interval: 15000
},
filter: {}
}
};


// Load modules
const Monitor = require('./monitor');

const internals = {
onPostStop(monitor) {

const onPostStop = (monitor) => {

return (server, next) => {

monitor.stop(next);
};
};

return (server, next) => {

const onPreStart = (monitor, options) => {
monitor.stop(next);
};
},
onPreStart(monitor, options) {

return (server, next) => {
return (server, next) => {

const interval = options.ops.interval;
monitor.startOps(interval);
return next();
};
const interval = options.ops.interval;
monitor.startOps(interval);
return next();
};
}
};


exports.register = (server, options, next) => {

options = Hoek.applyToDefaultsWithShallow(internals.defaults, options, ['reporters']);

// Validate settings
Schema.assert('monitorOptions', options);
const result = Joi.validate(options, Schema);
Hoek.assert(!result.error, 'Invalid', 'monitorOptions', 'options', result.error);

const monitor = new Monitor(server, options);
const monitor = new Monitor(server, result.value);
server.expose('monitor', monitor);
server.ext([{
type: 'onPostStop',
method: onPostStop(monitor)
method: internals.onPostStop(monitor)
}, {
type: 'onPreStart',
method: onPreStart(monitor, options)
method: internals.onPreStart(monitor, options)
}]);

return monitor.start(next);
Expand Down
18 changes: 11 additions & 7 deletions lib/monitor.js
Expand Up @@ -42,7 +42,7 @@ class Monitor extends Events.EventEmitter {
responsePayload: this.settings.responsePayload
};

this._ops = new Oppsy(server, options.ops.config);
this._ops = this.settings.ops && new Oppsy(server, this.settings.ops.config);

// Event handlers

Expand Down Expand Up @@ -93,7 +93,7 @@ class Monitor extends Events.EventEmitter {

startOps(interval) {

this._ops.start(interval);
this._ops && this._ops.start(interval);
}

start(callback) {
Expand Down Expand Up @@ -157,11 +157,14 @@ class Monitor extends Events.EventEmitter {
this._server.on(event, handler);
});

this._ops.on('ops', this._opsHandler);
this._ops.on('error', (err) => {
if (this._ops) {
this._ops.on('ops', this._opsHandler);
this._ops.on('error', (err) => {

console.error(err);
});
}

console.error(err);
});
Wreck.on('response', this._state.wreckHandler);

// Events can not be any of ['log', 'request-error', 'ops', 'request', 'response', 'tail']
Expand All @@ -180,7 +183,8 @@ class Monitor extends Events.EventEmitter {
const state = this._state;

Wreck.removeListener('response', state.wreckHandler);
this._ops.stop();

this._ops && this._ops.stop();

internals.iterateOverEventHash(state.handlers, (event, handler) => {

Expand Down
19 changes: 5 additions & 14 deletions lib/schema.js
Expand Up @@ -2,25 +2,16 @@

// Load Modules

const Hoek = require('hoek');
const Joi = require('joi');

const internals = {};

exports.assert = function (type, options) {

const error = Joi.validate(options, internals[type]).error;
Hoek.assert(!error, 'Invalid', type, 'options', error);
};

internals.monitorOptions = Joi.object().keys({
requestHeaders: Joi.boolean().default(false),
requestPayload: Joi.boolean().default(false),
responsePayload: Joi.boolean().default(false),
module.exports = Joi.object().keys({
requestHeaders: Joi.bool().default(false),
requestPayload: Joi.bool().default(false),
responsePayload: Joi.bool().default(false),
reporters: Joi.array().items(Joi.object(), Joi.string()).default([]),
responseEvent: Joi.string().valid('response', 'tail').default('tail'),
extensions: Joi.array().items(Joi.string().invalid('log', 'request-error', 'ops', 'request', 'response', 'tail')).default([]),
ops: Joi.object().optional().default({
ops: Joi.alternatives([Joi.object(), Joi.bool().allow(false)]).default({
config: {},
interval: 15000
}),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -30,7 +30,7 @@
"code": "2.x.x",
"hapi": "10.x.x",
"insync": "2.1.x",
"lab": "7.3.x"
"lab": "7.3.0"
},
"scripts": {
"test": "lab -m 5000 -t 100 -v -La code",
Expand Down
32 changes: 23 additions & 9 deletions test/monitor.js
Expand Up @@ -30,9 +30,7 @@ const internals = {
responsePayload: false,
extensions: [],
reporters: [],
ops: {
interval: 15000
},
ops: false,
filter: {}
};
return new Monitor(server, Object.assign({}, defaults, options));
Expand All @@ -45,13 +43,11 @@ const expect = Code.expect;
const describe = lab.describe;
const it = lab.it;



describe('Monitor', () => {

it('logs an error if one occurs doing ops information collection', (done) => {

const monitor = internals.monitorFactory(new Hapi.Server());
const monitor = internals.monitorFactory(new Hapi.Server(), { ops: { interval: 15000 } });
const error = console.error;
console.error = (err) => {

Expand All @@ -66,6 +62,18 @@ describe('Monitor', () => {
});
});

it('allows starting the monitor without the ops monitoring', (done) => {

const monitor = internals.monitorFactory(new Hapi.Server());
monitor.start((err) => {

expect(err).to.not.exist();
monitor.startOps(100);
expect(monitor._ops).to.be.false();
monitor.stop(done);
});
});

describe('start()', () => {

it('calls the init methods of all the reporters', (done) => {
Expand Down Expand Up @@ -123,7 +131,7 @@ describe('Monitor', () => {

it(`attaches events for 'ops', 'tail', 'log', and 'request-error'`, (done) => {

const monitor = internals.monitorFactory(new Hapi.Server(), { reporters: [new GoodReporter()] } );
const monitor = internals.monitorFactory(new Hapi.Server(), { reporters: [new GoodReporter()], ops: { interval: 15000 } } );
monitor.start((error) => {

expect(error).to.not.exist();
Expand Down Expand Up @@ -212,7 +220,10 @@ describe('Monitor', () => {
const two = new GoodReporter({ ops: '*' });
const monitor = internals.monitorFactory(new Hapi.Server(), {
reporters: [one, two],
extensions: ['request-internal']
extensions: ['request-internal'],
ops: {
interval: 1500
}
});

Insync.series([
Expand Down Expand Up @@ -375,7 +386,10 @@ describe('Monitor', () => {

const one = new GoodReporter({ ops: '*' });
const monitor = internals.monitorFactory(server, {
reporters: [one]
reporters: [one],
ops: {
interval: 1500
}
});

Insync.series([
Expand Down

0 comments on commit 5582343

Please sign in to comment.