Skip to content

Commit

Permalink
Merge branch 'etsymaster' into p/debugupdates
Browse files Browse the repository at this point in the history
  • Loading branch information
david raistrick committed Jan 17, 2013
2 parents 9fef316 + 68fdd41 commit 4eb4b31
Show file tree
Hide file tree
Showing 6 changed files with 537 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StatsD [![Build Status](https://secure.travis-ci.org/etsy/statsd.png)](http://travis-ci.org/etsy/statsd)
StatsD [![Build Status](https://travis-ci.org/etsy/statsd.png?branch=backends-as-packages)](https://travis-ci.org/etsy/statsd)
======

A network daemon that runs on the [Node.js][node] platform and
Expand Down
22 changes: 13 additions & 9 deletions backends/graphite.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
*/

var net = require('net'),
util = require('util');
logger = require('../lib/logger');

// this will be instantiated to the logger
var l;

var debug;
var flushInterval;
Expand Down Expand Up @@ -46,12 +49,12 @@ var post_stats = function graphite_post_stats(statString) {
var graphite = net.createConnection(graphitePort, graphiteHost);
graphite.addListener('error', function(connectionException){
if (debug) {
util.log(connectionException);
l.log(connectionException);
}
});
graphite.on('connect', function() {
var ts = Math.round(new Date().getTime() / 1000);
var namespace = globalNamespace.concat('statsd');
var namespace = globalNamespace.concat(prefixStats);
statString += namespace.join(".") + '.graphiteStats.last_exception ' + last_exception + ' ' + ts + "\n";
statString += namespace.join(".") + '.graphiteStats.last_flush ' + last_flush + ' ' + ts + "\n";
this.write(statString);
Expand All @@ -60,7 +63,7 @@ var post_stats = function graphite_post_stats(statString) {
});
} catch(e){
if (debug) {
util.log(e);
l.log(e);
}
graphiteStats.last_exception = Math.round(new Date().getTime() / 1000);
}
Expand Down Expand Up @@ -122,12 +125,12 @@ var flush_stats = function graphite_flush(ts, metrics) {
numStats += 1;
}

var namespace = globalNamespace.concat('statsd');
var namespace = globalNamespace.concat(prefixStats);
if (legacyNamespace === true) {
statString += 'statsd.numStats ' + numStats + ts_suffix;
statString += 'stats.statsd.graphiteStats.calculationtime ' + (Date.now() - starttime) + ts_suffix;
statString += prefixStats + '.numStats ' + numStats + ts_suffix;
statString += 'stats.' + prefixStats + '.graphiteStats.calculationtime ' + (Date.now() - starttime) + ts_suffix;
for (key in statsd_metrics) {
statString += 'stats.statsd.' + key + ' ' + statsd_metrics[key] + ts_suffix;
statString += 'stats.' + prefixStats + '.' + key + ' ' + statsd_metrics[key] + ts_suffix;
}
} else {
statString += namespace.join(".") + '.numStats ' + numStats + ts_suffix;
Expand All @@ -145,12 +148,13 @@ var flush_stats = function graphite_flush(ts, metrics) {
};

var backend_status = function graphite_status(writeCb) {
for (stat in graphiteStats) {
for (var stat in graphiteStats) {
writeCb(null, 'graphite', stat, graphiteStats[stat]);
}
};

exports.init = function graphite_init(startup_time, config, events) {
l = new logger.Logger(config.log || {});
debug = config.debug;
graphiteHost = config.graphiteHost;
graphitePort = config.graphitePort;
Expand Down
8 changes: 4 additions & 4 deletions exampleConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Optional Variables:
percent: percentage of frequent keys to log [%, default: 100]
log: location of log file for frequent keys [default: STDOUT]
deleteCounters: don't send values to graphite for inactive counters, as opposed to sending 0 [default: false]
prefixStats: prefix to use for the statsd statistics data for this running instance of statsd [default: statsd]
applies to both legacy and new namespacing
console:
prettyprint: whether to prettyprint the console backend
Expand All @@ -59,14 +61,12 @@ Optional Variables:
e.g. [ { host: '10.10.10.10', port: 8125 },
{ host: 'observer', port: 88125 } ]
repeaterProtocol: whether to use udp4 or udp4 for repeaters.
repeaterProtocol: whether to use udp4 or udp6 for repeaters.
["udp4" or "udp6", default: "udp4"]
*/
{
graphitePort: 2003
, graphiteHost: "graphite.host.com"
, graphiteHost: "graphite.example.com"
, port: 8125
, backends: [ "./backends/graphite" ]
, repeater: [ { host: "10.8.3.214", port: 8125 } ]
, repeaterProtocol: "udp4"
}
42 changes: 25 additions & 17 deletions stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ var dgram = require('dgram')

// initialize data structures with defaults for statsd stats
var keyCounter = {};
var counters = {
"statsd.packets_received": 0,
"statsd.bad_lines_seen": 0
};
var counters = {};
var timers = {};
var gauges = {};
var sets = {};
Expand Down Expand Up @@ -61,7 +58,7 @@ function flushMetrics() {
backendEvents.once('flush', function clear_metrics(ts, metrics) {
// Clear the counters
conf.deleteCounters = conf.deleteCounters || false;
for (key in metrics.counters) {
for (var key in metrics.counters) {
if (conf.deleteCounters) {
delete(metrics.counters[key]);
} else {
Expand All @@ -70,12 +67,12 @@ function flushMetrics() {
}

// Clear the timers
for (key in metrics.timers) {
for (var key in metrics.timers) {
metrics.timers[key] = [];
}

// Clear the sets
for (key in metrics.sets) {
for (var key in metrics.sets) {
metrics.sets[key] = new set.Set();
}
});
Expand Down Expand Up @@ -117,17 +114,28 @@ config.configFile(process.argv[2], function (config, oldConfig) {
}, config.debugInterval || 10000);
}

// setup config for stats prefix
prefixStats = config.prefixStats;
prefixStats = prefixStats !== undefined ? prefixStats : "statsd";
//setup the names for the stats stored in counters{}
bad_lines_seen = prefixStats + ".bad_lines_seen";
packets_received = prefixStats + ".packets_received";

//now set to zero so we can increment them
counters[bad_lines_seen] = 0;
counters[packets_received] = 0;

if (server === undefined) {

// key counting
var keyFlushInterval = Number((config.keyFlush && config.keyFlush.interval) || 0);

server = dgram.createSocket('udp4', function (msg, rinfo) {
backendEvents.emit('packet', msg, rinfo);
counters["statsd.packets_received"]++;
counters[packets_received]++;
var metrics = msg.toString().split("\n");

for (midx in metrics) {
for (var midx in metrics) {
if (config.dumpMessages) {
l.log(metrics[midx].toString());
}
Expand All @@ -153,7 +161,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
var fields = bits[i].split("|");
if (fields[1] === undefined) {
l.log('Bad line: ' + fields + ' in msg "' + metrics[midx] +'"');
counters["statsd.bad_lines_seen"]++;
counters[bad_lines_seen]++;
stats['messages']['bad_lines_seen']++;
continue;
}
Expand All @@ -175,7 +183,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
sampleRate = Number(fields[2].match(/^@([\d\.]+)/)[1]);
} else {
l.log('Bad line: ' + fields + ' in msg "' + metrics[midx] +'"; has invalid sample rate');
counters["statsd.bad_lines_seen"]++;
counters[bad_lines_seen]++;
stats['messages']['bad_lines_seen']++;
continue;
}
Expand Down Expand Up @@ -227,8 +235,8 @@ config.configFile(process.argv[2], function (config, oldConfig) {
};

// Loop through the base stats
for (group in stats) {
for (metric in stats[group]) {
for (var group in stats) {
for (var metric in stats[group]) {
stat_writer(group, metric, stats[group][metric]);
}
}
Expand Down Expand Up @@ -265,23 +273,23 @@ config.configFile(process.argv[2], function (config, oldConfig) {
break;

case "delcounters":
for (index in cmdline) {
for (var index in cmdline) {
delete counters[cmdline[index]];
stream.write("deleted: " + cmdline[index] + "\n");
}
stream.write("END\n\n");
break;

case "deltimers":
for (index in cmdline) {
for (var index in cmdline) {
delete timers[cmdline[index]];
stream.write("deleted: " + cmdline[index] + "\n");
}
stream.write("END\n\n");
break;

case "delgauges":
for (index in cmdline) {
for (var index in cmdline) {
delete gauges[cmdline[index]];
stream.write("deleted: " + cmdline[index] + "\n");
}
Expand Down Expand Up @@ -333,7 +341,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
var key;
var sortedKeys = [];

for (key in keyCounter) {
for (var key in keyCounter) {
sortedKeys.push([key, keyCounter[key]]);
}

Expand Down
Loading

0 comments on commit 4eb4b31

Please sign in to comment.