| 1 | package Net::Statsite::Client; | ||
| 2 | use 5.008001; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '1.0.2'; | ||
| 7 | |||
| 8 | use IO::Socket; | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Net::Statsite::Client - Object-Oriented Client for L<statsite|http://armon.github.io/statsite> server | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use Net::Statsite::Client; | ||
| 18 | my $statsite = Net::Statsite::Client->new( | ||
| 19 | host => 'localhost', | ||
| 20 | prefix => 'test', | ||
| 21 | ); | ||
| 22 | |||
| 23 | $statsite->increment('item'); #increment key test.item | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | Net::Statsite::Client is based on L<Etsy::StatsD> but with new - C<new> interface and C<unique> method. | ||
| 28 | |||
| 29 | |||
| 30 | =head1 METHODS | ||
| 31 | |||
| 32 | =head2 new (host => $host, port => $port, sample_rate => $sample_rate, prefix => $prefix) | ||
| 33 | |||
| 34 | Create a new instance. | ||
| 35 | |||
| 36 | I<host> - hostname of statsite server (default: localhost) | ||
| 37 | |||
| 38 | I<port> - port of statsite server (port: 8125) | ||
| 39 | |||
| 40 | I<sample_rate> - rate of sends metrics (default: 1) | ||
| 41 | |||
| 42 | I<prefix> - prefix metric name (default: '') | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my ($class, %options) = @_; | ||
| 48 | $options{host} = 'localhost' unless defined $options{host}; | ||
| 49 | $options{port} = 8125 unless defined $options{port}; | ||
| 50 | $options{prefix} = '' unless defined $options{prefix}; | ||
| 51 | |||
| 52 | my $sock = new IO::Socket::INET( | ||
| 53 | PeerAddr => $options{host}, | ||
| 54 | PeerPort => $options{port}, | ||
| 55 | Proto => 'udp', | ||
| 56 | ) or croak "Failed to initialize socket: $!"; | ||
| 57 | |||
| 58 | bless { socket => $sock, sample_rate => $options{sample_rate}, prefix => $options{prefix} }, $class; | ||
| 59 | } | ||
| 60 | |||
| 61 | =head2 timing(STAT, TIME, SAMPLE_RATE) | ||
| 62 | |||
| 63 | Log timing information (should be in miliseconds) | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | sub timing { | ||
| 68 | my ($self, $stat, $time, $sample_rate) = @_; | ||
| 69 | $self->send({ $stat => "$time|ms" }, $sample_rate); | ||
| 70 | } | ||
| 71 | |||
| 72 | =head2 increment(STATS, SAMPLE_RATE) | ||
| 73 | |||
| 74 | Increment one of more stats counters. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub increment { | ||
| 79 | my ($self, $stats, $sample_rate) = @_; | ||
| 80 | $self->update($stats, 1, $sample_rate); | ||
| 81 | } | ||
| 82 | |||
| 83 | =head2 decrement(STATS, SAMPLE_RATE) | ||
| 84 | |||
| 85 | Decrement one of more stats counters. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub decrement { | ||
| 90 | my ($self, $stats, $sample_rate) = @_; | ||
| 91 | $self->update($stats, -1, $sample_rate); | ||
| 92 | } | ||
| 93 | |||
| 94 | =head2 update(STATS, DELTA, SAMPLE_RATE) | ||
| 95 | |||
| 96 | Update one of more stats counters by arbitrary amounts. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | sub update { | ||
| 101 | my ($self, $stats, $delta, $sample_rate) = @_; | ||
| 102 | $delta = 1 unless defined $delta; | ||
| 103 | my %data; | ||
| 104 | if (ref($stats) eq 'ARRAY') { | ||
| 105 | %data = map { $_ => "$delta|c" } @$stats; | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | %data = ($stats => "$delta|c"); | ||
| 109 | } | ||
| 110 | $self->send(\%data, $sample_rate); | ||
| 111 | } | ||
| 112 | |||
| 113 | =head2 unique(STATS, ITEM, SAMPLE_RATE) | ||
| 114 | |||
| 115 | Unique Set | ||
| 116 | |||
| 117 | For example if you need count of unique ip adresses (per flush interval) | ||
| 118 | $stats->unique('ip.unique', $ip); | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | sub unique { | ||
| 123 | my ($self, $stats, $item, $sample_rate) = @_; | ||
| 124 | my %data = ($stats => "$item|s"); | ||
| 125 | $self->send(\%data, $sample_rate); | ||
| 126 | } | ||
| 127 | |||
| 128 | =head2 gauge(STATS, VALUE, SAMPLE_RATE) | ||
| 129 | |||
| 130 | Gauge Set (Gauge, similar to kv but only the last value per key is retained) | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | sub gauge { | ||
| 135 | my ($self, $stats, $value, $sample_rate) = @_; | ||
| 136 | my %data = ($stats => "$value|g"); | ||
| 137 | $self->send(\%data, $sample_rate); | ||
| 138 | } | ||
| 139 | |||
| 140 | =head2 send(DATA, SAMPLE_RATE) | ||
| 141 | |||
| 142 | Sending logging data; implicitly called by most of the other methods. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub send { | ||
| 147 | my ($self, $data, $sample_rate) = @_; | ||
| 148 | $sample_rate = $self->{sample_rate} unless defined $sample_rate; | ||
| 149 | |||
| 150 | my $sampled_data; | ||
| 151 | if (defined($sample_rate) and $sample_rate < 1) { | ||
| 152 | while (my ($stat, $value) = each %$data) { | ||
| 153 | $sampled_data->{$stat} = "$value|\@$sample_rate" if rand() <= $sample_rate; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | else { | ||
| 157 | $sampled_data = $data; | ||
| 158 | } | ||
| 159 | |||
| 160 | return '0 but true' unless keys %$sampled_data; | ||
| 161 | |||
| 162 | #failures in any of this can be silently ignored | ||
| 163 | my $count = 0; | ||
| 164 | my $socket = $self->{socket}; | ||
| 165 | while (my ($stat, $value) = each %$sampled_data) { | ||
| 166 | |||
| 167 | my $key = $stat; | ||
| 168 | if ($$self{prefix}) { | ||
| 169 | $key = "$$self{ prefix }.$stat"; | ||
| 170 | } | ||
| 171 | |||
| 172 | #sanitize key (remove statsite separators) | ||
| 173 | #https://github.com/armon/statsite#protocol | ||
| 174 | $key =~ s/[:|]/_/g; | ||
| 175 | |||
| 176 | _send_to_sock($socket, "$key:$value\n"); | ||
| 177 | ++$count; | ||
| 178 | } | ||
| 179 | return $count; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub _send_to_sock( $$ ) { | ||
| 183 | my ($sock, $msg) = @_; | ||
| 184 | CORE::send($sock, $msg, 0); | ||
| 185 | } | ||
| 186 | |||
| 187 | =head1 LICENSE | ||
| 188 | |||
| 189 | Copyright (C) Avast Software. | ||
| 190 | |||
| 191 | This library is free software; you can redistribute it and/or modify | ||
| 192 | it under the same terms as Perl itself. | ||
| 193 | |||
| 194 | =head1 AUTHOR | ||
| 195 | |||
| 196 | Jan Seidl E<lt>seidl@avast.comE<gt> | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | 1; |