| 1 | package HealthCheck::Diagnostic::RemoteHealth; | ||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use parent 'HealthCheck::Diagnostic::WebRequest'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Get results from an HTTP HealthCheck | ||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use version; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = 'v0.1.0'; # VERSION | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | sub new { | ||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | my ($class, @params) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH' | ||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | ? %{ $params[0] } : @params; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
ref $params[0] || ''
MC/DC0%0/2
@params == 1 and (ref $params[0] || "") eq "HASH"
@params == 1
ref $params[0]
Truth table33%1/3
A and B
A= @params == 1B= ref $params[0]
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | # Allows 200 and 503 codes by default. | ||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | $params{status_code} //= '200, 503'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$params{"status_code"} //= "200, 503"
MC/DC0%0/1
$params{"status_code"} //= "200, 503"
$params{"status_code"}
Truth table50%1/2
A
A= $params{"status_code"}
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 1 | return $class->SUPER::new( | |||||||||||||||||||||||||||||||
| 22 | id => 'remotehealth', | ||||||||||||||||||||||||||||||||
| 23 | label => 'RemoteHealth', | ||||||||||||||||||||||||||||||||
| 24 | %params, | ||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | sub run { | ||||||||||||||||||||||||||||||||
| 29 | 6 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 30 | 6 | my $result = $self->next::method(%params); | |||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | # Throws away the HTTP status check if OK, | ||||||||||||||||||||||||||||||||
| 33 | # since it's implied to be successful | ||||||||||||||||||||||||||||||||
| 34 | # if it retrieves the encoded JSON object. | ||||||||||||||||||||||||||||||||
| 35 | return $result->{results}->[1] | ||||||||||||||||||||||||||||||||
| 36 | 6 | ▶ | if @{$result->{results} || []} == 2 | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 6 | ▶ | and $result->{results}->[0]->{status} eq 'OK'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@{$result->{"results"} or [];} == 2 and $result->{"results"}[0]{"status"} eq "OK"
MC/DC0%0/2
@{$result->{"results"} or [];} == 2 and $result->{"results"}[0]{"status"} eq "OK"
@{$result->{"results"} or [];} == 2
$result->{"results"}[0]{"status"} eq "OK"
Truth table66%2/3
A and B
A= @{$result->{"results"} or [];} == 2B= $result->{"results"}[0]{"status"} eq "OK"
| |||||||||||||||||||||||||||||||||
| 38 | 6 | return $result; | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | # Checking for content regex from JSON seems unnecessary, so this has been | ||||||||||||||||||||||||||||||||
| 42 | # repurposed to return the decoded JSON object. | ||||||||||||||||||||||||||||||||
| 43 | sub check_content { | ||||||||||||||||||||||||||||||||
| 44 | 4 | ▶ | my ($self, $response) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
check_content: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 4 | local $@; | |||||||||||||||||||||||||||||||
| 47 | 4 | my $remote_result = eval { decode_json($response->content) }; | |||||||||||||||||||||||||||||||
| 48 | return { | ||||||||||||||||||||||||||||||||
| 49 | 4 | ▶ | status => 'CRITICAL', | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ or ref $remote_result ne "HASH"
MC/DC50%1/2
$@ or ref $remote_result ne "HASH"
$@
ref $remote_result ne "HASH"
Truth table66%2/3
A or B
A= $@B= ref $remote_result ne "HASH"
| |||||||||||||||||||||||||||||||||
| 50 | info => 'Could not decode JSON.', | ||||||||||||||||||||||||||||||||
| 51 | data => $response->content, | ||||||||||||||||||||||||||||||||
| 52 | } if $@ or ref($remote_result) ne 'HASH'; | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 3 | return { results => [ $remote_result ] }; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 2 | 1; | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | __END__ | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | =pod | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | HealthCheck::Diagnostic::RemoteHealth - Get results from an HTTP HealthCheck | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | version v0.1.0 | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | Returns the decoded JSON object from a HTTP HealthCheck endpoint. | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | $health_check->register( | ||||||||||||||||||||||||||||||||
| 78 | HealthCheck::Diagnostic::RemoteHealth->new( | ||||||||||||||||||||||||||||||||
| 79 | url => "https://example.com/healthz", | ||||||||||||||||||||||||||||||||
| 80 | ) | ||||||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | Takes in a C<url> to a HealthCheck JSON endpoint | ||||||||||||||||||||||||||||||||
| 86 | and checks to see if a connection can be made. | ||||||||||||||||||||||||||||||||
| 87 | If the connection fails or the JSON object cannot be decoded, | ||||||||||||||||||||||||||||||||
| 88 | the C<status> is set to "CRITICAL". | ||||||||||||||||||||||||||||||||
| 89 | If both the connection succeeds and the JSON object is successfully decoded, | ||||||||||||||||||||||||||||||||
| 90 | it returns the decoded JSON object from the remote endpoint. | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | This diagnostic inherits all attributes from | ||||||||||||||||||||||||||||||||
| 95 | L<HealthCheck::Diagnostic::WebRequest> in addition to its own. | ||||||||||||||||||||||||||||||||
| 96 | C<status_code> is by default set to "200, 503". | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | =head2 url | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | The URL to the remote HealthCheck JSON endpoint. This typically ends in | ||||||||||||||||||||||||||||||||
| 101 | "I</healthz>". | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | =over 4 | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | =item * | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | L<HealthCheck::Diagnostic::WebRequest> | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | =item * | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | L<JSON> | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | =back | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | Grant Street Group <developers@grantstreet.com> | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | This software is Copyright (c) 2021 by Grant Street Group. | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||||||