| 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"
MC/DC0%0/1
$config->{"path"} // "/health"
$config->{"path"}
Truth table50%1/2
A
A= $config->{"path"}
| |||||||||||||||||||||||||||||
| 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"} // {}
MC/DC100%1/1
$config->{"checks"} // {}
$config->{"checks"}
Truth table100%2/2
A
A= $config->{"checks"}
| |||||||||||||||||||||||||||||
| 68 | 4 | ▶ | $self->{include_details} = $config->{include_details} // 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"include_details"} // 1
MC/DC0%0/1
$config->{"include_details"} // 1
$config->{"include_details"}
Truth table50%1/2
A
A= $config->{"include_details"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $self->{"live_path"} and $path eq $self->{"live_path"}
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"}
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $self->{"ready_path"} and $path eq $self->{"ready_path"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 133 | status => $healthy ? 'ok' : 'error', | ||||||||||||||||||||||||||||
| 134 | }; | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | $response->{checks} = $results if $self->{include_details}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my $body = JSON::MaybeXS::encode_json($response); | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | my $status = $healthy ? 200 : 503; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 162 | status => $healthy ? 'ok' : 'error', | ||||||||||||||||||||||||||||
| 163 | timestamp => time(), | ||||||||||||||||||||||||||||
| 164 | }; | ||||||||||||||||||||||||||||
| 165 | 2 | ▶ | $response->{checks} = $results if $self->{include_details} && keys %{$self->{checks}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"include_details"} and keys %{$$self{"checks"};}
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"};}
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 2 | my $body = JSON::MaybeXS::encode_json($response); | |||||||||||||||||||||||||||
| 168 | 2 | ▶ | my $status = $healthy ? 200 : 503; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 199 | 1 | $result->{status} = 'error'; | |||||||||||||||||||||||||||
| 200 | 1 | $all_healthy = 0; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | }; | ||||||||||||||||||||||||||||
| 203 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||