File Coverage

blib/lib/DataDog/DogStatsd/Helper.pm
Criterion Covered Total %
statement 50 51 98.0
branch 14 16 87.5
condition 8 9 88.8
subroutine 15 15 100.0
pod 9 10 90.0
total 96 101 95.0


line stmt bran cond sub pod time code
1             package DataDog::DogStatsd::Helper;
2              
3 2     2   166037 use strict;
  2         22  
  2         60  
4 2     2   11 use warnings;
  2         4  
  2         102  
5             our $VERSION = '0.06';
6              
7 2     2   11 use base qw( Exporter );
  2         4  
  2         392  
8             our @EXPORT_OK = qw/stats_inc stats_dec stats_timing stats_gauge stats_count stats_histogram stats_event stats_timed/;
9              
10 2     2   911 use DataDog::DogStatsd;
  2         6  
  2         72  
11 2     2   1168 use Time::HiRes qw(tv_interval gettimeofday);
  2         2826  
  2         9  
12              
13             ## no critic (Subroutines::RequireFinalReturn)
14             sub stats_inc {
15 6     6 1 3618 my @args = @_;
16             # support stats_inc('test.blabla', 0.1); as well
17 6 100 100     53 if (@args > 1 and $args[1] =~ /^[\d\.]+$/) {
18             # actually ppl wants stats_count
19 1 50       5 warn "stats_inc sample_rate makes no sense for more than 1\n" if $args[1] > 1;
20 1         5 $args[1] = {sample_rate => $args[1]};
21             }
22 6         16 get_dogstatsd()->increment(@args);
23             }
24 4     4 1 3722 sub stats_dec { get_dogstatsd()->decrement(@_); }
25              
26             sub stats_timing {
27 5     5 1 4794 my @args = @_;
28             # support stats_timing('connection_time', 1000 * $interval, 0.1); as well
29 5 100 100     43 if (@args > 2 and $args[2] =~ /^[\d\.]+$/) {
30             # actually ppl wants stats_count
31 1 50       5 warn "stats_timing sample_rate makes no sense for more than 1\n" if $args[2] > 1;
32 1         4 $args[2] = {sample_rate => $args[2]};
33             }
34 5         13 get_dogstatsd()->timing(@args);
35             }
36              
37             sub stats_timed (&@) { ## no critic (ProhibitSubroutinePrototypes)
38 4     4 1 7009 my ($sub, $what, $opts) = splice @_, 0, 3;
39              
40 4         21 my $start = [gettimeofday];
41              
42 4         11 my (@res, $no_ex);
43 4 100       15 if (wantarray) {
    100          
44 1         3 $no_ex = eval { @res = $sub->(@_); 1; };
  1         4  
  1         100199  
45             } elsif (defined wantarray) {
46 1         3 $no_ex = eval { $res[0] = $sub->(@_); 1; };
  1         4  
  1         100226  
47             } else {
48 2         6 $no_ex = eval { $sub->(@_); 1; };
  2         12  
  0         0  
49             }
50              
51 4 100       35 if ($no_ex) {
52 2         16 get_dogstatsd()->timing($what, 1000 * tv_interval($start), $opts);
53             } else {
54 2         7 get_dogstatsd()->increment("$what.failure", $opts);
55 2         12 die $@;
56             }
57              
58 2 100       19 return wantarray ? @res : $res[0];
59             }
60              
61 4     4 1 3718 sub stats_gauge { get_dogstatsd()->gauge(@_); }
62 1     1 1 343 sub stats_count { get_dogstatsd()->count(@_); }
63 1     1 1 1184 sub stats_histogram { get_dogstatsd()->histogram(@_); }
64 2     2 1 6564 sub stats_event { get_dogstatsd()->event(@_); }
65              
66             my $DOGSTATSD;
67             sub get_dogstatsd {
68             $DOGSTATSD ||= DataDog::DogStatsd->new(
69             host => $ENV{DATADOG_AGENT_HOST},
70             port => $ENV{DATADOG_AGENT_PORT},
71             namespace => $ENV{DATADOG_AGENT_NAMESPACE}
72 27   66 27 1 99 );
73 27         122 return $DOGSTATSD;
74             }
75              
76             sub set_dogstatsd {
77 1     1 0 3 $DOGSTATSD = pop;
78             }
79              
80             1;
81             __END__