line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::QueryCounter::DBI; |
2
|
1
|
|
|
1
|
|
477
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
7
|
use utf8; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
13
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
37
|
use parent 'Plack::Middleware'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
7
|
1
|
|
|
1
|
|
8574
|
use DBIx::Tracer; |
|
1
|
|
|
|
|
16020
|
|
|
1
|
|
|
|
|
28
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
6
|
use Plack::Util::Accessor qw/prefix/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub prepare_app { |
12
|
2
|
|
|
2
|
1
|
6510
|
my $self = shift; |
13
|
|
|
|
|
|
|
|
14
|
2
|
|
100
|
|
|
7
|
$self->{__prefix} = $self->prefix || 'X-QueryCounter-DBI'; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub call { |
18
|
2
|
|
|
2
|
1
|
16413
|
my ($self, $env) = @_; |
19
|
|
|
|
|
|
|
|
20
|
2
|
|
|
|
|
6
|
my $stats = { |
21
|
|
|
|
|
|
|
total => 0, |
22
|
|
|
|
|
|
|
read => 0, |
23
|
|
|
|
|
|
|
write => 0, |
24
|
|
|
|
|
|
|
other => 0, |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $tracer = DBIx::Tracer->new( |
28
|
|
|
|
|
|
|
sub{ |
29
|
10
|
|
|
10
|
|
9116
|
my %args = @_; |
30
|
10
|
|
|
|
|
21
|
_callback(\%args, $stats); |
31
|
|
|
|
|
|
|
} |
32
|
2
|
|
|
|
|
15
|
); |
33
|
2
|
|
|
|
|
136
|
my $res = $self->app->($env); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# add header to response |
36
|
|
|
|
|
|
|
return Plack::Util::response_cb($res, sub { |
37
|
2
|
|
|
2
|
|
28
|
my $res = shift; |
38
|
2
|
|
|
|
|
12
|
Plack::Util::header_set($res->[1], $self->{__prefix} . '-Total', $stats->{total}); |
39
|
2
|
|
|
|
|
34
|
Plack::Util::header_set($res->[1], $self->{__prefix} . '-Read', $stats->{read}); |
40
|
2
|
|
|
|
|
32
|
Plack::Util::header_set($res->[1], $self->{__prefix} . '-Write', $stats->{write}); |
41
|
2
|
|
|
|
|
33
|
Plack::Util::header_set($res->[1], $self->{__prefix} . '-Other', $stats->{other}); |
42
|
2
|
|
|
|
|
112
|
}); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub _callback { |
46
|
12
|
|
|
12
|
|
4765
|
my ($args, $stats) = @_; |
47
|
12
|
|
|
|
|
20
|
my $inputs = $args->{sql}; |
48
|
12
|
|
|
|
|
36
|
$inputs =~ s{/\*(.*)\*/}{}g; |
49
|
|
|
|
|
|
|
|
50
|
12
|
|
|
|
|
37
|
my @sqls = split /;/, $inputs; |
51
|
|
|
|
|
|
|
|
52
|
12
|
|
|
|
|
22
|
for my $sql (@sqls) { |
53
|
16
|
|
|
|
|
119
|
$sql =~ s/^\s*(.*?)\s*$/$1/; |
54
|
16
|
|
|
|
|
30
|
$stats->{total}++; |
55
|
|
|
|
|
|
|
|
56
|
16
|
100
|
|
|
|
53
|
if ($sql =~ /^SELECT/i) { |
|
|
100
|
|
|
|
|
|
57
|
8
|
|
|
|
|
20
|
$stats->{read}++; |
58
|
|
|
|
|
|
|
} elsif ($sql =~ /^(INSERT|UPDATE|DELETE)/i) { |
59
|
5
|
|
|
|
|
13
|
$stats->{write}++; |
60
|
|
|
|
|
|
|
} else { |
61
|
3
|
|
|
|
|
9
|
$stats->{other}++; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
1; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
__END__ |