monitor-dog
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -12,5 +12,5 @@ 'use strict'; | ||
var pluck = require('101/pluck'); | ||
var SocketsMonitor = require('./sockets-monitor'); | ||
/** | ||
@@ -17,0 +17,0 @@ * @module monitor-dog:monitor |
'use strict'; | ||
var util = require('util'); | ||
var child = require('child_process'); | ||
var globalAgent = require('http').globalAgent; | ||
var exists = require('101/exists'); | ||
var IntervalMonitor = require('./interval-monitor'); | ||
/** | ||
* @module monitor-dog:sockets-monitor | ||
* @author Anand Kumar Patel | ||
* @author Anton Podviaznikov | ||
* @author Anand Kumar Patel, Anton Podviaznikov, Ryan Sandor Richards | ||
*/ | ||
@@ -27,25 +28,25 @@ module.exports = SocketsMonitor; | ||
function SocketsMonitor (monitor, prefix, interval) { | ||
this.monitor = monitor; | ||
this.prefix = prefix || 'socket'; | ||
this.interval = interval || this.monitor.interval; | ||
IntervalMonitor.call(this, monitor, prefix || 'socket', interval); | ||
} | ||
util.inherits(SocketsMonitor, IntervalMonitor); | ||
/** | ||
* Begin capturing socket stats. | ||
* Captures and reports socket stats. This method is executed from the context | ||
* of the collection interval and should not be explictly called. | ||
*/ | ||
SocketsMonitor.prototype.start = function () { | ||
if (exists(this.intervalId)) { return; } | ||
this.intervalId = setInterval( | ||
this._captureSocketStats.bind(this), | ||
this.interval | ||
); | ||
}; | ||
SocketsMonitor.prototype.run = function () { | ||
var sockets = globalAgent.sockets; | ||
var requests = globalAgent.requests; | ||
var pidTag = 'pid:' + process.pid; | ||
var tags = [pidTag]; | ||
/** | ||
* Stop capturing socket stats. | ||
*/ | ||
SocketsMonitor.prototype.stop = function () { | ||
if (!exists(this.intervalId)) { return; } | ||
clearInterval(this.intervalId); | ||
this.intervalId = null; | ||
this._gaugeCollections('open', sockets); | ||
this._gaugeCollections('pending', requests); | ||
var self = this; | ||
child.exec('lsof -p ' + process.pid + ' | wc -l', function (err, stdout) { | ||
if (err) { return; } | ||
var action = self.prefix + '.openFiles'; | ||
self.monitor.gauge(action, parseInt(stdout), 1, tags); | ||
}.bind(this)); | ||
}; | ||
@@ -66,21 +67,1 @@ | ||
}; | ||
/** | ||
* Captures and reports socket stats. This method is executed from the context | ||
* of the collection interval and should not be explictly called. | ||
*/ | ||
SocketsMonitor.prototype._captureSocketStats = function () { | ||
var sockets = globalAgent.sockets; | ||
var requests = globalAgent.requests; | ||
var pidTag = 'pid:' + process.pid; | ||
var tags = [pidTag]; | ||
this._gaugeCollections('open', sockets); | ||
this._gaugeCollections('pending', requests); | ||
child.exec('lsof -p ' + process.pid + ' | wc -l', function (err, stdout) { | ||
if (err) { return; } | ||
var action = this.prefix + '.openFiles'; | ||
this.monitor.gauge(action, parseInt(stdout), 1, tags); | ||
}.bind(this)); | ||
}; |
{ | ||
"name": "monitor-dog", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "A helpful wrapper for dogstatsd.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -39,4 +39,32 @@ # monitor-dog | ||
## Documentation | ||
## Interval Monitoring | ||
Creating specialized periodic interval monitors (similar to the sockets monitor) | ||
is fairly straight-forward. Here's an example of how it is done: | ||
```js | ||
var monitor = require('monitor-dog'); | ||
var IntervalMonitor = require('monitor-dog/lib/interval-monitor'); | ||
var util = require('util'); | ||
/** | ||
* 1. Subclass the IntervalMonitor class | ||
*/ | ||
function MyCustomMonitor = function(monitor, prefix, interval) { | ||
IntervalMonitor.call(this, monitor, prefix, interval); | ||
} | ||
util.inherits(MyCustomMonitor, IntervalMonitor); | ||
/** | ||
* 2. Define your run function to periodically monitor what you wish | ||
*/ | ||
MyCustomMonitor.prototype.run = function() { | ||
// ... Perform custom periodic reporting here using this.monitor | ||
} | ||
// 3. Instantiate and start your new interval monitor! | ||
(new MyCustomMonitor(monitor)).start(); | ||
``` | ||
## API Documentation | ||
### .set, .increment, .decrement, .histogram, .gauge | ||
@@ -90,7 +118,6 @@ | ||
### .startSocketsMonitor() | ||
### .startSocketsMonitor(), .stopSocketsMonitor() | ||
Automatically track number of open & pending sockets and open files. | ||
```js | ||
@@ -103,3 +130,2 @@ // start monitoring once you start web server | ||
monitor.stopSocketsMonitor(); | ||
``` | ||
@@ -112,10 +138,7 @@ | ||
```js | ||
monitor.captureStream('some-name', yourStream); | ||
``` | ||
## License | ||
MIT |
@@ -49,49 +49,4 @@ 'use strict'; | ||
}); | ||
it('should use given interval', function(done) { | ||
var socketMonitor = new SocketsMonitor({}, 'prefix', 1000); | ||
expect(socketMonitor.interval).to.equal(1000); | ||
done(); | ||
}); | ||
it('should use default interval if none given', function(done) { | ||
var socketMonitor = new SocketsMonitor({ interval: 120 }, 'prefix'); | ||
expect(socketMonitor.interval).to.equal(120); | ||
done(); | ||
}); | ||
}); | ||
it('should not start started monitor', function (done) { | ||
var custom = monitor.createMonitor({interval: 50, prefix: 'git'}); | ||
expect(custom.socketsMonitor.intervalId).to.not.exist(); | ||
custom.startSocketsMonitor(); | ||
var oldInterval = custom.socketsMonitor.intervalId; | ||
expect(oldInterval).to.exist(); | ||
custom.startSocketsMonitor(); | ||
expect(oldInterval).to.equal(custom.socketsMonitor.intervalId); | ||
done(); | ||
}); | ||
it('should stop monitor', function (done) { | ||
var custom = monitor.createMonitor({interval: 50, prefix: 'git'}); | ||
expect(custom.socketsMonitor.intervalId).to.not.exist(); | ||
custom.startSocketsMonitor(); | ||
expect(custom.socketsMonitor.intervalId).to.exist(); | ||
custom.stopSocketsMonitor(); | ||
expect(custom.socketsMonitor.intervalId).to.not.exist(); | ||
done(); | ||
}); | ||
it('should not stop stopped monitor', function (done) { | ||
var custom = monitor.createMonitor({interval: 50, prefix: 'git'}); | ||
expect(custom.socketsMonitor.intervalId).to.not.exist(); | ||
custom.startSocketsMonitor(); | ||
expect(custom.socketsMonitor.intervalId).to.exist(); | ||
custom.stopSocketsMonitor(); | ||
expect(custom.socketsMonitor.intervalId).to.not.exist(); | ||
custom.stopSocketsMonitor(); | ||
expect(custom.socketsMonitor.intervalId).to.not.exist(); | ||
done(); | ||
}); | ||
it('should start monitor and report open files data & sockets info', function (done) { | ||
@@ -98,0 +53,0 @@ http.globalAgent.sockets = [{id: 1}]; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38754
16
938
141