This commit is contained in:
Danny Coates 2013-06-17 17:49:33 -07:00
Родитель c7abb0c156
Коммит 380c4b4c1d
7 изменённых файлов: 143 добавлений и 75 удалений

Просмотреть файл

@ -7,11 +7,6 @@
const bunyan = require('bunyan');
const config = require('../lib/config').root();
const stats = require('../lib/stats');
const memoryMonitor = new (require('../lib/memory_monitor'))();
memoryMonitor.on('mem', stats.mem);
memoryMonitor.start();
const logStreams = [
{
@ -39,6 +34,17 @@ const log = bunyan.createLogger(
}
);
log.info(config, "starting config");
var statsBackend = config.stats.backend;
var statsConfig = config[statsBackend];
const stats = require('../lib/stats')(log)(statsBackend, statsConfig);
const memoryMonitor = new (require('../lib/memory_monitor'))();
memoryMonitor.on('mem', stats.mem.bind(stats));
memoryMonitor.start();
const routes = require('../routes');
const server = require('../server.js')(config, routes, log);

Просмотреть файл

@ -1,67 +0,0 @@
const os = require('os');
const Heka = require('heka');
//const Statsd = require('node-statsd').StatsD;
//TODO read config
const HOSTNAME = os.hostname();
const PID = process.pid;
var heka = Heka.clientFromJsonConfig(
JSON.stringify(
{
sender: {
factory: './senders:udpSenderFactory',
hosts: '127.0.0.1',
ports: 4880
},
logger: 'picl-idp',
severity: 5
}
)
);
// var statsd = new Statsd(
// {
// host: '127.0.0.1',
// port: 8125
// }
// );
module.exports = {
mem: function (usage) {
heka.heka(
'mem',
{
timestamp: new Date(),
severity: 6,
fields: {
rss: usage.rss,
heapTotal: usage.heapTotal,
heapUsed: usage.heapUsed
},
pid: PID,
hostname: HOSTNAME
}
);
},
request: function (event) {
heka.heka(
'request',
{
timestamp: new Date(event.timestamp),
severity: 6,
fields: {
statusCode: event.statusCode,
path: event.path,
responseTime: event.responseTime
},
pid: PID,
hostname: HOSTNAME
}
);
}
};

58
lib/stats/heka.js Normal file
Просмотреть файл

@ -0,0 +1,58 @@
const inherits = require('util').inherits;
const Heka = require('heka');
module.exports = function (Stats) {
function HekaStats(host, port) {
Stats.call(this);
this.client = Heka.clientFromJsonConfig(
JSON.stringify(
{
sender: {
factory: './senders:udpSenderFactory',
hosts: host,
ports: port
},
logger: 'picl-idp',
severity: 5
}
)
);
}
inherits(HekaStats, Stats);
HekaStats.prototype.mem = function (usage) {
this.client.heka(
'mem',
{
timestamp: new Date(),
severity: 6,
fields: {
rss: usage.rss,
heapTotal: usage.heapTotal,
heapUsed: usage.heapUsed
},
pid: this.pid,
hostname: this.hostname
}
);
};
HekaStats.prototype.request = function (event) {
this.client.heka(
'request',
{
timestamp: new Date(event.timestamp),
severity: 6,
fields: {
statusCode: event.statusCode,
path: event.path,
responseTime: event.responseTime
},
pid: this.pid,
hostname: this.hostname
}
);
};
return HekaStats;
};

21
lib/stats/index.js Normal file
Просмотреть файл

@ -0,0 +1,21 @@
module.exports = function (log) {
const Stats = require('./stats')(log);
const HekaStats = require('./heka')(Stats);
const StatsdStats = require('./statsd')(Stats);
return function create(type, options) {
var stats = null;
switch (type) {
case 'heka':
stats = new HekaStats(options.host, options.port);
break;
case 'statsd':
stats = new StatsdStats(options.host, options.port);
break;
default:
stats = new Stats();
break;
}
return stats;
};
};

25
lib/stats/stats.js Normal file
Просмотреть файл

@ -0,0 +1,25 @@
const os = require('os');
module.exports = function (log) {
function Stats() {
this.hostname = os.hostname();
this.pid = process.pid;
}
Stats.prototype.mem = function (usage) {
log.info(usage);
};
Stats.prototype.request = function (event) {
log.info(
{
code: event.statusCode,
path: event.path,
ms: event.responseTime
}
);
};
return Stats;
};

27
lib/stats/statsd.js Normal file
Просмотреть файл

@ -0,0 +1,27 @@
const inherits = require('util').inherits;
const Statsd = require('node-statsd').StatsD;
module.exports = function (Stats) {
function StatsdStats(host, port) {
Stats.call(this);
this.client = new Statsd(
{
host: host,
port: port
}
);
}
inherits(StatsdStats, Stats);
StatsdStats.prototype.mem = function (usage) {
this.client.gauge('rss', usage.rss);
this.client.gauge('heapTotal', usage.heapTotal);
this.client.gauge('heapUsed', usage.heapUsed);
};
StatsdStats.prototype.request = function () {
//TODO
};
return StatsdStats;
};

Просмотреть файл

@ -1,7 +1,5 @@
#!/usr/bin/env bash
NODE_ENV="local"
pid=`ps -aefw | grep "hekad" | grep -v " grep " | awk '{print $2}'`
if [[ $pid ]] ; then
echo "stopping heka"
@ -10,4 +8,4 @@ fi
echo "starting heka"
hekad -config=heka/hekad.toml &
./bin/idp.js
NODE_ENV="local" ./bin/idp.js