| 1 | package HealthCheck; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use parent 'HealthCheck::Diagnostic'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: A health check for your code | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = 'v1.9.3'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Hash::Util::FieldHash; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use List::Util qw(any uniq); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | # Create a place outside of $self to store the checks | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # as everything in the self hashref will be copied into | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # the result. | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | Hash::Util::FieldHash::fieldhash my %registered_checks; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod use HealthCheck; | ||||||||||||||||||||||||||||||||||||||||||
| 25 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 26 | #pod # a check can return a hashref containing anything at all, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | #pod # however some values are special. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | #pod # See the HealthCheck Standard for details. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | #pod sub my_check { | ||||||||||||||||||||||||||||||||||||||||||
| 30 | #pod return { | ||||||||||||||||||||||||||||||||||||||||||
| 31 | #pod anything => "at all", | ||||||||||||||||||||||||||||||||||||||||||
| 32 | #pod id => "my_check", | ||||||||||||||||||||||||||||||||||||||||||
| 33 | #pod status => 'WARNING', | ||||||||||||||||||||||||||||||||||||||||||
| 34 | #pod }; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | #pod } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 37 | #pod my $checker = HealthCheck->new( | ||||||||||||||||||||||||||||||||||||||||||
| 38 | #pod id => 'main_checker', | ||||||||||||||||||||||||||||||||||||||||||
| 39 | #pod label => 'Main Health Check', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | #pod runbook => 'https://grantstreetgroup.github.io/HealthCheck.html', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | #pod tags => [qw( fast cheap )], | ||||||||||||||||||||||||||||||||||||||||||
| 42 | #pod checks => [ | ||||||||||||||||||||||||||||||||||||||||||
| 43 | #pod sub { return { id => 'coderef', status => 'OK' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | #pod 'my_check', # Name of a method on caller | ||||||||||||||||||||||||||||||||||||||||||
| 45 | #pod ], | ||||||||||||||||||||||||||||||||||||||||||
| 46 | #pod ); | ||||||||||||||||||||||||||||||||||||||||||
| 47 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 48 | #pod my $other_checker = HealthCheck->new( | ||||||||||||||||||||||||||||||||||||||||||
| 49 | #pod id => 'my_health_check', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | #pod label => "My Health Check", | ||||||||||||||||||||||||||||||||||||||||||
| 51 | #pod runbook => 'https://grantstreetgroup.github.io/HealthCheck.html', | ||||||||||||||||||||||||||||||||||||||||||
| 52 | #pod tags => [qw( cheap easy )], | ||||||||||||||||||||||||||||||||||||||||||
| 53 | #pod other => "Other details to pass to the check call", | ||||||||||||||||||||||||||||||||||||||||||
| 54 | #pod )->register( | ||||||||||||||||||||||||||||||||||||||||||
| 55 | #pod 'My::Checker', # Name of a loaded class that ->can("check") | ||||||||||||||||||||||||||||||||||||||||||
| 56 | #pod My::Checker->new, # Object that ->can("check") | ||||||||||||||||||||||||||||||||||||||||||
| 57 | #pod ); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 59 | #pod # It's possible to add ids, labels, and tags to your checks | ||||||||||||||||||||||||||||||||||||||||||
| 60 | #pod # and they will be copied to the Result. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | #pod $other_checker->register( My::Checker->new( | ||||||||||||||||||||||||||||||||||||||||||
| 62 | #pod id => 'my_checker', | ||||||||||||||||||||||||||||||||||||||||||
| 63 | #pod label => 'My Checker', | ||||||||||||||||||||||||||||||||||||||||||
| 64 | #pod runbook => 'https://grantstreetgroup.github.io/HealthCheck.html', | ||||||||||||||||||||||||||||||||||||||||||
| 65 | #pod tags => [qw( cheap copied_to_the_result )], | ||||||||||||||||||||||||||||||||||||||||||
| 66 | #pod ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 68 | #pod # You can add HealthCheck instances as checks | ||||||||||||||||||||||||||||||||||||||||||
| 69 | #pod # You could add a check to itself to create an infinite loop of checks. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | #pod $checker->register( $other_checker ); | ||||||||||||||||||||||||||||||||||||||||||
| 71 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 72 | #pod # A hashref of the check config | ||||||||||||||||||||||||||||||||||||||||||
| 73 | #pod # This whole hashref is passed as an argument | ||||||||||||||||||||||||||||||||||||||||||
| 74 | #pod # to My::Checker->another_check | ||||||||||||||||||||||||||||||||||||||||||
| 75 | #pod $checker->register( { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | #pod invocant => 'My::Checker', # to call the "check" on | ||||||||||||||||||||||||||||||||||||||||||
| 77 | #pod check => 'another_check', # name of the check method | ||||||||||||||||||||||||||||||||||||||||||
| 78 | #pod runbook => 'https://grantstreetgroup.github.io/HealthCheck.html', | ||||||||||||||||||||||||||||||||||||||||||
| 79 | #pod tags => [qw( fast easy )], | ||||||||||||||||||||||||||||||||||||||||||
| 80 | #pod more_params => 'anything', | ||||||||||||||||||||||||||||||||||||||||||
| 81 | #pod } ); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 83 | #pod my @tags = $checker->tags; # returns fast, cheap | ||||||||||||||||||||||||||||||||||||||||||
| 84 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 85 | #pod my %result = %{ $checker->check( tags => ['cheap'] ) }; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | #pod # OR run the opposite checks | ||||||||||||||||||||||||||||||||||||||||||
| 87 | #pod %result = %{ $checker->check( tags => ['!cheap'] ) }; | ||||||||||||||||||||||||||||||||||||||||||
| 88 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 89 | #pod # A checker class or object just needs to have either | ||||||||||||||||||||||||||||||||||||||||||
| 90 | #pod # a check method, which is used by default, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | #pod # or another method as specified in a hash config. | ||||||||||||||||||||||||||||||||||||||||||
| 92 | #pod package My::Checker; | ||||||||||||||||||||||||||||||||||||||||||
| 93 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 94 | #pod # Optionally subclass HealthCheck::Diagnostic | ||||||||||||||||||||||||||||||||||||||||||
| 95 | #pod use parent 'HealthCheck::Diagnostic'; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 97 | #pod # and provide a 'run' method, the Diagnostic base class will | ||||||||||||||||||||||||||||||||||||||||||
| 98 | #pod # pass your results through the 'summarize' helper that | ||||||||||||||||||||||||||||||||||||||||||
| 99 | #pod # will add warnings about invalid values as well as | ||||||||||||||||||||||||||||||||||||||||||
| 100 | #pod # summarizing multiple results. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | #pod sub run { | ||||||||||||||||||||||||||||||||||||||||||
| 102 | #pod return { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | #pod id => ( ref $_[0] ? "object_method" : "class_method" ), | ||||||||||||||||||||||||||||||||||||||||||
| 104 | #pod status => "WARNING", | ||||||||||||||||||||||||||||||||||||||||||
| 105 | #pod }; | ||||||||||||||||||||||||||||||||||||||||||
| 106 | #pod } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 108 | #pod # Any checks *must* return a valid "Health Check Result" hashref. | ||||||||||||||||||||||||||||||||||||||||||
| 109 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 110 | #pod # You can add your own check that doesn't call 'summarize' | ||||||||||||||||||||||||||||||||||||||||||
| 111 | #pod # or, overload the 'check' helper in the parent class. | ||||||||||||||||||||||||||||||||||||||||||
| 112 | #pod sub another_check { | ||||||||||||||||||||||||||||||||||||||||||
| 113 | #pod my ($self, %params) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 114 | #pod return { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | #pod id => 'another_check', | ||||||||||||||||||||||||||||||||||||||||||
| 116 | #pod label => 'A Super custom check', | ||||||||||||||||||||||||||||||||||||||||||
| 117 | #pod runbook => 'https://grantstreetgroup.github.io/HealthCheck.html', | ||||||||||||||||||||||||||||||||||||||||||
| 118 | #pod status => ( $params{more_params} eq 'fine' ? "OK" : "CRITICAL" ), | ||||||||||||||||||||||||||||||||||||||||||
| 119 | #pod }; | ||||||||||||||||||||||||||||||||||||||||||
| 120 | #pod } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 122 | #pod C<%result> will be from the subset of checks run due to the tags. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 124 | #pod $checker->check(tags => ['cheap']); | ||||||||||||||||||||||||||||||||||||||||||
| 125 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 126 | #pod id => "main_checker", | ||||||||||||||||||||||||||||||||||||||||||
| 127 | #pod label => "Main Health Check", | ||||||||||||||||||||||||||||||||||||||||||
| 128 | #pod runbook => 'https://grantstreetgroup.github.io/HealthCheck.html', | ||||||||||||||||||||||||||||||||||||||||||
| 129 | #pod tags => [ "fast", "cheap" ], | ||||||||||||||||||||||||||||||||||||||||||
| 130 | #pod status => "WARNING", | ||||||||||||||||||||||||||||||||||||||||||
| 131 | #pod results => [ | ||||||||||||||||||||||||||||||||||||||||||
| 132 | #pod { id => "coderef", | ||||||||||||||||||||||||||||||||||||||||||
| 133 | #pod status => "OK", | ||||||||||||||||||||||||||||||||||||||||||
| 134 | #pod tags => [ "fast", "cheap" ] # inherited | ||||||||||||||||||||||||||||||||||||||||||
| 135 | #pod }, | ||||||||||||||||||||||||||||||||||||||||||
| 136 | #pod { anything => "at all", | ||||||||||||||||||||||||||||||||||||||||||
| 137 | #pod id => "my_check", | ||||||||||||||||||||||||||||||||||||||||||
| 138 | #pod status => "WARNING", | ||||||||||||||||||||||||||||||||||||||||||
| 139 | #pod tags => [ "fast", "cheap" ] # inherited | ||||||||||||||||||||||||||||||||||||||||||
| 140 | #pod }, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | #pod { id => "my_health_check", | ||||||||||||||||||||||||||||||||||||||||||
| 142 | #pod label => "My Health Check", | ||||||||||||||||||||||||||||||||||||||||||
| 143 | #pod tags => [ "cheap", "easy" ], | ||||||||||||||||||||||||||||||||||||||||||
| 144 | #pod status => "WARNING", | ||||||||||||||||||||||||||||||||||||||||||
| 145 | #pod results => [ | ||||||||||||||||||||||||||||||||||||||||||
| 146 | #pod { id => "class_method", | ||||||||||||||||||||||||||||||||||||||||||
| 147 | #pod tags => [ "cheap", "easy" ], | ||||||||||||||||||||||||||||||||||||||||||
| 148 | #pod status => "WARNING", | ||||||||||||||||||||||||||||||||||||||||||
| 149 | #pod }, | ||||||||||||||||||||||||||||||||||||||||||
| 150 | #pod { id => "object_method", | ||||||||||||||||||||||||||||||||||||||||||
| 151 | #pod tags => [ "cheap", "easy" ], | ||||||||||||||||||||||||||||||||||||||||||
| 152 | #pod status => "WARNING", | ||||||||||||||||||||||||||||||||||||||||||
| 153 | #pod }, | ||||||||||||||||||||||||||||||||||||||||||
| 154 | #pod { id => "object_method_1", | ||||||||||||||||||||||||||||||||||||||||||
| 155 | #pod label => "My Checker", | ||||||||||||||||||||||||||||||||||||||||||
| 156 | #pod tags => [ "cheap", "copied_to_the_result" ], | ||||||||||||||||||||||||||||||||||||||||||
| 157 | #pod status => "WARNING", | ||||||||||||||||||||||||||||||||||||||||||
| 158 | #pod } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | #pod ], | ||||||||||||||||||||||||||||||||||||||||||
| 160 | #pod } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | #pod ], | ||||||||||||||||||||||||||||||||||||||||||
| 162 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 163 | #pod There is also runtime support, | ||||||||||||||||||||||||||||||||||||||||||
| 164 | #pod which can be enabled by adding a truthy C<runtime> param to the C<check>. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 166 | #pod $checker->check( tags => [ 'easy', '!fast' ], runtime => 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 167 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 168 | #pod id => "my_health_check", | ||||||||||||||||||||||||||||||||||||||||||
| 169 | #pod label => "My Health Check", | ||||||||||||||||||||||||||||||||||||||||||
| 170 | #pod runtime => "0.000", | ||||||||||||||||||||||||||||||||||||||||||
| 171 | #pod runbook => 'https://grantstreetgroup.github.io/HealthCheck.html', | ||||||||||||||||||||||||||||||||||||||||||
| 172 | #pod tags => [ "cheap", "easy" ], | ||||||||||||||||||||||||||||||||||||||||||
| 173 | #pod status => "WARNING", | ||||||||||||||||||||||||||||||||||||||||||
| 174 | #pod results => [ | ||||||||||||||||||||||||||||||||||||||||||
| 175 | #pod { id => "class_method", | ||||||||||||||||||||||||||||||||||||||||||
| 176 | #pod runtime => "0.000", | ||||||||||||||||||||||||||||||||||||||||||
| 177 | #pod tags => [ "cheap", "easy" ], | ||||||||||||||||||||||||||||||||||||||||||
| 178 | #pod status => "WARNING", | ||||||||||||||||||||||||||||||||||||||||||
| 179 | #pod }, | ||||||||||||||||||||||||||||||||||||||||||
| 180 | #pod { id => "object_method", | ||||||||||||||||||||||||||||||||||||||||||
| 181 | #pod runtime => "0.000", | ||||||||||||||||||||||||||||||||||||||||||
| 182 | #pod tags => [ "cheap", "easy" ], | ||||||||||||||||||||||||||||||||||||||||||
| 183 | #pod status => "WARNING", | ||||||||||||||||||||||||||||||||||||||||||
| 184 | #pod } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | #pod ], | ||||||||||||||||||||||||||||||||||||||||||
| 186 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 187 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 188 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 189 | #pod Allows you to create callbacks that check the health of your application | ||||||||||||||||||||||||||||||||||||||||||
| 190 | #pod and return a status result. | ||||||||||||||||||||||||||||||||||||||||||
| 191 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 192 | #pod There are several things this is trying to enable: | ||||||||||||||||||||||||||||||||||||||||||
| 193 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 194 | #pod =over | ||||||||||||||||||||||||||||||||||||||||||
| 195 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 196 | #pod =item * | ||||||||||||||||||||||||||||||||||||||||||
| 197 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 198 | #pod A fast HTTP endpoint that can be used to verify that a web app can | ||||||||||||||||||||||||||||||||||||||||||
| 199 | #pod serve traffic. | ||||||||||||||||||||||||||||||||||||||||||
| 200 | #pod To this end, it may be useful to use the runtime support option, | ||||||||||||||||||||||||||||||||||||||||||
| 201 | #pod available in L<HealthChecks::Diagnostic>. | ||||||||||||||||||||||||||||||||||||||||||
| 202 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 203 | #pod =item * | ||||||||||||||||||||||||||||||||||||||||||
| 204 | #pod A more complete check that verifies all the things work after a deployment. | ||||||||||||||||||||||||||||||||||||||||||
| 205 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 206 | #pod =item * | ||||||||||||||||||||||||||||||||||||||||||
| 207 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 208 | #pod The ability for a script, such as a cronjob, to verify that it's dependencies | ||||||||||||||||||||||||||||||||||||||||||
| 209 | #pod are available before starting work. | ||||||||||||||||||||||||||||||||||||||||||
| 210 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 211 | #pod =item * | ||||||||||||||||||||||||||||||||||||||||||
| 212 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 213 | #pod Different sorts of monitoring checks that are defined in your codebase. | ||||||||||||||||||||||||||||||||||||||||||
| 214 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 215 | #pod =back | ||||||||||||||||||||||||||||||||||||||||||
| 216 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 217 | #pod Results returned by these checks should correspond to the GSG | ||||||||||||||||||||||||||||||||||||||||||
| 218 | #pod L<Health Check Standard|https://grantstreetgroup.github.io/HealthCheck.html>. | ||||||||||||||||||||||||||||||||||||||||||
| 219 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 220 | #pod You may want to use L<HealthCheck::Diagnostic> to simplify writing your | ||||||||||||||||||||||||||||||||||||||||||
| 221 | #pod check slightly. | ||||||||||||||||||||||||||||||||||||||||||
| 222 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 223 | #pod =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 224 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 225 | #pod =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 226 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 227 | #pod my $checker = HealthCheck->new( id => 'my_checker' ); | ||||||||||||||||||||||||||||||||||||||||||
| 228 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 229 | #pod =head3 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 230 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 231 | #pod =over | ||||||||||||||||||||||||||||||||||||||||||
| 232 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 233 | #pod =item checks | ||||||||||||||||||||||||||||||||||||||||||
| 234 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 235 | #pod An arrayref that is passed to L</register> to initialize checks. | ||||||||||||||||||||||||||||||||||||||||||
| 236 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 237 | #pod =item tags | ||||||||||||||||||||||||||||||||||||||||||
| 238 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 239 | #pod An arrayref used as the default set of tags for any checks that don't | ||||||||||||||||||||||||||||||||||||||||||
| 240 | #pod override them. | ||||||||||||||||||||||||||||||||||||||||||
| 241 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 242 | #pod =back | ||||||||||||||||||||||||||||||||||||||||||
| 243 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 244 | #pod Any other parameters are included in the "Result" hashref returned. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 246 | #pod Some recommended things to include are: | ||||||||||||||||||||||||||||||||||||||||||
| 247 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 248 | #pod =over | ||||||||||||||||||||||||||||||||||||||||||
| 249 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 250 | #pod =item id | ||||||||||||||||||||||||||||||||||||||||||
| 251 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 252 | #pod The unique id for this check. | ||||||||||||||||||||||||||||||||||||||||||
| 253 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 254 | #pod =item label | ||||||||||||||||||||||||||||||||||||||||||
| 255 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 256 | #pod A human readable name for this check. | ||||||||||||||||||||||||||||||||||||||||||
| 257 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 258 | #pod =item runbook | ||||||||||||||||||||||||||||||||||||||||||
| 259 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 260 | #pod A runbook link to help troubleshooting if the status is not OK. | ||||||||||||||||||||||||||||||||||||||||||
| 261 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 262 | #pod =back | ||||||||||||||||||||||||||||||||||||||||||
| 263 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 264 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | 36 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 36 | my ( $class, %params ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 268 | 36 | my $checks = delete $params{checks}; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 36 | my $self = bless {%params}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 270 | 36 | ▶ | return $checks ? $self->register($checks) : $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | #pod =head2 register | ||||||||||||||||||||||||||||||||||||||||||
| 274 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 275 | #pod $checker->register({ | ||||||||||||||||||||||||||||||||||||||||||
| 276 | #pod invocant => $class_or_object, | ||||||||||||||||||||||||||||||||||||||||||
| 277 | #pod check => $method_on_invocant_or_coderef, | ||||||||||||||||||||||||||||||||||||||||||
| 278 | #pod more => "any other params are passed to the check", | ||||||||||||||||||||||||||||||||||||||||||
| 279 | #pod }); | ||||||||||||||||||||||||||||||||||||||||||
| 280 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 281 | #pod Takes a list or arrayref of check definitions to be added to the object. | ||||||||||||||||||||||||||||||||||||||||||
| 282 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 283 | #pod Each registered check must return a valid GSG Health Check response, | ||||||||||||||||||||||||||||||||||||||||||
| 284 | #pod either as a hashref or an even-sized list. | ||||||||||||||||||||||||||||||||||||||||||
| 285 | #pod See the GSG Health Check Standard (linked in L</DESCRIPTION>) | ||||||||||||||||||||||||||||||||||||||||||
| 286 | #pod for the fields that checks should return. | ||||||||||||||||||||||||||||||||||||||||||
| 287 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 288 | #pod Rather than having to always pass in the full hashref definition, | ||||||||||||||||||||||||||||||||||||||||||
| 289 | #pod several common cases are detected and used to fill out the check. | ||||||||||||||||||||||||||||||||||||||||||
| 290 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 291 | #pod =over | ||||||||||||||||||||||||||||||||||||||||||
| 292 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 293 | #pod =item coderef | ||||||||||||||||||||||||||||||||||||||||||
| 294 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 295 | #pod If passed a coderef, this will be called as the C<check> without an C<invocant>. | ||||||||||||||||||||||||||||||||||||||||||
| 296 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 297 | #pod =item object | ||||||||||||||||||||||||||||||||||||||||||
| 298 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 299 | #pod If a blessed object is passed in | ||||||||||||||||||||||||||||||||||||||||||
| 300 | #pod and it has a C<check> method, use that for the C<check>, | ||||||||||||||||||||||||||||||||||||||||||
| 301 | #pod otherwise throw an exception. | ||||||||||||||||||||||||||||||||||||||||||
| 302 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 303 | #pod =item string | ||||||||||||||||||||||||||||||||||||||||||
| 304 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 305 | #pod If a string is passed in, | ||||||||||||||||||||||||||||||||||||||||||
| 306 | #pod check if it is the name of a loaded class that has a C<check> method, | ||||||||||||||||||||||||||||||||||||||||||
| 307 | #pod and if so use it as the C<invocant> with the method as the C<check>. | ||||||||||||||||||||||||||||||||||||||||||
| 308 | #pod Otherwise if our L<caller> has a method with this name, | ||||||||||||||||||||||||||||||||||||||||||
| 309 | #pod the L<caller> becomes the C<invocant> and this becomes the C<check>, | ||||||||||||||||||||||||||||||||||||||||||
| 310 | #pod otherwise throws an exception. | ||||||||||||||||||||||||||||||||||||||||||
| 311 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 312 | #pod =item full hashref of params | ||||||||||||||||||||||||||||||||||||||||||
| 313 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 314 | #pod The full hashref can consist of a C<check> key that the above heuristics | ||||||||||||||||||||||||||||||||||||||||||
| 315 | #pod are applied, | ||||||||||||||||||||||||||||||||||||||||||
| 316 | #pod or include an C<invocant> key that is used as either | ||||||||||||||||||||||||||||||||||||||||||
| 317 | #pod an C<object> or C<class name>. | ||||||||||||||||||||||||||||||||||||||||||
| 318 | #pod With the C<invocant> specified, the now optional C<check> key | ||||||||||||||||||||||||||||||||||||||||||
| 319 | #pod defaults to "check" and is used as the method to call on C<invocant>. | ||||||||||||||||||||||||||||||||||||||||||
| 320 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 321 | #pod All attributes other than C<invocant> and C<check> are passed to the check. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 323 | #pod =back | ||||||||||||||||||||||||||||||||||||||||||
| 324 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 325 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | 29 | ▶ | sub register { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=20 SCAR=30.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 29 | my ($self, @checks) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 329 | 29 | ▶ | croak("register cannot be called as a class method") unless ref $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 28 | ▶ | return $self unless @checks; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 28 | my $class = ref $self; | |||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | 28 | ▶ | @checks = @{ $checks[0] } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
@checks == 1 and (ref $checks[0] || "") eq "ARRAY"
ref $checks[0] || ''
MC/DC50%1/2
@checks == 1 and (ref $checks[0] || "") eq "ARRAY"
@checks == 1
ref $checks[0]
Truth table66%2/3
A and B
A= @checks == 1B= ref $checks[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | if @checks == 1 and ( ref $checks[0] || '' ) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | # If the check that was passed in is just the name of a method | ||||||||||||||||||||||||||||||||||||||||||
| 337 | # we are going to use our caller as the invocant. | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 28 | my $caller; | |||||||||||||||||||||||||||||||||||||||||
| 339 | 7 | ▶ | my $find_caller = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | 7 | my ( $i, $c ) = ( 1, undef ); | |||||||||||||||||||||||||||||||||||||||||
| 341 | 7 | ▶ | do { ($c) = caller( $i++ ) } while $c->isa(__PACKAGE__); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 7 | $c; | |||||||||||||||||||||||||||||||||||||||||
| 343 | 28 | }; | |||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | 28 | foreach (@checks) { | |||||||||||||||||||||||||||||||||||||||||
| 346 | 45 | ▶ | my $type = ref $_ || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
ref $_ || ''
MC/DC100%1/1
ref $_ || ''
ref $_
Truth table100%2/2
A
A= ref $_
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | my %c | ||||||||||||||||||||||||||||||||||||||||||
| 348 | 45 | ▶ | = $type eq 'HASH' ? ( %{$_} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | : $type eq 'ARRAY' ? ( check => $class->register($_) ) | ||||||||||||||||||||||||||||||||||||||||||
| 350 | : ( check => $_ ); | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | 45 | ▶ | croak("check parameter required") unless $c{check}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | # If it's not a coderef, | ||||||||||||||||||||||||||||||||||||||||||
| 355 | # it must be the name of a method to call on an invocant. | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 42 | ▶ | unless ( ( ref $c{check} || '' ) eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $c{'check'} || ''
MC/DC100%1/1
ref $c{'check'} || ''
ref $c{'check'}
Truth table100%2/2
A
A= ref $c{'check'}
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | # If they passed in an object or a class that can('check') | ||||||||||||||||||||||||||||||||||||||||||
| 359 | # then we want to set that as the invocant so the check | ||||||||||||||||||||||||||||||||||||||||||
| 360 | # runner does the right thing. | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 13 | ▶ | if ( $c{check} and not $c{invocant} and do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$c{"check"} and not $c{"invocant"} and do {
local $@;
eval {
do {
$c{"check"}->can("check")
}
}
}
$c{"check"} and not $c{"invocant"}
MC/DC66%2/3
$c{"check"} and not $c{"invocant"} and do {
local $@;
eval {
do {
$c{"check"}->can("check")
}
}
}
$c{"check"}
not $c{"invocant"}
do {
local $@;
eval {
do {
$c{"check"}->can("check")
}
}
}
Truth table75%3/4
A and not(B) and C
A= $c{"check"}B= not $c{"invocant"}C= do {
local $@;
eval {
do {
$c{"check"}->can("check")
}
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 11 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 363 | 11 | eval { $c{check}->can('check') }; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } ) | ||||||||||||||||||||||||||||||||||||||||||
| 365 | { | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 4 | $c{invocant} = $c{check}; | |||||||||||||||||||||||||||||||||||||||||
| 367 | 4 | $c{check} = 'check'; | |||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | # If they just passed in a method name, | ||||||||||||||||||||||||||||||||||||||||||
| 371 | # we can see if the caller has that method. | ||||||||||||||||||||||||||||||||||||||||||
| 372 | 13 | ▶ | unless ($c{invocant}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 7 | ▶ | $caller ||= $find_caller->(); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$caller ||= &$find_caller()
MC/DC0%0/2
$caller ||= &$find_caller()
$caller
&$find_caller()
Truth table33%1/3
A ||= B
A= $callerB= &$find_caller()
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | 7 | ▶ | if ($caller->can($c{check}) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | 4 | $c{invocant} = $caller; | |||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||
| 378 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 379 | 3 | croak("Can't determine what to do with '$c{check}'"); | |||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | croak("'$c{invocant}' cannot '$c{check}'") | ||||||||||||||||||||||||||||||||||||||||||
| 384 | 10 | ▶ | unless $c{invocant}->can( $c{check} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | 37 | push @{ $registered_checks{$self} }, \%c; | |||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | 20 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | #pod =head2 check | ||||||||||||||||||||||||||||||||||||||||||
| 394 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 395 | #pod my %results = %{ $checker->check(%params) } | ||||||||||||||||||||||||||||||||||||||||||
| 396 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 397 | #pod Calls all of the registered checks and returns a hashref of the results of | ||||||||||||||||||||||||||||||||||||||||||
| 398 | #pod processing the checks passed through L<HealthCheck::Diagnostic/summarize>. | ||||||||||||||||||||||||||||||||||||||||||
| 399 | #pod Passes the L</full hashref of params> as an even-sized list to the check, | ||||||||||||||||||||||||||||||||||||||||||
| 400 | #pod without the C<invocant> or C<check> keys. | ||||||||||||||||||||||||||||||||||||||||||
| 401 | #pod This hashref is shallow merged with and duplicate keys overridden by | ||||||||||||||||||||||||||||||||||||||||||
| 402 | #pod the C<%params> passed in. | ||||||||||||||||||||||||||||||||||||||||||
| 403 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 404 | #pod If there is both an C<invocant> and C<check> in the params, | ||||||||||||||||||||||||||||||||||||||||||
| 405 | #pod it the C<check> is called as a method on the C<invocant>, | ||||||||||||||||||||||||||||||||||||||||||
| 406 | #pod otherwise C<check> is used as a callback coderef. | ||||||||||||||||||||||||||||||||||||||||||
| 407 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 408 | #pod If only a single check is registered, | ||||||||||||||||||||||||||||||||||||||||||
| 409 | #pod the results from that check are merged with, and will override | ||||||||||||||||||||||||||||||||||||||||||
| 410 | #pod the L</ATTRIBUTES> set on the object instead of being put in | ||||||||||||||||||||||||||||||||||||||||||
| 411 | #pod a C<results> arrayref. | ||||||||||||||||||||||||||||||||||||||||||
| 412 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 413 | #pod Throws an exception if no checks have been registered. | ||||||||||||||||||||||||||||||||||||||||||
| 414 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 415 | #pod =head3 run | ||||||||||||||||||||||||||||||||||||||||||
| 416 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 417 | #pod Main implementation of the checker is here. | ||||||||||||||||||||||||||||||||||||||||||
| 418 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 419 | #pod Passes C<< summarize_result => 0 >> to each registered check | ||||||||||||||||||||||||||||||||||||||||||
| 420 | #pod unless overridden to avoid running C<summarize> multiple times. | ||||||||||||||||||||||||||||||||||||||||||
| 421 | #pod See L<HealthCheck::Diagnostic/check>. | ||||||||||||||||||||||||||||||||||||||||||
| 422 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 423 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | 29 | ▶ | sub check { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | 29 | my ( $self, @params ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 427 | 29 | ▶ | croak("check cannot be called as a class method") unless ref $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | 28 | ▶ | croak("No registered checks") unless @{ $registered_checks{$self} || [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$registered_checks{$self} || []
MC/DC100%1/1
$registered_checks{$self} || []
$registered_checks{$self}
Truth table100%2/2
A
A= $registered_checks{$self}
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 27 | $self->SUPER::check(@params); | |||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | #pod =head2 get_registered_tags | ||||||||||||||||||||||||||||||||||||||||||
| 433 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 434 | #pod Read-only accessor that returns the list of 'top-level' tags registered with | ||||||||||||||||||||||||||||||||||||||||||
| 435 | #pod this object. Sub-check tags are not included - only those which will result in | ||||||||||||||||||||||||||||||||||||||||||
| 436 | #pod checks being run when passed to L</check> on the given object. | ||||||||||||||||||||||||||||||||||||||||||
| 437 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 438 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | 3 | ▶ | sub get_registered_tags { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_registered_tags: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 441 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | 3 | ▶ | my @checks = @{ $registered_checks{$self} || [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$registered_checks{$self} || []
MC/DC0%0/1
$registered_checks{$self} || []
$registered_checks{$self}
Truth table50%1/2
A
A= $registered_checks{$self}
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 3 | my @tags; | |||||||||||||||||||||||||||||||||||||||||
| 445 | 3 | for my $check (@checks) { | |||||||||||||||||||||||||||||||||||||||||
| 446 | 15 | $self->_set_check_response_defaults($check); | |||||||||||||||||||||||||||||||||||||||||
| 447 | 15 | ▶ | push @tags, @{ $check->{_respond}{tags} || [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$check->{'_respond'}{'tags'} || []
MC/DC100%1/1
$check->{'_respond'}{'tags'} || []
$check->{'_respond'}{'tags'}
Truth table100%2/2
A
A= $check->{'_respond'}{'tags'}
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||
| 449 | 3 | push @tags, $self->tags; | |||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | 3 | return uniq sort @tags; | |||||||||||||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | 27 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 455 | 27 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | # If we are going to summarize things, no need for our children to | ||||||||||||||||||||||||||||||||||||||||||
| 458 | 27 | ▶ | $params{summarize_result} = 0 unless exists $params{summarize_result}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | my @results = $self->_run_checks( | ||||||||||||||||||||||||||||||||||||||||||
| 461 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 462 | 69 | grep { $self->should_run( $_, %params ) } | |||||||||||||||||||||||||||||||||||||||||
| 463 | 27 | ▶ | @{ $registered_checks{$self} || [] } | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$registered_checks{$self} || []
MC/DC0%0/1
$registered_checks{$self} || []
$registered_checks{$self}
Truth table50%1/2
A
A= $registered_checks{$self}
| |||||||||||||||||||||||||||||||||||||||||||
| 464 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 465 | \%params, | ||||||||||||||||||||||||||||||||||||||||||
| 466 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | 27 | ▶ | return unless @results; # don't return undef, instead an empty list | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 469 | 25 | ▶ | return $results[0] if @{ $registered_checks{$self} || [] } == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$registered_checks{$self} || []
MC/DC0%0/1
$registered_checks{$self} || []
$registered_checks{$self}
Truth table50%1/2
A
A= $registered_checks{$self}
| |||||||||||||||||||||||||||||||||||||||||||
| 470 | 11 | return { results => \@results }; | |||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | 27 | ▶ | sub _run_checks { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_checks: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 474 | 27 | my ( $self, $checks, $params ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | 27 | return map { $self->_run_check( $_, $params ) } @$checks; | |||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | 43 | ▶ | sub _run_check { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_check: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | 43 | my ( $self, $check, $params ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | 43 | my %c = %{ $check }; | |||||||||||||||||||||||||||||||||||||||||
| 483 | 43 | $self->_set_check_response_defaults(\%c); | |||||||||||||||||||||||||||||||||||||||||
| 484 | 43 | my $defaults = delete $c{_respond}; | |||||||||||||||||||||||||||||||||||||||||
| 485 | 43 | ▶ | my $i = delete $c{invocant} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $c{'invocant'} || ''
MC/DC100%1/1
delete $c{'invocant'} || ''
delete $c{'invocant'}
Truth table100%2/2
A
A= delete $c{'invocant'}
| |||||||||||||||||||||||||||||||||||||||||||
| 486 | 43 | ▶ | my $m = delete $c{check} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $c{'check'} || ''
MC/DC0%0/1
delete $c{'check'} || ''
delete $c{'check'}
Truth table50%1/2
A
A= delete $c{'check'}
| |||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | 43 | my @r; | |||||||||||||||||||||||||||||||||||||||||
| 489 | # Exceptions will probably not contain child health check's metadata, | ||||||||||||||||||||||||||||||||||||||||||
| 490 | # as HealthCheck::Diagnostic->summarize would normally populate these | ||||||||||||||||||||||||||||||||||||||||||
| 491 | # and was not called. | ||||||||||||||||||||||||||||||||||||||||||
| 492 | # This could theoretically be a pain for prodsupport. If we find this | ||||||||||||||||||||||||||||||||||||||||||
| 493 | # happening frequently, we should reassess our decision not to attempt | ||||||||||||||||||||||||||||||||||||||||||
| 494 | # to call summarize here | ||||||||||||||||||||||||||||||||||||||||||
| 495 | # (for fear of exception-catching magic and rabbitholes). | ||||||||||||||||||||||||||||||||||||||||||
| 496 | { | ||||||||||||||||||||||||||||||||||||||||||
| 497 | 43 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 498 | 43 | ▶ | @r = eval { $i ? $i->$m( %c, %$params ) : $m->( %c, %$params ) }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | 43 | ▶ | @r = { status => 'CRITICAL', info => $@ } if $@ and not @r; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ and not @r
MC/DC50%1/2
$@ and not @r
$@
not @r
Truth table66%2/3
A and not(B)
A= $@B= not @r
| |||||||||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | @r | ||||||||||||||||||||||||||||||||||||||||||
| 503 | = @r == 1 && ref $r[0] eq 'HASH' ? $r[0] | ||||||||||||||||||||||||||||||||||||||||||
| 504 | : @r % 2 == 0 ? {@r} | ||||||||||||||||||||||||||||||||||||||||||
| 505 | 43 | ▶ | : do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
@r == 1 and ref $r[0] eq "HASH"
MC/DC100%2/2
@r == 1 and ref $r[0] eq "HASH"
@r == 1
ref $r[0] eq "HASH"
Truth table100%3/3
A and B
A= @r == 1B= ref $r[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | 2 | ▶ | my $c = $i ? "$i->$m" : "$m"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 507 | 2 | carp("Invalid return from $c (@r)"); | |||||||||||||||||||||||||||||||||||||||||
| 508 | 2 | (); | |||||||||||||||||||||||||||||||||||||||||
| 509 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | 43 | ▶ | if (@r) { @r = +{ %$defaults, %{ $r[0] } } } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | 43 | return @r; | |||||||||||||||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | 136 | ▶ | sub _set_check_response_defaults { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_check_response_defaults: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||
| 517 | 136 | my ($self, $c) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 518 | 136 | ▶ | return if exists $c->{_respond}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | 47 | my %defaults; | |||||||||||||||||||||||||||||||||||||||||
| 521 | 47 | FIELD: for my $field ( qw(id label runbook tags) ) { | |||||||||||||||||||||||||||||||||||||||||
| 522 | 188 | ▶ | if (exists $c->{$field}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 29 | $defaults{$field} = $c->{$field}; | |||||||||||||||||||||||||||||||||||||||||
| 524 | 29 | next FIELD; | |||||||||||||||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | 159 | ▶ | if ( $c->{invocant} && $c->{invocant}->can($field) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c->{"invocant"} and $c->{"invocant"}->can($field)
MC/DC100%2/2
$c->{"invocant"} and $c->{"invocant"}->can($field)
$c->{"invocant"}
$c->{"invocant"}->can($field)
Truth table100%3/3
A and B
A= $c->{"invocant"}B= $c->{"invocant"}->can($field)
| |||||||||||||||||||||||||||||||||||||||||||
| 528 | 20 | my $val; | |||||||||||||||||||||||||||||||||||||||||
| 529 | 20 | ▶ | if ( $field eq 'tags' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 530 | 4 | ▶ | if (my @tags = $c->{invocant}->$field) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 531 | 2 | $val = [@tags]; | |||||||||||||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||||||||||||||||
| 534 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 535 | 16 | $val = $c->{invocant}->$field; | |||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | 20 | ▶ | if (defined $val) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 539 | 6 | $defaults{$field} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 540 | 6 | next FIELD; | |||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | # we only copy tags from the checker to the sub-checks, | ||||||||||||||||||||||||||||||||||||||||||
| 545 | # and only if they don't exist. | ||||||||||||||||||||||||||||||||||||||||||
| 546 | 153 | ▶ | $self->_set_default_fields(\%defaults, $field) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 547 | if $field eq 'tags'; | ||||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | # deref the tags, just in case someone decides to adjust them later. | ||||||||||||||||||||||||||||||||||||||||||
| 551 | 47 | ▶ | $defaults{tags} = [ @{ $defaults{tags} } ] if $defaults{tags}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | 47 | $c->{_respond} = \%defaults; | |||||||||||||||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||
| 557 | #pod =head1 INTERNALS | ||||||||||||||||||||||||||||||||||||||||||
| 558 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 559 | #pod These methods may be useful for subclassing, | ||||||||||||||||||||||||||||||||||||||||||
| 560 | #pod but are not intended for general use. | ||||||||||||||||||||||||||||||||||||||||||
| 561 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 562 | #pod =head2 should_run | ||||||||||||||||||||||||||||||||||||||||||
| 563 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 564 | #pod my $bool = $checker->should_run( \%check, tags => ['apple', '!banana'] ); | ||||||||||||||||||||||||||||||||||||||||||
| 565 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 566 | #pod Takes a check definition hash and paramters and returns true | ||||||||||||||||||||||||||||||||||||||||||
| 567 | #pod if the check should be run. | ||||||||||||||||||||||||||||||||||||||||||
| 568 | #pod Used by L</check> to determine which checks to run. | ||||||||||||||||||||||||||||||||||||||||||
| 569 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 570 | #pod Supported parameters: | ||||||||||||||||||||||||||||||||||||||||||
| 571 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 572 | #pod =over | ||||||||||||||||||||||||||||||||||||||||||
| 573 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 574 | #pod =item tags | ||||||||||||||||||||||||||||||||||||||||||
| 575 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 576 | #pod Tags can be either "positive" or "negative". A negative tag is indicated by a | ||||||||||||||||||||||||||||||||||||||||||
| 577 | #pod leading C<!>. | ||||||||||||||||||||||||||||||||||||||||||
| 578 | #pod A check is run if its tags match any of the passed in positive tags and none | ||||||||||||||||||||||||||||||||||||||||||
| 579 | #pod of the negative ones. | ||||||||||||||||||||||||||||||||||||||||||
| 580 | #pod If no tags are passed in, all checks will be run. | ||||||||||||||||||||||||||||||||||||||||||
| 581 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 582 | #pod If the C<invocant> C<can('tags')> and there are no tags in the | ||||||||||||||||||||||||||||||||||||||||||
| 583 | #pod L</full hashref of params> then the return value of that method is used. | ||||||||||||||||||||||||||||||||||||||||||
| 584 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 585 | #pod If a check has no tags defined, will use the default tags defined | ||||||||||||||||||||||||||||||||||||||||||
| 586 | #pod when the object was created. | ||||||||||||||||||||||||||||||||||||||||||
| 587 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 588 | #pod =back | ||||||||||||||||||||||||||||||||||||||||||
| 589 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 590 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||
| 592 | 78 | ▶ | sub _has_tags { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_has_tags: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 593 | 78 | my ($self, $check, @want_tags) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | 78 | $self->_set_check_response_defaults($check); | |||||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||||||
| 597 | # Look at what the check responds to, not what was initially specified | ||||||||||||||||||||||||||||||||||||||||||
| 598 | # (in case tags are inherited) | ||||||||||||||||||||||||||||||||||||||||||
| 599 | 78 | ▶ | my %have_tags = map { $_ => 1 } @{ $check->{_respond}{tags} || [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$check->{'_respond'}{'tags'} || []
MC/DC0%0/1
$check->{'_respond'}{'tags'} || []
$check->{'_respond'}{'tags'}
Truth table50%1/2
A
A= $check->{'_respond'}{'tags'}
| |||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | 78 | ▶ | return any { $have_tags{$_} } @want_tags; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | 117 | ▶ | sub should_run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
should_run: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 605 | 117 | my ( $self, $check, %params ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||||||||||
| 607 | 117 | my (@positive_tags, @negative_tags); | |||||||||||||||||||||||||||||||||||||||||
| 608 | 117 | for my $tag ( @{ $params{tags} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 609 | 86 | ▶ | if ( $tag =~ /^!/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 610 | 12 | push @negative_tags, substr($tag, 1); | |||||||||||||||||||||||||||||||||||||||||
| 611 | } | ||||||||||||||||||||||||||||||||||||||||||
| 612 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 613 | 74 | push @positive_tags, $tag; | |||||||||||||||||||||||||||||||||||||||||
| 614 | } | ||||||||||||||||||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||
| 617 | 117 | ▶ | return 0 if @negative_tags && $self->_has_tags($check, @negative_tags); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@negative_tags and $self->_has_tags($check, @negative_tags)
MC/DC100%2/2
@negative_tags and $self->_has_tags($check, @negative_tags)
@negative_tags
$self->_has_tags($check, @negative_tags)
Truth table100%3/3
A and B
A= @negative_tagsB= $self->_has_tags($check, @negative_tags)
| |||||||||||||||||||||||||||||||||||||||||||
| 618 | 114 | ▶ | return 1 unless @positive_tags; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 619 | 66 | return $self->_has_tags($check, @positive_tags); | |||||||||||||||||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||
| 622 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | __END__ | ||||||||||||||||||||||||||||||||||||||||||