blib/lib/PAGI/Middleware/Healthcheck.pm

filter: Statementstmt 80.4% 70 / 87 Branchbran 62.5% 15 / 24 Conditioncond 60.0% 9 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 91.6% 11 / 12 Podpod 100% 1 / 1 totaltotal 73.6% 109 / 148 CC 20 SCAR 18.8
CC
20
Cov
75%
_send_ready
20.0
__ANON__
8.3
_send_health
5.0
CRAP 6.6
« blib/lib/PAGI/Request/Negotiate.pm ↑ Summary blib/lib/PAGI/Request/MultipartStream.pm »
1 package PAGI::Middleware::Healthcheck;
2 1 $PAGI::Middleware::Healthcheck::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use parent 'PAGI::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 PAGI::Middleware::Healthcheck - Health check endpoint middleware
12  
13 =head1 SYNOPSIS
14  
15 use PAGI::Middleware::Builder;
16  
17 my $app = builder {
18 enable 'Healthcheck',
19 path => '/health',
20 checks => {
21 database => sub { check_db_connection() },
22 cache => sub { check_redis() },
23 };
24 $my_app;
25 };
26  
27 =head1 DESCRIPTION
28  
29 PAGI::Middleware::Healthcheck provides a health check endpoint for
30 load balancers and monitoring systems. Returns JSON status information.
31  
32 =head1 CONFIGURATION
33  
34 =over 4
35  
36 =item * path (default: '/health')
37  
38 Path for the health check endpoint.
39  
40 =item * live_path (optional)
41  
42 Separate path for liveness probe (always returns 200 if server is running).
43  
44 =item * ready_path (optional)
45  
46 Separate path for readiness probe (runs all checks).
47  
48 =item * checks (optional)
49  
50 Hashref of named health checks. Each check is a coderef that returns
51 true (healthy) or false (unhealthy), or throws an exception.
52  
53 =item * include_details (default: 1)
54  
55 Include individual check results in response.
56  
57 =back
58  
59 =cut
60  
61 4 ▶sub _init {
Subroutine _init: called CC=4 SCAR=14.0
62 4 my ($self, $config) = @_;
63  
64 4 ▶ $self->{path} = $config->{path} // '/health';
Condition50%1/2
$config->{"path"} // "/health"
l!l
40
MC/DC0%0/1
$config->{"path"} // "/health"
$config->{"path"}
Truth table50%1/2
A
A= $config->{"path"}
Aresult
11
00
65 4 $self->{live_path} = $config->{live_path};
66 4 $self->{ready_path} = $config->{ready_path};
67 4 ▶ $self->{checks} = $config->{checks} // {};
Condition100%2/2
$config->{"checks"} // {}
l!l
22
MC/DC100%1/1
$config->{"checks"} // {}
$config->{"checks"}
Truth table100%2/2
A
A= $config->{"checks"}
Aresult
11
00
68 4 ▶ $self->{include_details} = $config->{include_details} // 1;
Condition50%1/2
$config->{"include_details"} // 1
l!l
04
MC/DC0%0/1
$config->{"include_details"} // 1
$config->{"include_details"}
Truth table50%1/2
A
A= $config->{"include_details"}
Aresult
11
00
69 }
70  
71 4 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.2
Pod documented
72 4 my ($self, $app) = @_;
73  
74 4 ▶ return async sub {
Subroutine __ANON__: called CC=7 SCAR=21.2
75 4 my ($scope, $receive, $send) = @_;
76 4 ▶ if ($scope->{type} ne 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "http") 0 4 4
77 0 await $app->($scope, $receive, $send);
78 0 return;
79 }
80  
81 4 my $path = $scope->{path};
82  
83 # Liveness probe (just check server is responding)
84 4 ▶ if (defined $self->{live_path} && $path eq $self->{live_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"live_path"} and $path eq $self->{"live_path"}) 1 3 4
Condition66%2/3
defined $self->{"live_path"} and $path eq $self->{"live_path"}
!ll&&!rl&&r
301
MC/DC50%1/2
defined $self->{"live_path"} and $path eq $self->{"live_path"}
defined $self->{"live_path"} $path eq $self->{"live_path"}
Truth table66%2/3
A and B
A= defined $self->{"live_path"}B= $path eq $self->{"live_path"}
ABresult
0X0
100
111
85 1 await $self->_send_live($send);
86 1 return;
87 }
88  
89 # Readiness probe (run all checks)
90 3 ▶ if (defined $self->{ready_path} && $path eq $self->{ready_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ready_path"} and $path eq $self->{"ready_path"}) 0 3 3
Condition33%1/3
defined $self->{"ready_path"} and $path eq $self->{"ready_path"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $self->{"ready_path"} and $path eq $self->{"ready_path"}
defined $self->{"ready_path"} $path eq $self->{"ready_path"}
Truth table33%1/3
A and B
A= defined $self->{"ready_path"}B= $path eq $self->{"ready_path"}
ABresult
0X0
100
111
91 0 await $self->_send_ready($send);
92 0 return;
93 }
94  
95 # Main health check endpoint
96 3 ▶ if ($path eq $self->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq $self->{"path"}) 2 1 3
97 2 await $self->_send_health($send);
98 2 return;
99 }
100  
101 # Not a health check path, pass through
102 1 await $app->($scope, $receive, $send);
103 4 };
104 }
105  
106 1 ▶async sub _send_live {
Subroutine _send_live: called CC=1 SCAR=0.0
107 1 my ($self, $send) = @_;
108  
109 1 my $body = JSON::MaybeXS::encode_json({ status => 'ok' });
110  
111 1 await $send->({
112 type => 'http.response.start',
113 status => 200,
114 headers => [
115 ['Content-Type', 'application/json'],
116 ['Content-Length', length($body)],
117 ['Cache-Control', 'no-cache, no-store'],
118 ],
119 });
120 1 await $send->({
121 type => 'http.response.body',
122 body => $body,
123 more => 0,
124 });
125 1 }
126  
127 0 ▶async sub _send_ready {
Subroutine _send_ready: not called CC=4 SCAR=30.0
128 0 my ($self, $send) = @_;
129  
130 0 my ($healthy, $results) = $self->_run_checks();
131  
132 0 ▶ my $response = {
Branch: 1 branch
BranchTrueFalseTotal
$healthy ? : 0 0 0
133 status => $healthy ? 'ok' : 'error',
134 };
135 0 ▶ $response->{checks} = $results if $self->{include_details};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_details"} 0 0 0
136  
137 0 my $body = JSON::MaybeXS::encode_json($response);
138 0 ▶ my $status = $healthy ? 200 : 503;
Branch: 1 branch
BranchTrueFalseTotal
$healthy ? : 0 0 0
139  
140 0 await $send->({
141 type => 'http.response.start',
142 status => $status,
143 headers => [
144 ['Content-Type', 'application/json'],
145 ['Content-Length', length($body)],
146 ['Cache-Control', 'no-cache, no-store'],
147 ],
148 });
149 0 await $send->({
150 type => 'http.response.body',
151 body => $body,
152 more => 0,
153 });
154 1 }
155  
156 2 ▶async sub _send_health {
Subroutine _send_health: called CC=5 SCAR=16.1
157 2 my ($self, $send) = @_;
158  
159 2 my ($healthy, $results) = $self->_run_checks();
160  
161 2 ▶ my $response = {
Branch: 1 branch
BranchTrueFalseTotal
$healthy ? : 1 1 2
162 status => $healthy ? 'ok' : 'error',
163 timestamp => time(),
164 };
165 2 ▶ $response->{checks} = $results if $self->{include_details} && keys %{$self->{checks}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_details"} and keys %{$$self{"checks"};} 1 1 2
Condition66%2/3
$self->{"include_details"} and keys %{$$self{"checks"};}
!ll&&!rl&&r
011
MC/DC50%1/2
$self->{"include_details"} and keys %{$$self{"checks"};}
$self->{"include_details"} keys %{$$self{"checks"};}
Truth table66%2/3
A and B
A= $self->{"include_details"}B= keys %{$$self{"checks"};}
ABresult
0X0
100
111
166  
167 2 my $body = JSON::MaybeXS::encode_json($response);
168 2 ▶ my $status = $healthy ? 200 : 503;
Branch: 1 branch
BranchTrueFalseTotal
$healthy ? : 1 1 2
169  
170 2 await $send->({
171 type => 'http.response.start',
172 status => $status,
173 headers => [
174 ['Content-Type', 'application/json'],
175 ['Content-Length', length($body)],
176 ['Cache-Control', 'no-cache, no-store'],
177 ],
178 });
179 2 await $send->({
180 type => 'http.response.body',
181 body => $body,
182 more => 0,
183 });
184 1 }
185  
186 2 ▶sub _run_checks {
Subroutine _run_checks: called CC=4 SCAR=14.3
187 2 my ($self) = @_;
188  
189 2 my %results;
190 2 my $all_healthy = 1;
191  
192 2 for my $name (sort keys %{$self->{checks}}) {
193 2 my $check = $self->{checks}{$name};
194 2 my $result = { status => 'ok' };
195  
196 2 eval {
197 2 my $ok = $check->();
198 2 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 1 2
199 1 $result->{status} = 'error';
200 1 $all_healthy = 0;
201 }
202 };
203 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
204 0 $result->{status} = 'error';
205 0 $result->{message} = "$@";
206 0 $result->{message} =~ s/\s+$//;
207 0 $all_healthy = 0;
208 }
209  
210 2 $results{$name} = $result;
211 }
212  
213 2 return ($all_healthy, \%results);
214 }
215  
216 1 1;
217  
218 __END__
« blib/lib/PAGI/Request/Negotiate.pm ↑ Summary blib/lib/PAGI/Request/MultipartStream.pm »