| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::Statsd::Client::Timer; | 
| 2 | 3 |  |  | 3 |  | 28 | use Moo; | 
|  | 3 |  |  |  |  | 10 |  | 
|  | 3 |  |  |  |  | 36 |  | 
| 3 | 3 |  |  | 3 |  | 1446 | use Sub::Quote; | 
|  | 3 |  |  |  |  | 10 |  | 
|  | 3 |  |  |  |  | 406 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | # ABSTRACT: Measure event timings and send them to StatsD | 
| 6 |  |  |  |  |  |  | our $VERSION = '0.33'; # VERSION | 
| 7 |  |  |  |  |  |  | our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 3 |  |  | 3 |  | 1390 | use Time::HiRes qw(gettimeofday tv_interval); | 
|  | 3 |  |  |  |  | 3365 |  | 
|  | 3 |  |  |  |  | 22 |  | 
| 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 | 12105 | my ($self) = @_; | 
| 27 | 6 |  |  |  |  | 62 | my (undef, $file, $line) = caller(1); | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 6 |  |  |  |  | 88 | $self->start([gettimeofday]); | 
| 30 | 6 |  |  |  |  | 31 | $self->_file($file); | 
| 31 | 6 |  |  |  |  | 47 | $self->_line($line); | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | sub finish { | 
| 35 | 5 |  |  | 5 | 1 | 2100444 | 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 |  |  |  |  | 231 | ); | 
| 42 | 5 |  |  |  |  | 520 | delete $self->{_pending}; | 
| 43 | 5 |  |  |  |  | 108 | 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 | 4 | my $self = shift; | 
| 53 | 1 | 50 |  |  |  | 6 | if (defined $self->warning_callback) { | 
| 54 | 1 |  |  |  |  | 6 | $self->warning_callback->(@_); | 
| 55 |  |  |  |  |  |  | } else { | 
| 56 | 0 |  |  |  |  | 0 | warn(@_); | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub DEMOLISH { | 
| 61 | 6 |  |  | 6 | 0 | 6582 | my ($self) = @_; | 
| 62 | 6 | 100 |  |  |  | 157 | 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__ |