File Coverage

blib/lib/Net/Statsd/Client/Timer.pm
Criterion Covered Total %
statement 27 30 90.0
branch 4 6 66.6
condition 1 3 33.3
subroutine 7 8 87.5
pod 2 5 40.0
total 41 52 78.8


line stmt bran cond sub pod time code
1             package Net::Statsd::Client::Timer;
2 3     3   19 use Moo;
  3         7  
  3         22  
3 3     3   948 use Sub::Quote;
  3         6  
  3         238  
4              
5             # ABSTRACT: Measure event timings and send them to StatsD
6             our $VERSION = '0.32'; # VERSION
7             our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY
8              
9 3     3   579 use Time::HiRes qw(gettimeofday tv_interval);
  3         1751  
  3         17  
10              
11             has 'statsd' => (
12             is => 'ro',
13             required => 1,
14             );
15              
16             has '_pending' => (
17             is => 'rw',
18             default => quote_sub q{1},
19             );
20              
21             has ['metric', 'start', '_file', '_line', 'warning_callback'] => (
22             is => 'rw',
23             );
24              
25             sub BUILD {
26 6     6 0 9013 my ($self) = @_;
27 6         48 my (undef, $file, $line) = caller(1);
28              
29 6         70 $self->start([gettimeofday]);
30 6         24 $self->_file($file);
31 6         41 $self->_line($line);
32             }
33              
34             sub finish {
35 5     5 1 2100517 my ($self) = @_;
36 5         42 my $duration = tv_interval($self->{start}) * 1000;
37             $self->{statsd}->timing_ms(
38             $self->{metric},
39             $duration,
40             $self->{sample_rate},
41 5         185 );
42 5         479 delete $self->{_pending};
43 5         97 return $duration;
44             }
45              
46             sub cancel {
47 0     0 1 0 my ($self) = @_;
48 0         0 delete $self->{_pending};
49             }
50              
51             sub emit_warning {
52 1     1 0 3 my $self = shift;
53 1 50       5 if (defined $self->warning_callback) {
54 1         5 $self->warning_callback->(@_);
55             } else {
56 0         0 warn(@_);
57             }
58             }
59              
60             sub DEMOLISH {
61 6     6 0 4877 my ($self) = @_;
62 6 100       132 if ($self->{_pending}) {
63 1         3 my $metric = $self->{metric};
64 1 50 33     8 $metric = $self->{statsd}{prefix} . $metric if $self->{statsd} && $self->{statsd}{prefix};
65 1         7 $self->emit_warning("Unfinished timer for stat $metric (created at $self->{_file} line $self->{_line})");
66             }
67             }
68              
69             1;
70              
71             __END__