blib/lib/Net/Dogstatsd.pm

filter: Statementstmt 98.6% 142 / 144 Branchbran 96.7% 89 / 92 Conditioncond 90.9% 40 / 44 MC/DCmcdc 86.2% 25 / 29 Subroutinesub 95.2% 20 / 21 Podpod 100% 9 / 9 totaltotal 95.8% 325 / 339 CC 70 SCAR 21.8
CC
70
Cov
97%
_send_metric
15.0
_counter
12.1
_error_checks
12.0
CRAP 8.8
↑ Summary
1 package Net::Dogstatsd;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Data::Validate::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 =head1 NAME
14  
15 Net::Dogstatsd - Perl client to Datadog's dogstatsd metrics collector.
16  
17  
18 =head1 VERSION
19  
20 Version 1.0.3
21  
22 =cut
23  
24 12 our $VERSION = '1.0.3';
25  
26  
27 =head1 SYNOPSIS
28  
29 This module allows you to send multiple types of metrics to the Datadog service
30 via dogstatsd, a local daemon installed by Datadog agent package.
31  
32 use Net::Dogstatsd;
33  
34 # Create an object to communicate with Dogstatsd, using default server and
35 # port settings.
36 my $dogstatsd = Net::Dogstatsd->new(
37 host => 'localhost', #optional. Default = 127.0.0.1
38 port => '8125', #optional. Default = 8125
39 verbose => 0, #optional. Default = 0
40 );
41  
42 # Set, and print, the 'verbose' option value.
43 $dogstatsd->verbose(1);
44 print "In verbose mode." if $dogstatsd->verbose();
45  
46 # Before sending metrics, we have to get or create a socket to dogstatsd
47 my $socket = $dogstatsd->get_socket();
48  
49 # Counter metrics can be incremented or decremented
50 # By default, they will be incremented or decremented by 1, unless the
51 # optional 'value' parameter is passed
52 $dogstatsd->increment(
53 name => 'test_metric.sample_counter',
54 value => $increment_value, #optional; default = 1
55 tags => [ 'env:production', db ], #optional
56 );
57  
58 $dogstatsd->decrement(
59 name => $metric_name,
60 value => $decrement_value, #optional; default = 1
61 tags => [ 'env:devel', web ], #optional
62 );
63  
64  
65 # Gauge metrics can be used for capturing value of something over time
66 # Example: Gas gauge, inventory level, free memory
67 $dogstatsd->gauge(
68 name => 'test_metric.inventory_level',
69 value => $gauge_value, #required - must be a number
70 tags => [ 'warehouse:us' ], #optional
71 );
72  
73  
74 # Histogram metrics measure the statistical distribution of a set of values.
75 # Provides min/max/avg as well as 75th, 85th, 95th and 99th percentiles.
76 # NOTE: do not use this for timers. Use timer() instead.
77 $dogstatsd->histogram(
78 name => $metric_name,
79 value => $value,
80 tags => [ 'tag1', 'tag2:value', 'tag3' ], #optional
81 );
82  
83  
84 # Timers are a special type of histogram.
85 $dogstatsd->timer(
86 name => $metric_name,
87 value => $metric_value,
88 unit => $metric_unit, # 'ms' (milliseconds) or 's|sec' (seconds)
89 tags => [ 'tag1', 'tag2:value', 'tag3' ], #optional
90 );
91  
92  
93 # Set metrics are special counters that can track unique elements in a group.
94 # Example: the number of unique visitors currently on a website
95 $dogstatsd->sets(
96 name => 'unique.site_visitors',
97 value => $account_id,
98 tags => [ 'referer:Google' ], #optional
99 );
100  
101 =cut
102  
103 =head1 MAIN
104  
105 =cut
106  
107 # Used to build the UDP datagram
108 12 my $METRIC_TYPES =
109 {
110 'counter' => 'c',
111 'gauge' => 'g',
112 'histogram' => 'h',
113 'timer' => 'ms',
114 'sets' => 's',
115 };
116  
117 =head1 METHODS
118  
119 =head2 new()
120  
121 Create a new Net::Dogstatsd object that will be used to interact with dogstatsd.
122  
123 use Net::Dogstatsd;
124  
125 my $dogstatsd = Net::Dogstatsd->new(
126 host => 'localhost', #optional. Default = 127.0.0.1
127 port => '8125', #optional. Default = 8125
128 verbose => 1, #optional. Default = 0
129 );
130  
131 =cut
132  
133 sub new
134 12 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod documented
135 12 my ( $class, %args ) = @_;
136  
137 # Defaults
138 12 ▶ my $host = defined( $args{'host'} ) ? $args{'host'} : '127.0.0.1';
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'host'} ? : 1 11 12
139 12 ▶ my $port = defined( $args{'port'} ) ? $args{'port'} : '8125';
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'port'} ? : 1 11 12
140 12 ▶ my $verbose = defined( $args{'verbose'} ) ? $args{'verbose'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'verbose'} ? : 1 11 12
141  
142 12 my $self = {
143 host => $host,
144 port => $port,
145 verbose => $verbose,
146 };
147  
148 12 bless( $self, $class );
149  
150 12 return $self;
151 }
152  
153  
154 =head2 verbose()
155  
156 Get or set the 'verbose' property.
157  
158 my $verbose = $dogstatsd->verbose();
159 $dogstatsd->verbose( 1 );
160  
161 =cut
162  
163 sub verbose
164 316 ▶{
Subroutine verbose: called CC=3 SCAR=11.0
Pod documented
165 316 my ( $self, $value ) = @_;
166  
167 316 ▶ if ( defined $value && $value =~ /^[01]$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value =~ /^[01]$/) { } 1 315 316
Condition100%3/3
defined $value and $value =~ /^[01]$/
!ll&&!rl&&r
31411
MC/DC100%2/2
defined $value and $value =~ /^[01]$/
defined $value $value =~ /^[01]$/
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /^[01]$/
ABresult
0X0
100
111
168 {
169 1 $self->{'verbose'} = $value;
170 }
171 else
172 {
173 315 return $self->{'verbose'};
174 }
175  
176 1 return;
177 }
178  
179  
180 =head2 get_socket()
181  
182 Create a new socket, if one does not already exist.
183  
184 my $socket = $dogstatsd->get_socket();
185  
186 =cut
187  
188 sub get_socket
189 58 ▶{
Subroutine get_socket: called CC=2 SCAR=7.1
Pod documented
190 58 my ( $self ) = @_;
191 58 my $verbose = $self->verbose();
192  
193 58 ▶ if ( !defined $self->{'socket'} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"socket"}) 8 50 58
194 {
195 try
196 8 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.6
197 $self->{'socket'} = IO::Socket::INET->new(
198 PeerAddr => $self->{'host'},
199 PeerPort => $self->{'port'},
200 Proto => 'udp'
201 )
202 8 ▶ || die 'Could not open UDP connection to' . $self->{'host'} . ':' . $self->{'port'} . "\n";
Condition50%1/2
'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $self->{'port'}, 'Proto', 'udp') || die('Could not open UDP connection to' . $self->{'host'} . ':' . $self->{'port'} . "\n")
l!l
80
MC/DC0%0/1
'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $self->{'port'}, 'Proto', 'udp') || die('Could not open UDP connection to' . $self->{'host'} . ':' . $self->{'port'} . "\n")
'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $self->{'port'}, 'Proto', 'udp')
Truth table50%1/2
A
A= 'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $self->{'port'}, 'Proto', 'udp')
Aresult
11
00
203  
204 }
205 catch
206 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 #TODO how to reach this to test it?
208 0 croak( "Could not open connection to metrics server. Error: >$_<" );
209 8 };
210 }
211  
212 58 return $self->{'socket'};
213 }
214  
215  
216  
217 =head2 increment()
218  
219 Increment a counter metric. Include optional 'value' argument to increment by >1.
220 Include optional arrayref of tags/tag-values.
221  
222 $dogstatsd->increment(
223 name => $metric_name,
224 value => $increment_value, #optional; default = 1
225 );
226  
227 $dogstatsd->increment(
228 name => $metric_name,
229 value => $increment_value, #optional; default = 1
230 tags => [ tag1, tag2:value, tag3 ],
231 );
232  
233 =cut
234  
235 sub increment
236 23 ▶{
Subroutine increment: called CC=1 SCAR=0.0
Pod documented
237 23 my ( $self, %args ) = @_;
238  
239 23 $self->_counter( action => 'increment', %args );
240 9 return;
241 }
242  
243  
244 =head2 decrement()
245  
246 Decrement a counter metric. Include optional 'value' argument to decrement by >1.
247 Include optional arrayref of tags/tag-values.
248  
249 $dogstatsd->decrement(
250 name => $metric_name,
251 value => $decrement_value, #optional; default = 1
252 );
253  
254 $dogstatsd->decrement(
255 name => $metric_name,
256 value => $decrement_value, #optional; default = 1
257 tags => [ tag1, tag2:value, tag3 ],
258 );
259  
260 =cut
261  
262 sub decrement
263 22 ▶{
Subroutine decrement: called CC=1 SCAR=0.0
Pod documented
264 22 my ( $self, %args ) = @_;
265  
266 22 $self->_counter( action => 'decrement', %args );
267 9 return;
268 }
269  
270  
271 =head2 gauge()
272  
273 Send a 'gauge' metric. ex: gas gauge value, inventory stock level
274 Value must be positive number.
275 Include optional arrayref of tags/tag-values.
276  
277 $dogstatsd->gauge(
278 name => $metric_name,
279 value => $gauge_value,
280 );
281  
282 $dogstatsd->gauge(
283 name => $metric_name,
284 value => $gauge_value,
285 tags => [ 'tag1', 'tag2:value', 'tag3' ],
286 );
287  
288 =cut
289  
290 sub gauge
291 21 ▶{
Subroutine gauge: called CC=7 SCAR=19.5
Pod documented
292 21 my ( $self, %args ) = @_;
293 21 my $verbose = $self->verbose();
294  
295 # Check for mandatory parameters
296 21 foreach my $arg ( qw( name value ) )
297 {
298 croak "Argument '$arg' is a required argument"
299 41 ▶ if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{$arg} or $args{$arg} eq "" 3 38 41
Condition100%3/3
not defined $args{$arg} or $args{$arg} eq ""
l!l&&r!l&&!r
2138
MC/DC100%2/2
not defined $args{$arg} or $args{$arg} eq ""
not defined $args{$arg} $args{$arg} eq ""
Truth table100%3/3
not(A) or B
A= not defined $args{$arg}B= $args{$arg} eq ""
ABresult
1X1
011
000
300 }
301  
302 # Check that value is a number
303 croak "Value >$args{'value'}< is not a number, which is required for gauge()"
304 18 ▶ unless Data::Validate::Type::is_number( $args{'value'} );
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_number($args{"value"}) 1 17 18
305  
306 # Error checks common to all metric types
307 17 $self->_error_checks( %args );
308  
309 $self->_send_metric(
310 type => 'gauge',
311 value => $args{'value'},
312 name => $args{'name'},
313 tags => defined $args{'tags'} ? $args{'tags'} : [],
314 8 ▶ sample_rate => defined $args{'sample_rate'} ? $args{'sample_rate'} : 1,
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'tags'} ? : 3 5 8
defined $args{'sample_rate'} ? : 1 7 8
315 );
316  
317 8 return;
318 }
319  
320  
321 =head2 histogram()
322  
323 Send a 'histogram' metric. Provides min/max/avg as well as 75th, 85th, 95th and
324 99th percentiles.
325 NOTE: do not use this for timers. Use timer() instead.
326 Include optional arrayref of tags/tag-values.
327  
328 $dogstatsd->histogram(
329 name => $metric_name,
330 value => $value,
331 );
332  
333 $dogstatsd->histogram(
334 name => $metric_name,
335 value => $value,
336 tags => [ 'tag1', 'tag2:value', 'tag3' ],
337 );
338  
339 =cut
340  
341 sub histogram
342 21 ▶{
Subroutine histogram: called CC=7 SCAR=19.5
Pod documented
343 21 my ( $self, %args ) = @_;
344 21 my $verbose = $self->verbose();
345  
346 # Check for mandatory parameters
347 21 foreach my $arg ( qw( name value ) )
348 {
349 croak "Argument '$arg' is a required argument"
350 41 ▶ if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{$arg} or $args{$arg} eq "" 3 38 41
Condition100%3/3
not defined $args{$arg} or $args{$arg} eq ""
l!l&&r!l&&!r
2138
MC/DC100%2/2
not defined $args{$arg} or $args{$arg} eq ""
not defined $args{$arg} $args{$arg} eq ""
Truth table100%3/3
not(A) or B
A= not defined $args{$arg}B= $args{$arg} eq ""
ABresult
1X1
011
000
351 }
352  
353 # Check that value is a number
354 croak "Value >$args{'value'}< is not a number, which is required for histogram()"
355 18 ▶ unless Data::Validate::Type::is_number( $args{'value'} );
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_number($args{"value"}) 1 17 18
356  
357 # Error checks common to all metric types
358 17 $self->_error_checks( %args );
359  
360 $self->_send_metric(
361 type => 'histogram',
362 value => $args{'value'},
363 name => $args{'name'},
364 tags => defined $args{'tags'} ? $args{'tags'} : [],
365 8 ▶ sample_rate => defined $args{'sample_rate'} ? $args{'sample_rate'} : 1,
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'tags'} ? : 3 5 8
defined $args{'sample_rate'} ? : 1 7 8
366 );
367  
368 8 return;
369 }
370  
371  
372 =head2 timer()
373  
374 Send a 'timer' metric. Provides min/max/avg as well as 75th, 85th, 95th
375 and 99th percentiles.
376 Ex: time to run a database query.
377 Include optional arrayref of tags/tag-values.
378  
379 $dogstatsd->timer(
380 name => $metric_name,
381 value => $metric_value,
382 unit => $metric_unit, # 'ms' (milliseconds) or 's|sec' (seconds)
383 );
384  
385 $dogstatsd->timer(
386 name => $metric_name,
387 value => $metric_value,
388 unit => $metric_unit, # 'ms' (milliseconds) or 's|sec' (seconds)
389 tags => [ 'tag1', 'tag2:value', 'tag3' ],
390 );
391  
392 =cut
393  
394 sub timer
395 25 ▶{
Subroutine timer: called CC=10 SCAR=23.0
Pod documented
396 25 my ( $self, %args ) = @_;
397 25 my $verbose = $self->verbose();
398  
399 # Check for mandatory parameters
400 25 foreach my $arg ( qw( name value unit ) )
401 {
402 croak "Argument '$arg' is a required argument for timer()"
403 71 ▶ if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{$arg} or $args{$arg} eq "" 4 67 71
Condition100%3/3
not defined $args{$arg} or $args{$arg} eq ""
l!l&&r!l&&!r
3167
MC/DC100%2/2
not defined $args{$arg} or $args{$arg} eq ""
not defined $args{$arg} $args{$arg} eq ""
Truth table100%3/3
not(A) or B
A= not defined $args{$arg}B= $args{$arg} eq ""
ABresult
1X1
011
000
404 }
405  
406 # Check that value is a positive number
407 croak "Value >$args{'value'}< is not a positive number, which is required for timer()"
408 21 ▶ unless Data::Validate::Type::is_number( $args{'value'}, positive => 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_number($args{"value"}, "positive", 1) 1 20 21
409  
410 # Check that unit is one of the accepted values
411 20 ▶ unless ( $args{'unit'} =~ m/^(s|sec|ms)$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"unit"} =~ /^(s|sec|ms)$/) 1 19 20
412 {
413 1 croak "Argument 'unit' has invalid value >" . $args{'unit'} . "<. Allowed values are 's', 'sec' or 'ms'";
414 }
415  
416 19 ▶ if ( $args{'unit'} eq 's' || $args{'unit'} eq 'sec' )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"unit"} eq "s" or $args{"unit"} eq "sec") 18 1 19
Condition100%3/3
$args{"unit"} eq "s" or $args{"unit"} eq "sec"
l!l&&r!l&&!r
1171
MC/DC100%2/2
$args{"unit"} eq "s" or $args{"unit"} eq "sec"
$args{"unit"} eq "s" $args{"unit"} eq "sec"
Truth table100%3/3
A or B
A= $args{"unit"} eq "s"B= $args{"unit"} eq "sec"
ABresult
1X1
011
000
417 {
418 # Convert to milliseconds
419 18 $args{'value'} *= 1000;
420 18 $args{'value'} = sprintf( "%.2f", $args{'value'} ); #for things that run in microseconds
421 }
422  
423 # Error checks common to all metric types
424 19 $self->_error_checks( %args );
425  
426 $self->_send_metric(
427 type => 'timer',
428 value => $args{'value'},
429 name => $args{'name'},
430 tags => defined $args{'tags'} ? $args{'tags'} : [],
431 10 ▶ sample_rate => defined $args{'sample_rate'} ? $args{'sample_rate'} : 1,
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'tags'} ? : 3 7 10
defined $args{'sample_rate'} ? : 1 9 10
432 );
433  
434 10 return;
435 }
436  
437  
438 =head2 sets()
439  
440 Send a 'sets' metric. Used to count the number of unique elements in a group.
441 Ex: unique site visitors.
442 Include optional arrayref of tags/tag-values.
443  
444 $dogstatsd->sets(
445 name => 'unique.site_visitors',
446 value => $account_id,
447 );
448  
449 $dogstatsd->sets(
450 name => 'unique.site_visitors',
451 value => $account_id,
452 tags => [ 'tag1', 'tag2:value', 'tag3' ],
453 );
454  
455 =cut
456  
457 sub sets
458 20 ▶{
Subroutine sets: called CC=6 SCAR=17.9
Pod documented
459 20 my ( $self, %args ) = @_;
460 20 my $verbose = $self->verbose();
461  
462 # Check for mandatory parameters
463 20 foreach my $arg ( qw( name value ) )
464 {
465 croak "Argument '$arg' is a required argument"
466 39 ▶ if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{$arg} or $args{$arg} eq "" 3 36 39
Condition100%3/3
not defined $args{$arg} or $args{$arg} eq ""
l!l&&r!l&&!r
2136
MC/DC100%2/2
not defined $args{$arg} or $args{$arg} eq ""
not defined $args{$arg} $args{$arg} eq ""
Truth table100%3/3
not(A) or B
A= not defined $args{$arg}B= $args{$arg} eq ""
ABresult
1X1
011
000
467 }
468  
469 # Error checks common to all metric types
470 17 $self->_error_checks( %args );
471  
472 $self->_send_metric(
473 type => 'sets',
474 value => $args{'value'},
475 name => $args{'name'},
476 tags => defined $args{'tags'} ? $args{'tags'} : [],
477 8 ▶ sample_rate => defined $args{'sample_rate'} ? $args{'sample_rate'} : 1,
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'tags'} ? : 3 5 8
defined $args{'sample_rate'} ? : 1 7 8
478 );
479  
480 8 return;
481 }
482  
483  
484 =head1 INTERNAL FUNCTIONS
485  
486 =head2 _counter
487  
488 $self->_counter(
489 action => [ increment | decrement ],
490 %args
491 );
492  
493 =cut
494  
495 sub _counter
496 47 ▶{
Subroutine _counter: called CC=12 SCAR=24.9
497 47 my ( $self, %args ) = @_;
498  
499 47 my $action = delete( $args{'action'} );
500 47 my $multipliers = {
501 'increment' => 1,
502 'decrement' => -1,
503 };
504  
505 47 ▶ croak "Error - invalid action >$action<" unless exists( $multipliers->{ $action } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $multipliers->{$action} 1 46 47
506  
507 46 my $multiplier = $multipliers->{ $action };
508  
509 # Check for mandatory parameters
510 46 foreach my $arg ( qw( name ) )
511 {
512 croak "Argument '$arg' is a required argument"
513 46 ▶ if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{$arg} or $args{$arg} eq "" 2 44 46
Condition66%2/3
not defined $args{$arg} or $args{$arg} eq ""
l!l&&r!l&&!r
2044
MC/DC50%1/2
not defined $args{$arg} or $args{$arg} eq ""
not defined $args{$arg} $args{$arg} eq ""
Truth table66%2/3
not(A) or B
A= not defined $args{$arg}B= $args{$arg} eq ""
ABresult
1X1
011
000
514 }
515  
516 # Check that value, if provided, is a positive integer
517 44 ▶ if ( defined( $args{'value'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"value"}) 10 34 44
518 {
519 croak "Value >$args{'value'}< is not a positive integer, which is required for decrement()"
520 10 ▶ if ( $args{'value'} !~ /^\d+$/ || $args{'value'} <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if not $args{"value"} =~ /^\d+$/ or $args{"value"} <= 0 8 2 10
Condition66%2/3
not $args{"value"} =~ /^\d+$/ or $args{"value"} <= 0
l!l&&r!l&&!r
802
MC/DC50%1/2
not $args{"value"} =~ /^\d+$/ or $args{"value"} <= 0
not $args{"value"} =~ /^\d+$/ $args{"value"} <= 0
Truth table66%2/3
not(A) or B
A= not $args{"value"} =~ /^\d+$/B= $args{"value"} <= 0
ABresult
1X1
011
000
521 }
522  
523 # Error checks common to all metric types
524 36 $self->_error_checks( %args );
525  
526 $self->_send_metric(
527 type => 'counter',
528 name => $args{'name'},
529 value =>
530 ( defined $args{'value'} && $args{'value'} ne '' )
531 ? ( $args{'value'} * $multiplier )
532 : $multiplier,
533 tags => defined $args{'tags'} ? $args{'tags'} : [],
534 18 ▶ sample_rate => defined $args{'sample_rate'} ? $args{'sample_rate'} : 1,
Branch: 3 branches
BranchTrueFalseTotal
defined $args{'value'} && $args{'value'} ne '' ? : 2 16 18
defined $args{'tags'} ? : 6 12 18
defined $args{'sample_rate'} ? : 2 16 18
Condition66%2/3
defined $args{"value"} and $args{"value"} ne ""
!ll&&!rl&&r
1602
MC/DC50%1/2
defined $args{"value"} and $args{"value"} ne ""
defined $args{"value"} $args{"value"} ne ""
Truth table66%2/3
A and B
A= defined $args{"value"}B= $args{"value"} ne ""
ABresult
0X0
100
111
535 );
536  
537 18 return;
538 }
539  
540  
541 =head2 _error_checks()
542  
543 $self->_error_checks( %args );
544  
545 Common error checking for all metric types.
546  
547 =cut
548  
549 sub _error_checks
550 106 ▶{
Subroutine _error_checks: called CC=12 SCAR=24.9
551 106 my ( $self, %args ) = @_;
552 106 my $verbose = $self->verbose();
553  
554 # Metric name starts with a letter
555 106 ▶ if ( $args{'name'} !~ /^[a-zA-Z]/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"name"} =~ /^[a-zA-Z]/) 6 100 106
556 {
557 6 croak( 'ERROR - Invalid metric name >' . $args{'name'} . '<. Names must start with a letter, a-z. Not sending.' );
558 }
559  
560 # Tags, if exist...
561 100 ▶ if ( defined( $args{'tags'} ) && scalar( @{ $args{'tags'} } ) != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"tags"} and scalar @{$args{"tags"};} != 0) 30 64 94
Condition100%3/3
defined $args{"tags"} and scalar @{$args{"tags"};} != 0
!ll&&!rl&&r
58636
MC/DC100%2/2
defined $args{"tags"} and scalar @{$args{"tags"};} != 0
defined $args{"tags"} scalar @{$args{"tags"};} != 0
Truth table100%3/3
A and B
A= defined $args{"tags"}B= scalar @{$args{"tags"};} != 0
ABresult
0X0
100
111
562 {
563 30 ▶ if ( !Data::Validate::Type::is_arrayref( $args{'tags'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (Data::Validate::Type::is_arrayref($args{"tags"})) 0 30 30
564 {
565 0 croak 'ERROR - Tag list is invalid. Must be an arrayref.';
566 }
567  
568 30 foreach my $tag ( @{ $args{'tags'} } )
569 {
570 # Must start with a letter
571 36 ▶ croak( 'ERROR - Invalid tag >' . $tag . '< on metric >' . $args{'name'} . '<. Tags must start with a letter, a-z. Not sending.' )
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /^[a-zA-Z]/ 6 30 36
572 if ( $tag !~ /^[a-zA-Z]/ );
573  
574 # Must be < 200 characters [ discovered this limitation while testing. Datadog stated it should truncate, but I received various errors ]
575 30 ▶ croak( 'ERROR - Invalid tag >' . $tag . '< on metric >' . $args{'name'} . '<. Tags must be 200 chars or less. Not sending.' )
Branch: 1 branch
BranchTrueFalseTotal
if length $tag > 200 6 24 30
576 if ( length( $tag ) > 200 );
577  
578 # NOTE: This check isn't required by Datadog, they will allow this through.
579 # However, this tag will not behave as expected in the graphs, if we were to allow it.
580 24 ▶ croak( 'ERROR - Invalid tag >' . $tag . '< on metric >' . $args{'name'} . '<. Tags should only contain a single colon (:). Not sending.' )
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /^\S+:\S+:/ 6 18 24
581 if ( $tag =~ /^\S+:\S+:/ );
582 }
583 }
584  
585 # Check that optional 'sample_rate' argument is valid ( 1, or a float between 0 and 1 )
586 76 ▶ if ( defined $args{'sample_rate'} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"sample_rate"}) 30 46 76
587 {
588 30 ▶ if ( !Data::Validate::Type::is_number( $args{'sample_rate'} , strictly_positive => 1 ) || $args{'sample_rate'} > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (not Data::Validate::Type::is_number($args{"sample_rate"}, "strictly_positive", 1) or $args{"sample_rate"} > 1) 24 6 30
Condition100%3/3
not Data::Validate::Type::is_number($args{"sample_rate"}, "strictly_positive", 1) or $args{"sample_rate"} > 1
l!l&&r!l&&!r
1866
MC/DC100%2/2
not Data::Validate::Type::is_number($args{"sample_rate"}, "strictly_positive", 1) or $args{"sample_rate"} > 1
not Data::Validate::Type::is_number($args{"sample_rate"}, "strictly_positive", 1) $args{"sample_rate"} > 1
Truth table100%3/3
not(A) or B
A= not Data::Validate::Type::is_number($args{"sample_rate"}, "strictly_positive", 1)B= $args{"sample_rate"} > 1
ABresult
1X1
011
000
589 {
590 24 croak 'ERROR - Invalid sample rate >' . $args{'sample_rate'} . '<. Must be 1, or a float between 0 and 1.';
591 }
592 }
593  
594 52 return;
595 }
596  
597  
598 =head2 _send_metric()
599  
600 Send metric to stats server.
601  
602 =cut
603  
604 sub _send_metric
605 62 ▶{
Subroutine _send_metric: called CC=15 SCAR=27.1
606 62 my ( $self, %args ) = @_;
607 62 my $verbose = $self->verbose();
608  
609 # Check for mandatory parameters
610 62 foreach my $arg ( qw( name type value ) )
611 {
612 croak "Argument '$arg' is a required argument"
613 181 ▶ if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{$arg} or $args{$arg} eq "" 5 176 181
Condition100%3/3
not defined $args{$arg} or $args{$arg} eq ""
l!l&&r!l&&!r
41176
MC/DC100%2/2
not defined $args{$arg} or $args{$arg} eq ""
not defined $args{$arg} $args{$arg} eq ""
Truth table100%3/3
not(A) or B
A= not defined $args{$arg}B= $args{$arg} eq ""
ABresult
1X1
011
000
614 }
615  
616 57 my $original_name = $args{'name'};
617 # Metric name should only contain alphanumeric, "_", ".". Convert anything else to underscore and warn about substitution
618 # NOTE: Datadog will do this for you anyway, but won't warn you what the actual metric name will become.
619 57 $args{'name'} =~ s/[^a-zA-Z0-9_\.]/_/;
620  
621 #TODO change to Log::Any output
622 carp( 'WARNING: converted metric name from >$original_name< to >', $args{'name'}, '<. Names should only contain: a-z, 0-9, underscores, and dots/periods.' )
623 57 ▶ if $args{'name'} ne $original_name;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"name"} ne $original_name 18 39 57
624  
625 # Default sample rate = 1
626 57 ▶ if( !defined( $args{'sample_rate'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"sample_rate"}) 4 53 57
627 {
628 4 $args{'sample_rate'} = 1;
629 }
630  
631 57 my $socket = $self->get_socket();
632 57 ▶ return unless defined $socket;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $socket 0 57 57
633  
634 # Datagram format. More info at http://docs.datadoghq.com/guides/dogstatsd/
635 # dashboard.metricname:value|type|@sample_rate|#tag1:value,tag2
636 57 my $metric_string = $args{'name'} . ':' . $args{'value'} . '|' . $METRIC_TYPES->{ $args{'type'} } . '|@' . $args{'sample_rate'} ;
637  
638 57 ▶ if ( defined $args{'tags'} && scalar ( @{ $args{'tags'} } ) != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"tags"} and scalar @{$args{"tags"};} != 0) 14 42 56
Condition100%3/3
defined $args{"tags"} and scalar @{$args{"tags"};} != 0
!ll&&!rl&&r
14214
MC/DC100%2/2
defined $args{"tags"} and scalar @{$args{"tags"};} != 0
defined $args{"tags"} scalar @{$args{"tags"};} != 0
Truth table100%3/3
A and B
A= defined $args{"tags"}B= scalar @{$args{"tags"};} != 0
ABresult
0X0
100
111
639 {
640 14 foreach my $tag ( @{ $args{'tags'} } )
641 {
642 20 my $original_tag = $tag;
643  
644 20 $tag =~ s/\s+$//; # Strip trailing whitespace
645 # Tags should only contain alphanumeric, "_", "-",".", "/", ":". Convert anything else to underscore and warn about substitution
646 20 $tag =~ s/[^a-zA-Z0-9_\-\.\/:]/_/g;
647 20 $tag =~ s/\s+/_/g; # Replace remaining whitespace with underscore
648 20 ▶ carp( "WARNING: converted tag from >$original_tag< to >$tag<. Tags should only contain: a-z, 0-9, underscores, dashes, dots/periods, forward slashes, colons." )
Branch: 1 branch
BranchTrueFalseTotal
if $tag ne $original_tag 6 14 20
649 if $tag ne $original_tag;
650 }
651 14 $metric_string .= '|#' . join( ',', @{ $args{'tags'} } );
652 }
653  
654 # Force to all lower case because Datadog has case sensitive tags and metric
655 # names. We don't want to end up with multiple case variations of the same
656 # metric name/tag
657 56 $metric_string = lc( $metric_string );
658  
659 56 ▶ carp( "\nbuilt metric string >$metric_string<" ) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 56 56
660  
661 # Use of rand() is how the Ruby and Python clients implement sampling, so we will too.
662 56 ▶ if ( $args{'sample_rate'} == 1 || ( rand() < $args{'sample_rate'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"sample_rate"} == 1 or rand() < $args{"sample_rate"}) 52 4 56
Condition100%3/3
$args{"sample_rate"} == 1 or rand() < $args{"sample_rate"}
l!l&&r!l&&!r
4934
MC/DC100%2/2
$args{"sample_rate"} == 1 or rand() < $args{"sample_rate"}
$args{"sample_rate"} == 1 rand() < $args{"sample_rate"}
Truth table100%3/3
A or B
A= $args{"sample_rate"} == 1B= rand() < $args{"sample_rate"}
ABresult
1X1
011
000
663 {
664 52 my $response = IO::Socket::send( $socket, $metric_string, 0 );
665 52 ▶ unless (defined $response)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response) 24 28 52
666 {
667 24 carp( "error sending metric [string >$metric_string<]: $!" );
668 }
669 }
670  
671 56 return;
672 }
673  
674  
675 =head1 RUNNING TESTS
676  
677 By default, only basic tests that do not require a connection to Datadog's
678 platform are run in t/.
679  
680 To run the developer tests, you will need to do the following:
681  
682 =over 4
683  
684 =item * Sign up to become a Datadog customer ( if you are not already), at
685 L<https://app.datadoghq.com/signup>. Free trial accounts are available.
686  
687 =item * Install and configure Datadog agent software (requires python 2.6)
688 L<https://app.datadoghq.com/account/settings#agent>
689  
690 =back
691  
692  
693 =head1 AUTHOR
694  
695 Jennifer Pinkham, C<< <jpinkham at cpan.org> >>.
696  
697  
698 =head1 BUGS
699  
700 Please report any bugs or feature requests to the GitHub Issue Tracker at
701 L<https://github.com/jpinkham/net-dogstatsd/issues>.
702 I will be notified, and then you'll automatically be notified of progress on
703 your bug as I make changes.
704  
705  
706 =head1 SUPPORT
707  
708 You can find documentation for this module with the perldoc command.
709  
710 perldoc Net::Dogstatsd
711  
712  
713 You can also look for information at:
714  
715 =over 4
716  
717 =item * Bugs: GitHub Issue Tracker
718  
719 L<https://github.com/jpinkham/net-dogstatsd/issues>
720  
721 =item * AnnoCPAN: Annotated CPAN documentation
722  
723 L<http://annocpan.org/dist/Net-Dogstatsd>
724  
725 =item * CPAN Ratings
726  
727 L<http://cpanratings.perl.org/d/Net-Dogstatsd>
728  
729 =item * MetaCPAN
730  
731 L<https://metacpan.org/release/Net-Dogstatsd>
732  
733 =back
734  
735  
736 =head1 ACKNOWLEDGEMENTS
737  
738 I originally developed this project for ThinkGeek (L<http://www.thinkgeek.com/>).
739 Thanks for allowing me to open-source it!
740  
741 =head1 COPYRIGHT & LICENSE
742  
743 Copyright 2015 Jennifer Pinkham.
744  
745 This program is free software: you can redistribute it and/or modify it under
746 the terms of the GNU General Public License version 3 as published by the Free
747 Software Foundation.
748  
749 This program is distributed in the hope that it will be useful, but WITHOUT ANY
750 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
751 PARTICULAR PURPOSE. See the GNU General Public License for more details.
752  
753 You should have received a copy of the GNU General Public License along with
754 this program. If not, see http://www.gnu.org/licenses/
755 =cut
756  
757 12 1;
↑ Summary