Skip to content

Commit

Permalink
Merge pull request statsd#57 from sanberg/master - thanks Steve!
Browse files Browse the repository at this point in the history
  • Loading branch information
ickymettle committed Feb 11, 2012
2 parents 72ac121 + 89e6854 commit eaaee71
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 13 deletions.
6 changes: 3 additions & 3 deletions config.js
@@ -1,5 +1,5 @@
var fs = require('fs')
, sys = require('sys')
, util = require('util')

var Configurator = function (file) {

Expand All @@ -8,7 +8,7 @@ var Configurator = function (file) {
var oldConfig = {};

this.updateConfig = function () {
sys.log('reading config file: ' + file);
util.log('reading config file: ' + file);

fs.readFile(file, function (err, data) {
if (err) { throw err; }
Expand All @@ -26,7 +26,7 @@ var Configurator = function (file) {
});
};

sys.inherits(Configurator, process.EventEmitter);
util.inherits(Configurator, process.EventEmitter);

exports.Configurator = Configurator;

Expand Down
127 changes: 127 additions & 0 deletions examples/Etsy/StatsD.pm
@@ -0,0 +1,127 @@
package Etsy::StatsD;
use strict;
use warnings;
use IO::Socket;
use Carp;

=head1 NAME
Etsy::StatsD
=head1 DESCRIPTION
=cut

=over
=item new (HOST, PORT, SAMPLE_RATE)
Create a new instance.
=cut

sub new {
my ($class, $host, $port, $sample_rate) = @_;
$host = 'localhost' unless defined $host;
$port = 8125 unless defined $port;

my $sock = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'udp',
) or croak "Failed to initialize socket: $!";

bless {socket=>$sock, sample_rate=>$sample_rate}, $class;
}

=item timing(STAT, TIME, SAMPLE_RATE)
Log timing information
=cut

sub timing {
my ($self, $stat, $time, $sample_rate) = @_;
$self->send({$stat => "$time|ms"}, $sample_rate);
}

=item increment(STATS, SAMPLE_RATE)
Increment one of more stats counters.
=cut

sub increment {
my ($self, $stats, $sample_rate) = @_;
$self->update($stats, 1, $sample_rate);
}

=item increment(STATS, SAMPLE_RATE)
Decrement one of more stats counters.
=cut

sub decrement {
my ($self, $stats, $sample_rate) = @_;
$self->update($stats, -1, $sample_rate);
}

=item increment(STATS, DELTA, SAMPLE_RATE)
Update one of more stats counters by arbitrary amounts.
=cut

sub update {
my ($self, $stats, $delta, $sample_rate) = @_;
$delta = 1 unless defined $delta;
my %data;
if (ref($stats) eq 'ARRAY') {
%data = map {$_ => "$delta|c"} @$stats;
} else {
%data = ($stats => "$delta|c");
}
$self->send(\%data, $sample_rate);
}

=item send(DATA, SAMPLE_RATE)
Sending logging data; implicitly called by most of the other methods.
=back
=cut

sub send {
my ($self, $data, $sample_rate) = @_;
$sample_rate = $self->{sample_rate} unless defined $sample_rate;

my $sampled_data;
if ( defined($sample_rate) and $sample_rate < 1 ){
while (my($stat,$value) = each %$sampled_data) {
$sampled_data->{$stat} = "$value|\@$sample_rate" if rand() <= $sample_rate;
}
} else {
$sampled_data = $data;
}

return '0 but true' unless keys %$sampled_data;

#failures in any of this can be silently ignored
my $count=0;
my $socket = $self->{socket};
while (my($stat,$value) = each %$sampled_data) {
print $socket "$stat:$value\n";
++$count;
}
return $count;
}

=head1 AUTHOR
Steve Sanbeg L<http://www.buzzfeed.com/stv>
=cut

1;
33 changes: 33 additions & 0 deletions examples/perl-example.pl
@@ -0,0 +1,33 @@
#! /usr/bin/perl

# example perl code for Etsy StatsD
# Steve Sanbeg http://www.buzzfeed.com/stv
# host and port are passed in as command line options, default to
# localhost & 8125.

use strict;
use warnings;
use Getopt::Long;
use lib '.';
use Etsy::StatsD;

my %opt;

GetOptions(\%opt, 'host=s', 'port=s', 'sample=f', 'time=f', 'increment', 'decrement', 'update=i') or die;

my $bucket = shift or die "Need to provide a bucket";

my $statsd = Etsy::StatsD->new($opt{host}, $opt{port}, $opt{rate});
if ($opt{time}) {
$statsd->timing($bucket,$opt{time});
}
if ($opt{increment}) {
$statsd->increment($bucket);
}
if ($opt{update}) {
$statsd->update($bucket, $opt{update});
}
if ($opt{decrement}) {
$statsd->decrement($bucket);
}

18 changes: 9 additions & 9 deletions stats.js
@@ -1,5 +1,5 @@
var dgram = require('dgram')
, sys = require('sys')
, util = require('util')
, net = require('net')
, config = require('./config')
, fs = require('fs')
Expand Down Expand Up @@ -30,7 +30,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
if (config.debug) {
if (debugInt !== undefined) { clearInterval(debugInt); }
debugInt = setInterval(function () {
sys.log("Counters:\n" + sys.inspect(counters) + "\nTimers:\n" + sys.inspect(timers));
util.log("Counters:\n" + util.inspect(counters) + "\nTimers:\n" + util.inspect(timers));
}, config.debugInterval || 10000);
}

Expand All @@ -40,7 +40,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
var keyFlushInterval = Number((config.keyFlush && config.keyFlush.interval) || 0);

server = dgram.createSocket('udp4', function (msg, rinfo) {
if (config.dumpMessages) { sys.log(msg.toString()); }
if (config.dumpMessages) { util.log(msg.toString()); }
var bits = msg.toString().split(':');
var key = bits.shift()
.replace(/\s+/g, '_')
Expand All @@ -62,7 +62,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
var sampleRate = 1;
var fields = bits[i].split("|");
if (fields[1] === undefined) {
sys.log('Bad line: ' + fields);
util.log('Bad line: ' + fields);
stats['messages']['bad_lines_seen']++;
continue;
}
Expand Down Expand Up @@ -121,12 +121,12 @@ config.configFile(process.argv[2], function (config, oldConfig) {
break;

case "counters":
stream.write(sys.inspect(counters) + "\n");
stream.write(util.inspect(counters) + "\n");
stream.write("END\n\n");
break;

case "timers":
stream.write(sys.inspect(timers) + "\n");
stream.write(util.inspect(timers) + "\n");
stream.write("END\n\n");
break;

Expand Down Expand Up @@ -161,7 +161,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
server.bind(config.port || 8125, config.address || undefined);
mgmtServer.listen(config.mgmt_port || 8126, config.mgmt_address || undefined);

sys.log("server is up");
util.log("server is up");

var flushInterval = Number(config.flushInterval || 10000);

Expand Down Expand Up @@ -229,7 +229,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
var graphite = net.createConnection(config.graphitePort, config.graphiteHost);
graphite.addListener('error', function(connectionException){
if (config.debug) {
sys.log(connectionException);
util.log(connectionException);
}
});
graphite.on('connect', function() {
Expand All @@ -239,7 +239,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
});
} catch(e){
if (config.debug) {
sys.log(e);
util.log(e);
}
stats['graphite']['last_exception'] = Math.round(new Date().getTime() / 1000);
}
Expand Down
2 changes: 1 addition & 1 deletion test/graphite_tests.js
Expand Up @@ -2,7 +2,7 @@ var fs = require('fs'),
net = require('net'),
temp = require('temp'),
spawn = require('child_process').spawn,
sys = require('sys'),
util = require('util'),
urlparse = require('url').parse,
_ = require('underscore'),
dgram = require('dgram'),
Expand Down

0 comments on commit eaaee71

Please sign in to comment.