Skip to content

Commit 7469d75

Browse files
committed
Adding example for using monitr, process-watcher and mod_statuspage together
1 parent 37ed9cf commit 7469d75

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2013, Yahoo! Inc. All rights reserved.
3+
* Copyrights licensed under the New BSD License.
4+
* See the accompanying LICENSE file for terms.
5+
*/
6+
7+
// Reference: TODO put the blog link here
8+
9+
var cluster = require('cluster'),
10+
express = require('express'),
11+
http = require('http'),
12+
monitor = require('monitr'),
13+
status = require('mod_statuspage'),
14+
timers = require('timers');
15+
// Spawn numCPUs worker processes
16+
var numCPUs = require('os').cpus().length;
17+
18+
// Master spawns worker processes
19+
if (cluster.isMaster) {
20+
21+
// Fork workers
22+
for (var i = 0; i < numCPUs; i++) {
23+
console.log('Starting worker ' + i);
24+
cluster.fork();
25+
}
26+
27+
// If any worker process dies, spawn a new one to bring back number of processes
28+
// to be back to numCPUs
29+
cluster.on('exit', function(worker, code, signal) {
30+
console.log('worker ' + worker.process.pid + ' died');
31+
cluster.fork();
32+
});
33+
34+
// Logs to know what workers are active
35+
cluster.on('online', function (worker) {
36+
console.log('Worker ' + worker.process.pid + ' online');
37+
});
38+
39+
} else {
40+
// Worker process
41+
// Start monitoring
42+
monitor.start();
43+
44+
// Simple express app
45+
var app = express();
46+
// Server statuspage to get application metrics
47+
app.use(status({
48+
url: '/status',
49+
check: function(req) {
50+
if (req.something == false) {
51+
return false; //Don't show status
52+
}
53+
return true; //Show status
54+
},
55+
responseContentType : 'html'
56+
}));
57+
// An example of a request that makes CPU idle for some time
58+
app.get('/late-response', function(req, res) {
59+
var end = Date.now() + 5000
60+
// CPU cycles wasted for 5000ms
61+
// Blocking
62+
while (Date.now() < end) ;
63+
res.writeHead(200, {'Content-Type': 'text/plain'});
64+
res.end('I came after a delay\n');
65+
});
66+
// All other requests served normal
67+
app.get('*', function (req, res) {
68+
res.writeHead(200, {'Content-Type': 'text/plain'});
69+
res.end('I am being monitored\n');
70+
});
71+
72+
console.log('Go to: http://127.0.0.1:8000/status');
73+
app.listen(8000);
74+
75+
// Stop monitoring
76+
process.on('exit', function () {
77+
monitor.stop();
78+
});
79+
80+
// Graceful shutdown
81+
process.on('SIGINT', function () {
82+
process.exit();
83+
});
84+
} // end worker
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2013, Yahoo! Inc. All rights reserved.
3+
* Copyrights licensed under the New BSD License.
4+
* See the accompanying LICENSE file for terms.
5+
*/
6+
// Reference : Put the blog post here
7+
var watcher = require('process-watcher');
8+
9+
/*
10+
* Dummy metric monitoring object.
11+
*/
12+
var watcher_metric = {
13+
/**
14+
* Increments metric
15+
*/
16+
increment : function (name, v) {
17+
console.log('Increment ' + name + ' with ' + v);
18+
},
19+
/**
20+
* Set the metric or multiple metrics at the same time.
21+
*/
22+
set : function (name, v) {
23+
console.log('Setting the following metrics: ' + require('util').inspect(name));
24+
}
25+
};
26+
var dgpath = '/tmp/nodejs.mon',
27+
statusPath = '/tmp/watcher_status_path_test',
28+
watcher_config = { max_inactive : 0.001, monitor : 0.001, monPath: dgpath,
29+
timeout : 3, timeout_start : 60 };
30+
31+
//Instantiate watcher
32+
var watcher_instance = new watcher.Watcher({ metric : watcher_metric, config : watcher_config });

0 commit comments

Comments
 (0)