forked from statsd/statsd
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request statsd#57 from sanberg/master - thanks Steve!
- Loading branch information
Showing
5 changed files
with
173 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters