Statsd/Client.pm

filter: Statementstmt 85.4% 47 / 55 Branchbran 70.0% 7 / 10 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 80.9% 17 / 21 Podpod 100% 16 / 16 totaltotal 79.1% 91 / 115 SCAR 23.9
CC
10
Cov
79%
queue_stat
5.5
configure
3.0
default_rate
2.1
CRAP 10.9
« Statsd/Server.pm ↑ Summary Statsd.pm »
1 package Net::Async::Statsd::Client;
2 4 $Net::Async::Statsd::Client::VERSION = '0.004';
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Net::Async::Statsd::Client - asynchronous API for Etsy's statsd protocol
11  
12 =head1 VERSION
13  
14 Version 0.004
15  
16 =head1 SYNOPSIS
17  
18 use Future;
19 use IO::Async::Loop;
20 use Net::Async::Statsd::Client;
21 my $loop = IO::Async::Loop->new;
22 $loop->add(my $statsd = Net::Async::Statsd::Client->new(
23 host => 'localhost',
24 port => 3001,
25 ));
26 # Wait until the stats are written before proceeding
27 Future->needs_all(
28 $statsd->timing(
29 'some.task' => 133,
30 ),
31 $statsd->gauge(
32 'some.value' => 80,
33 )
34 )->get;
35 # Fire-and-forget stat, record 25% of the time:
36 $statsd->increment('startup', 0.25);
37  
38 =head1 DESCRIPTION
39  
40 Provides an asynchronous API for statsd.
41  
42 =head1 METHODS
43  
44 All public methods return a L<Future> indicating when the write has completed.
45 Since writes are UDP packets, there is no guarantee that the remote will
46 receive the value, so this is mostly intended as a way to detect when
47 statsd writes are slow.
48  
49 =cut
50  
51 =head2 timing
52  
53 Records timing information in milliseconds. Takes up to three parameters:
54  
55 =over 4
56  
57 =item * $k - the statsd key
58  
59 =item * $v - the elapsed time in milliseconds
60  
61 =item * $rate - optional sampling rate
62  
63 =back
64  
65 Only the integer part of the elapsed time will be sent.
66  
67 Example usage:
68  
69 $statsd->timing('some.key' => $ms, 0.1); # record this 10% of the time
70  
71 Returns a L<Future> which will be resolved when the write completes.
72  
73 =cut
74  
75 sub timing {
76 1 ▶ my ($self, $k, $v, $rate) = @_;
Subroutine timing: called CC=1 SCAR=0.0
Pod documented
77  
78 1 $self->queue_stat(
79 $k => int($v) . '|ms',
80 $rate
81 );
82 }
83  
84 =head2 gauge
85  
86 Records a current value. Takes up to three parameters:
87  
88 =over 4
89  
90 =item * $k - the statsd key
91  
92 =item * $v - the new value
93  
94 =item * $rate - optional sampling rate
95  
96 =back
97  
98 Only the integer value will be sent.
99  
100 Example usage:
101  
102 $statsd->timing('some.key' => 123);
103  
104 Returns a L<Future> which will be resolved when the write completes.
105  
106 =cut
107  
108 sub gauge {
109 1 ▶ my ($self, $k, $v, $rate) = @_;
Subroutine gauge: called CC=1 SCAR=0.0
Pod documented
110  
111 1 $self->queue_stat(
112 $k => int($v) . '|g',
113 $rate
114 );
115 }
116  
117 =head2 delta
118  
119 Records changed value. Takes up to three parameters:
120  
121 =over 4
122  
123 =item * $k - the statsd key
124  
125 =item * $v - the change (positive or negative)
126  
127 =item * $rate - optional sampling rate
128  
129 =back
130  
131 Values are truncated to integers.
132  
133 Example usage:
134  
135 $statsd->timing('some.key' => -12);
136  
137 Returns a L<Future> which will be resolved when the write completes.
138  
139 =cut
140  
141 sub delta {
142 1 ▶ my ($self, $k, $v, $rate) = @_;
Subroutine delta: called CC=1 SCAR=0.0
Pod documented
143  
144 1 $self->queue_stat(
145 $k => int($v) . '|c',
146 $rate
147 );
148 }
149  
150 =head2 count
151  
152 Alias for L</delta>.
153  
154 =cut
155  
156 # an alias for good measure
157 4 *count = *delta;
158  
159 =head2 increment
160  
161 Shortcut for L</delta> with a value of +1.
162  
163 =cut
164  
165 sub increment {
166 0 ▶ my ($self, $k, $rate) = @_;
Subroutine increment: not called CC=1 SCAR=6.9
Pod documented
167  
168 0 $self->queue_stat(
169 $k => '1|c',
170 $rate
171 );
172 }
173  
174 =head2 decrement
175  
176 Shortcut for L</delta> with a value of -1.
177  
178 =cut
179  
180 sub decrement {
181 0 ▶ my ($self, $k, $rate) = @_;
Subroutine decrement: not called CC=1 SCAR=6.9
Pod documented
182  
183 0 $self->queue_stat(
184 $k => '-1|c',
185 $rate
186 );
187 }
188  
189 =head2 configure
190  
191 Standard L<IO::Async::Notifier> configuration - called on construction or
192 manually when values need updating.
193  
194 Accepts the following named parameters:
195  
196 =over 4
197  
198 =item * host - the host we'll connect to
199  
200 =item * port - the UDP port to send messages to
201  
202 =item * default_rate - default sampling rate when none is provided for a given call
203  
204 =item * prefix - string to prepend to any stats we record
205  
206 =back
207  
208 =cut
209  
210 sub configure {
211 2 ▶ my ($self, %args) = @_;
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
212 2 for (qw(port host default_rate prefix)) {
213 8 ▶ $self->{$_} = delete $args{$_} if exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$_} 2 6 8
214 }
215 2 $self->SUPER::configure(%args);
216 }
217  
218 =head1 INTERNAL METHODS
219  
220 These methods are used internally, and are documented
221 for completeness. They may be of use when subclassing
222 this module.
223  
224 =cut
225  
226 =head2 queue_stat
227  
228 Queues a statistic for write.
229  
230 =cut
231  
232 sub queue_stat {
233 3 ▶ my ($self, $k, $v, $rate) = @_;
Subroutine queue_stat: called CC=5 SCAR=17.0
Pod documented
234  
235 3 ▶ $rate //= $self->default_rate;
Condition33%1/3
$rate //= $self->default_rate
l!l&&r!l&&!r
030
MC/DC0%0/2
$rate //= $self->default_rate
$rate $self->default_rate
Truth table33%1/3
A //= B
A= $rateB= $self->default_rate
ABresult
1X1
011
000
236 3 ▶ return Future->wrap unless $self->sample($rate);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sample($rate) 0 3 3
237  
238 3 ▶ $k = $self->{prefix} . '.' . $k if exists $self->{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"prefix"} 0 3 3
239  
240 # Append rate if we're only sampling part of the data
241 3 ▶ $v .= '|@' . $rate if $rate < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rate < 1 0 3 3
242 3 my $f;
243 $f = $self->statsd->then(sub {
244 # FIXME Someday IO::Async::Socket may support
245 # Futures for UDP send, update this if/when
246 # that happens.
247 3 ▶ shift->send("$k:$v");
Subroutine __ANON__: called CC=1 SCAR=0.0
248 3 Future->wrap
249 3 ▶ })->on_ready(sub { undef $f });
Subroutine __ANON__: called CC=1 SCAR=0.0
250 }
251  
252 =head2 sample
253  
254 Applies sampling based on the given rate - returns true if
255 we should record this, false otherwise.
256  
257 =cut
258  
259 sub sample {
260 60003 ▶ my ($self, $rate) = @_;
Subroutine sample: called CC=2 SCAR=6.9
Pod documented
261 60003 ▶ return 1 if rand() <= $rate;
Branch: 1 branch
BranchTrueFalseTotal
if rand() <= $rate 27667 32336 60003
262 32336 return 0;
263 }
264  
265 =head2 default_rate
266  
267 Default sampling rate. Currently 1 if not overidden in constructor or L</configure>.
268  
269 =cut
270  
271 3 ▶sub default_rate { shift->{default_rate} // 1 }
Subroutine default_rate: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"default_rate"} // 1
l!l
03
MC/DC0%0/1
+(shift())->{"default_rate"} // 1
+(shift())->{"default_rate"}
Truth table50%1/2
A
A= +(shift())->{"default_rate"}
Aresult
11
00
272  
273 =head2 port
274  
275 Statsd UDP port.
276  
277 =cut
278  
279 1 ▶sub port { shift->{port} }
Subroutine port: called CC=1 SCAR=0.0
Pod documented
280  
281 =head2 host
282  
283 Statsd host to connect to.
284 =cut
285  
286 1 ▶sub host { shift->{host} }
Subroutine host: called CC=1 SCAR=0.0
Pod documented
287  
288 sub statsd {
289 3 ▶ my ($self) = @_;
Subroutine statsd: called CC=2 SCAR=7.0
Pod documented
290 3 ▶ $self->{statsd} ||= do {
Condition66%2/3
$self->{"statsd"} ||= do { $self->connect }
l!l&&r!l&&!r
210
MC/DC0%0/2
$self->{"statsd"} ||= do { $self->connect }
$self->{"statsd"} do { $self->connect }
Truth table66%2/3
A ||= B
A= $self->{"statsd"}B= do { $self->connect }
ABresult
1X1
011
000
291 1 $self->connect
292 }
293 }
294  
295 =head2 connect
296  
297 Establishes the underlying UDP socket.
298  
299 =cut
300  
301 sub connect {
302 1 ▶ my ($self) = @_;
Subroutine connect: called CC=1 SCAR=0.0
Pod documented
303 # IO::Async::Loop
304 1 $self->loop->connect(
305 family => 'inet',
306 socktype => 'dgram',
307 service => $self->port,
308 host => $self->host,
309 on_socket => $self->curry::on_socket,
310 );
311 }
312  
313 =head2 on_socket
314  
315 Called when the socket is established.
316  
317 =cut
318  
319 sub on_socket {
320 1 ▶ my ($self, $sock) = @_;
Subroutine on_socket: called CC=1 SCAR=0.0
Pod documented
321 1 $self->debug_printf("UDP socket established: %s", $sock->write_handle->sockhost_service);
322 # FIXME Don't really want this - we're sending only, no bi-directional shenanigans
323 # required, might need to replace ->connect with an IO::Async::Socket for this?
324 1 $sock->configure(
325 on_recv => $self->curry::weak::on_recv,
326 on_recv_error => $self->curry::weak::on_recv_error,
327 );
328 1 $self->add_child($sock);
329 }
330  
331 =head2 on_recv
332  
333 Called if we receive data.
334  
335 =cut
336  
337 sub on_recv {
338 0 ▶ my ($self, undef, $dgram, $addr) = @_;
Subroutine on_recv: not called CC=1 SCAR=6.9
Pod documented
339 0 $self->debug_printf("UDP packet [%s] received from %s", $dgram, join ':', $self->loop->resolver->getnameinfo(
340 addr => $addr,
341 numeric => 1,
342 dgram => 1,
343 ));
344 }
345  
346 =head2 on_recv_error
347  
348 Called if we had an error while receiving.
349  
350 =cut
351  
352 sub on_recv_error {
353 0 ▶ my ($self, undef, $err) = @_;
Subroutine on_recv_error: not called CC=1 SCAR=6.9
Pod documented
354 0 $self->debug_printf("UDP packet receive error: %s", $err);
355 }
356  
357 4 1;
358  
359 __END__
« Statsd/Server.pm ↑ Summary Statsd.pm »