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   159566 use strict;
  2         28  
  2         60  
4 2     2   11 use warnings;
  2         54  
  2         96  
5             our $VERSION = '0.07';
6              
7 2     2   12 use base qw( Exporter );
  2         4  
  2         376  
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   813 use DataDog::DogStatsd;
  2         5  
  2         69  
11 2     2   1185 use Time::HiRes qw(tv_interval gettimeofday);
  2         2704  
  2         8  
12              
13             ## no critic (Subroutines::RequireFinalReturn)
14             sub stats_inc {
15 6     6 1 3423 my @args = @_;
16             # support stats_inc('test.blabla', 0.1); as well
17 6 100 100     50 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         4 $args[1] = {sample_rate => $args[1]};
21             }
22 6         19 get_dogstatsd()->increment(@args);
23             }
24 4     4 1 3474 sub stats_dec { get_dogstatsd()->decrement(@_); }
25              
26             sub stats_timing {
27 5     5 1 4668 my @args = @_;
28             # support stats_timing('connection_time', 1000 * $interval, 0.1); as well
29 5 100 100     38 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         15 get_dogstatsd()->timing(@args);
35             }
36              
37             sub stats_timed (&@) { ## no critic (ProhibitSubroutinePrototypes, Subroutines::RequireArgUnpacking)
38 4     4 1 10350 my ($sub, $what, $opts) = splice @_, 0, 3;
39              
40 4         23 my $start = [gettimeofday];
41              
42 4         14 my (@res, $no_ex);
43 4 100       19 if (wantarray) {
    100          
44 1         4 $no_ex = eval { @res = $sub->(@_); 1; };
  1         7  
  1         100240  
45             } elsif (defined wantarray) {
46 1         2 $no_ex = eval { $res[0] = $sub->(@_); 1; };
  1         4  
  1         100235  
47             } else {
48 2         8 $no_ex = eval { $sub->(@_); 1; };
  2         16  
  0         0  
49             }
50              
51 4 100       46 if ($no_ex) {
52 2         21 get_dogstatsd()->timing($what, 1000 * tv_interval($start), $opts);
53             } else {
54 2         9 get_dogstatsd()->increment("$what.failure", $opts);
55 2         17 die $@; ## no critic (Variables::ProhibitEvilVariables)
56             }
57              
58 2 100       45 return wantarray ? @res : $res[0];
59             }
60              
61 4     4 1 3487 sub stats_gauge { get_dogstatsd()->gauge(@_); }
62 1     1 1 290 sub stats_count { get_dogstatsd()->count(@_); }
63 1     1 1 1024 sub stats_histogram { get_dogstatsd()->histogram(@_); }
64 2     2 1 1351 sub stats_event { get_dogstatsd()->event(@_); }
65              
66             my $DOGSTATSD;
67              
68             sub get_dogstatsd {
69             $DOGSTATSD ||= DataDog::DogStatsd->new(
70             host => $ENV{DATADOG_AGENT_HOST},
71             port => $ENV{DATADOG_AGENT_PORT},
72 27   66 27 1 105 namespace => $ENV{DATADOG_AGENT_NAMESPACE});
73 27         145 return $DOGSTATSD;
74             }
75              
76             sub set_dogstatsd {
77 1     1 0 3 $DOGSTATSD = pop;
78             }
79              
80             1;
81             __END__