| 1 | package Etsy::StatsD; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use IO::Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = 1.002002; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # The CPAN verion at https://github.com/sanbeg/Etsy-Statsd should be kept in | ||||||||||||||||||||||||||||
| 10 | # sync with the version distributed with StatsD, at | ||||||||||||||||||||||||||||
| 11 | # https://github.com/etsy/statsd (in the exmaples directory), so you can get | ||||||||||||||||||||||||||||
| 12 | # it from either location. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Etsy::StatsD - Object-Oriented Client for Etsy's StatsD Server | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use Etsy::StatsD; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Increment a counter | ||||||||||||||||||||||||||||
| 23 | my $statsd = Etsy::StatsD->new(); | ||||||||||||||||||||||||||||
| 24 | $statsd->increment( 'app.method.success' ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Time something | ||||||||||||||||||||||||||||
| 28 | use Time::HiRes; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | my $start_time = time; | ||||||||||||||||||||||||||||
| 31 | $app->do_stuff; | ||||||||||||||||||||||||||||
| 32 | my $done_time = time; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Timers are expected in milliseconds | ||||||||||||||||||||||||||||
| 35 | $statsd->timing( 'app.method', ($done_time - $start_time) * 1000 ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Send to two StatsD Endpoints simultaneously | ||||||||||||||||||||||||||||
| 38 | my $repl_statsd = Etsy::StatsD->new(["statsd1","statsd2"]); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # On two different ports: | ||||||||||||||||||||||||||||
| 41 | my $repl_statsd = Etsy::StatsD->new(["statsd1","statsd1:8126"]); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Use TCP to a collector (you must specify a port) | ||||||||||||||||||||||||||||
| 44 | my $important_stats = Etsy::StatsD->new(["bizstats1:8125:tcp"]); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =over | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =item new (HOST, PORT, SAMPLE_RATE) | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Create a new instance. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =over | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =item HOST | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | If the argument is a string, it must be a hostname or IP only. The default is | ||||||||||||||||||||||||||||
| 63 | 'localhost'. The argument may also be an array reference of strings in the | ||||||||||||||||||||||||||||
| 64 | form of "<host>", "<host>:<port>", or "<host>:<port>:<proto>". If the port is | ||||||||||||||||||||||||||||
| 65 | not specified, the default port specified by the PORT argument will be used. | ||||||||||||||||||||||||||||
| 66 | If the protocol is not specified, or is not "tcp" or "udp", "udp" will be set. | ||||||||||||||||||||||||||||
| 67 | The only way to change the protocol, is to specify the host, port and protocol. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =item PORT | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Default is 8125. Will be used as the default port for any HOST argument not explicitly defining port. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =item SAMPLE_RATE | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Default is undefined, or no sampling performed. Specify a rate as a decimal between 0 and 1 to enable | ||||||||||||||||||||||||||||
| 76 | sampling. e.g. 0.5 for 50%. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =back | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 5 | my ( $class, $host, $port, $sample_rate ) = @_; | |||||||||||||||||||||||||||
| 84 | 5 | ▶ | $host = 'localhost' unless defined $host; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 5 | ▶ | $port = 8125 unless defined $port; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Handle multiple connections and | ||||||||||||||||||||||||||||
| 88 | # allow different ports to be specified | ||||||||||||||||||||||||||||
| 89 | # in the form of "<host>:<port>:<proto>" | ||||||||||||||||||||||||||||
| 90 | 5 | my %protos = map { $_ => 1 } qw(tcp udp); | |||||||||||||||||||||||||||
| 91 | 5 | my @connections = (); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 5 | ▶ | if( ref $host eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 2 | foreach my $addr ( @{ $host } ) { | |||||||||||||||||||||||||||
| 95 | 3 | my ($addr_host,$addr_port,$addr_proto) = split /:/, $addr; | |||||||||||||||||||||||||||
| 96 | 3 | ▶ | $addr_port ||= $port; | ||||||||||||||||||||||||||
|
Condition66%2/3
$addr_port ||= $port
MC/DC0%0/2
$addr_port ||= $port
$addr_port
$port
Truth table66%2/3
A ||= B
A= $addr_portB= $port
| |||||||||||||||||||||||||||||
| 97 | # Validate the protocol | ||||||||||||||||||||||||||||
| 98 | 3 | ▶ | if( defined $addr_proto ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 2 | $addr_proto = lc $addr_proto; # Normalize to lowercase | |||||||||||||||||||||||||||
| 100 | # Check validity | ||||||||||||||||||||||||||||
| 101 | 2 | ▶ | if( !exists $protos{$addr_proto} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 2 | croak sprintf("Invalid protocol '%s', valid: %s", $addr_proto, join(', ', sort keys %protos)); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||
| 106 | 1 | $addr_proto = 'udp'; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 1 | push @connections, [ $addr_host, $addr_port, $addr_proto ]; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | else { | ||||||||||||||||||||||||||||
| 112 | 3 | push @connections, [ $host, $port, 'udp' ]; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 3 | my @sockets = (); | |||||||||||||||||||||||||||
| 116 | 3 | foreach my $conn ( @connections ) { | |||||||||||||||||||||||||||
| 117 | 3 | ▶ | my $sock = new IO::Socket::INET( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | PeerAddr => $conn->[0], | ||||||||||||||||||||||||||||
| 119 | PeerPort => $conn->[1], | ||||||||||||||||||||||||||||
| 120 | Proto => $conn->[2], | ||||||||||||||||||||||||||||
| 121 | ) or carp "Failed to initialize socket: $!"; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 3 | ▶ | push @sockets, $sock if defined $sock; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | # Check that we have at least 1 socket to send to | ||||||||||||||||||||||||||||
| 126 | 3 | ▶ | croak "Failed to initialize any sockets." unless @sockets; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 3 | bless { sockets => \@sockets, sample_rate => $sample_rate }, $class; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item timing(STAT, TIME, SAMPLE_RATE) | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Log timing information | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 4 | ▶ | sub timing { | ||||||||||||||||||||||||||
|
Subroutine
timing: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 4 | my ( $self, $stat, $time, $sample_rate ) = @_; | |||||||||||||||||||||||||||
| 139 | 4 | $self->send( { $stat => "$time|ms" }, $sample_rate ); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =item increment(STATS, SAMPLE_RATE) | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Increment one of more stats counters. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 1 | ▶ | sub increment { | ||||||||||||||||||||||||||
|
Subroutine
increment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 1 | my ( $self, $stats, $sample_rate ) = @_; | |||||||||||||||||||||||||||
| 150 | 1 | $self->update( $stats, 1, $sample_rate ); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =item decrement(STATS, SAMPLE_RATE) | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Decrement one of more stats counters. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | ▶ | sub decrement { | ||||||||||||||||||||||||||
|
Subroutine
decrement: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 1 | my ( $self, $stats, $sample_rate ) = @_; | |||||||||||||||||||||||||||
| 161 | 1 | $self->update( $stats, -1, $sample_rate ); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item update(STATS, DELTA, SAMPLE_RATE) | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Update one of more stats counters by arbitrary amounts. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 5 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 5 | my ( $self, $stats, $delta, $sample_rate ) = @_; | |||||||||||||||||||||||||||
| 172 | 5 | ▶ | $delta = 1 unless defined $delta; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 5 | my %data; | |||||||||||||||||||||||||||
| 174 | 5 | ▶ | if ( ref($stats) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 1 | %data = map { $_ => "$delta|c" } @$stats; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | else { | ||||||||||||||||||||||||||||
| 178 | 4 | %data = ( $stats => "$delta|c" ); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 5 | $self->send( \%data, $sample_rate ); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =item send(DATA, SAMPLE_RATE) | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Sending logging data; implicitly called by most of the other methods. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =back | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 3 | ▶ | sub send { | ||||||||||||||||||||||||||
|
Subroutine
send: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 192 | 3 | my ( $self, $data, $sample_rate ) = @_; | |||||||||||||||||||||||||||
| 193 | 3 | ▶ | $sample_rate = $self->{sample_rate} unless defined $sample_rate; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 3 | my $sampled_data; | |||||||||||||||||||||||||||
| 196 | 3 | ▶ | if ( defined($sample_rate) and $sample_rate < 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $sample_rate and $sample_rate < 1
MC/DC100%2/2
defined $sample_rate and $sample_rate < 1
defined $sample_rate
$sample_rate < 1
Truth table100%3/3
A and B
A= defined $sample_rateB= $sample_rate < 1
| |||||||||||||||||||||||||||||
| 197 | 1 | ▶ | while ( my ( $stat, $value ) = each %$data ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 1 | ▶ | $sampled_data->{$stat} = "$value|\@$sample_rate" if rand() <= $sample_rate; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | else { | ||||||||||||||||||||||||||||
| 202 | 2 | $sampled_data = $data; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 3 | ▶ | return '0 but true' unless keys %$sampled_data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | #failures in any of this can be silently ignored | ||||||||||||||||||||||||||||
| 208 | 2 | my $count = 0; | |||||||||||||||||||||||||||
| 209 | 2 | foreach my $socket ( @{ $self->{sockets} } ) { | |||||||||||||||||||||||||||
| 210 | # calling keys() resets the each() iterator | ||||||||||||||||||||||||||||
| 211 | 2 | keys %$sampled_data; | |||||||||||||||||||||||||||
| 212 | 2 | ▶ | while ( my ( $stat,$value ) = each %$sampled_data ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 2 | _send_to_sock($socket, "$stat:$value\n", 0); | |||||||||||||||||||||||||||
| 214 | 2 | ++$count; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | 2 | return $count; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 0 | ▶ | sub _send_to_sock( $$ ) { | ||||||||||||||||||||||||||
|
Subroutine
_send_to_sock: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 221 | 0 | my ($sock,$msg) = @_; | |||||||||||||||||||||||||||
| 222 | 0 | CORE::send( $sock, $msg, 0 ); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | L<http://codeascraft.etsy.com/2011/02/15/measure-anything-measure-everything/> | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | Steve Sanbeg L<http://www.buzzfeed.com/stv> | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | Same as perl. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =cut | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 2 | 1; | |||||||||||||||||||||||||||