Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Turn to plugin

  • Loading branch information...
commit dfb6d6c1f762d5bd35e4d1f10653c44897524a66 1 parent f46d1fd
@hueniverse hueniverse authored
View
53 README.md
@@ -6,3 +6,56 @@
[![Build Status](https://secure.travis-ci.org/walmartlabs/good.png)](http://travis-ci.org/walmartlabs/good)
The _'Monitor'_ should be configured using a _'hapi'_ server instead of calling the _'Monitor'_ constructor directly.
+
+
+**hapi** comes with a built-in process monitor for three types of events:
+- System and process performance (ops) - CPU, memory, disk, and other metrics.
+- Requests logging (request) - framework and application generated logs generated during the lifecycle of each incoming request.
+- General events (log) - logging information not bound to a specific request such as system errors, background processing, configuration errors, etc. Described in [General Events Logging](#general-events-logging).
+
+The monitor is _off_ by default and can be turned on using the `monitor` server option. To use the default settings, simply set the value to _true_.
+Applications with multiple server instances, each with its own monitor should only include one _log_ subscription per destination as general events (log)
+are a process-wide facility and will result in duplicated log events. To override some or all of the defaults, set `monitor` to an object with the following
+optional settings:
+- `broadcastInterval` - the interval in milliseconds to send collected events to subscribers. _0_ means send immediately. Defaults to _0_.
+- `opsInterval` - the interval in milliseconds to sample system and process performance metrics. Minimum is _100ms_. Defaults to _15 seconds_.
+- `extendedRequests` - determines if the full request log is sent or only the event summary. Defaults to _false_.
+- `requestsEvent` - the event type used to capture completed requests. Defaults to 'tail'. Options are:
+ - 'response' - the response was sent but request tails may still be pending.
+ - 'tail' - the response was sent and all request tails completed.
+- `subscribers` - an object where each key is a destination and each value is either an array or object with an array of subscriptions. The subscriptions that are available are _ops_, _request_, and _log_. The destination can be a URI or _console_. Defaults to a console subscription to all three. To disable the console output for the server instance pass an empty array into the subscribers "console" configuration.
+
+For example:
+```javascript
+var options = {
+ monitor: {
+ subscribers: {
+ console: ['ops', 'request', 'log'],
+ 'http://localhost/logs': ['log']
+ }
+ }
+};
+```
+
+Disabling hapi console output:
+```javascript
+var options = {
+ monitor: {
+ subscribers: {
+ console: [],
+ 'http://localhost/logs': ['log']
+ }
+ }
+};
+```
+
+Log messages are created with tags. Usually a log will include a tag to indicate if it is related to an error or info along with where the message originates. If, for example, the console should only output error's that were logged you can use the following configuration:
+```javascript
+var options = {
+ monitor: {
+ subscribers: {
+ console: { tags: ['error'], events: ['log'] }
+ }
+ }
+};
+```
View
20 lib/index.js 100644 → 100755
@@ -1,3 +1,17 @@
-module.exports = require('./log');
-module.exports.log = require('./log');
-module.exports.Monitor = require('./monitor');
+// Load modules
+
+var Hoek = require('hoek');
+var Monitor = require('./monitor');
+
+
+// Declare internals
+
+var internals = {};
+
+
+exports.register = function (pack, options, next) {
+
+ pack.api(new Monitor(pack, options), 'monitor');
+ return next();
+};
+
View
74 lib/log.js
@@ -1,74 +0,0 @@
-// Load modules
-
-var NodeUtil = require('util');
-var Events = require('events');
-
-
-// Declare internals
-
-var internals = {};
-
-
-internals.Logger = function () {
-
- Events.EventEmitter.call(this);
-
- this.console = console.log;
- return this;
-};
-
-NodeUtil.inherits(internals.Logger, Events.EventEmitter);
-
-
-internals.Logger.prototype.event = function (tags, data, timestamp) {
-
- tags = (tags instanceof Array ? tags : [tags]);
- var now = (timestamp ? (timestamp instanceof Date ? timestamp : new Date(timestamp)) : new Date());
-
- var event = {
- ets: now.getTime(),
- tags: tags,
- data: data
- };
-
- if (!this.emit('log', event)) {
- // Output to console if no listeners
- this.print(event, true);
- }
-};
-
-
-internals.Logger.prototype.print = function (event, _isBypass) {
-
- var pad = function (value) {
-
- return (value < 10 ? '0' : '') + value;
- };
-
- var now = new Date(event.ets);
- var timestring = (now.getYear() - 100).toString() +
- pad(now.getMonth() + 1) +
- pad(now.getDate()) +
- '/' +
- pad(now.getHours()) +
- pad(now.getMinutes()) +
- pad(now.getSeconds()) +
- '.' +
- now.getMilliseconds();
-
- var data = event.data;
- if (typeof event.data !== 'string') {
- try {
- data = JSON.stringify(event.data);
- }
- catch (e) {
- data = 'JSON Error: ' + e.message;
- }
- }
-
- var output = (_isBypass ? '*' : '') + timestring + ', ' + event.tags[0] + ', ' + data;
- this.console(output);
-};
-
-
-module.exports = new internals.Logger();
View
383 lib/monitor/index.js → lib/monitor.js
@@ -1,34 +1,44 @@
// Load modules
var Os = require('os');
+var Events = require('events');
+var NodeUtil = require('util');
var Async = require('async');
var Request = require('request');
var Hoek = require('hoek');
var System = require('./system');
var Process = require('./process');
-var Log = require('../log');
// Declare internals
-var internals = {
- hostname: Os.hostname(),
- version: Hoek.loadPackage().version || 'unknown'
+var internals = {};
+
+
+internals.defaults = {
+ broadcastInterval: 0, // MSec, 0 for immediately
+ opsInterval: 15000, // MSec, equal to or greater than 100
+ extendedRequests: false,
+ requestsEvent: 'tail', // Sets the event used by the monitor to listen to finished requests. Other options: 'response'.
+ subscribers: null, // { console: ['ops', 'request', 'log'] }
+ alwaysMeasureOps: false // Measures ops even if no subscribers
};
-/**
- * Monitor Constructor
- *
- * @param {Object} server (HTTP Server), must be an EventEmitter
- * @api public
- */
-module.exports = internals.Monitor = function (server) {
+module.exports = internals.Monitor = function (pack, options) {
+
+ var self = this;
Hoek.assert(this.constructor === internals.Monitor, 'Monitor must be instantiated using new');
- this.server = server;
- this.settings = Hoek.clone(this.server.settings.monitor) || {};
+ this.pack = pack;
+ this.settings = Hoek.applyToDefaults(internals.defaults, options || {});
+
+ if (!this.settings.subscribers) {
+ this.settings.subscribers = {
+ console: ['ops', 'request', 'log']
+ };
+ }
// Validate settings
@@ -36,23 +46,28 @@ module.exports = internals.Monitor = function (server) {
Hoek.assert(this.settings.subscribers, 'Invalid monitor.subscribers configuration');
Hoek.assert(this.settings.requestsEvent === 'response' || this.settings.requestsEvent === 'tail', 'Invalid monitor.requestsEvent configuration');
+ // Register as event emitter
+
+ Events.EventEmitter.call(this);
+
// Private members
this._subscriberQueues = {}; // { destination -> subscriberQueue }
this._eventQueues = {}; // { eventType -> [subscriberQueue] }
this._subscriberTags = {};
+ this._background = {}; // internval ids
- this._eventHeader = {
+ this._eventHeader = {
module: 'hapi',
- host: internals.hostname,
- appVer: internals.version,
- events: []
+ host: Os.hostname(),
+ appVer: Hoek.loadPackage().version || 'unknown',
+ events: []
};
// Identify subscriptions
var subscriberKeys = Object.keys(this.settings.subscribers);
- for (var i = 0, il = subscriberKeys.length; i < il; ++i) {
+ for (var i = 0, il = subscriberKeys.length; i < il; ++i) {
var dest = subscriberKeys[i];
this._subscriberQueues[dest] = [];
@@ -61,208 +76,200 @@ module.exports = internals.Monitor = function (server) {
var eventTypes = Array.isArray(subscriptions) ? subscriptions : subscriptions.events;
this._subscriberTags[dest] = subscriptions.tags;
- for (var s = 0, sl = eventTypes.length; s < sl; ++s) {
+ for (var s = 0, sl = eventTypes.length; s < sl; ++s) {
var eventType = eventTypes[s];
this._eventQueues[eventType] = this._eventQueues[eventType] || [];
- this._eventQueues[eventType].push(this._subscriberQueues[dest]);
- }
+ this._eventQueues[eventType].push(this._subscriberQueues[dest]);
+ }
}
- if (Object.keys(this._eventQueues).length) {
+ if (Object.keys(this._eventQueues).length ||
+ this.settings.alwaysMeasureOps) {
// Setup broadcast interval
- if (this.settings.broadcastInterval) {
- setInterval(this._broadcast(), this.settings.broadcastInterval);
+ if (this.settings.broadcastInterval) {
+ this._background.broadcastInterval = setInterval(this._broadcast(), this.settings.broadcastInterval);
}
// Initialize Events
- if (this._eventQueues.log) {
- Log.on('log', this._handle('log'));
- Log.event(['info', 'config'], this.server.settings.nickname + ': Log monitoring enabled');
- }
-
- if (this._eventQueues.ops) {
- this._initOps();
+ if (this._eventQueues.log) {
+ this._background.log = this._handle('log');
+ this.pack.events.on('log', this._background.log);
}
- if (this._eventQueues.request) {
- this.server.on(this.settings.requestsEvent, this._handle('request'));
- Log.event(['info', 'config'], this.server.settings.nickname + ': Requests monitoring enabled');
- }
+ if (this._eventQueues.request) {
+ this._background.request = this._handle('request');
+ this.pack.events.on(this.settings.requestsEvent, this._background.request);
+ }
+
+ if (this._eventQueues.ops ||
+ this.settings.alwaysMeasureOps) {
+
+ this._process = new Process.Monitor();
+ this._os = new System.Monitor();
+
+ this._background.ops = this._handle('ops');
+ self.on('ops', this._background.ops);
+
+ // Set ops interval timer
+
+ var opsFunc = function () {
+
+ // Gather operational statistics in parallel
+
+ Async.parallel({
+ oscpu: self._os.cpu,
+ osdisk: self._os.disk,
+ osload: self._os.loadavg,
+ osmem: self._os.mem,
+ osup: self._os.uptime,
+ psup: self._process.uptime,
+ psmem: self._process.memory,
+ pscpu: self._process.cpu
+ },
+ function (err, results) {
+
+ if (!err) {
+ self.emit('ops', results);
+ }
+ });
+ };
+
+ this._background.opsInterval = setInterval(opsFunc, this.settings.opsInterval);
+ }
}
- return this;
+ return this;
};
-
-/**
- * Initialize Operations Monitoring if configured
- *
- * @api private
- */
-internals.Monitor.prototype._initOps = function () {
-
- var self = this;
-
- // Initialize helpers
-
- this._process = new Process.Monitor();
- this._os = new System.Monitor();
-
- // Subscribe to opts interval
-
- this.server.on('ops', this._handle('ops'));
-
- // Set ops interval timer
-
- var opsFunc = function () {
-
- // Gather operational statistics in parallel
-
- Async.parallel({
- oscpu: self._os.cpu,
- osdisk: self._os.disk,
- osload: self._os.loadavg,
- osmem: self._os.mem,
- osup: self._os.uptime,
- psup: self._process.uptime,
- psmem: self._process.memory,
- pscpu: self._process.cpu
- },
- function (err, results) {
-
- if (!err) {
- self.server.emit('ops', results);
- }
- else {
- Log.event(['err', 'monitor', 'ops'], err);
- }
- });
- };
-
- setInterval(opsFunc, this.settings.opsInterval);
- Log.event(['info', 'config'], this.server.settings.nickname + ': Ops monitoring enabled');
+NodeUtil.inherits(internals.Monitor, Events.EventEmitter);
+
+
+internals.Monitor.prototype.stop = function () {
+
+ if (this._background.opsInterval) {
+ clearInterval(this._background.opsInterval);
+ }
+
+ if (this._background.broadcastInterval) {
+ clearInterval(this._background.broadcastInterval);
+ }
+
+ if (this._background.log) {
+ this.pack.events.removeListener('log', this._background.log);
+ }
+
+ if (this._background.request) {
+ this.pack.events.removeListener(this.settings.requestsEvent, this._background.request);
+ }
+
+ if (this._background.ops) {
+ this.removeListener('ops', this._background.ops);
+ }
};
-/**
- * Broadcast queued events
- *
- * @api private
- */
-internals.Monitor.prototype._broadcast = function () {
+internals.Monitor.prototype._broadcast = function () {
var self = this;
- return function () {
+ return function () {
var subscriberQueueKeys = Object.keys(self._subscriberQueues);
- for (var i = 0, il = subscriberQueueKeys.length; i < il; ++i) {
+ for (var i = 0, il = subscriberQueueKeys.length; i < il; ++i) {
var dest = subscriberQueueKeys[i];
var subscriberQueue = self._subscriberQueues[dest];
- if (subscriberQueue.length) {
+ if (subscriberQueue.length) {
var data = Hoek.clone(self._eventHeader);
data.mts = Date.now();
var destFilterTags = self._subscriberTags[dest];
- var filteredQueue = subscriberQueue.filter(function (event) {
+ var filteredQueue = subscriberQueue.filter(function (event) {
- var containsEventTag = function (tag) {
+ var containsEventTag = function (tag) {
- return event.tags && event.tags.indexOf(tag) >= 0;
+ return event.tags && event.tags.indexOf(tag) >= 0;
};
- return !destFilterTags || destFilterTags.some(containsEventTag);
+ return !destFilterTags || destFilterTags.some(containsEventTag);
});
- filteredQueue.forEach(function (event) {
+ filteredQueue.forEach(function (event) {
- data.events.push(event);
+ data.events.push(event);
});
subscriberQueue.length = 0; // Empty queue (must not set to [] or queue reference will change)
- if (dest === 'console') {
- self._display(data);
+ if (dest === 'console') {
+ self._display(data);
}
- else {
- var request = {
+ else {
+ var request = {
method: 'post',
uri: dest,
- json: data
+ json: data
};
Request(request, function (err, res, body) {
// Ignore errors
- });
- }
- }
- }
- };
+ });
+ }
+ }
+ }
+ };
};
-/**
- * Respond to Monitoring Signals and dispatch handlers
- *
- * @param {String} eventName signal name
- * @api private
- */
-internals.Monitor.prototype._handle = function (eventName) {
+internals.Monitor.prototype._handle = function (eventName) {
var self = this;
var eventHandler = null;
- if (eventName === 'ops') {
- eventHandler = this._ops();
+ if (eventName === 'ops') {
+ eventHandler = this._ops();
}
- else if (eventName === 'request') {
- eventHandler = this._request();
+ else if (eventName === 'request') {
+ eventHandler = this._request();
}
- else if (eventName === 'log') {
- eventHandler = this._log();
+ else if (eventName === 'log') {
+ eventHandler = this._log();
}
Hoek.assert(eventHandler !== null, 'Invalid eventName specified');
- return function (context) {
+ return function (context) {
var subscriptions = self._eventQueues[eventName];
if (subscriptions &&
- subscriptions.length) {
+ subscriptions.length) {
var event = eventHandler(context);
- for (var i = 0, il = subscriptions.length; i < il; ++i) {
- subscriptions[i].push(event);
+ for (var i = 0, il = subscriptions.length; i < il; ++i) {
+ subscriptions[i].push(event);
}
- if (self.settings.broadcastInterval === 0) {
- self._broadcast()();
- }
- }
- };
+ if (self.settings.broadcastInterval === 0) {
+ self._broadcast()();
+ }
+ }
+ };
};
-/**
- * Generate and send Operational Monitoring data
- *
- * @param {Object} operations results object
- * @api private
- */
-internals.Monitor.prototype._ops = function () {
+internals.Monitor.prototype._ops = function () {
- return function (results) {
+ return function (results) {
- var event = {
+ var event = {
event: 'ops',
ets: Date.now(),
- os: {
+ os: {
load: results.osload,
mem: results.osmem,
disk: results.osdisk,
@@ -270,40 +277,33 @@ internals.Monitor.prototype._ops = function () {
// io: '', // Not yet implemented
// net: '' // Not yet implemented
},
- proc: {
+ proc: {
uptime: results.psup,
mem: results.psmem,
- cpu: results.pscpu
- }
+ cpu: results.pscpu
+ }
};
if (results.oscpu !== null &&
- results.oscpu !== '-') {
+ results.oscpu !== '-') {
- event.os.cpu = results.oscpu;
+ event.os.cpu = results.oscpu;
}
- return event;
- };
+ return event;
+ };
};
-
-/**
- * Generate and send Request Monitoring data
- *
- * @param {Object} client HTTP client to use for sending
- * @param {String} url path to POST data
- * @api private
- */
-internals.Monitor.prototype._request = function () {
+
+internals.Monitor.prototype._request = function () {
var self = this;
- return function (request) {
+ return function (request) {
var req = request.raw.req;
- var event = {
+ var event = {
event: 'request',
ets: request._analytics.startTime,
id: request.id,
@@ -311,75 +311,64 @@ internals.Monitor.prototype._request = function () {
method: request.method,
path: request.path,
query: request.query,
- source: {
+ source: {
remoteAddress: (req.connection ? req.connection.remoteAddress : 'unknown'),
userAgent: req.headers['user-agent'],
- referer: req.headers.referer
+ referer: req.headers.referer
},
- responseTime: Date.now() - request._analytics.startTime
+ responseTime: Date.now() - request._analytics.startTime
};
if (self.settings.extendedRequests &&
- request._log.length) {
+ request._log.length) {
- event.log = request._log;
+ event.log = request._log;
}
- return event;
- };
+ return event;
+ };
};
-/**
- * Generate and send logs events
- *
- * @param {Object} event details
- * @api private
- */
-internals.Monitor.prototype._log = function () {
+internals.Monitor.prototype._log = function () {
- return function (event) {
+ return function (event) {
- event = {
+ event = {
event: 'log',
ets: event.ets,
tags: event.tags,
- data: event.data
+ data: event.data
};
- return event;
- };
+ return event;
+ };
};
-/**
- * Display events on console
- *
- * @api private
- */
-internals.Monitor.prototype._display = function (data) {
+internals.Monitor.prototype._display = function (data) {
- for (var i = 0, il = data.events.length; i < il; ++i) {
+ for (var i = 0, il = data.events.length; i < il; ++i) {
var event = data.events[i];
- if (event.event === 'ops') {
+ if (event.event === 'ops') {
- Log.print({
+ Hoek.printEvent({
ets: event.ets,
tags: ['ops'],
- data: 'memory: ' + Math.round(event.proc.mem.rss / (1024 * 1024)) + 'M cpu: ' + event.proc.cpu
- });
+ data: 'memory: ' + Math.round(event.proc.mem.rss / (1024 * 1024)) + 'M cpu: ' + event.proc.cpu
+ });
}
- else if (event.event === 'request') {
+ else if (event.event === 'request') {
- Log.print({
+ Hoek.printEvent({
ets: event.ets,
tags: ['request'],
- data: event.instance + ': ' + event.method + ' ' + event.path + ' (' + event.responseTime + 'ms)'
- });
+ data: event.instance + ': ' + event.method + ' ' + event.path + ' (' + event.responseTime + 'ms)'
+ });
}
- else if (event.event === 'log') {
+ else if (event.event === 'log') {
- Log.print(event);
- }
- }
+ Hoek.printEvent(event);
+ }
+ }
};
View
0  lib/monitor/process.js → lib/process.js
File renamed without changes
View
0  lib/monitor/system.js → lib/system.js
File renamed without changes
View
12 package.json
@@ -1,7 +1,7 @@
{
"name": "good",
- "description": "hapi process monitoring",
- "version": "0.0.2",
+ "description": "Server and process monitoring plugin",
+ "version": "0.1.0",
"author": "Van Nguyen <the.gol.effect@gmail.com>",
"contributors":[
"Eran Hammer <eran@hueniverse.com> (http://hueniverse.com)",
@@ -13,7 +13,9 @@
"process",
"monitor",
"log",
- "report"
+ "report",
+ "hapi",
+ "plugin"
],
"engines": {
"node": "0.8.x"
@@ -23,7 +25,11 @@
"request": "2.11.x",
"async": "0.1.x"
},
+ "peerDependencies": {
+ "hapi": "0.15.x"
+ },
"devDependencies": {
+ "hapi": "0.15.x",
"sinon": "1.x.x",
"mocha": "1.x.x",
"chai": "1.x.x",
View
54 test/helpers.js
@@ -1,54 +0,0 @@
-// Load modules
-
-var NodeUtil = require('util');
-var Events = require('events');
-var Good = require('../lib');
-var ProcessMonitor = require('../lib/monitor/process');
-var SystemMonitor = require('../lib/monitor/system');
-
-
-// Declare internals
-
-var internals = {};
-
-
-module.exports = Good;
-module.exports.SystemMonitor = SystemMonitor;
-module.exports.ProcessMonitor = ProcessMonitor;
-
-
-internals.Logger = function () {
-
- Events.EventEmitter.call(this);
-
- return this;
-};
-
-NodeUtil.inherits(internals.Logger, Events.EventEmitter);
-module.exports._TEST = internals.logger = new internals.Logger();
-
-
-// Override Log's console method
-
-Good.log.console = function (message) {
-
- internals.logger.emit('log', message);
-};
-
-
-module.exports.Server = function (settings) {
-
- var server = new internals.FakeServer(settings);
- server._monitor = new Good.Monitor(server);
-
- return server;
-};
-
-
-internals.FakeServer = function (settings) {
-
- Events.EventEmitter.call(this);
- this.settings = settings;
-};
-
-NodeUtil.inherits(internals.FakeServer, Events.EventEmitter);
View
42 test/index.js
@@ -0,0 +1,42 @@
+// Load modules
+
+var Chai = require('chai');
+var Hapi = require('hapi');
+
+
+// Declare internals
+
+var internals = {};
+
+
+// Test shortcuts
+
+var expect = Chai.expect;
+
+
+describe('Plugin', function () {
+
+ it('emits ops data', function (done) {
+
+ var server = new Hapi.Server();
+
+ var options = {
+ subscribers: {},
+ opsInterval: 100,
+ alwaysMeasureOps: true
+ };
+
+ server.plugin().require('..', options, function (err) {
+
+ expect(err).to.not.exist;
+
+ server.plugins.good.monitor.once('ops', function (event) {
+
+ expect(event.osload).to.exist;
+ done();
+ });
+ });
+ });
+});
+
+
View
84 test/log.js
@@ -1,84 +0,0 @@
-// Load modules
-
-var Chai = require('chai');
-var Helpers = require('./helpers');
-
-
-// Declare internals
-
-var internals = {};
-
-
-// Test shortcuts
-
-var expect = Chai.expect;
-
-
-describe('Good', function () {
-
- describe('#event', function () {
-
- it('fires an event with the passed in tags', function (done) {
-
- var tags = ['hello'];
- Helpers.log.once('log', function (event) {
-
- expect(event).to.exist;
- expect(event.tags).to.exist;
- expect(event.tags[0]).to.equal('hello');
- done();
- });
- Helpers.log.event(tags, null, Date.now());
- });
-
- it('outputs to stdout if no listeners exist', function (done) {
-
- var tags = ['hello'];
- Helpers.log.removeAllListeners('log');
- Helpers._TEST.once('log', function (output) {
-
- expect(output).to.contain('hello');
- done();
- });
-
- Helpers.log.event(tags, null, Date.now());
- });
- });
-
- describe('#print', function () {
-
- it('outputs correct text to stdout', function (done) {
-
- var event = {
- tags: ['tag1'],
- data: 'test'
- };
-
- Helpers._TEST.once('log', function (output) {
-
- expect(output).to.contain('test');
- expect(output).to.contain('tag1');
- done();
- });
-
- Helpers.log.print(event, false);
- });
-
- it('outputs correct error text to stdout', function (done) {
-
- var event = {
- tags: ['tag1'],
- data: { a: 1 }
- };
- event.data.b = event.data;
-
- Helpers._TEST.once('log', function (output) {
-
- expect(output).to.contain('JSON Error');
- done();
- });
-
- Helpers.log.print(event, false);
- });
- });
-});
View
528 test/monitor.js
@@ -0,0 +1,528 @@
+// Load modules
+
+var Chai = require('chai');
+var Hapi = require('hapi');
+var Hoek = require('hoek');
+var Monitor = require('../lib/monitor');
+
+
+// Declare internals
+
+var internals = {};
+
+
+// Test shortcuts
+
+var expect = Chai.expect;
+
+
+describe('Monitor', function () {
+
+ var makePack = function (callback) {
+
+ var holder = null;
+
+ var plugin = {
+ name: '--test',
+ version: '0.0.0',
+ register: function (pack, options, next) {
+
+ holder = pack;
+ next();
+ }
+ };
+
+ var server = new Hapi.Server();
+ server.plugin().register(plugin, function (err) {
+
+ expect(err).to.not.exist;
+ expect(holder).to.exist;
+ callback(holder, server);
+ });
+ };
+
+ it('throws an error constructed without new', function (done) {
+
+ var fn = function () {
+
+ var monitor = Monitor();
+ };
+
+ expect(fn).throws(Error, 'Monitor must be instantiated using new');
+ done();
+ });
+
+ it('throws an error if opsInterval is too small', function (done) {
+
+ var options = {
+ subscribers: {},
+ opsInterval: 50
+ };
+
+ makePack(function (pack, server) {
+
+ var fn = function () {
+
+ var monitor = new Monitor(pack, options);
+ };
+
+ expect(fn).throws(Error, 'Invalid monitor.opsInterval configuration');
+ done();
+ });
+ });
+
+ it('doesn\'t throw an error when opsInterval is more than 100', function (done) {
+
+ var options = {
+ subscribers: {},
+ opsInterval: 100
+ };
+
+ makePack(function (pack, server) {
+
+ var fn = function () {
+
+ var monitor = new Monitor(pack, options);
+ };
+
+ expect(fn).not.to.throw(Error);
+ done();
+ });
+ });
+
+ it('throws an error if requestsEvent is not response or tail', function (done) {
+
+ var options = {
+ subscribers: {},
+ requestsEvent: 'test'
+ };
+
+ makePack(function (pack, server) {
+
+ var fn = function () {
+
+ var monitor = new Monitor(pack, options);
+ };
+
+ expect(fn).throws(Error, 'Invalid monitor.requestsEvent configuration');
+ done();
+ });
+ });
+
+ it('uses the passed in broadcastInterval and sets the event queue correctly', function (done) {
+
+ var options = {
+ broadcastInterval: 5
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ expect(monitor._subscriberQueues.console).to.exist;
+ expect(monitor._eventQueues.request).to.exist;
+ expect(monitor._eventQueues.log).to.exist;
+ monitor.stop();
+ done();
+ });
+ });
+
+ describe('#_broadcast', function () {
+
+ it('doesn\'t do anything if there are no subscribers', function (done) {
+
+ var options = {
+ subscribers: {}
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ expect(monitor._broadcast()()).to.not.exist;
+ done();
+ });
+ });
+
+ it('filters out events that don\'t contain the subscribers tag', function (done) {
+
+ var options = {
+ subscribers: {
+ 'console': { tags: ['ERROR', 'WARNING'], events: ['log'] }
+ }
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ expect(monitor._subscriberQueues.console).to.exist;
+ expect(monitor._eventQueues.log).to.exist;
+
+ Hoek.consoleFunc = function (string) {
+
+ expect(string).to.not.exist;
+ };
+
+ server.log('other', 'not used');
+ Hoek.consoleFunc = console.log;
+ monitor.stop();
+ done();
+ });
+ });
+
+ it('shows events that the subscriber tags match', function (done) {
+
+ var options = {
+ subscribers: {
+ 'console': { tags: ['ERROR', 'WARNING'], events: ['log'] }
+ }
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ expect(monitor._subscriberQueues.console).to.exist;
+ expect(monitor._eventQueues.log).to.exist;
+
+ Hoek.consoleFunc = function (string) {
+
+ Hoek.consoleFunc = console.log;
+ expect(string).to.contain('included in output');
+ monitor.stop();
+ done();
+ };
+
+ server.log('ERROR', 'included in output');
+ });
+ });
+
+ it('broadcasts all events when no tags are provided', function (done) {
+
+ var options = {
+ subscribers: {
+ 'console': { events: ['log'] }
+ }
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ expect(monitor._subscriberQueues.console).to.exist;
+ expect(monitor._eventQueues.log).to.exist;
+
+ Hoek.consoleFunc = function (string) {
+
+ Hoek.consoleFunc = console.log;
+ expect(string).to.contain('included in output');
+ monitor.stop();
+ done();
+ };
+
+ server.log('ERROR', 'included in output');
+ monitor._broadcast()();
+ });
+ });
+ });
+
+ describe('#_ops', function () {
+
+ it('sets the event with the result data correctly', function (done) {
+
+ var results = {
+ osload: 1,
+ osmem: 20,
+ osdisk: 30,
+ osup: 50
+ };
+
+ var options = {
+ subscribers: {}
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ var event = monitor._ops()(results);
+
+ expect(event.os.load).to.equal(1);
+ expect(event.os.mem).to.equal(20);
+ expect(event.os.disk).to.equal(30);
+ done();
+ });
+ });
+
+ it('emits ops data', function (done) {
+
+ var options = {
+ subscribers: {
+ 'http://localhost:1023/': ['ops']
+ },
+ opsInterval: 100
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ monitor.once('ops', function (event) {
+
+ expect(event.osload).to.exist;
+ monitor.stop();
+ done();
+ });
+ });
+ });
+
+ it('emits an event when everything succeeds', function (done) {
+
+ var options = {
+ subscribers: {},
+ opsInterval: 100,
+ alwaysMeasureOps: true
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ monitor.once('ops', function (event) {
+
+ expect(event.osdisk.total).to.equal(100);
+ expect(event.osup).to.equal(1000);
+ monitor.stop();
+ done();
+ });
+
+ monitor._os = {
+ cpu: function (cb) {
+
+ cb(null, 1);
+ },
+ disk: function (cb) {
+
+ cb(null, { total: 100, free: 10 });
+ },
+ loadavg: function (cb) {
+
+ cb();
+ },
+ mem: function (cb) {
+
+ cb();
+ },
+ uptime: function (cb) {
+
+ cb(null, 1000);
+ }
+ };
+
+ monitor._process = {
+ uptime: function (cb) {
+
+ cb(null, 1000);
+ },
+ memory: function (cb) {
+
+ cb(null, { rss: 100 });
+ },
+ cpu: function (cb) {
+
+ cb();
+ }
+ };
+ });
+ });
+ });
+
+ describe('#_handle', function () {
+
+ it('dispatches immediately when broadcastInterval is 0', function (done) {
+
+ var options = {
+ subscribers: {
+ 'http://localhost:1023/': ['log']
+ },
+ broadcastInterval: 0
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ monitor._broadcast = function () {
+
+ return function () {
+
+ done();
+ };
+ };
+
+ monitor._handle('log')({ ets: Date.now(), tags: ['test'], data: 'test' });
+ });
+ });
+
+ it('throws an error when eventName is invalid', function (done) {
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, { subscribers: {} });
+
+ expect(function () {
+
+ monitor._handle('notFound');
+ }).to.throw();
+ done();
+ });
+ });
+ });
+
+ describe('#_request', function () {
+
+ it('sets the event with the request data correctly', function (done) {
+
+ makePack(function (pack, server) {
+
+ var request = {
+ raw: {
+ req: {
+ headers: {
+ 'user-agent': 'test'
+ }
+ }
+ },
+ _analytics: {},
+ server: server
+ };
+
+ var monitor = new Monitor(pack, { subscribers: {} });
+ var event = monitor._request()(request);
+
+ expect(event.event).to.equal('request');
+ expect(event.source.userAgent).to.equal('test');
+ done();
+ });
+ });
+
+ it('logs errors when they occur', function (done) {
+
+ var options = {
+ subscribers: {},
+ extendedRequests: true
+ };
+
+ makePack(function (pack, server) {
+
+ var request = {
+ raw: {
+ req: {
+ headers: {
+ 'user-agent': 'test'
+ }
+ }
+ },
+ _analytics: {},
+ server: server,
+ _log: 'test'
+ };
+
+ var monitor = new Monitor(pack, options);
+
+ var event = monitor._request()(request);
+
+ expect(event.event).to.equal('request');
+ expect(event.source.userAgent).to.equal('test');
+ expect(event.log).to.equal('test');
+ done();
+ });
+ });
+ });
+
+ describe('#_display', function () {
+
+ it('prints to the log event data for ops events', function (done) {
+
+ var options = {
+ subscribers: {}
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ var data = {
+ events: [{
+ event: 'ops',
+ proc: {
+ mem: {
+ rss: 1
+ },
+ cpu: 10
+ }
+ }]
+ };
+
+ Hoek.consoleFunc = function (string) {
+
+ Hoek.consoleFunc = console.log;
+ expect(string).to.contain('memory');
+ done();
+ };
+
+ monitor._display(data);
+ });
+ });
+
+ it('prints to the log event data for request events', function (done) {
+
+ var options = {
+ subscribers: {}
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ var data = {
+ events: [{
+ event: 'request',
+ instance: 'testInstance',
+ method: 'testMethod'
+ }]
+ };
+
+ Hoek.consoleFunc = function (string) {
+
+ Hoek.consoleFunc = console.log;
+ expect(string).to.contain('testMethod');
+ done();
+ };
+
+ monitor._display(data);
+ });
+ });
+ });
+
+ describe('#_log', function () {
+
+ it('returns wrapped events', function (done) {
+
+ var options = {
+ subscribers: {}
+ };
+
+ makePack(function (pack, server) {
+
+ var monitor = new Monitor(pack, options);
+
+ var event = monitor._log()({});
+
+ expect(event.event).to.equal('log');
+ done();
+ });
+ });
+ });
+});
View
693 test/monitor/index.js
@@ -1,693 +0,0 @@
-// Load modules
-
-var Chai = require('chai');
-var Helpers = require('../helpers');
-
-
-// Declare internals
-
-var internals = {};
-
-
-// Test shortcuts
-
-var expect = Chai.expect;
-
-
-describe('Monitor', function () {
-
- it('throws an error constructed without new', function (done) {
-
- var fn = function () {
-
- var config = {
- settings: {
- monitor: {
- opsInterval: 50,
- subscribers: {},
- requestsEvent: 'response'
- }
- }
- };
-
- var monitor = Helpers.Monitor(config);
- };
-
- expect(fn).throws(Error, 'Monitor must be instantiated using new');
- done();
- });
-
- it('throws an error if opsInterval is too small', function (done) {
-
- var fn = function () {
-
- var config = {
- settings: {
- monitor: {
- opsInterval: 50,
- subscribers: {},
- requestsEvent: 'response'
- }
- }
- };
-
- var monitor = new Helpers.Monitor(config);
- };
-
- expect(fn).throws(Error, 'Invalid monitor.opsInterval configuration');
- done();
- });
-
- it('doesn\'t throw an error when opsInterval is more than 100', function (done) {
-
- var fn = function () {
-
- var config = {
- settings: {
- monitor: {
- opsInterval: 200,
- subscribers: {},
- requestsEvent: 'response'
- }
- }
- };
- var monitor = new Helpers.Monitor(config);
- };
-
- expect(fn).not.to.throw(Error);
- done();
- });
-
- it('throws an error if subscribers is null', function (done) {
-
- var fn = function () {
-
- var config = {
- settings: {
- monitor: {
- opsInterval: 200,
- subscribers: null,
- requestsEvent: 'response'
- }
- }
- };
- var monitor = new Helpers.Monitor(config);
- };
-
- expect(fn).throws(Error, 'Invalid monitor.subscribers configuration');
- done();
- });
-
- it('throws an error if requestsEvent is not response or tail', function (done) {
-
- var fn = function () {
-
- var config = {
- settings: {
- monitor: {
- opsInterval: 200,
- subscribers: {},
- requestsEvent: 'test'
- }
- }
- };
- var monitor = new Helpers.Monitor(config);
- };
-
- expect(fn).throws(Error, 'Invalid monitor.requestsEvent configuration');
- done();
- });
-
- it('uses the passed in broadcastInterval and sets the event queue correctly', function (done) {
-
- var subscribers = {
- console: ['request', 'log'],
- 'http://localhost/logs': ['log']
- };
-
- var settings = {
- monitor: {
- opsInterval: 200,
- subscribers: subscribers,
- requestsEvent: 'response',
- broadcastInterval: 5
- }
- };
-
- var monitor = new Helpers.Server(settings)._monitor;
-
- expect(monitor._subscriberQueues.console).to.exist;
- expect(monitor._eventQueues.request).to.exist;
- expect(monitor._eventQueues.log).to.exist;
- done();
- });
-
-
- describe('#_broadcast', function () {
-
- it('doesn\'t do anything if there are no subscribers', function (done) {
-
- var config = {
- settings: {
- monitor: {
- opsInterval: 200,
- subscribers: {},
- requestsEvent: 'response'
- }
- }
- };
-
- var monitor = new Helpers.Monitor(config);
- var broadcast = monitor._broadcast();
-
- expect(broadcast()).to.not.exist;
- done();
- });
-
- it('filters out events that don\'t contain the subscribers tag', function (done) {
-
- var subscribers = {
- 'console': { tags: ['ERROR', 'WARNING'], events: ['log'] }
- };
-
- var settings = {
- monitor: {
- opsInterval: 200,
- subscribers: subscribers,
- requestsEvent: 'response',
- broadcastInterval: 5
- }
- };
-
- var monitor = new Helpers.Server(settings)._monitor;
-
- expect(monitor._subscriberQueues.console).to.exist;
- expect(monitor._eventQueues.log).to.exist;
-
- var broadcast = monitor._broadcast();
-
- Helpers._TEST.removeAllListeners('log');
- Helpers._TEST.once('log', function (data) {
-
- expect(data).to.not.exist;
- });
- broadcast();
- Helpers._TEST.removeAllListeners('log');
- done();
- });
-
- it('shows events that the subscriber tags match', function (done) {
-
- var subscribers = {
- 'console': { tags: ['config'], events: ['log'] }
- };
-
- var settings = {
- monitor: {
- opsInterval: 200,
- subscribers: subscribers,
- requestsEvent: 'response',
- broadcastInterval: 5
- }
- };
-
- var server = new Helpers.Server(settings);
- var monitor = new Helpers.Monitor(server);
-
- expect(monitor._subscriberQueues.console).to.exist;
- expect(monitor._eventQueues.log).to.exist;
-
- var broadcast = monitor._broadcast();
-
- Helpers._TEST.removeAllListeners('log');
- Helpers._TEST.once('log', function (data) {
-
- expect(data).to.exist;
- expect(data).to.contain('Log monitoring enabled');
- done();
- });
- broadcast();
- });
-
- it('broadcasts all events when no tags are provided', function (done) {
-
- var subscribers = {
- 'console': ['log']
- };
-
- var settings = {
- monitor: {
- opsInterval: 200,
- subscribers: subscribers,
- requestsEvent: 'response',
- broadcastInterval: 5
- }
- };
-
- var monitor = new Helpers.Server(settings)._monitor;
-
- expect(monitor._subscriberQueues.console).to.exist;
- expect(monitor._eventQueues.log).to.exist;
-
- var broadcast = monitor._broadcast();
-
- Helpers._TEST.removeAllListeners('log');
- Helpers._TEST.once('log', function (data) {
-
- expect(data).to.exist;
- expect(data).to.contain('Log monitoring enabled');
- done();
- });
- broadcast();
- });
- });
-
-
- describe('#_ops', function () {
-
- it('sets the event with the result data correctly', function (done) {
-
- var results = {
- osload: 1,
- osmem: 20,
- osdisk: 30,
- osup: 50
- };
-
- var subscribers = {
- console: ['ops']
- };
-
- var settings = {
- monitor: {
- opsInterval: 10000,
- subscribers: subscribers,
- requestsEvent: 'response',
- broadcastInterval: 5
- }
- };
-
- var server = new Helpers.Server(settings);
- var monitor = server._monitor;
-
- expect(monitor._subscriberQueues.console).to.exist;
- expect(monitor._eventQueues.ops).to.exist;
-
- var event = monitor._ops()(results);
-
- expect(event.os.load).to.equal(1);
- expect(event.os.mem).to.equal(20);
- expect(event.os.disk).to.equal(30);
- done();
- });
- });
-
-
- describe('#_handle', function () {
-
- it('dispatches immediately when broadcastInterval is 0', function (done) {
-
- var results = {
- osload: 1,
- osmem: 20,
- osdisk: 30,
- osup: 50
- };
-
- var subscribers = {
- console: ['ops']
- };
-
- var settings = {
- monitor: {
- opsInterval: 10000,
- subscribers: subscribers,
- requestsEvent: 'response',
- broadcastInterval: 0
- }
- };
-
- var server = new Helpers.Server(settings);
- var monitor = server._monitor;
-
- monitor._broadcast = function () {
-
- return function () {
-
- done();
- };
- };
-
- var handler = monitor._handle('ops');
- handler(results);
- });
-
- it('throws an error when eventName is invalid', function (done) {
-
- var results = {
- osload: 1,
- osmem: 20,
- osdisk: 30,
- osup: 50
- };
-
- var subscribers = {
- console: ['ops']
- };
-
- var settings = {
- monitor: {
- opsInterval: 10000,
- subscribers: subscribers,
- requestsEvent: 'response',
- broadcastInterval: 0
- }
- };
-
- var server = new Helpers.Server(settings);
- var monitor = server._monitor;
-
- try {
- var handler = monitor._handle('notFound');
- }
- catch (error) {
- expect(error).to.exist;
- done();
- }
- });
- });
-
-
- describe('#_request', function () {
-
- it('sets the event with the request data correctly', function (done) {
-
- var subscribers = {
- console: ['ops']
- };
-
- var server = {
- settings: {
- monitor: {
- opsInterval: 100000,
- subscribers: subscribers,
- requestsEvent: 'response',
- broadcastInterval: 5
- }
- },
- on: function () { },
- emit: function () { }
- };
- var request = {
- raw: {
- req: {
- headers: {
- 'user-agent': 'test'
- }
- }
- },
- _analytics: {},
- server: server
- };
- var monitor = new Helpers.Monitor(server);
-
- expect(monitor._subscriberQueues.console).to.exist;
- expect(monitor._eventQueues.ops).to.exist;
-
- var requestFn = monitor._request();
- var event = requestFn(request);
-
- expect(event.event).to.equal('request');
- expect(event.source.userAgent).to.equal('test');
- done();
- });
-
- it('logs errors when they occur', function (done) {
-
- var subscribers = {
- console: ['ops']
- };
-
- var server = {
- settings: {
- monitor: {
- opsInterval: 100000,
- subscribers: subscribers,
- requestsEvent: 'response',
- broadcastInterval: 5,
- extendedRequests: true
- }
- },
- on: function () { },
- emit: function () { }
- };
- var request = {
- raw: {
- req: {
- headers: {
- 'user-agent': 'test'
- }
- }
- },
- _analytics: {},
- server: server,
- _log: 'test'
- };
-
- var monitor = new Helpers.Monitor(server);
- var event = monitor._request()(request);
-
- expect(event.event).to.equal('request');
- expect(event.source.userAgent).to.equal('test');
- expect(event.log).to.equal('test');
- done();
- });
- });
-
- describe('#_initOps', function () {
-
- it('emits an ops event when everything succeeds', function (done) {
-
- var subscribers = {
- console: ['ops']
- };
-
- var settings = {
- monitor: {
- opsInterval: 100,
- subscribers: subscribers,
- requestsEvent: 'response',
- broadcastInterval: 5
- }
- };
-
- var server = new Helpers.Server(settings);
-
- server._monitor._initOps();
- server.removeAllListeners('ops');
-
- server.once('ops', function (event) {
-
- expect(event.osdisk.total).to.equal(100);
- expect(event.osup).to.equal(1000);
- done();
- });
-
- server._monitor._os = {
- cpu: function (cb) {
-
- cb(null, 1);
- },
- disk: function (cb) {
-
- cb(null, { total: 100, free: 10 });
- },
- loadavg: function (cb) {
-
- cb();
- },
- mem: function (cb) {
-
- cb();
- },
- uptime: function (cb) {
-
- cb(null, 1000);
- }
- };
- server._monitor._process = {
- uptime: function (cb) {
-
- cb(null, 1000);
- },
- memory: function (cb) {
-
- cb();
- },
- cpu: function (cb) {
-
- cb();
- }
- };
- });
-
- it('logs errors when they occur', function (done) {
-
- var subscribers = {
- console: ['ops']
- };
-
- var settings = {
- monitor: {
- opsInterval: 100,
- subscribers: subscribers,
- requestsEvent: 'response',
- broadcastInterval: 5
- }
- };
-
- var server = new Helpers.Server(settings);
-
- server._monitor._initOps();
- server.removeAllListeners('ops');
-
- server._monitor._os = {
- cpu: function (cb) {
-
- cb(new Error(), 1);
- },
- disk: function (cb) {
-
- cb(null, { total: 100, free: 10 });
- },
- loadavg: function (cb) {
-
- cb();
- },
- mem: function (cb) {
-
- cb();
- },
- uptime: function (cb) {
-
- cb(null, 1000);
- }
- };
- server._monitor._process = {
- uptime: function (cb) {
-
- cb(null, 1000);
- },
- memory: function (cb) {
-
- cb();
- },
- cpu: function (cb) {
-
- cb();
- }
- };
-
- done();
- });
- });
-
- describe('#_display', function () {
-
- it('prints to the log event data for ops events', function (done) {
-
- var settings = {
- monitor: {
- opsInterval: 10000,
- subscribers: {
- console: ['ops']
- },
- requestsEvent: 'response',
- broadcastInterval: 5
- }
- };
-
- var server = new Helpers.Server(settings);
- var monitor = server._monitor;
-
- var data = {
- events: [{
- event: 'ops',
- proc: {
- mem: {
- rss: 1
- },
- cpu: 10
- }
- }]
- };
-
- Helpers._TEST.once('log', function (message) {
-
- expect(message).to.contain('memory');
- done();
- });
-
- monitor._display(data);
- });
-
- it('prints to the log event data for request events', function (done) {
-
- var settings = {
- monitor: {
- opsInterval: 10000,
- subscribers: {
- console: ['ops']
- },
- requestsEvent: 'response',
- broadcastInterval: 5
- }
- };
-
- var server = new Helpers.Server(settings);
- var monitor = server._monitor;
-
- var data = {
- events: [{
- event: 'request',
- instance: 'testInstance',
- method: 'testMethod'
- }]
- };
-
- Helpers._TEST.once('log', function (message) {
-
- expect(message).to.contain('testMethod');
- done();
- });
-
- monitor._display(data);
- });
- });
-
- describe('#_log', function () {
-
- it('returns wrapped events', function (done) {
-
- var settings = {
- monitor: {
- opsInterval: 10000,
- subscribers: {
- console: ['ops']
- },
- requestsEvent: 'response',
- broadcastInterval: 5
- }
- };
- var event = {};
-
- var server = new Helpers.Server(settings);
- var monitor = server._monitor;
-
- event = monitor._log()(event);
-
- expect(event.event).to.equal('log');
- done();
- });
- });
-});
View
47 test/monitor/process.js → test/process.js 100644 → 100755
@@ -3,7 +3,7 @@
var Chai = require('chai');
var ChildProcess = require('child_process');
var Sinon = require('sinon');
-var Helpers = require('../helpers');
+var ProcessMonitor = require('../lib/process');
// Declare internals
@@ -14,35 +14,34 @@ var internals = {};
// Test shortcuts
var expect = Chai.expect;
-var ProcessMonitor = Helpers.ProcessMonitor;
- describe('Process Monitor', function () {
+describe('Process Monitor', function () {
- it('throws an error when constructed without new', function (done) {
+ it('throws an error when constructed without new', function (done) {
- var fn = function () {
+ var fn = function () {
- ProcessMonitor.Monitor();
+ ProcessMonitor.Monitor();
};
expect(fn).throws(Error, 'ProcessMonitor must be instantiated using new');
- done();
+ done();
});
- describe('#cpu', function () {
+ describe('#cpu', function () {
- it('passes the current cpu usage to the callback', function (done) {
+ it('passes the current cpu usage to the callback', function (done) {
var monitor = new ProcessMonitor.Monitor();
- monitor.cpu(function (err, cpu) {
+ monitor.cpu(function (err, cpu) {
expect(err).not.to.exist;
expect(cpu).to.exist;
- done();
- });
+ done();
+ });
});
- it('passes any errors to the callback', function (done) {
+ it('passes any errors to the callback', function (done) {
var monitor = new ProcessMonitor.Monitor();
var args = 'ps -eo pcpu,pid | grep ' + process.pid + ' | awk \'{print $1}\'';
@@ -50,26 +49,26 @@ var ProcessMonitor = Helpers.ProcessMonitor;
var execStub = Sinon.stub(ChildProcess, 'exec');
execStub.withArgs(args).callsArgWith(1, new Error());
- monitor.cpu(function (err, cpu) {
+ monitor.cpu(function (err, cpu) {
expect(err).to.exist;
execStub.restore();
- done();
- });
- });
+ done();
+ });
+ });
});
- describe('#memory', function () {
+ describe('#memory', function () {
- it('passes the current memory usage to the callback', function (done) {
+ it('passes the current memory usage to the callback', function (done) {
var monitor = new ProcessMonitor.Monitor();
- monitor.memory(function (err, mem) {
+ monitor.memory(function (err, mem) {
expect(err).not.to.exist;
expect(mem).to.exist;
- done();
- });
- });
- });
+ done();
+ });
+ });
+ });
});
View
3  test/monitor/system.js → test/system.js 100644 → 100755
@@ -4,7 +4,7 @@ var Chai = require('chai');
var ChildProcess = require('child_process');
var Fs = require('fs');
var Sinon = require('sinon');
-var Helpers = require('../helpers');
+var SystemMonitor = require('../lib/system');
// Declare internals
@@ -15,7 +15,6 @@ var internals = {};
// Test shortcuts
var expect = Chai.expect;
-var SystemMonitor = Helpers.SystemMonitor;
describe('System Monitor', function () {
Please sign in to comment.
Something went wrong with that request. Please try again.