| 1 | package Starch::Plugin::Net::Statsd::Store; | ||||||||||||||
| 2 | 1 | ▶ | use 5.008001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | ▶ | use strictures 2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | our $VERSION = '0.05'; | |||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | ▶ | use Net::Statsd; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use Types::Common::String -types; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use Time::HiRes qw( gettimeofday tv_interval ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | ▶ | use Moo::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 1 | with 'Starch::Plugin::ForStore'; | |||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | has statsd_path => ( | |||||||||||||
| 17 | is => 'lazy', | ||||||||||||||
| 18 | isa => NonEmptySimpleStr, | ||||||||||||||
| 19 | ); | ||||||||||||||
| 20 | 4 | ▶ | sub _build_statsd_path { | ||||||||||||
|
Subroutine
_build_statsd_path: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 4 | my ($self) = @_; | |||||||||||||
| 22 | 4 | my $path = $self->short_store_class_name(); | |||||||||||||
| 23 | |||||||||||||||
| 24 | # Path sanitization stolen, and slightly modified, from the statsd source. | ||||||||||||||
| 25 | 4 | $path =~ s{\s+}{_}g; | |||||||||||||
| 26 | 4 | $path =~ s{/}{-}g; | |||||||||||||
| 27 | 4 | $path =~ s{::}{-}g; | |||||||||||||
| 28 | 4 | $path =~ s{[^a-zA-Z_\-0-9\.]}{}g; | |||||||||||||
| 29 | |||||||||||||||
| 30 | 4 | return $path; | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 1 | has statsd_full_path => ( | |||||||||||||
| 34 | is => 'lazy', | ||||||||||||||
| 35 | isa => NonEmptySimpleStr, | ||||||||||||||
| 36 | ); | ||||||||||||||
| 37 | 6 | ▶ | sub _build_statsd_full_path { | ||||||||||||
|
Subroutine
_build_statsd_full_path: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | 6 | my ($self) = @_; | |||||||||||||
| 39 | 6 | return $self->manager->statsd_root_path() . '.' . $self->statsd_path(); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 1 | foreach my $method (qw( set get remove )) { | |||||||||||||
| 43 | 55 | ▶ | around $method => sub{ | ||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||
| 44 | 55 | my ($orig, $self, @args) = @_; | |||||||||||||
| 45 | |||||||||||||||
| 46 | 55 | ▶ | return $self->$orig( @args ) if $self->isa('Starch::Store::Layered'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | |||||||||||||||
| 48 | 52 | my $path = $self->statsd_full_path() . '.' . $method; | |||||||||||||
| 49 | |||||||||||||||
| 50 | 52 | my $start = [gettimeofday]; | |||||||||||||
| 51 | |||||||||||||||
| 52 | 52 | my ($errored, $error); | |||||||||||||
| 53 | 52 | ▶ | my $data = try { $self->$orig( @args ) } | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 54 | 52 | ▶ | catch { ($errored, $error) = (1, $_) }; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | 52 | my $end = [gettimeofday]; | |||||||||||||
| 57 | |||||||||||||||
| 58 | 52 | ▶ | if ($errored) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 59 | 3 | $path .= '-error'; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | elsif ($method eq 'get') { | ||||||||||||||
| 62 | 23 | ▶ | $path .= '-' . ($data ? 'hit' : 'miss'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 52 | my $host = $self->manager->statsd_host(); | |||||||||||||
| 66 | 52 | ▶ | local $Net::Statsd::HOST = $host if defined $host; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | |||||||||||||||
| 68 | 52 | my $port = $self->manager->statsd_port(); | |||||||||||||
| 69 | 52 | ▶ | local $Net::Statsd::PORT = $port if defined $port; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | |||||||||||||||
| 71 | 52 | Net::Statsd::timing( | |||||||||||||
| 72 | $path, | ||||||||||||||
| 73 | tv_interval($start, $end) * 1000, | ||||||||||||||
| 74 | $self->manager->statsd_sample_rate(), | ||||||||||||||
| 75 | ); | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 52 | ▶ | die $error if $errored; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | |||||||||||||||
| 79 | 49 | ▶ | return if $method ne 'get'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 23 | return $data; | |||||||||||||
| 81 | 3 | }; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 1 | 1; | |||||||||||||