Perlbal/Plugin/Stats.pm

filter: Statementstmt 21.9% 18 / 82 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 35.7% 5 / 14 Podpod 0.0% 0 / 4 totaltotal 19.6% 23 / 117 CC 16 SCAR 25.5
CC
16
Cov
18%
__ANON__
30.0
__ANON__
20.0
register
12.0
CRAP 12.8
« Perlbal/Plugin/Cgilike.pm ↑ Summary Perlbal/SocketSSL.pm »
1 ###########################################################################
2 # basic Perlbal statistics gatherer
3 ###########################################################################
4  
5 package Perlbal::Plugin::Stats;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶no warnings qw(deprecated);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # setup our package variables
14 1 our %statobjs; # { svc_name => [ service, statobj ], svc_name => [ service, statobj ], ... }
15  
16 # define all stats keys here
17 1 our @statkeys = qw( files_sent files_reproxied
18 web_requests proxy_requests
19 proxy_requests_highpri );
20  
21 # called when we're being added to a service
22 0 ▶sub register {
Subroutine register: not called CC=3 SCAR=24.8
Pod undocumented
23 0 my ($class, $svc) = @_;
24  
25 # create a stats object
26 0 my $sobj = Perlbal::Plugin::Stats::Storage->new();
27 0 $statobjs{$svc->{name}} = [ $svc, $sobj ];
28  
29 # simple events we count are done here. when the hook on the left side is called,
30 # we simply increment the count of the stat on the right side.
31 0 my %simple = qw(
32 start_send_file files_sent
33 start_file_reproxy files_reproxied
34 start_web_request web_requests
35 );
36  
37 # create hooks for %simple things
38 0 ▶ while (my ($hook, $stat) = each %simple) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %simple) 0 0 0
39 0 eval "\$svc->register_hook('Stats', '$hook', sub { \$sobj->{'$stat'}++; return 0; });";
40 0 ▶ return undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
41 }
42  
43 # more complicated statistics
44 0 ▶ $svc->register_hook('Stats', 'backend_client_assigned', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
45 0 my Perlbal::BackendHTTP $be = shift;
46 0 my Perlbal::ClientProxy $cp = $be->{client};
47 0 $sobj->{pending}->{"$cp"} = [ gettimeofday() ];
48 0 ▶ ($cp->{high_priority} ? $sobj->{proxy_requests_highpri} : $sobj->{proxy_requests})++;
Branch: 1 branch
BranchTrueFalseTotal
$cp->{'high_priority'} ? : 0 0 0
49 0 return 0;
50 0 });
51 0 ▶ $svc->register_hook('Stats', 'backend_response_received', sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
52 0 my Perlbal::BackendHTTP $be = shift;
53 0 my Perlbal::ClientProxy $obj = $be->{client};
54 0 my $ot = delete $sobj->{pending}->{"$obj"};
55 0 ▶ return 0 unless defined $ot;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ot 0 0 0
56  
57 # now construct data to put in recent
58 0 ▶ if (defined $obj->{req_headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj->{"req_headers"}) 0 0 0
59 0 ▶ my $uri = 'http://' . ($obj->{req_headers}->header('Host') || 'unknown') . $obj->{req_headers}->request_uri;
Condition0%0/2
$obj->{'req_headers'}->header('Host') || 'unknown'
l!l
00
MC/DC0%0/1
$obj->{'req_headers'}->header('Host') || 'unknown'
$obj->{'req_headers'}->header('Host')
Truth table0%0/2
A
A= $obj->{'req_headers'}->header('Host')
Aresult
11
00
60 0 push @{$sobj->{recent}}, sprintf('%-6.4f %s', tv_interval($ot), $uri);
61 0 ▶ shift(@{$sobj->{recent}}) if scalar(@{$sobj->{recent}}) > 100; # if > 100 items, lose one
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$sobj->{"recent"};} > 100 0 0 0
62 }
63 0 return 0;
64 0 });
65  
66 0 return 1;
67 }
68  
69 # called when we're no longer active on a service
70 0 ▶sub unregister {
Subroutine unregister: not called CC=1 SCAR=6.9
Pod undocumented
71 0 my ($class, $svc) = @_;
72  
73 # clean up time
74 0 $svc->unregister_hooks('Stats');
75 0 delete $statobjs{$svc->{name}};
76 0 return 1;
77 }
78  
79 # called when we are loaded
80 0 ▶sub load {
Subroutine load: not called CC=1 SCAR=6.9
Pod undocumented
81 # setup a management command to dump statistics
82 0 ▶ Perlbal::register_global_hook("manage_command.stats", sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
83 0 my @res;
84  
85 # create temporary object for stats storage
86 0 my $gsobj = Perlbal::Plugin::Stats::Storage->new();
87  
88 # dump per service
89 0 foreach my $svc (keys %statobjs) {
90 0 my $sobj = $statobjs{$svc}->[1];
91  
92 # for now, simply dump the numbers we have
93 0 foreach my $key (sort @statkeys) {
94 0 push @res, sprintf("%-15s %-25s %12d", $svc, $key, $sobj->{$key});
95 0 $gsobj->{$key} += $sobj->{$key};
96 }
97 }
98  
99 # global stats
100 0 foreach my $key (sort @statkeys) {
101 0 push @res, sprintf("%-15s %-25s %12d", 'total', $key, $gsobj->{$key});
102 }
103  
104 0 push @res, ".";
105 0 return \@res;
106 0 });
107  
108 # recent requests and how long they took
109 0 ▶ Perlbal::register_global_hook("manage_command.recent", sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
110 0 my @res;
111 0 foreach my $svc (keys %statobjs) {
112 0 my $sobj = $statobjs{$svc}->[1];
113 push @res, "$svc $_"
114 0 foreach @{$sobj->{recent}};
115 }
116  
117 0 push @res, ".";
118 0 return \@res;
119 0 });
120  
121 0 return 1;
122 }
123  
124 # called for a global unload
125 0 ▶sub unload {
Subroutine unload: not called CC=2 SCAR=17.9
Pod undocumented
126 # unregister our global hooks
127 0 Perlbal::unregister_global_hook('manage_command.stats');
128 0 Perlbal::unregister_global_hook('manage_command.recent');
129  
130 # take out all service stuff
131 0 foreach my $statref (values %statobjs) {
132 0 $statref->[0]->unregister_hooks('Stats');
133 }
134 0 %statobjs = ();
135  
136 0 return 1;
137 }
138  
139 # statistics storage object
140 package Perlbal::Plugin::Stats::Storage;
141  
142 1 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
143 1 'files_sent', # files sent from disk (includes reproxies and regular web requests)
144 'files_reproxied', # files we've sent via reproxying (told to by backend)
145 'web_requests', # requests we sent ourselves (no reproxy, no backend)
146 'proxy_requests', # regular requests that went to a backend to be served
147 'proxy_requests_highpri', # same as above, except high priority
148  
149 'pending', # hashref; { "obj" => time_start }
150 'recent', # arrayref; strings of recent URIs and times
151 1 );
152  
153 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
154 0 my Perlbal::Plugin::Stats::Storage $self = shift;
155 0 ▶ $self = fields::new($self) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
156  
157 # 0 initialize everything here
158 0 $self->{$_} = 0 foreach @Perlbal::Plugin::Stats::statkeys;
159  
160 # other setup
161 0 $self->{pending} = {};
162 0 $self->{recent} = [];
163  
164 0 return $self;
165 }
166  
167 1 1;
« Perlbal/Plugin/Cgilike.pm ↑ Summary Perlbal/SocketSSL.pm »