Skip to content

Commit 54e863d

Browse files
committed
Merge pull request #503 from CodeNow/monit
add socket monit back
2 parents cff0446 + 2919226 commit 54e863d

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var Boom = require('dat-middleware').Boom;
1111
var activeApi = require('models/redis/active-api');
1212
var mongooseControl = require('models/mongo/mongoose-control');
1313
var envIs = require('101/env-is');
14+
var dogstatsd = require('models/datadog');
1415

1516
if (process.env.NEWRELIC_KEY) {
1617
require('newrelic');
@@ -22,6 +23,9 @@ Api.prototype.start = function (cb) {
2223
debug('start');
2324
// start github ssh key generator
2425
keyGen.start();
26+
// start sending socket count
27+
dogstatsd.monitorStart();
28+
2529
var count = createCount(callback);
2630
// connect to mongoose
2731
mongooseControl.start(count.inc.next);
@@ -65,6 +69,8 @@ Api.prototype.stop = function (cb) {
6569
var count = createCount(cb);
6670
// stop github ssh key generator
6771
keyGen.stop();
72+
// stop sending socket count
73+
dogstatsd.monitorStart();
6874
// express server
6975
mongooseControl.stop(count.inc().next);
7076
events.close(count.inc().next);

configs/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ POLL_MONGO_TIMEOUT="30 minutes"
4444
GITHUB_SCOPE="user:email,repo,repo_deployment,read:repo_hook"
4545
GITHUB_HOOK_SECRET="3V3RYTHINGisAW3S0ME!"
4646
DOCKER_IMAGE_BUILDER_CACHE="/git-cache"
47+
MONITOR_INTERVAL="1 minute"

lib/express-app.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ if (envIs('production')) {
1717
'method':true,
1818
'tags': ['name:api', 'logType:express', 'env:'+process.env.NODE_ENV]
1919
}));
20-
21-
// app.use(dogstatsd.captureSocketCount);
2220
}
2321

2422
if (envIs('development', 'local', 'stage')) {

lib/models/datadog/index.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,41 @@ function captureSteamData (streamName, stream) {
2020
});
2121
}
2222

23-
24-
function captureSocketCount (req, res, next) {
23+
function captureSocketCount () {
2524
var sockets = require('http').globalAgent.sockets;
2625
var request = require('http').globalAgent.requests;
2726
var key;
2827

2928
for (key in sockets) {
30-
client.gauge('api.sockets_open', sockets[key].length, 1, ['target:'+key]);
29+
client.gauge('api.sockets_open', sockets[key].length, 1,
30+
['target:'+key, 'pid:'+process.pid]);
3131
}
3232

3333
for (key in request) {
34-
client.gauge('api.sockets_pending', request[key].length, 1, ['target:'+key]);
34+
client.gauge('api.sockets_pending', request[key].length, 1,
35+
['target:'+key, 'pid:'+process.pid]);
3536
}
3637

3738
exec('lsof -p ' + process.pid + ' | wc -l', function (err, stdout) {
3839
if (err) { return; }
3940
client.gauge('api.openFiles', parseInt(stdout), 1, ['pid:'+process.pid]);
4041
});
42+
}
43+
44+
var interval;
4145

42-
next();
46+
function monitorStart () {
47+
if (interval) { return; }
48+
interval = setInterval(captureSocketCount, process.env.MONITOR_INTERVAL);
49+
}
50+
51+
function monitorStop () {
52+
if (interval) {
53+
clearInterval(interval);
54+
interval = null;
55+
}
4356
}
4457

4558
module.exports.captureSteamData = captureSteamData;
46-
module.exports.captureSocketCount = captureSocketCount;
59+
module.exports.monitorStart = monitorStart;
60+
module.exports.monitorStop = monitorStop;

0 commit comments

Comments
 (0)