line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
5
|
|
255638
|
use v5.14; |
|
5
|
|
|
|
|
28
|
|
11
|
5
|
|
|
5
|
|
30
|
use warnings; |
|
5
|
|
|
|
|
27
|
|
|
5
|
|
|
|
|
150
|
|
12
|
|
|
|
|
|
|
|
13
|
5
|
|
|
5
|
|
35
|
use parent qw/ Plack::Middleware /; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
30
|
|
14
|
|
|
|
|
|
|
|
15
|
5
|
|
|
5
|
|
29059
|
use Feature::Compat::Try; |
|
5
|
|
|
|
|
1540
|
|
|
5
|
|
|
|
|
24
|
|
16
|
5
|
|
|
5
|
|
13629
|
use List::Util qw/ first /; |
|
5
|
|
|
|
|
20
|
|
|
5
|
|
|
|
|
485
|
|
17
|
5
|
|
|
5
|
|
41
|
use Plack::Util; |
|
5
|
|
|
|
|
31
|
|
|
5
|
|
|
|
|
155
|
|
18
|
|
|
|
|
|
|
use Plack::Util::Accessor |
19
|
5
|
|
|
5
|
|
31
|
qw/ client sample_rate histogram increment set_add catch_errors /; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
37
|
|
20
|
5
|
|
|
5
|
|
2936
|
use Ref::Util qw/ is_coderef /; |
|
5
|
|
|
|
|
8619
|
|
|
5
|
|
|
|
|
349
|
|
21
|
5
|
|
|
5
|
|
38
|
use Time::HiRes; |
|
5
|
|
|
|
|
14
|
|
|
5
|
|
|
|
|
41
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $VERSION = 'v0.6.2'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Note: You may be able to omit the client if there is a client |
26
|
|
|
|
|
|
|
# defined in the environment hash at C, and the |
27
|
|
|
|
|
|
|
# L, L and L are set. But that |
28
|
|
|
|
|
|
|
# is a strange case and unsupported. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub prepare_app { |
31
|
5
|
|
|
5
|
1
|
6727
|
my ($self) = @_; |
32
|
|
|
|
|
|
|
|
33
|
5
|
50
|
|
|
|
40
|
if ( my $client = $self->client ) { |
34
|
5
|
|
|
|
|
278
|
foreach my $init ( |
35
|
|
|
|
|
|
|
[qw/ histogram timing_ms timing /], |
36
|
|
|
|
|
|
|
[qw/ increment increment /], |
37
|
|
|
|
|
|
|
[qw/ set_add set_add /], |
38
|
|
|
|
|
|
|
) |
39
|
|
|
|
|
|
|
{ |
40
|
15
|
|
|
|
|
106
|
my ( $attr, @methods ) = @$init; |
41
|
15
|
100
|
|
|
|
50
|
next if defined $self->$attr; |
42
|
14
|
|
|
16
|
|
122
|
my $method = first { $client->can($_) } @methods; |
|
16
|
|
|
|
|
159
|
|
43
|
14
|
100
|
|
|
|
190
|
warn "No $attr method found for client " . ref($client) |
44
|
|
|
|
|
|
|
unless defined $method; |
45
|
|
|
|
|
|
|
$self->$attr( |
46
|
|
|
|
|
|
|
sub { |
47
|
49
|
50
|
|
49
|
|
110
|
return unless defined $method; |
48
|
49
|
|
|
|
|
114
|
my ($env, @args) = @_; |
49
|
|
|
|
|
|
|
try { |
50
|
|
|
|
|
|
|
$client->$method( grep { defined $_ } @args ); |
51
|
|
|
|
|
|
|
} |
52
|
49
|
|
|
|
|
97
|
catch($e) { |
53
|
|
|
|
|
|
|
if (my $logger = $env->{'psgix.logger'}) { |
54
|
|
|
|
|
|
|
$logger->( { message => $e, level => 'error' } ); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
else { |
57
|
|
|
|
|
|
|
$env->{'psgi.errors'}->print($e); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
}; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
} |
62
|
14
|
|
|
|
|
125
|
); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
5
|
100
|
|
14
|
|
69
|
if (my $attr = first { !is_coderef($self->$_) } qw/ histogram increment set_add /) { |
|
14
|
|
|
|
|
63
|
|
67
|
1
|
|
|
|
|
18
|
die "$attr is not a coderef"; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
4
|
100
|
|
|
|
37
|
if ( my $catch = $self->catch_errors ) { |
71
|
|
|
|
|
|
|
|
72
|
1
|
50
|
|
|
|
7
|
unless ( is_coderef($catch) ) { |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
$self->catch_errors( |
75
|
|
|
|
|
|
|
sub { |
76
|
1
|
|
|
1
|
|
3
|
my ( $env, $error ) = @_; |
77
|
1
|
50
|
|
|
|
5
|
if ( my $logger = $env->{'psgix.logger'} ) { |
78
|
1
|
|
|
|
|
7
|
$logger->( { level => 'error', message => $error } ); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
else { |
81
|
0
|
|
|
|
|
0
|
$env->{'psgi.errors'}->print($error); |
82
|
|
|
|
|
|
|
} |
83
|
1
|
|
|
|
|
6
|
my $message = 'Internal Error'; |
84
|
1
|
|
|
|
|
6
|
return [ 500, [ 'Content-Type' => 'text/plain', 'Content-Length' => length($message) ], [$message] ]; |
85
|
|
|
|
|
|
|
} |
86
|
1
|
|
|
|
|
5
|
); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub call { |
94
|
6
|
|
|
6
|
1
|
246527
|
my ( $self, $env ) = @_; |
95
|
|
|
|
|
|
|
|
96
|
6
|
|
33
|
|
|
55
|
my $client = ( $env->{'psgix.monitor.statsd'} //= $self->client ); |
97
|
|
|
|
|
|
|
|
98
|
6
|
|
|
|
|
76
|
my $start = [Time::HiRes::gettimeofday]; |
99
|
6
|
|
|
|
|
11
|
my $res; |
100
|
|
|
|
|
|
|
|
101
|
6
|
100
|
|
|
|
21
|
if (my $catch = $self->catch_errors) { |
102
|
|
|
|
|
|
|
try { |
103
|
|
|
|
|
|
|
$res = $self->app->($env); |
104
|
|
|
|
|
|
|
} |
105
|
1
|
|
|
|
|
9
|
catch($e) { |
106
|
|
|
|
|
|
|
$res = $catch->( $env, $e ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
else { |
110
|
5
|
|
|
|
|
51
|
$res = $self->app->($env); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
return Plack::Util::response_cb( |
114
|
|
|
|
|
|
|
$res, |
115
|
|
|
|
|
|
|
sub { |
116
|
6
|
50
|
|
6
|
|
150
|
return unless $client; |
117
|
|
|
|
|
|
|
|
118
|
6
|
|
|
|
|
18
|
my ($res) = @_; |
119
|
|
|
|
|
|
|
|
120
|
6
|
|
|
|
|
25
|
my $rate = $self->sample_rate; |
121
|
|
|
|
|
|
|
|
122
|
6
|
50
|
33
|
|
|
43
|
$rate = undef if ( defined $rate ) && ( $rate >= 1 ); |
123
|
|
|
|
|
|
|
|
124
|
6
|
|
|
|
|
22
|
my $histogram = $self->histogram; |
125
|
6
|
|
|
|
|
31
|
my $increment = $self->increment; |
126
|
6
|
|
|
|
|
57
|
my $set_add = $self->set_add; |
127
|
|
|
|
|
|
|
|
128
|
6
|
|
|
|
|
52
|
my $elapsed = Time::HiRes::tv_interval($start); |
129
|
|
|
|
|
|
|
|
130
|
6
|
|
|
|
|
136
|
$histogram->( $env, 'psgi.response.time', $elapsed * 1000, $rate ); |
131
|
|
|
|
|
|
|
|
132
|
6
|
50
|
|
|
|
105
|
if ( defined $env->{CONTENT_LENGTH} ) { |
133
|
|
|
|
|
|
|
$histogram->( $env, |
134
|
|
|
|
|
|
|
'psgi.request.content-length', |
135
|
6
|
|
|
|
|
21
|
$env->{CONTENT_LENGTH}, $rate |
136
|
|
|
|
|
|
|
); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
6
|
50
|
|
|
|
58
|
if ( my $method = $env->{REQUEST_METHOD} ) { |
140
|
6
|
|
|
|
|
34
|
$increment->( $env, 'psgi.request.method.' . $method, $rate ); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
6
|
100
|
|
|
|
61
|
if ( my $type = _mime_type_to_metric( $env->{CONTENT_TYPE} ) ) { |
144
|
1
|
|
|
|
|
7
|
$increment->( $env, 'psgi.request.content-type.' . $type, $rate ); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$set_add->( $env, 'psgi.request.remote_addr', $env->{REMOTE_ADDR} ) |
148
|
6
|
50
|
|
|
|
36
|
if $env->{REMOTE_ADDR}; |
149
|
|
|
|
|
|
|
|
150
|
6
|
|
|
|
|
56
|
$set_add->( $env, 'psgi.worker.pid', $$ ); |
151
|
|
|
|
|
|
|
|
152
|
6
|
|
|
|
|
62
|
my $h = Plack::Util::headers( $res->[1] ); |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
my $xsendfile = |
155
|
|
|
|
|
|
|
$env->{'plack.xsendfile.type'} |
156
|
|
|
|
|
|
|
|| $ENV{HTTP_X_SENDFILE_TYPE} |
157
|
6
|
|
50
|
|
|
228
|
|| 'X-Sendfile'; |
158
|
|
|
|
|
|
|
|
159
|
6
|
50
|
|
|
|
51
|
if ( $h->exists($xsendfile) ) { |
160
|
0
|
|
|
|
|
0
|
$increment->( $env, 'psgi.response.x-sendfile', $rate ); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
6
|
50
|
|
|
|
221
|
if ( $h->exists('Content-Length') ) { |
164
|
6
|
|
100
|
|
|
161
|
my $length = $h->get('Content-Length') || 0; |
165
|
6
|
|
|
|
|
203
|
$histogram->( $env, 'psgi.response.content-length', $length, $rate ); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
6
|
50
|
|
|
|
91
|
if ( my $type = _mime_type_to_metric( $h->get('Content-Type') ) ) { |
169
|
6
|
|
|
|
|
36
|
$increment->( $env, 'psgi.response.content-type.' . $type, $rate ); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
6
|
|
|
|
|
73
|
$increment->( $env, 'psgi.response.status.' . $res->[0], $rate ); |
173
|
|
|
|
|
|
|
|
174
|
6
|
50
|
|
|
|
79
|
if ( |
|
|
50
|
|
|
|
|
|
175
|
|
|
|
|
|
|
$env->{'psgix.harakiri.supported'} |
176
|
|
|
|
|
|
|
? $env->{'psgix.harakiri'} |
177
|
|
|
|
|
|
|
: $env->{'psgix.harakiri.commit'} |
178
|
|
|
|
|
|
|
) |
179
|
|
|
|
|
|
|
{ |
180
|
0
|
|
|
|
|
0
|
$increment->( $env, 'psgix.harakiri' ); # rate == 1 |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
6
|
50
|
|
|
|
43
|
$client->flush if $client->can('flush'); |
184
|
|
|
|
|
|
|
|
185
|
6
|
|
|
|
|
59
|
return; |
186
|
|
|
|
|
|
|
} |
187
|
6
|
|
|
|
|
1582
|
); |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub _mime_type_to_metric { |
192
|
12
|
100
|
|
12
|
|
216
|
my $type = $_[0] or return; |
193
|
7
|
|
|
|
|
56
|
return $type =~ s#\.#-#gr =~ s#/#.#gr =~ s/;.*$//r; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
1; |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
__END__ |