blib/lib/Net/Prometheus/ServerStatusLiteCollector.pm

filter: Statementstmt 28.8% 36 / 125 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 62.5% 10 / 16 Podpod 0.0% 0 / 5 totaltotal 22.0% 46 / 209 CC 30 SCAR 59.4
CC
30
Cov
20%
collect
650.9
new
20.0
__ANON__
6.0
CRAP 378.5
↑ Summary
1 package Net::Prometheus::ServerStatusLiteCollector;
2  
3 # ABSTRACT: A Net::Prometheus Collector that works in tandem with Plack::Middleware::ServerStatus::Lite
4  
5 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DateTime::Format::ISO8601::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Net::Prometheus::Types qw(MetricSamples Sample);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Parallel::Scoreboard;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = "0.02";
16  
17 1 my $JSON = JSON->new->utf8(0);
18 1 my $dt_formatter = DateTime::Format::ISO8601::Format->new(second_precision => 3);
19  
20 # Basic get/set attribute methods
21 1 my @attrs = qw(
22 counter_file
23 scoreboard
24 labels
25 );
26 1 foreach my $attr (@attrs) {
27 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 0 ▶ *$attr = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
29 0 my $self = shift;
30 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
31 0 return $self->{$attr} = $_[0];
32 } else {
33 0 return $self->{$attr};
34 }
35 3 };
36 }
37  
38 0 ▶sub log {
Subroutine log: not called CC=1 SCAR=6.9
Pod undocumented
39 0 my $self = shift;
40 0 my ($level, $msg) = @_;
41  
42 0 my $timestamp = DateTime->now(
43 formatter => $dt_formatter,
44 time_zone => 'America/Los_Angeles',
45 )->stringify;
46  
47 0 print STDERR $JSON->encode({
48 category => 'Plack',
49 middleware => __PACKAGE__,
50 level => $level,
51 pid => $$,
52 timestamp => $timestamp,
53 message => $msg,
54 });
55 }
56  
57 0 ▶sub warn {
Subroutine warn: not called CC=1 SCAR=6.9
Pod undocumented
58 0 my $self = shift;
59 0 my ($msg) = @_;
60  
61 0 $self->log('WARN', $msg);
62 }
63  
64 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my $self = shift;
66 0 my ($msg) = @_;
67  
68 0 $self->log('ERROR', $msg)
69 }
70  
71 # Simple class initialization, pass in a hashref of arguments or directly
72 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
73 0 my $class = shift;
74 0 ▶ my %args = ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
75  
76 0 my $self = bless {}, $class;
77  
78 # Copy keys from %args, but ignore invalid attributes
79 0 for my $k (@attrs) {
80 0 ▶ $self->$k($args{$k}) if defined $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$k} 0 0 0
81 }
82  
83 0 return $self
84 }
85  
86 0 ▶sub collect {
Subroutine collect: not called CC=26 SCAR=64.8
Pod undocumented
87 0 my $self = shift;
88  
89 # Ripped from https://metacpan.org/source/KAZEBURO/Plack-Middleware-ServerStatus-Lite-0.36/bin/server-status
90  
91 0 my $stats = {};
92 # Must use an already open instance of Parallel::Scoreboard since
93 # initializing new will clear out scoreboard directory
94 0 ▶ if ($self->scoreboard && ref $self->scoreboard eq 'Parallel::Scoreboard') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scoreboard and ref $self->scoreboard eq "Parallel::Scoreboard") 0 0 0
Condition0%0/3
$self->scoreboard and ref $self->scoreboard eq "Parallel::Scoreboard"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->scoreboard and ref $self->scoreboard eq "Parallel::Scoreboard"
$self->scoreboard ref $self->scoreboard eq "Parallel::Scoreboard"
Truth table0%0/3
A and B
A= $self->scoreboardB= ref $self->scoreboard eq "Parallel::Scoreboard"
ABresult
0X0
100
111
95 0 $stats = $self->scoreboard->read_all();
96 }
97 0 ▶ if (! scalar %$stats) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar %$stats) 0 0 0
98 0 $self->warn("There is no status file in scoreboard directory. Maybe all processes are idle state and do not serve any request yet.");
99 0 return ();
100 }
101  
102 # Check counter file
103 0 my $counter_fh;
104 0 ▶ if ($self->counter_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->counter_file) 0 0 0
105 0 ▶ unless (open($counter_fh, '<:unix', $self->counter_file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $counter_fh, "<:unix", $self->counter_file) 0 0 0
106 0 $self->error("Could not open counter file: $!");
107 0 return ();
108 }
109 }
110  
111 # Check scoreboard stats is valid
112 0 my @all_workers = keys %$stats;
113 0 my $pstatus = eval {
114 0 ▶ $JSON->decode($stats->{$all_workers[0]} || '{}');
Condition0%0/2
$$stats{$all_workers[0]} || '{}'
l!l
00
MC/DC0%0/1
$$stats{$all_workers[0]} || '{}'
$$stats{$all_workers[0]}
Truth table0%0/2
A
A= $$stats{$all_workers[0]}
Aresult
11
00
115 };
116 0 ▶ if (!$pstatus->{ppid} || !$pstatus->{uptime} || !$pstatus->{ppid}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pstatus->{"ppid"} and $pstatus->{"uptime"} and $pstatus->{"ppid"}) 0 0 0
Condition0%0/6
$pstatus->{"ppid"} and $pstatus->{"uptime"} and $pstatus->{"ppid"}
!ll&&!rl&&r
000
$pstatus->{"ppid"} and $pstatus->{"uptime"}
!ll&&!rl&&r
000
MC/DC0%0/3
$pstatus->{"ppid"} and $pstatus->{"uptime"} and $pstatus->{"ppid"}
$pstatus->{"ppid"} $pstatus->{"uptime"} $pstatus->{"ppid"}
Truth table0%0/4
A and B and C
A= $pstatus->{"ppid"}B= $pstatus->{"uptime"}C= $pstatus->{"ppid"}
ABCresult
0XX0
10X0
1100
1111
117 0 $self->error("Status file does not have some necessary variables");
118 0 return ();
119 }
120 0 my $parent_pid = $pstatus->{ppid};
121  
122 # Begin compiling stats
123 0 my @samples = ();
124 push @samples,
125 MetricSamples('plack_uptime', gauge => 'Uptime of Plack server',
126 0 [Sample('plack_uptime', $self->labels, (time - $pstatus->{uptime}))]);
127  
128 # Compile request counter stats
129 0 ▶ if ($counter_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($counter_fh) 0 0 0
130 0 seek $counter_fh, 10, 0;
131 0 sysread $counter_fh, my $counter, 20;
132 0 sysread $counter_fh, my $total_bytes, 20;
133 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 0 $counter += 0;
135 0 $total_bytes += 0;
136 0 my $total_kbytes = int($total_bytes / 1_000);
137 0 push @samples,
138 MetricSamples('plack_number_served_requests', gauge => 'Number of requests served by Plack process',
139 [Sample('plack_number_served_requests', $self->labels, $counter)]);
140 0 push @samples,
141 MetricSamples('plack_total_kbytes_served', gauge => 'Total Kilobytes served by Plack process',
142 [Sample('plack_total_kbytes_served', $self->labels, $total_kbytes)]);
143 }
144  
145 # Obtain all worker process IDs
146 0 @all_workers = ();
147 0 ▶ my $psopt = $^O =~ m/bsd$/ ? '-ax' : '-e';
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /bsd$/ ? : 0 0 0
148 0 my $ps = `LC_ALL=C command ps $psopt -o ppid,pid`;
149 0 $ps =~ s/^\s+//mg;
150 0 for my $line (split /\n/, $ps) {
151 0 ▶ next if $line =~ m/^\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\D/ 0 0 0
152 0 my ($ppid, $pid) = split /\s+/, $line, 2;
153 0 ▶ push @all_workers, $pid if $ppid == $parent_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $ppid == $parent_pid 0 0 0
154 }
155  
156 # Count busy and idle workers
157 0 my $idle = 0;
158 0 my $busy = 0;
159 0 my @process_status;
160 0 for my $pid (@all_workers) {
161 0 my $json = $stats->{$pid};
162 0 $pstatus = eval {
163 0 ▶ $JSON->decode($json || '{}');
Condition0%0/2
$json || '{}'
l!l
00
MC/DC0%0/1
$json || '{}'
$json
Truth table0%0/2
A
A= $json
Aresult
11
00
164 };
165 0 ▶ $pstatus ||= {};
Condition0%0/2
$pstatus ||= {}
l!l
00
MC/DC0%0/1
$pstatus ||= {}
$pstatus
Truth table0%0/2
A
A= $pstatus
Aresult
11
00
166 0 ▶ if ($pstatus->{status} && $pstatus->{status} eq 'A') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pstatus->{'status'} and $pstatus->{'status'} eq 'A') { } 0 0 0
Condition0%0/3
$pstatus->{"status"} and $pstatus->{"status"} eq "A"
!ll&&!rl&&r
000
MC/DC0%0/2
$pstatus->{"status"} and $pstatus->{"status"} eq "A"
$pstatus->{"status"} $pstatus->{"status"} eq "A"
Truth table0%0/3
A and B
A= $pstatus->{"status"}B= $pstatus->{"status"} eq "A"
ABresult
0X0
100
111
167 0 $busy++;
168 }
169 else {
170 0 $idle++;
171 }
172  
173 0 ▶ if (defined $pstatus->{time}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pstatus->{"time"}) 0 0 0
174 0 $pstatus->{ss} = time - $pstatus->{time};
175 }
176 0 ▶ $pstatus->{pid} ||= $pid;
Condition0%0/3
$pstatus->{"pid"} ||= $pid
l!l&&r!l&&!r
000
MC/DC0%0/2
$pstatus->{"pid"} ||= $pid
$pstatus->{"pid"} $pid
Truth table0%0/3
A ||= B
A= $pstatus->{"pid"}B= $pid
ABresult
1X1
011
000
177 0 delete $pstatus->{time};
178 0 delete $pstatus->{ppid};
179 0 delete $pstatus->{uptime};
180 0 push @process_status, $pstatus;
181 }
182 0 push @samples,
183 MetricSamples('plack_busy_workers', gauge => 'Number of busy Plack workers',
184 [Sample('plack_busy_workers', $self->labels, $busy)]);
185 0 push @samples,
186 MetricSamples('plack_idle_workers', gauge => 'Number of idle Plack workers',
187 [Sample('plack_idle_workers', $self->labels, $idle)]);
188  
189 0 $stats = {};
190 0 foreach my $pstatus (@process_status) {
191 0 foreach my $stat (qw(method uri remote_addr protocol)) {
192 0 ▶ $stats->{$stat}{$pstatus->{$stat}}++ if $pstatus->{$stat};
Branch: 1 branch
BranchTrueFalseTotal
if $pstatus->{$stat} 0 0 0
193 }
194 }
195 0 foreach my $stat (qw(method uri remote_addr protocol)) {
196 0 my $stat_counts = $stats->{$stat};
197 push @samples,
198 MetricSamples("plack_sample_$stat", gauge => "Count of $stat for sample of requests",
199 [
200 map {
201 0 Sample(
202 "plack_sample_$stat",
203 0 [@{$self->labels}, $stat => $_],
204 0 $stat_counts->{$_}
205 )
206 }
207 (keys %$stat_counts)
208 ]
209 );
210 }
211  
212 0 return @samples;
213 }
214  
215 1 1;
216 __END__
↑ Summary