blib/lib/HealthCheck/Diagnostic/Redis.pm

filter: Statementstmt 95.9% 47 / 49 Branchbran 65.0% 13 / 20 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 82.9% 73 / 88 CC 14 SCAR 16.7
CC
14
Cov
82%
run
8.6
new
6.6
check
4.0
CRAP 5.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  
11 # ABSTRACT: Check for Redis connectivity and operations in HealthCheck
12 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 our $VERSION = 'v0.0.4'; # VERSION
14  
15 sub new {
16 2 ▶ my ($class, @params) = @_;
Subroutine new: called CC=4 SCAR=18.9
Pod documented
17  
18 my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
19 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
20  
21 2 return $class->SUPER::new(
22 id => 'redis',
23 label => 'redis',
24 %params,
25 );
26 }
27  
28 sub check {
29 5 ▶ my ($self, %params) = @_;
Subroutine check: called CC=4 SCAR=13.9
Pod documented
30
31 # Allow the diagnostic to be called as a class as well.
32 5 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 2 3 5
33 $params{$_} = $self->{$_}
34 2 foreach grep { ! defined $params{$_} } keys %$self;
35 }
36
37 # The host is the only required parameter.
38 5 ▶ croak "No host" unless $params{host};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"host"} 1 4 5
39
40 4 return $self->SUPER::check(%params);
41 }
42  
43 sub run {
44 4 ▶ my ($self, %params) = @_;
Subroutine run: called CC=8 SCAR=21.5
Pod documented
45  
46 4 my $host = $params{host};
47  
48 4 my $name = $params{name};
49 4 ▶ my $description = $name ? "$name ($host) Redis" : "$host Redis";
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 4 4
50  
51 # Add on the port if need be.
52 4 ▶ $host .= ':6379' unless $host =~ /:\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $host =~ /:\d+$/ 4 0 4
53  
54 # Connect to the host...
55 4 my $redis;
56 4 local $@;
57 4 eval {
58 4 local $SIG{__DIE__};
59 4 $redis = Redis::Fast->new(
60 server => $host,
61  
62 # HealthCheck should not reconnect
63 reconnect => 0,
64  
65 # Make this quick...
66 cnx_timeout => 0.5,
67 read_timeout => 0.5,
68 write_timeout => 0.5,
69 );
70 };
71 return {
72 4 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 3 4
73 info => "Error for $description: $@",
74 } if $@;
75  
76 3 ▶ unless ($redis->ping) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($redis->ping) 0 3 3
77 return {
78 0 status => 'CRITICAL',
79 info => "Error for $description: Redis ping failed",
80 };
81 }
82  
83 3 my ($key, $error) = $redis->randomkey();
84 return {
85 3 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 3 3
86 info => "Error for $description: Getting Random entry failed - $error",
87 } if ($error);
88  
89 # At this point, the only way this fails is if there are no entries in the
90 # Redis DB.
91 3 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 3 0 3
92 3 my $val = $redis->get($key);
93 return {
94 3 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 3 3
95 info => "Error for $description: Failed fetching value of key $key",
96 } unless defined $val;
97 }
98  
99 return {
100 3 status => 'OK',
101 info => "Successful connection for $description",
102 };
103 }
104  
105 2 1;
106  
107 __END__
108  
109 =pod
110  
111 =encoding UTF-8
112  
113 =head1 NAME
114  
115 HealthCheck::Diagnostic::Redis - Check for Redis connectivity and operations in HealthCheck
116  
117 =head1 VERSION
118  
119 version v0.0.4
120  
121 =head1 SYNOPSIS
122  
123 use HealthCheck::Diagnostic::Redis;
124  
125 # Just check that we can connect to a host, and get a random value back.
126 HealthCheck::Diagnostic::Redis->check(
127 host => 'redis.example.com',
128 );
129  
130 =head1 DESCRIPTION
131  
132 This Diagnostic will simply allow for an application to test for connectivity
133 to a Redis server, and additionally validate that it can successfully read keys
134 from that server.
135  
136 =head1 ATTRIBUTES
137  
138 =head2 name
139  
140 A descriptive name for the connection test.
141 This gets populated in the resulting C<info> tag.
142  
143 =head2 host
144  
145 The server name to connect to for the test.
146 This is required.
147  
148 =head1 DEPENDENCIES
149  
150 L<Redis::Fast>
151 L<HealthCheck::Diagnostic>
152  
153 =head1 CONFIGURATION AND ENVIRONMENT
154  
155 None
156  
157 =head1 AUTHOR
158  
159 Grant Street Group <developers@grantstreet.com>
160  
161 =head1 COPYRIGHT AND LICENSE
162  
163 This software is Copyright (c) 2019 - 2020 by Grant Street Group.
164  
165 This is free software, licensed under:
166  
167 The Artistic License 2.0 (GPL Compatible)
168  
169 =cut
↑ Summary