Skip to content

Commit

Permalink
Changed return structure and added support for first global metrics.
Browse files Browse the repository at this point in the history
  • Loading branch information
evangelion1204 committed Feb 17, 2016
1 parent 919386e commit d329ee7
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 13 deletions.
5 changes: 5 additions & 0 deletions examples/metrics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Basic example

This will demonstrate the basic usage of the RestService to check health and metrics.

Execute `npm run basic` from the root directory and open `http://localhost:8000` for the "webpage" and `http://localhost:8001/healthcheck` or `http://localhost:8001/metrics` for data from the RestServie.
14 changes: 14 additions & 0 deletions examples/metrics/pm2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"apps": [
{
"name": "status",
"script": "examples/metrics/status.js",
"instances": 1
},
{
"name": "server",
"script": "examples/metrics/server.js",
"instances": 1
}
]
}
16 changes: 16 additions & 0 deletions examples/metrics/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var http = require('http');
var MessageBus = require('../..').MessageBus;
var bus = new MessageBus();

var server = this._server = http.createServer(function (req, res) {
(function delayRequest(start) {
setTimeout(function () {
bus.publish('total_requests');
bus.publish('average_request_time', new Date() - start);
res.writeHead(200);
res.end('Ok');
}, Math.random() * 20 + 100);
})(new Date());
});

server.listen(8000);
13 changes: 13 additions & 0 deletions examples/metrics/status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var RestService = require('../..').RestService;
var Counter = require('../..').Metrics.Counter;
var Average = require('../..').Metrics.Average;
var instance = new RestService({
monitor_apps: ['server']
});

instance.registerMetric('total_requests', new Counter());
instance.registerMetric('average_request_time', new Average());

instance.connect().then(function () {
instance.listen(8001);
});
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
'use strict'

//exports.Starter = require('./lib/starter').default;
exports.RestService = require('./lib/rest').default;
exports.RestService = require('./lib/rest').default;
exports.MessageBus = require('./lib/bus').default;
exports.Metrics = {
Counter: require('./lib/metrics/counter').default,
Average: require('./lib/metrics/average').default
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"coverage": "babel-node ./node_modules/.bin/isparta cover ./node_modules/.bin/_mocha -- --recursive test",
"distribute": "npm publish",
"prebasic": "npm run compile",
"basic": "pm2 --no-daemon start examples/basic/pm2.json"
"premetrics": "npm run compile",
"basic": "pm2 --no-daemon start examples/basic/pm2.json",
"metrics": "pm2 --no-daemon start examples/metrics/pm2.json"
},
"keywords": [
"PM2",
Expand Down
23 changes: 23 additions & 0 deletions src/bus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const pm2 = require('pm2');

export default class MessageBus {
constructor () {
process.on('message', this.onMessage.bind(this));
}

onMessage(message) {
console.log(message);
}

publish(event, payload) {
process.send({
type: 'process:msg',
data: {
event,
payload
}
})
}
}


47 changes: 36 additions & 11 deletions src/rest.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
const pm2 = require('pm2');
const http = require('http');
const EventEmitter = require('events').EventEmitter;

const defaultOptions = {
monitor_apps: []
};

export default class RestInterface {
export default class RestInterface extends EventEmitter {
constructor (options = {}) {
super();
this.options = Object.assign({}, defaultOptions, options);
this.metrics = {};
}

connect() {
return new Promise(function (resolve, reject) {
pm2.connect(err => err ? reject(err) : resolve());
}.bind(this))

pm2.launchBus(function(err, bus) {
bus.on('process:msg', this.onMessage.bind(this));
}.bind(this));
}.bind(this));
}

onMessage(message) {
console.log(message);
this.emit(message.data.event, message.data.payload);
}

registerMetric(event, metric) {
console.log(`Registering metric on ${event}`);

this.on(event, metric.push.bind(metric));
this.metrics[event] = metric;
}

listen(port) {
this._server = http.createServer(this.onRequest.bind(this));

console.log(`Listening on port ${port}`)
console.log(`Listening on port ${port}`);

return this._server.listen(port);
}
Expand Down Expand Up @@ -47,16 +66,22 @@ export default class RestInterface {
case '/metrics':
pm2.list(function (err, list) {
if (!err) {
const processMetrics = list.filter(function (app) {
const processMetrics = {
processes: list.filter(function (app) {
return this.options.monitor_apps.length ? this.options.monitor_apps.indexOf(app.name) >= 0 : true;
}.bind(this)).map(function (app) {
return {
process: app.monit,
restarts: app.pm2_env.restart_time,
status: app.pm2_env.status,
name: app.name
}
}, false);
return {
process: app.monit,
restarts: app.pm2_env.restart_time,
status: app.pm2_env.status,
name: app.name
}
}, false)
};

Object.keys(this.metrics).forEach(function (metric) {
processMetrics[metric] = this.metrics[metric].serialize();
}.bind(this));

response.writeHead(200);
response.end(JSON.stringify(processMetrics));
Expand Down

0 comments on commit d329ee7

Please sign in to comment.