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   25 use Moo;
  3         8  
  3         33  
3 3     3   1209 use Sub::Quote;
  3         8  
  3         341  
4              
5             # ABSTRACT: Measure event timings and send them to StatsD
6             our $VERSION = '0.34'; # VERSION
7             our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY
8              
9 3     3   1111 use Time::HiRes qw(gettimeofday tv_interval);
  3         2753  
  3         18  
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', 'sample_rate'] => (
22             is => 'rw',
23             );
24              
25             sub BUILD {
26 6     6 0 10145 my ($self) = @_;
27 6         48 my (undef, $file, $line) = caller(1);
28              
29 6         69 $self->start([gettimeofday]);
30 6         24 $self->_file($file);
31 6         42 $self->_line($line);
32             }
33              
34             sub finish {
35 5     5 1 2100662 my ($self) = @_;
36 5         50 my $duration = tv_interval($self->{start}) * 1000;
37             $self->{statsd}->timing_ms(
38             $self->{metric},
39             $duration,
40             $self->{sample_rate},
41 5         238 );
42 5         714 delete $self->{_pending};
43 5         186 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       7 if (defined $self->warning_callback) {
54 1         7 $self->warning_callback->(@_);
55             } else {
56 0         0 warn(@_);
57             }
58             }
59              
60             sub DEMOLISH {
61 6     6 0 6147 my ($self) = @_;
62 6 100       123 if ($self->{_pending}) {
63 1         3 my $metric = $self->{metric};
64 1 50 33     9 $metric = $self->{statsd}{prefix} . $metric if $self->{statsd} && $self->{statsd}{prefix};
65 1         9 $self->emit_warning("Unfinished timer for stat $metric (created at $self->{_file} line $self->{_line})");
66             }
67             }
68              
69             1;
70              
71             __END__