зеркало из https://github.com/microsoft/statsd.git
Merge pull request #57 from sanberg/master - thanks Steve!
This commit is contained in:
Коммит
eaaee7183c
|
@ -1,5 +1,5 @@
|
|||
var fs = require('fs')
|
||||
, sys = require('sys')
|
||||
, util = require('util')
|
||||
|
||||
var Configurator = function (file) {
|
||||
|
||||
|
@ -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; }
|
||||
|
@ -26,7 +26,7 @@ var Configurator = function (file) {
|
|||
});
|
||||
};
|
||||
|
||||
sys.inherits(Configurator, process.EventEmitter);
|
||||
util.inherits(Configurator, process.EventEmitter);
|
||||
|
||||
exports.Configurator = Configurator;
|
||||
|
||||
|
|
|
@ -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;
|
|
@ -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
stats.js
18
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')
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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, '_')
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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'),
|
||||
|
|
Загрузка…
Ссылка в новой задаче