blib/lib/Punk/Observe/Health.pm

filter: Statementstmt 91.6% 229 / 250 Branchbran 68.1% 90 / 132 Conditioncond 51.2% 62 / 121 MC/DCmcdc 19.7% 13 / 66 Subroutinesub 91.6% 22 / 24 Podpod 75.0% 6 / 8 totaltotal 70.2% 422 / 601 CC 124 SCAR 34.6
CC
124
Cov
76%
page_vars
68.6
health_job
42.0
uptime_events
28.5
CRAP 31.7
« blib/lib/Punk/Observe/Ingest.pm ↑ Summary blib/lib/Punk/Observe/Evaluate.pm »
1 package Punk::Observe::Health;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Punk::Observe ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Punk::Observe::Store ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Punk::Observe::Target ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Raw::JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = $Punk::Observe::VERSION;
15  
16 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 M_OK => 'punk.health.ok',
18 M_MS => 'punk.health.ms',
19 2 };
20  
21 # The store's budgets default to 500,000 when left unset. A STRING, not
22 # `1 << 62`, because a perl with 32-bit integers does not have that number and
23 # would quietly pass something else; the store parses nanosecond-width decimals
24 # everywhere already.
25 2 ▶use constant NO_CEILING => '4611686018427387904';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # Every query here is one this file wrote, and every one of them is an
28 # aggregate over the window - so none can be answered by stopping part way. An
29 # uptime band that stops mid-window reports a service as up because the scan
30 # ran out, which is the one reading this page must never give. It is also what
31 # keeps the page cheap: a chunk that truncates is never cached, so a query left
32 # on the default ceiling has its busiest chunk rescanned on every poll.
33 15 ▶sub _read {
Subroutine _read: called CC=2 SCAR=6.9
34 15 my ($store, @args) = @_;
35 15 push @args, limit => NO_CEILING, hard_max => NO_CEILING,
36 max_rows => NO_CEILING;
37 15 ▶ return $store->can('cached_query')
Branch: 1 branch
BranchTrueFalseTotal
$store->can('cached_query') ? : 14 1 15
38 ? $store->cached_query(@args)
39 : $store->query(@args);
40 }
41  
42 519 ▶sub _rec {
Subroutine _rec: called CC=1 SCAR=0.0
43 519 my ($t, $name, $value, $attrs) = @_;
44 519 return { t => $t, kind => 1, body => $name, value => $value + 0,
45 severity => 0, span_kind => 0, status => 0,
46 trace_hi => 0, trace_lo => 0, span_id => 0, parent_id => 0,
47 attrs => $attrs };
48 }
49  
50 172 ▶sub points {
Subroutine points: called CC=22 SCAR=31.2
Pod documented
51 172 my ($target, $status, $body, $now, $rtt_ms) = @_;
52 172 ▶ $now = Punk::Observe::now_ns() unless defined $now;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $now 0 172 172
53 172 my @recs;
54  
55 172 ▶ push @recs, _rec($now, M_MS, 0 + sprintf('%.3f', $rtt_ms),
Branch: 1 branch
BranchTrueFalseTotal
if defined $rtt_ms and $status 3 169 172
Condition100%3/3
defined $rtt_ms and $status
!ll&&!rl&&r
16723
MC/DC100%2/2
defined $rtt_ms and $status
defined $rtt_ms $status
Truth table100%3/3
A and B
A= defined $rtt_msB= $status
ABresult
0X0
100
111
56 { target => $target })
57 if defined $rtt_ms && $status;
58  
59 172 ▶ if (!defined $status || !$status) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $status and $status) 8 164 172
Condition66%2/3
defined $status and $status
!ll&&!rl&&r
08164
MC/DC50%1/2
defined $status and $status
defined $status $status
Truth table66%2/3
A and B
A= defined $statusB= $status
ABresult
0X0
100
111
60 8 push @recs, _rec($now, M_OK, 0,
61 { target => $target, state => 'unreachable' });
62 8 return (\@recs, 'error');
63 }
64  
65 164 my $doc = eval { File::Raw::JSON::file_json_decode($body) };
66 164 ▶ if (!$doc || ref $doc ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $doc or ref $doc ne "HASH") 1 163 164
Condition66%2/3
not $doc or ref $doc ne "HASH"
l!l&&r!l&&!r
10163
MC/DC50%1/2
not $doc or ref $doc ne "HASH"
not $doc ref $doc ne "HASH"
Truth table66%2/3
not(A) or B
A= not $docB= ref $doc ne "HASH"
ABresult
1X1
011
000
67 1 push @recs, _rec($now, M_OK, 0,
68 { target => $target, state => 'unknown' });
69 1 return (\@recs, 'error');
70 }
71  
72 163 ▶ my $ready = (defined $doc->{status} && $doc->{status} eq 'ok') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $doc->{'status'} && $doc->{'status'} eq 'ok' ? : 161 2 163
Condition66%2/3
defined $doc->{"status"} and $doc->{"status"} eq "ok"
!ll&&!rl&&r
02161
MC/DC50%1/2
defined $doc->{"status"} and $doc->{"status"} eq "ok"
defined $doc->{"status"} $doc->{"status"} eq "ok"
Truth table66%2/3
A and B
A= defined $doc->{"status"}B= $doc->{"status"} eq "ok"
ABresult
0X0
100
111
73 163 ▶ $ready = 0 if $status >= 500;
Branch: 1 branch
BranchTrueFalseTotal
if $status >= 500 1 162 163
74 163 ▶ push @recs, _rec($now, M_OK, $ready,
Branch: 1 branch
BranchTrueFalseTotal
$ready ? : 161 2 163
75 { target => $target,
76 state => ($ready ? 'ready' : 'unready') });
77  
78 163 my $checks = $doc->{checks};
79 163 ▶ return (\@recs, $ready ? 'ok' : 'error')
Branch: 2 branches
BranchTrueFalseTotal
unless ref $checks eq "HASH" and %$checks 2 161 163
$ready ? : 2 0 2
Condition66%2/3
ref $checks eq "HASH" and %$checks
!ll&&!rl&&r
20161
MC/DC50%1/2
ref $checks eq "HASH" and %$checks
ref $checks eq "HASH" %$checks
Truth table66%2/3
A and B
A= ref $checks eq "HASH"B= %$checks
ABresult
0X0
100
111
80 unless ref $checks eq 'HASH' && %$checks;
81  
82 161 for my $name (sort keys %$checks) {
83 174 my $c = $checks->{$name};
84 174 ▶ next unless ref $c eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "HASH" 0 174 174
85 174 my $labels = { target => $target, check => $name };
86  
87 174 ▶ if ($c->{skipped}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"skipped"}) 1 173 174
88 1 $labels->{skipped} = 'true';
89 1 push @recs, _rec($now, M_OK, 0, $labels);
90 1 next;
91 }
92  
93 173 ▶ push @recs, _rec($now, M_OK, ($c->{ok} ? 1 : 0), $labels);
Branch: 1 branch
BranchTrueFalseTotal
$c->{'ok'} ? : 164 9 173
94 push @recs, _rec($now, M_MS, ($c->{ms} || 0) + 0, $labels)
95 173 ▶ if defined $c->{ms};
Branch: 1 branch
BranchTrueFalseTotal
if defined $c->{"ms"} 170 3 173
Condition50%1/2
$c->{'ms'} || 0
l!l
1700
MC/DC0%0/1
$c->{'ms'} || 0
$c->{'ms'}
Truth table50%1/2
A
A= $c->{'ms'}
Aresult
11
00
96 }
97  
98 161 ▶ return (\@recs, $ready ? 'ok' : 'error');
Branch: 1 branch
BranchTrueFalseTotal
$ready ? : 159 2 161
99 }
100  
101 8 ▶sub poll {
Subroutine poll: called CC=8 SCAR=21.6
Pod documented
102 8 my ($target, %opt) = @_;
103 8 ▶ my $now = $opt{now} || Punk::Observe::now_ns();
Condition33%1/3
$opt{'now'} || Punk::Observe::now_ns()
l!l&&r!l&&!r
800
MC/DC0%0/2
$opt{'now'} || Punk::Observe::now_ns()
$opt{'now'} Punk::Observe::now_ns()
Truth table33%1/3
A || B
A= $opt{'now'}B= Punk::Observe::now_ns()
ABresult
1X1
011
000
104 8 my $url = $target->{url};
105  
106 8 my $t = Punk::Observe::Target::check($url, $opt{allow});
107 8 ▶ return (points($target->{name}, 0, '', $now))[0] unless $t->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"ok"} 5 3 8
108  
109 3 ▶ my $timeout = ($target->{timeout_ms} || 5000) / 1000;
Condition100%2/2
$target->{'timeout_ms'} || 5000
l!l
21
MC/DC100%1/1
$target->{'timeout_ms'} || 5000
$target->{'timeout_ms'}
Truth table100%2/2
A
A= $target->{'timeout_ms'}
Aresult
11
00
110 3 my ($status, $body) = (0, '');
111 3 require Time::HiRes;
112 3 my $t0 = Time::HiRes::time();
113  
114 3 eval {
115 3 my $ua = $opt{ua};
116 3 ▶ unless ($ua) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ua) 0 3 3
117 $ua = Fetch->new(timeout => $timeout,
118 0 ▶ ($opt{loop} ? (loop => $opt{loop}) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$opt{'loop'} ? : 0 0 0
119 }
120 3 my $res = $ua->get($url)->get;
121 2 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 2 0 2
122 2 $status = $res->status;
123 2 $body = $res->content;
124 2 ▶ $body = '' unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 2 2
125 }
126 2 1;
127 };
128 3 my $rtt_ms = (Time::HiRes::time() - $t0) * 1000;
129 3 return (points($target->{name}, $status, $body, $now, $rtt_ms))[0];
130 }
131  
132 6 ▶sub run {
Subroutine run: called CC=10 SCAR=25.2
Pod documented
133 6 my (%opt) = @_;
134 6 ▶ my $db = $opt{db} or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $db = $opt{"db"} or return [] 6 0 6
135 6 ▶ my $tenant = defined $opt{tenant} ? $opt{tenant} : 'default';
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'tenant'} ? : 0 6 6
136 6 ▶ my $now = $opt{now} || Punk::Observe::now_ns();
Condition66%2/3
$opt{'now'} || Punk::Observe::now_ns()
l!l&&r!l&&!r
150
MC/DC0%0/2
$opt{'now'} || Punk::Observe::now_ns()
$opt{'now'} Punk::Observe::now_ns()
Truth table66%2/3
A || B
A= $opt{'now'}B= Punk::Observe::now_ns()
ABresult
1X1
011
000
137  
138 6 require Punk::Observe::Config;
139 6 my $targets = Punk::Observe::Config::health_targets($db, $tenant);
140 6 ▶ return [] unless ref $targets eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $targets eq "ARRAY" 0 6 6
141  
142 6 my $ua = $opt{ua};
143 6 ▶ if (!$ua && $opt{loop}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ua and $opt{"loop"}) 0 6 6
Condition66%2/3
not $ua and $opt{"loop"}
!ll&&!rl&&r
240
MC/DC0%0/2
not $ua and $opt{"loop"}
not $ua $opt{"loop"}
Truth table66%2/3
not(A) and B
A= not $uaB= $opt{"loop"}
ABresult
0X0
100
111
144 0 eval { require Fetch; $ua = Fetch->new(loop => $opt{loop}); 1 };
145 }
146  
147 6 my @recs;
148 6 for my $t (@$targets) {
149 2 ▶ next unless $t->{enabled};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"enabled"} 1 1 2
150 1 my $r = eval { poll($t, ua => $ua, allow => $opt{allow}, now => $now) };
151 1 ▶ push @recs, @$r if ref $r eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $r eq "ARRAY" 1 0 1
152 }
153 6 return \@recs;
154 }
155  
156 5 ▶sub run_and_store {
Subroutine run_and_store: called CC=7 SCAR=21.3
Pod documented
157 5 my (%opt) = @_;
158 5 ▶ my $store = $opt{store} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $store = $opt{"store"} or return 0 5 0 5
159 5 my $recs = run(%opt);
160 5 ▶ return 0 unless @$recs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$recs 4 1 5
161  
162 1 require Punk::Observe::WAL;
163 1 my $r = Punk::Observe::WAL::append($store->wal_path, $recs, 1, '200000000');
164 1 ▶ return 0 unless $r && $r->{ok} && $r->{frames};
Branch: 1 branch
BranchTrueFalseTotal
unless $r and $r->{"ok"} and $r->{"frames"} 0 1 1
Condition33%2/6
$r and $r->{"ok"} and $r->{"frames"}
!ll&&!rl&&r
001
$r and $r->{"ok"}
!ll&&!rl&&r
001
MC/DC0%0/3
$r and $r->{"ok"} and $r->{"frames"}
$r $r->{"ok"} $r->{"frames"}
Truth table25%1/4
A and B and C
A= $rB= $r->{"ok"}C= $r->{"frames"}
ABCresult
0XX0
10X0
1100
1111
165 1 ▶ $store->seal_if_full($r->{bytes} || 0);
Condition50%1/2
$r->{'bytes'} || 0
l!l
10
MC/DC0%0/1
$r->{'bytes'} || 0
$r->{'bytes'}
Truth table50%1/2
A
A= $r->{'bytes'}
Aresult
11
00
166 1 return scalar @$recs;
167 }
168  
169 3 ▶sub page_vars {
Subroutine page_vars: called CC=43 SCAR=42.3
Pod undocumented
170 3 my (%opt) = @_;
171 3 my $store = $opt{store};
172 3 my $db = $opt{db};
173 3 ▶ my $tenant = defined $opt{tenant} ? $opt{tenant} : 'default';
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'tenant'} ? : 0 3 3
174 3 ▶ my $now = $opt{now} || Punk::Observe::now_ns();
Condition33%1/3
$opt{'now'} || Punk::Observe::now_ns()
l!l&&r!l&&!r
300
MC/DC0%0/2
$opt{'now'} || Punk::Observe::now_ns()
$opt{'now'} Punk::Observe::now_ns()
Truth table33%1/3
A || B
A= $opt{'now'}B= Punk::Observe::now_ns()
ABresult
1X1
011
000
175 3 ▶ my $window = $opt{window_ns} || 3_600 * 1_000_000_000;
Condition50%1/2
$opt{'window_ns'} || 3600000000000
l!l
03
MC/DC0%0/1
$opt{'window_ns'} || 3600000000000
$opt{'window_ns'}
Truth table50%1/2
A
A= $opt{'window_ns'}
Aresult
11
00
176 3 my $from = Punk::Observe::Store::nsub($now, $window);
177  
178 my $targets = $db
179 3 ▶ ? eval { require Punk::Observe::Config;
Branch: 1 branch
BranchTrueFalseTotal
$db ? : 3 0 3
180 3 Punk::Observe::Config::health_targets($db, $tenant) }
181 : undef;
182 3 ▶ $targets = [] unless ref $targets eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $targets eq "ARRAY" 0 3 3
183  
184 3 my @out;
185 3 for my $t (@$targets) {
186 my $row = {
187 name => $t->{name},
188 url => $t->{url},
189 every_s => int(($t->{every_ns} || 60_000_000_000) / 1_000_000_000),
190 timeout_ms => $t->{timeout_ms} || 5000,
191 enabled => ($t->{enabled} ? 1 : 0),
192 6 ▶ state => ($t->{enabled} ? 'never polled' : 'disabled'),
Branch: 2 branches
BranchTrueFalseTotal
$t->{'enabled'} ? : 6 0 6
$t->{'enabled'} ? : 6 0 6
Condition50%2/4
$t->{'every_ns'} || 60000000000
l!l
60
$t->{'timeout_ms'} || 5000
l!l
60
MC/DC0%0/1
$t->{'every_ns'} || 60000000000
$t->{'every_ns'}
MC/DC0%0/1
$t->{'timeout_ms'} || 5000
$t->{'timeout_ms'}
Truth table50%1/2
A
A= $t->{'every_ns'}
Aresult
11
00
Truth table50%1/2
A
A= $t->{'timeout_ms'}
Aresult
11
00
193 ok => 0,
194 held => 0,
195 held_min => 0,
196 checks => [],
197 };
198 6 push @out, $row;
199 6 ▶ next unless $store && $t->{enabled};
Branch: 1 branch
BranchTrueFalseTotal
unless $store and $t->{"enabled"} 0 6 6
Condition33%1/3
$store and $t->{"enabled"}
!ll&&!rl&&r
006
MC/DC0%0/2
$store and $t->{"enabled"}
$store $t->{"enabled"}
Truth table33%1/3
A and B
A= $storeB= $t->{"enabled"}
ABresult
0X0
100
111
200  
201 6 ▶ my $secs = int(($t->{every_ns} || 60_000_000_000) / 1_000_000_000) || 60;
Condition50%2/4
int(($t->{'every_ns'} || 60000000000) / 1000000000) || 60
l!l
60
$t->{'every_ns'} || 60000000000
l!l
60
MC/DC0%0/1
int(($t->{'every_ns'} || 60000000000) / 1000000000) || 60
$t->{'every_ns'}
Truth table50%1/2
A
A= $t->{'every_ns'}
Aresult
11
00
202 my $q = sprintf('metric %s | where target == "%s" | bucket(%ds) min by check',
203 6 M_OK, $t->{name}, $secs);
204 6 my $res = eval { _read($store, $q, from => $from, to => $now) };
205 6 ▶ next unless $res && $res->{ok} && ref $res->{series} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $res and $res->{"ok"} and ref $res->{"series"} eq "ARRAY" 0 6 6
Condition33%2/6
$res and $res->{"ok"} and ref $res->{"series"} eq "ARRAY"
!ll&&!rl&&r
006
$res and $res->{"ok"}
!ll&&!rl&&r
006
MC/DC0%0/3
$res and $res->{"ok"} and ref $res->{"series"} eq "ARRAY"
$res $res->{"ok"} ref $res->{"series"} eq "ARRAY"
Truth table25%1/4
A and B and C
A= $resB= $res->{"ok"}C= ref $res->{"series"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
206  
207 6 for my $s (@{ $res->{series} }) {
208 9 ▶ my @pts = @{ $s->{points} || [] };
Condition50%1/2
$s->{'points'} || []
l!l
90
MC/DC0%0/1
$s->{'points'} || []
$s->{'points'}
Truth table50%1/2
A
A= $s->{'points'}
Aresult
11
00
209 9 ▶ next unless @pts;
Branch: 1 branch
BranchTrueFalseTotal
unless @pts 0 9 9
210 9 @pts = sort { Punk::Observe::Store::ncmp($a->[0], $b->[0]) } @pts;
211  
212 9 my $last = $pts[-1];
213 9 ▶ my $val = ($last->[1] || 0) >= 1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($last->[1] || 0) >= 1 ? : 7 2 9
Condition100%2/2
$last->[1] || 0
l!l
72
MC/DC100%1/1
$last->[1] || 0
$last->[1]
Truth table100%2/2
A
A= $last->[1]
Aresult
11
00
214  
215 9 my $since = $last->[0];
216 9 my $walked = 0;
217 9 for my $p (reverse @pts) {
218 178 ▶ last if ((($p->[1] || 0) >= 1) ? 1 : 0) != $val;
Branch: 2 branches
BranchTrueFalseTotal
if (($p->[1] || 0) >= 1 ? 1 : 0) != $val 2 176 178
($p->[1] || 0) >= 1 ? : 172 6 178
Condition100%2/2
$p->[1] || 0
l!l
1726
MC/DC100%1/1
$p->[1] || 0
$p->[1]
Truth table100%2/2
A
A= $p->[1]
Aresult
11
00
219 176 $since = $p->[0];
220 176 $walked++;
221 }
222 9 my $held = Punk::Observe::Store::nsub($now, $since);
223  
224 9 ▶ my $capped = ($walked >= scalar @pts) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$walked >= scalar @pts ? : 7 2 9
225  
226 9 ▶ my $key = defined $s->{key} ? $s->{key} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $s->{'key'} ? : 9 0 9
227 9 ▶ if (!length $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $key) { } 4 5 9
228 4 $row->{ok} = $val;
229 4 $row->{held} = $held;
230 4 $row->{held_min} = $capped;
231 4 ▶ $row->{state} = $val ? 'ready' : 'not ready';
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 4 0 4
232 }
233 else {
234 5 ▶ push @{ $row->{checks} }, {
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 3 2 5
235 name => $key,
236 ok => $val,
237 state => ($val ? 'ok' : 'failing'),
238 held => $held,
239 held_min => $capped,
240 };
241 }
242 }
243 1 @{ $row->{checks} } = sort { $a->{name} cmp $b->{name} }
244 6 @{ $row->{checks} };
245  
246 my $qm = sprintf('metric %s | where target == "%s" | bucket(%ds) max by check',
247 6 M_MS, $t->{name}, $secs);
248 6 my $ms = eval { _read($store, $qm, from => $from, to => $now) };
249 6 ▶ if ($ms && $ms->{ok} && ref $ms->{series} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ms and $ms->{"ok"} and ref $ms->{"series"} eq "ARRAY") 6 0 6
Condition33%2/6
$ms and $ms->{"ok"} and ref $ms->{"series"} eq "ARRAY"
!ll&&!rl&&r
006
$ms and $ms->{"ok"}
!ll&&!rl&&r
006
MC/DC0%0/3
$ms and $ms->{"ok"} and ref $ms->{"series"} eq "ARRAY"
$ms $ms->{"ok"} ref $ms->{"series"} eq "ARRAY"
Truth table25%1/4
A and B and C
A= $msB= $ms->{"ok"}C= ref $ms->{"series"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
250 6 my %by_check;
251 6 for my $s (@{ $ms->{series} }) {
252 135 my @pts = sort { Punk::Observe::Store::ncmp($a->[0], $b->[0]) }
253 5 ▶ @{ $s->{points} || [] };
Condition50%1/2
$s->{'points'} || []
l!l
50
MC/DC0%0/1
$s->{'points'} || []
$s->{'points'}
Truth table50%1/2
A
A= $s->{'points'}
Aresult
11
00
254 5 ▶ next unless @pts;
Branch: 1 branch
BranchTrueFalseTotal
unless @pts 0 5 5
255 5 ▶ my $key = defined $s->{key} ? $s->{key} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $s->{'key'} ? : 5 0 5
256 5 ▶ my $val = sprintf('%.2f', $pts[-1][1] || 0);
Condition50%1/2
$pts[-1][1] || 0
l!l
50
MC/DC0%0/1
$pts[-1][1] || 0
$pts[-1][1]
Truth table50%1/2
A
A= $pts[-1][1]
Aresult
11
00
257 0 ▶ if (!length $key) { $row->{ms} = $val }
Branch: 1 branch
BranchTrueFalseTotal
if (not length $key) { } 0 5 5
258 5 else { $by_check{$key} = $val }
259 }
260 6 for my $c (@{ $row->{checks} }) {
261 $c->{ms} = $by_check{ $c->{name} }
262 5 ▶ if exists $by_check{ $c->{name} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $by_check{$c->{"name"}} 5 0 5
263 }
264 }
265 }
266 3 return \@out;
267 }
268  
269 2 my $UP_BUCKETS = 120; # across the window, whatever it is
270 2 my $UP_MIN_NS = 30 * 1_000_000_000;
271  
272 3 ▶sub _up_bucket_ns {
Subroutine _up_bucket_ns: called CC=3 SCAR=11.0
273 3 my ($from, $to) = @_;
274 3 my $span = Punk::Observe::Store::nsub($to, $from);
275 3 my $each = int(($span + 0) / $UP_BUCKETS);
276 3 ▶ $each = $UP_MIN_NS if $each < $UP_MIN_NS;
Branch: 1 branch
BranchTrueFalseTotal
if $each < $UP_MIN_NS 1 2 3
277 3 ▶ my $secs = int($each / 1_000_000_000) || 1;
Condition50%1/2
int $each / 1000000000 || 1
l!l
30
MC/DC0%0/1
int $each / 1000000000 || 1
int $each / 1000000000
Truth table50%1/2
A
A= int $each / 1000000000
Aresult
11
00
278 3 return $secs;
279 }
280  
281 5 ▶sub uptime_events {
Subroutine uptime_events: called CC=21 SCAR=33.5
Pod documented
282 5 my (%opt) = @_;
283 5 ▶ my $store = $opt{store} or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $store = $opt{"store"} or return [] 4 1 5
284 4 my ($from, $to) = @opt{qw(from to)};
285 4 ▶ return [] unless defined $from && defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from and defined $to 1 3 4
Condition66%2/3
defined $from and defined $to
!ll&&!rl&&r
103
MC/DC50%1/2
defined $from and defined $to
defined $from defined $to
Truth table66%2/3
A and B
A= defined $fromB= defined $to
ABresult
0X0
100
111
286  
287 3 my $secs = _up_bucket_ns($from, $to);
288 3 my $res = eval {
289 3 _read($store, 'metric ' . M_OK . " | bucket(${secs}s) min by target, check",
290 from => $from, to => $to)
291 };
292 3 ▶ return [] unless $res && $res->{ok} && ($res->{shape} || '') eq 'buckets';
Branch: 1 branch
BranchTrueFalseTotal
unless $res and $res->{"ok"} and ($res->{"shape"} || "") eq "buckets" 0 3 3
Condition37%3/8
$res and $res->{"ok"} and ($res->{"shape"} || "") eq "buckets"
!ll&&!rl&&r
003
$res and $res->{"ok"}
!ll&&!rl&&r
003
$res->{'shape'} || ''
l!l
30
MC/DC0%0/3
$res and $res->{"ok"} and ($res->{"shape"} || "") eq "buckets"
$res $res->{"ok"} $res->{'shape'}
Truth table25%1/4
A and B and C
A= $resB= $res->{"ok"}C= $res->{'shape'}
ABCresult
0XX0
10X0
1100
1111
293  
294 3 my @ev;
295 3 ▶ for my $ser (@{ $res->{series} || [] }) {
Condition50%1/2
$res->{'series'} || []
l!l
30
MC/DC0%0/1
$res->{'series'} || []
$res->{'series'}
Truth table50%1/2
A
A= $res->{'series'}
Aresult
11
00
296 6 ▶ my ($target, $check) = split /\x1f/, ($ser->{key} // ''), 2;
Condition50%1/2
$ser->{"key"} // ""
l!l
60
MC/DC0%0/1
$ser->{"key"} // ""
$ser->{"key"}
Truth table50%1/2
A
A= $ser->{"key"}
Aresult
11
00
297 6 ▶ next unless defined $target && length $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target and length $target 0 6 6
Condition33%1/3
defined $target and length $target
!ll&&!rl&&r
006
MC/DC0%0/2
defined $target and length $target
defined $target length $target
Truth table33%1/3
A and B
A= defined $targetB= length $target
ABresult
0X0
100
111
298 6 ▶ my $label = (defined $check && length $check)
Branch: 1 branch
BranchTrueFalseTotal
defined $check && length $check ? : 2 4 6
Condition66%2/3
defined $check and length $check
!ll&&!rl&&r
042
MC/DC50%1/2
defined $check and length $check
defined $check length $check
Truth table66%2/3
A and B
A= defined $checkB= length $check
ABresult
0X0
100
111
299 ? "$target \x{203a} $check" : $target;
300  
301 60 ▶ my @pts = sort { length($a->[0]) <=> length($b->[0])
Condition33%1/3
length $Punk::Observe::Health::a->[0] <=> length $Punk::Observe::Health::b->[0] || $Punk::Observe::Health::a->[0] cmp $Punk::Observe::Health::b->[0]
l!l&&r!l&&!r
0600
MC/DC0%0/2
length $Punk::Observe::Health::a->[0] <=> length $Punk::Observe::Health::b->[0] || $Punk::Observe::Health::a->[0] cmp $Punk::Observe::Health::b->[0]
length $Punk::Observe::Health::a->[0] <=> length $Punk::Observe::Health::b->[0] $Punk::Observe::Health::a->[0] cmp $Punk::Observe::Health::b->[0]
Truth table33%1/3
A || B
A= length $Punk::Observe::Health::a->[0] <=> length $Punk::Observe::Health::b->[0]B= $Punk::Observe::Health::a->[0] cmp $Punk::Observe::Health::b->[0]
ABresult
1X1
011
000
302 6 ▶ || $a->[0] cmp $b->[0] } @{ $ser->{points} || [] };
Condition50%1/2
$ser->{'points'} || []
l!l
60
MC/DC0%0/1
$ser->{'points'} || []
$ser->{'points'}
Truth table50%1/2
A
A= $ser->{'points'}
Aresult
11
00
303 6 my $last = '';
304 6 for my $p (@pts) {
305 36 ▶ my $state = (($p->[1] // 0) >= 1) ? 'up' : 'down';
Branch: 1 branch
BranchTrueFalseTotal
($p->[1] // 0) >= 1 ? : 33 3 36
Condition50%1/2
$p->[1] // 0
l!l
360
MC/DC0%0/1
$p->[1] // 0
$p->[1]
Truth table50%1/2
A
A= $p->[1]
Aresult
11
00
306 36 ▶ next if $state eq $last;
Branch: 1 branch
BranchTrueFalseTotal
if $state eq $last 27 9 36
307 9 push @ev, { series => $label, at => "$p->[0]", to => $state };
308 9 $last = $state;
309 }
310 }
311 3 return \@ev;
312 }
313  
314 0 ▶sub _plugin_state {
Subroutine _plugin_state: not called CC=2 SCAR=17.9
315 0 my ($class) = @_;
316 0 require Punk::Plugin::Observe;
317 0 ▶ my $st = Punk::Plugin::Observe->state_for($class)
Branch: 1 branch
BranchTrueFalseTotal
my $st = "Punk::Plugin::Observe"->state_for($class) or Carp::croak('Punk::Observe::Health: no Observe plugin state for ' . "$class - is the worker running the same application " . 'class as the server?') 0 0 0
318 or Carp::croak("Punk::Observe::Health: no Observe plugin state for "
319 . "$class - is the worker running the same application "
320 . "class as the server?");
321 0 return $st;
322 }
323  
324 0 ▶sub health_job {
Subroutine health_job: not called CC=6 SCAR=37.4
Pod undocumented
325 0 my ($job, $class) = @_;
326 0 my $st = _plugin_state($class);
327 0 my $q = $job->queue_object;
328  
329 0 my $owner = 0 + $$;
330 0 ▶ return { skipped => 'lock' }
Branch: 1 branch
BranchTrueFalseTotal
unless $q->lock("observe.health", 60, "owner", $owner) 0 0 0
331 unless $q->lock('observe.health', 60, owner => $owner);
332  
333 0 my $n = eval { run_and_store(
334 db => $st->{db},
335 store => Punk::Plugin::Observe::store_for(
336 $st, $st->{tenant}{fixed} || 'default'),
337 allow => $st->{opts}{health_allow},
338 0 ▶ ) };
Condition0%0/2
$st->{'tenant'}{'fixed'} || 'default'
l!l
00
MC/DC0%0/1
$st->{'tenant'}{'fixed'} || 'default'
$st->{'tenant'}{'fixed'}
Truth table0%0/2
A
A= $st->{'tenant'}{'fixed'}
Aresult
11
00
339 0 my $err = $@;
340 0 ▶ $q->unlock('observe.health', $owner) if $q->can('unlock');
Branch: 1 branch
BranchTrueFalseTotal
if $q->can("unlock") 0 0 0
341 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
342 0 ▶ return { points => $n || 0 };
Condition0%0/2
$n || 0
l!l
00
MC/DC0%0/1
$n || 0
$n
Truth table0%0/2
A
A= $n
Aresult
11
00
343 }
344  
345 5 ▶sub cron_task {
Subroutine cron_task: called CC=4 SCAR=14.1
Pod documented
346 5 my (%opt) = @_;
347 5 my $db = $opt{db};
348 5 my $store = $opt{store};
349 5 my $owner = $opt{owner};
350 5 ▶ $owner = $$ unless defined $owner && $owner =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $owner and $owner =~ /\A[0-9]+\z/ 3 2 5
Condition100%3/3
defined $owner and $owner =~ /\A[0-9]+\z/
!ll&&!rl&&r
122
MC/DC100%2/2
defined $owner and $owner =~ /\A[0-9]+\z/
defined $owner $owner =~ /\A[0-9]+\z/
Truth table100%3/3
A and B
A= defined $ownerB= $owner =~ /\A[0-9]+\z/
ABresult
0X0
100
111
351 5 ▶ my $lease = $opt{lease_seconds} || 30;
Condition50%1/2
$opt{'lease_seconds'} || 30
l!l
05
MC/DC0%0/1
$opt{'lease_seconds'} || 30
$opt{'lease_seconds'}
Truth table50%1/2
A
A= $opt{'lease_seconds'}
Aresult
11
00
352  
353 6 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=20.6
354 6 my ($q) = @_;
355 6 ▶ return 0 unless $q && $db && $store;
Branch: 1 branch
BranchTrueFalseTotal
unless $q and $db and $store 0 6 6
Condition33%2/6
$q and $db and $store
!ll&&!rl&&r
006
$q and $db
!ll&&!rl&&r
006
MC/DC0%0/3
$q and $db and $store
$q $db $store
Truth table25%1/4
A and B and C
A= $qB= $dbC= $store
ABCresult
0XX0
10X0
1100
1111
356  
357 6 ▶ return 0 unless $q->lock('leader', $lease, owner => $owner);
Branch: 1 branch
BranchTrueFalseTotal
unless $q->lock("leader", $lease, "owner", $owner) 1 5 6
358  
359 5 my $n = eval {
360 run_and_store(db => $db, store => $store,
361 tenant => $opt{tenant}, allow => $opt{allow},
362 5 loop => $opt{loop}, ua => $opt{ua});
363 };
364 5 my $err = $@;
365  
366 5 $q->renew_lock('leader', $owner, $lease);
367  
368 5 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 5 5
369 5 ▶ return $n || 0;
Condition100%2/2
$n || 0
l!l
14
MC/DC0%0/1
$n || 0
$n
Truth table50%1/2
A
A= $n
Aresult
11
00
370 5 };
371 }
372  
373 2 1;
374  
375 __END__
« blib/lib/Punk/Observe/Ingest.pm ↑ Summary blib/lib/Punk/Observe/Evaluate.pm »