blib/lib/Plack/Middleware/Statsd.pm

filter: Statementstmt 96.6% 114 / 118 Branchbran 67.8% 38 / 56 Conditioncond 52.6% 10 / 19 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 26 / 26 Podpod 100% 2 / 2 totaltotal 83.1% 193 / 232 CC 38 SCAR 24.2
CC
38
Cov
84%
__ANON__
27.4
prepare_app
11.2
call
4.3
CRAP 11.3
↑ Summary
1 package Plack::Middleware::Statsd;
2  
3 # ABSTRACT: send statistics to statsd
4  
5 # RECOMMEND PREREQ: Net::Statsd::Tiny v0.3.0
6 # RECOMMEND PREREQ: HTTP::Status 6.16
7 # RECOMMEND PREREQ: List::Util::XS
8 # RECOMMEND PREREQ: Ref::Util::XS
9  
10 5 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use parent qw/ Plack::Middleware /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use Digest::SHA 5.96 qw/ hmac_sha256_base64 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use List::Util qw/ first /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 use Plack::Util::Accessor
19 5 ▶ qw/ client sample_rate histogram increment set_add secure_set_add secure_set_key catch_errors /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Ref::Util qw/ is_coderef /;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Scalar::Util qw/ weaken /;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶use experimental qw/ postderef signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 our $VERSION = 'v0.9.3';
28  
29 # Note: You may be able to omit the client if there is a client
30 # defined in the environment hash at C<psgix.monitor.statsd>, and the
31 # L</histogram>, L</increment> and L</attributes> are set. But that
32 # is a strange case and unsupported.
33  
34 5 ▶sub prepare_app($self) {
Subroutine prepare_app: called CC=11 SCAR=24.2
Pod documented
35  
36 5 ▶ if ( my $client = $self->client ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $client = $self->client) 5 0 5
37 5 foreach my $init (
38 [qw/ histogram timing_ms timing /],
39 [qw/ increment increment /],
40 [qw/ set_add set_add /],
41 [qw/ secure_set_add secure_set_add /],
42 )
43 {
44 20 my ( $attr, @methods ) = $init->@*;
45 20 ▶ next if defined $self->$attr;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->$attr 1 19 20
46 19 ▶ my $method = first { $client->can($_) } @methods;
Subroutine __ANON__: called CC=1 SCAR=0.0
47 19 ▶ warn "No $attr method found for client " . ref($client)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method or $attr eq "secure_set_add" 1 18 19
Condition100%3/3
defined $method or $attr eq "secure_set_add"
l!l&&r!l&&!r
1351
MC/DC100%2/2
defined $method or $attr eq "secure_set_add"
defined $method $attr eq "secure_set_add"
Truth table100%3/3
A or B
A= defined $methodB= $attr eq "secure_set_add"
ABresult
1X1
011
000
48 unless defined $method || $attr eq "secure_set_add";
49 $self->$attr(
50 58 ▶ sub($env, @args) {
Subroutine __ANON__: called CC=2 SCAR=7.0
51 58 ▶ return unless defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 2 56 58
52 56 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 56 $client->$method( grep { defined $_ } @args );
54 }
55 2 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=7.6
56 2 my ($e) = $_;
57 2 ▶ if (my $logger = $env->{'psgix.logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logger = $env->{'psgix.logger'}) { } 2 0 2
58 2 $logger->( { message => $e, level => 'error' } );
59 }
60 else {
61 0 $env->{'psgi.errors'}->print($e);
62 }
63 56 };
64  
65 }
66 19 );
67 }
68  
69 5 ▶ unless ( $client->can("secure_set_add") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client->can("secure_set_add")) 5 0 5
70 5 ▶ if ( my $key = $self->secure_set_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $self->secure_set_key) 1 4 5
71 $self->secure_set_add(
72 6 ▶ sub( $env, $metric, $string ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 6 my $obscure = hmac_sha256_base64( $string, $key );
74 6 $self->set_add->( $env, $metric, $obscure );
75 }
76 1 );
77 }
78 }
79  
80 }
81  
82 5 ▶ if (my $attr = first { !is_coderef($self->$_) } qw/ histogram increment set_add /) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr = &first(sub { not is_coderef($self->$_); } , "histogram", "increment", "set_add")) 1 4 5
83 1 die "$attr is not a coderef";
84 }
85  
86 4 ▶ if ( my $catch = $self->catch_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $catch = $self->catch_errors) 1 3 4
87  
88 1 ▶ unless ( is_coderef($catch) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_coderef($catch)) 1 0 1
89  
90 $self->catch_errors(
91 1 ▶ sub( $env, $error ) {
Subroutine __ANON__: called CC=2 SCAR=7.4
92 1 ▶ if ( my $logger = $env->{'psgix.logger'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logger = $env->{'psgix.logger'}) { } 1 0 1
93 1 $logger->( { level => 'error', message => $error } );
94 }
95 else {
96 0 $env->{'psgi.errors'}->print($error);
97 }
98 1 my $message = 'Internal Error';
99 1 return [ 500, [ 'Content-Type' => 'text/plain', 'Content-Length' => length($message) ], [$message] ];
100 }
101 1 );
102  
103 }
104  
105 }
106 }
107  
108 7 ▶sub call ( $self, $env ) {
Subroutine call: called CC=4 SCAR=14.5
Pod documented
109  
110 7 ▶ my $client = ( $env->{'psgix.monitor.statsd'} //= $self->client );
Condition33%1/3
$env->{"psgix.monitor.statsd"} //= $self->client
l!l&&r!l&&!r
070
MC/DC0%0/2
$env->{"psgix.monitor.statsd"} //= $self->client
$env->{"psgix.monitor.statsd"} $self->client
Truth table33%1/3
A //= B
A= $env->{"psgix.monitor.statsd"}B= $self->client
ABresult
1X1
011
000
111 7 my $secure = $self->secure_set_add;
112  
113 7 ▶ if ( defined $secure ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $secure) 7 0 7
114 7 weaken( my $ref = $env );
115 7 ▶ $env->{'psgix.monitor.statsd_secure_set_add'} = sub { $secure->( $ref, @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
116 }
117  
118  
119 7 my $start = [Time::HiRes::gettimeofday];
120 7 my $res;
121  
122 7 ▶ if (my $catch = $self->catch_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $catch = $self->catch_errors) { } 1 6 7
123 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 1 $res = $self->app->($env);
125 }
126 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
127 1 $res = $catch->( $env, $_ );
128 }
129 1 }
130 else {
131 6 $res = $self->app->($env);
132 }
133  
134 return Plack::Util::response_cb(
135 $res,
136 7 ▶ sub($res) {
Subroutine __ANON__: called CC=19 SCAR=33.1
137 7 ▶ return unless $client;
Branch: 1 branch
BranchTrueFalseTotal
unless $client 0 7 7
138  
139 7 my $rate = $self->sample_rate;
140  
141 7 ▶ $rate = undef if ( defined $rate ) && ( $rate >= 1 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rate and $rate >= 1 0 7 7
Condition33%1/3
defined $rate and $rate >= 1
!ll&&!rl&&r
700
MC/DC0%0/2
defined $rate and $rate >= 1
defined $rate $rate >= 1
Truth table33%1/3
A and B
A= defined $rateB= $rate >= 1
ABresult
0X0
100
111
142  
143 7 my $histogram = $self->histogram;
144 7 my $increment = $self->increment;
145 7 my $set_add = $self->set_add;
146  
147 7 my $elapsed = Time::HiRes::tv_interval($start);
148  
149 7 $histogram->( $env, 'psgi.response.time', $elapsed * 1000, $rate );
150  
151 7 ▶ if ( defined $env->{CONTENT_LENGTH} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{"CONTENT_LENGTH"}) 7 0 7
152 $histogram->( $env,
153 'psgi.request.content-length',
154 7 $env->{CONTENT_LENGTH}, $rate
155 );
156 }
157  
158 7 ▶ if ( my $method = $env->{REQUEST_METHOD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $env->{"REQUEST_METHOD"}) 7 0 7
159 7 ▶ $method = "other" unless $method =~ /^\w+$/a;
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /^\w+$/a 1 6 7
160 7 $increment->( $env, 'psgi.request.method.' . $method, $rate );
161 }
162  
163 7 ▶ if ( my $type = _mime_type_to_metric( $env->{CONTENT_TYPE} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = _mime_type_to_metric($env->{"CONTENT_TYPE"})) 1 6 7
164 1 $increment->( $env, 'psgi.request.content-type.' . $type, $rate );
165 }
166  
167 $secure->( $env, 'psgi.request.remote_addr', $env->{REMOTE_ADDR} )
168 7 ▶ if defined($secure) && $env->{REMOTE_ADDR};
Branch: 1 branch
BranchTrueFalseTotal
if defined $secure and $env->{"REMOTE_ADDR"} 7 0 7
Condition33%1/3
defined $secure and $env->{"REMOTE_ADDR"}
!ll&&!rl&&r
007
MC/DC0%0/2
defined $secure and $env->{"REMOTE_ADDR"}
defined $secure $env->{"REMOTE_ADDR"}
Truth table33%1/3
A and B
A= defined $secureB= $env->{"REMOTE_ADDR"}
ABresult
0X0
100
111
169  
170 7 $set_add->( $env, 'psgi.worker.pid', $$ );
171  
172 7 my $h = Plack::Util::headers( $res->[1] );
173  
174 my $xsendfile =
175 $env->{'plack.xsendfile.type'}
176 || $ENV{HTTP_X_SENDFILE_TYPE}
177 7 ▶ || 'X-Sendfile';
Condition40%2/5
$env->{'plack.xsendfile.type'} || $ENV{'HTTP_X_SENDFILE_TYPE'} || 'X-Sendfile'
l!l
07
$env->{"plack.xsendfile.type"} or $ENV{"HTTP_X_SENDFILE_TYPE"}
l!l&&r!l&&!r
007
MC/DC0%0/2
$env->{'plack.xsendfile.type'} || $ENV{'HTTP_X_SENDFILE_TYPE'} || 'X-Sendfile'
$env->{"plack.xsendfile.type"} $ENV{"HTTP_X_SENDFILE_TYPE"}
Truth table33%1/3
A or B
A= $env->{"plack.xsendfile.type"}B= $ENV{"HTTP_X_SENDFILE_TYPE"}
ABresult
1X1
011
000
178  
179 7 ▶ if ( $h->exists($xsendfile) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->exists($xsendfile)) 0 7 7
180 0 $increment->( $env, 'psgi.response.x-sendfile', $rate );
181 }
182  
183 7 ▶ if ( $h->exists('Content-Length') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->exists("Content-Length")) 7 0 7
184 7 ▶ my $length = $h->get('Content-Length') || 0;
Condition100%2/2
$h->get('Content-Length') || 0
l!l
34
MC/DC100%1/1
$h->get('Content-Length') || 0
$h->get('Content-Length')
Truth table100%2/2
A
A= $h->get('Content-Length')
Aresult
11
00
185 7 $histogram->( $env, 'psgi.response.content-length', $length, $rate );
186 }
187  
188 7 ▶ if ( my $type = _mime_type_to_metric( $h->get('Content-Type') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = _mime_type_to_metric($h->get("Content-Type"))) 7 0 7
189 7 $increment->( $env, 'psgi.response.content-type.' . $type, $rate );
190 }
191  
192 7 $increment->( $env, 'psgi.response.status.' . $res->[0], $rate );
193  
194 7 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($env->{"psgix.harakiri.supported"} ? $env->{"psgix.harakiri"} : $env->{"psgix.harakiri.commit"}) 0 7 7
$env->{'psgix.harakiri.supported'} ? : 0 7 7
195 $env->{'psgix.harakiri.supported'}
196 ? $env->{'psgix.harakiri'}
197 : $env->{'psgix.harakiri.commit'}
198 )
199 {
200 0 $increment->( $env, 'psgix.harakiri' ); # rate == 1
201 }
202  
203 7 ▶ $client->flush if $client->can('flush');
Branch: 1 branch
BranchTrueFalseTotal
if $client->can("flush") 0 7 7
204  
205 7 return;
206 }
207 7 );
208  
209 }
210  
211 14 ▶sub _mime_type_to_metric( $type = undef ) {
Subroutine _mime_type_to_metric: called CC=4 SCAR=13.9
212 14 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 6 8 14
213 8 ▶ return unless $type =~ m#^\w+/(?:\w+[\-\+])*\w+(?: *;\w+=\w+)?#a;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ m[^\w+/(?:\w+[\-\+])*\w+(?: *;\w+=\w+)?]a 0 8 8
214 8 return $type =~ s#\.#-#gr =~ s#/#.#gr =~ s/;.*$//r;
215 }
216  
217  
218 5 1;
219  
220 __END__
↑ Summary