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 100% 5 / 5 totaltotal 79.8% 103 / 129 CC 21 SCAR 16.1
CC
21
Cov
83%
test_read_only
7.3
run
7.1
new
6.6
CRAP 5.0
↑ 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.6'; # VERSION
15  
16 2 ▶sub new {
Subroutine new: called CC=4 SCAR=18.9
Pod documented
17 2 my ($class, @params) = @_;
18  
19 my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
20 0 ▶ ? %{ $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 8 ▶sub check {
Subroutine check: called CC=4 SCAR=13.9
Pod documented
30 8 my ($self, %params) = @_;
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 7 ▶sub run {
Subroutine run: called CC=7 SCAR=19.7
Pod documented
45 7 my ($self, %params) = @_;
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 # Attempt to reconnect up to 5 times every 1 second. It is common to
64 # need to reconnect when in a hiredis environment in particular.
65 reconnect => 5,
66 every => 1_000_000,
67  
68 # 5 second connect/read/write timeouts.
69 cnx_timeout => 5,
70 read_timeout => 5,
71 write_timeout => 5,
72 );
73 };
74 return {
75 7 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 6 7
76 info => "Error for $description: $@",
77 } if $@;
78  
79 6 ▶ unless ($redis->ping) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($redis->ping) 0 6 6
80 return {
81 0 status => 'CRITICAL',
82 info => "Error for $description: Redis ping failed",
83 };
84 }
85  
86 # Attempt to get a result from the readability or writeability
87 # test.
88 my $res = $params{read_only}
89 6 ▶ ? $self->test_read_only( $redis, $description, %params )
Branch: 1 branch
BranchTrueFalseTotal
$params{'read_only'} ? : 2 4 6
90 : $self->test_read_write( $redis, $description, %params );
91  
92 6 ▶ return $res if ref $res eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $res eq "HASH" 2 4 6
93 return {
94 4 status => 'OK',
95 info => "Successful connection for $description",
96 };
97 }
98  
99 2 ▶sub test_read_only {
Subroutine test_read_only: called CC=6 SCAR=19.9
Pod documented
100 2 my ($self, $redis, $description, %params) = @_;
101  
102 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
103 return {
104 2 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 2 2
105 info => sprintf( 'Error for %s: Failed getting random entry - %s',
106 $description,
107 $error,
108 ),
109 } if $error;
110  
111 # When there is no key, that means we don't have anything in the
112 # database. No need to ping on that.
113 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
114  
115 2 my $val = $redis->get( $key );
116 return {
117 2 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 1 2
118 info => sprintf( 'Error for %s: Failed reading value of key %s',
119 $description,
120 $key,
121 ),
122 } unless defined $val;
123 }
124  
125 4 ▶sub test_read_write {
Subroutine test_read_write: called CC=4 SCAR=14.0
Pod documented
126 4 my ($self, $redis, $description, %params) = @_;
127 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
128 '_health_check_%s',
129 String::Random->new->randregex('[A-Z0-9]{24}'),
130 );
131  
132 # Do not overwrite anything in the database.
133 return {
134 4 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if defined $redis->get($key) 1 3 4
135 info => sprintf( 'Error for %s: Cannot overwrite key %s',
136 $description,
137 $key,
138 ),
139 } if defined $redis->get( $key );
140  
141 # Set, get, and delete the temporary value. Also set an expiration
142 # date of 5 seconds after setting just in-case.
143 3 $redis->set( $key => 'temp', EX => 5 );
144 3 my $val = $redis->get( $key );
145 3 $redis->del( $key );
146  
147 return {
148 3 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 3 3
149 info => sprintf( 'Error for %s: Failed writing to key %s',
150 $description,
151 $key,
152 ),
153 } unless defined $val;
154 }
155  
156 2 1;
157  
158 __END__
↑ Summary