blib/lib/HealthCheck/Diagnostic/Redis.pm

filter: Statementstmt 96.7% 60 / 62 Branchbran 75.0% 21 / 28 Conditioncond 35.7% 5 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 12 / 12 Podpod 60.0% 3 / 5 totaltotal 78.2% 101 / 129 SCAR 31.5
CC
21
Cov
83%
test_read_only
7.3
run
7.1
new
6.6
CRAP 23.3
↑ Summary
1 package HealthCheck::Diagnostic::Redis;
2  
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 parent 'HealthCheck::Diagnostic';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Redis::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use String::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # ABSTRACT: Check for Redis connectivity and operations in HealthCheck
13 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 our $VERSION = 'v0.0.5'; # VERSION
15  
16 sub new {
17 2 ▶ my ($class, @params) = @_;
Subroutine new: called CC=4 SCAR=18.9
Pod documented
18  
19 my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
20 2 ▶ ? %{ $params[0] } : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 && (ref $params[0] || '') eq 'HASH' ? : 0 2 2
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
!ll&&!rl&&r
200
ref $params[0] || ''
l!l
00
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]
ABresult
0X0
100
111
21  
22 2 return $class->SUPER::new(
23 id => 'redis',
24 label => 'redis',
25 %params,
26 );
27 }
28  
29 sub check {
30 8 ▶ my ($self, %params) = @_;
Subroutine check: called CC=4 SCAR=13.9
Pod documented
31
32 # Allow the diagnostic to be called as a class as well.
33 8 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 2 6 8
34 $params{$_} = $self->{$_}
35 2 foreach grep { ! defined $params{$_} } keys %$self;
36 }
37
38 # The host is the only required parameter.
39 8 ▶ croak "No host" unless $params{host};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"host"} 1 7 8
40
41 7 return $self->SUPER::check(%params);
42 }
43  
44 sub run {
45 7 ▶ my ($self, %params) = @_;
Subroutine run: called CC=7 SCAR=19.7
Pod documented
46  
47 7 my $host = $params{host};
48  
49 7 my $name = $params{name};
50 7 ▶ my $description = $name ? "$name ($host) Redis" : "$host Redis";
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 7 7
51  
52 # Add on the port if need be.
53 7 ▶ $host .= ':6379' unless $host =~ /:\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $host =~ /:\d+$/ 7 0 7
54  
55 # Connect to the host...
56 7 my $redis;
57 7 local $@;
58 7 eval {
59 7 local $SIG{__DIE__};
60 7 $redis = Redis::Fast->new(
61 server => $host,
62  
63 # HealthCheck should not reconnect
64 reconnect => 0,
65  
66 # Make this quick...
67 cnx_timeout => 0.5,
68 read_timeout => 0.5,
69 write_timeout => 0.5,
70 );
71 };
72 return {
73 7 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 6 7
74 info => "Error for $description: $@",
75 } if $@;
76  
77 6 ▶ unless ($redis->ping) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($redis->ping) 0 6 6
78 return {
79 0 status => 'CRITICAL',
80 info => "Error for $description: Redis ping failed",
81 };
82 }
83  
84 # Attempt to get a result from the readability or writeability
85 # test.
86 my $res = $params{read_only}
87 6 ▶ ? $self->test_read_only( $redis, $description, %params )
Branch: 1 branch
BranchTrueFalseTotal
$params{'read_only'} ? : 2 4 6
88 : $self->test_read_write( $redis, $description, %params );
89  
90 6 ▶ return $res if ref $res eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $res eq "HASH" 2 4 6
91 return {
92 4 status => 'OK',
93 info => "Successful connection for $description",
94 };
95 }
96  
97 sub test_read_only {
98 2 ▶ my ($self, $redis, $description, %params) = @_;
Subroutine test_read_only: called CC=6 SCAR=19.9
Pod undocumented
99  
100 2 ▶ my ($key, $error) = ($params{key_name}) || $redis->randomkey;
Condition33%1/3
$params{'key_name'} || $redis->randomkey
l!l&&r!l&&!r
200
MC/DC0%0/2
$params{'key_name'} || $redis->randomkey
$params{'key_name'} $redis->randomkey
Truth table33%1/3
A || B
A= $params{'key_name'}B= $redis->randomkey
ABresult
1X1
011
000
101 return {
102 2 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 2 2
103 info => sprintf( 'Error for %s: Failed getting random entry - %s',
104 $description,
105 $error,
106 ),
107 } if $error;
108  
109 # When there is no key, that means we don't have anything in the
110 # database. No need to ping on that.
111 2 ▶ return unless $key || $params{key_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $key or $params{"key_name"} 0 2 2
Condition33%1/3
$key or $params{"key_name"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$key or $params{"key_name"}
$key $params{"key_name"}
Truth table33%1/3
A or B
A= $keyB= $params{"key_name"}
ABresult
1X1
011
000
112  
113 2 my $val = $redis->get( $key );
114 return {
115 2 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 1 2
116 info => sprintf( 'Error for %s: Failed reading value of key %s',
117 $description,
118 $key,
119 ),
120 } unless defined $val;
121 }
122  
123 sub test_read_write {
124 4 ▶ my ($self, $redis, $description, %params) = @_;
Subroutine test_read_write: called CC=4 SCAR=14.0
Pod undocumented
125 4 ▶ my $key = $params{key_name} || sprintf(
Condition66%2/3
$params{'key_name'} || sprintf('_health_check_%s', 'String::Random'->new->randregex('[A-Z0-9]{24}'))
l!l&&r!l&&!r
130
MC/DC0%0/2
$params{'key_name'} || sprintf('_health_check_%s', 'String::Random'->new->randregex('[A-Z0-9]{24}'))
$params{'key_name'} sprintf('_health_check_%s', 'String::Random'->new->randregex('[A-Z0-9]{24}'))
Truth table66%2/3
A || B
A= $params{'key_name'}B= sprintf('_health_check_%s', 'String::Random'->new->randregex('[A-Z0-9]{24}'))
ABresult
1X1
011
000
126 '_health_check_%s',
127 String::Random->new->randregex('[A-Z0-9]{24}'),
128 );
129  
130 # Do not overwrite anything in the database.
131 return {
132 4 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if defined $redis->get($key) 1 3 4
133 info => sprintf( 'Error for %s: Cannot overwrite key %s',
134 $description,
135 $key,
136 ),
137 } if defined $redis->get( $key );
138  
139 # Set, get, and delete the temporary value. Also set an expiration
140 # date of 5 seconds after setting just in-case.
141 3 $redis->set( $key => 'temp', EX => 5 );
142 3 my $val = $redis->get( $key );
143 3 $redis->del( $key );
144  
145 return {
146 3 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 3 3
147 info => sprintf( 'Error for %s: Failed writing to key %s',
148 $description,
149 $key,
150 ),
151 } unless defined $val;
152 }
153  
154 2 1;
155  
156 __END__
157  
158 =pod
159  
160 =encoding UTF-8
161  
162 =head1 NAME
163  
164 HealthCheck::Diagnostic::Redis - Check for Redis connectivity and operations in HealthCheck
165  
166 =head1 VERSION
167  
168 version v0.0.5
169  
170 =head1 SYNOPSIS
171  
172 use HealthCheck::Diagnostic::Redis;
173  
174 # Just check that we can connect to a host, and get a random value back.
175 HealthCheck::Diagnostic::Redis->check(
176 host => 'redis.example.com',
177 );
178  
179 =head1 DESCRIPTION
180  
181 This Diagnostic will simply allow for an application to test for connectivity
182 to a Redis server, and additionally validate that it can successfully read keys
183 from that server.
184  
185 =head1 ATTRIBUTES
186  
187 =head2 name
188  
189 A descriptive name for the connection test.
190 This gets populated in the resulting C<info> tag.
191  
192 =head2 host
193  
194 The server name to connect to for the test.
195 This is required.
196  
197 =head2 read_only
198  
199 Run a read-only check, instead of the read-and-write check provided
200 by-default.
201  
202 =head2 key_name
203  
204 Use a static key name instead of a randomly-generated one.
205  
206 =head1 DEPENDENCIES
207  
208 L<Redis::Fast>
209 L<HealthCheck::Diagnostic>
210  
211 =head1 CONFIGURATION AND ENVIRONMENT
212  
213 None
214  
215 =head1 AUTHOR
216  
217 Grant Street Group <developers@grantstreet.com>
218  
219 =head1 COPYRIGHT AND LICENSE
220  
221 This software is Copyright (c) 2019 - 2020 by Grant Street Group.
222  
223 This is free software, licensed under:
224  
225 The Artistic License 2.0 (GPL Compatible)
226  
227 =cut
↑ Summary