blib/lib/Plack/Middleware/StatsPerRequest.pm

filter: Statementstmt 98.7% 81 / 82 Branchbran 95.4% 21 / 22 Conditioncond 71.4% 10 / 14 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 93.0% 134 / 144 CC 21 SCAR 21.0
CC
21
Cov
95%
__ANON__
14.4
prepare_app
8.0
call
1.0
CRAP 8.2
↑ Summary
1 package Plack::Middleware::StatsPerRequest;
2  
3 # ABSTRACT: Measure HTTP stats on each request
4  
5 3 our $VERSION = '0.902'; # VERSION
6  
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Time::HiRes qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 use Plack::Util::Accessor
14 3 ▶ qw( app_name metric_name path_cleanups add_headers has_headers long_request );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Measure::Everything 1.002 qw($stats);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use HTTP::Headers::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 ▶sub prepare_app {
Subroutine prepare_app: called CC=8 SCAR=20.8
Pod documented
21 9 my $self = shift;
22  
23 9 ▶ $self->app_name('unknown') unless $self->app_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->app_name 8 1 9
24 9 ▶ $self->metric_name('http_request') unless $self->metric_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->metric_name 8 1 9
25 9 ▶ $self->path_cleanups( [ \&replace_idish ] ) unless $self->path_cleanups;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->path_cleanups 7 2 9
26 9 ▶ $self->long_request(5) unless defined $self->long_request;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->long_request 7 2 9
27 9 foreach my $check (qw(add_headers has_headers)) {
28 18 my $val = $self->$check;
29 18 ▶ if ( $val && ref($val) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val and ref $val ne "ARRAY") 2 16 18
Condition100%3/3
$val and ref $val ne "ARRAY"
!ll&&!rl&&r
1422
MC/DC100%2/2
$val and ref $val ne "ARRAY"
$val ref $val ne "ARRAY"
Truth table100%3/3
A and B
A= $valB= ref $val ne "ARRAY"
ABresult
0X0
100
111
30 2 $log->warn(
31 "Plack::Middleware::StatsPerRequest $check has to be an ARRAYREF, ignoring $val"
32 );
33 2 $self->$check(undef);
34 }
35 }
36 }
37  
38 17 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
39 17 my $self = shift;
40 17 my $env = shift;
41  
42 17 my $t0 = [Time::HiRes::gettimeofday];
43  
44 17 my $res = $self->app->($env);
45  
46 return Plack::Util::response_cb(
47 $res,
48 17 ▶ sub {
Subroutine __ANON__: called CC=14 SCAR=26.7
49 17 my $res = shift;
50 17 my $req;
51  
52 17 my $elapsed = Time::HiRes::tv_interval($t0);
53 17 ▶ $elapsed = sprintf( '%5f', $elapsed ) if $elapsed < .0001;
Branch: 1 branch
BranchTrueFalseTotal
if $elapsed < "0.0001" 15 2 17
54  
55 17 my $path = $env->{PATH_INFO};
56 17 foreach my $callback ( @{ $self->path_cleanups } ) {
57 16 $path = $callback->($path);
58 }
59  
60 my %tags = (
61 status => $res->[0],
62 method => $env->{REQUEST_METHOD},
63 17 app => $self->app_name,
64 path => $path,
65 );
66 17 ▶ if ( my $headers_to_add = $self->add_headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $headers_to_add = $self->add_headers) 4 13 17
67 4 $req = Plack::Request->new($env);
68 4 foreach my $header (@$headers_to_add) {
69 8 ▶ $tags{ 'header_' . lc($header) } = $req->header($header)
Condition100%2/2
$req->header($header) // "not_set"
l!l
62
MC/DC100%1/1
$req->header($header) // "not_set"
$req->header($header)
Truth table100%2/2
A
A= $req->header($header)
Aresult
11
00
70 // 'not_set';
71 }
72 }
73 17 ▶ if ( my $has_headers = $self->has_headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $has_headers = $self->has_headers) 4 13 17
74 4 ▶ $req ||= Plack::Request->new($env);
Condition33%1/3
$req ||= "Plack::Request"->new($env)
l!l&&r!l&&!r
040
MC/DC0%0/2
$req ||= "Plack::Request"->new($env)
$req "Plack::Request"->new($env)
Truth table33%1/3
A ||= B
A= $reqB= "Plack::Request"->new($env)
ABresult
1X1
011
000
75 4 foreach my $header (@$has_headers) {
76 8 ▶ $tags{ 'has_header_' . lc($header) } =
Branch: 1 branch
BranchTrueFalseTotal
$req->header($header) ? : 6 2 8
77 $req->header($header) ? 1 : 0;
78 }
79 }
80  
81 17 eval {
82 17 $stats->write( $self->metric_name,
83 { request_time => $elapsed, hit => 1 }, \%tags );
84 17 ▶ if ( $self->long_request && $elapsed > $self->long_request ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->long_request and $elapsed > $self->long_request) 1 16 17
Condition100%3/3
$self->long_request and $elapsed > $self->long_request
!ll&&!rl&&r
1151
MC/DC100%2/2
$self->long_request and $elapsed > $self->long_request
$self->long_request $elapsed > $self->long_request
Truth table100%3/3
A and B
A= $self->long_requestB= $elapsed > $self->long_request
ABresult
0X0
100
111
85 1 ▶ $req ||= Plack::Request->new($env);
Condition33%1/3
$req ||= "Plack::Request"->new($env)
l!l&&r!l&&!r
010
MC/DC0%0/2
$req ||= "Plack::Request"->new($env)
$req "Plack::Request"->new($env)
Truth table33%1/3
A ||= B
A= $reqB= "Plack::Request"->new($env)
ABresult
1X1
011
000
86 1 $log->warnf( "Long request, took %f: %s %s",
87 $elapsed, $req->method, $req->request_uri );
88 }
89 };
90 17 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 17 17
91 0 $log->errorf( "Could not write stats: %s", $@ );
92 }
93 }
94 17 );
95 }
96  
97  
98 37 ▶sub replace_idish {
Subroutine replace_idish: called CC=1 SCAR=0.0
Pod documented
99 37 my $path = shift;
100 37 $path = lc( $path . '/' );
101  
102 37 $path =~ s{/[a-f0-9\-.]+\@[a-z0-9\-.]+/}{/:msgid/}g;
103 37 $path =~ s{/[a-f0-9]+\/[a-f0-9\/]+/}{/:hexpath/}g;
104  
105 37 $path =~ s([a-f0-9]{40})(:sha1)g;
106 37 $path =~
107 s([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})(:uuid)g;
108 37 $path =~ s(\d{6,})(:int)g;
109 37 $path =~ s{\d+x\d+}{:imgdim}g;
110  
111 37 $path =~ s{/\d+/}{/:int/}g;
112 37 $path =~ s(/[^/]{55,}/)(/:long/)g;
113 37 $path =~ s(/[a-f0-9\-]{8,}/)(/:hex/)g;
114  
115 37 return substr( $path, 0, -1 );
116 }
117  
118 3 "42nd birthday release";
119  
120 __END__
↑ Summary