Skip to content

Commit

Permalink
linebuffer stats
Browse files Browse the repository at this point in the history
  • Loading branch information
leeliu committed Apr 1, 2016
1 parent 116818b commit 7e831d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/connection-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,26 @@ module.exports.connectLogServer = function (config, programName) {
};

module.exports.sendStats = function (config, socket) {
if (config.STATS_INTERVAL <= 0) {
return;
}

var stats = {
e: 's',
m: process.memoryUsage()
m: process.memoryUsage(),
b: config.bufferStats
};

if (config.STATS_INTERVAL <= 0) {
return;
if (stats.b.ts) {
stats.b.ms = Date.now() - stats.b.ts;
}

debug('sending stats:');
debug(stats);
return new Promise(resolve => {
if (socket.connected) {
socket.send(JSON.stringify(stats));
linebuffer.resetStats();
resolve();
}

Expand Down
11 changes: 11 additions & 0 deletions lib/linebuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ var dccount = 0;
module.exports.addMessage = function (message) {
if (buf.length > config.BUF_LIMIT) {
debug('buffer limit exceeded ' + buf.length);
config.bufferStats.buflimit++;
return;
}

if (message.l.length > 32000) {
debug('super long line ' + message.l.length);
config.bufferStats.longlines++;
message.l = message.l.substring(0, 32000) + ' (cut off, too long...)';
}

buf.push(JSON.stringify(message));
config.bufferStats.lines++;

// flush immediately if limit reached
if (buf.length === config.FLUSH_LIMIT) {
debug('flush limit reached, flushing...');
config.bufferStats.flushlimit++;
clearTimeout(flushtimeout);
exports.flush();
}
Expand All @@ -44,6 +48,8 @@ module.exports.flush = function () {
debug('sending data: ' + sendbuf[i].length);
socket.send(sendbuf[i]);
}
config.bufferStats.flushsize += sendbuf.length;
config.bufferStats.flushcount++;
sendbuf = null;

if (dccount > 0) {
Expand All @@ -57,6 +63,11 @@ module.exports.setSocket = function (sock) {

// kick off initial flush
if (!flushtimeout) {
exports.resetStats();
flushtimeout = setTimeout(exports.flush, config.FLUSH_INTERVAL);
}
};

module.exports.resetStats = function () {
config.bufferStats = { lines: 0, buflimit: 0, longlines: 0, flushlimit: 0, flushsize: 0, flushcount: 0, ts: Date.now() };
};

0 comments on commit 7e831d2

Please sign in to comment.