-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for the 4 permutations of Elasticache redis endpoints
chore: move redis bits to subclass Fixes #5 See https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/Endpoints.html
- Loading branch information
Showing
8 changed files
with
412 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
require 'redis' | ||
require 'uri' | ||
|
||
class SlowlogCheck::Redis | ||
MAXLENGTH = 1048576 #255 levels of recursion for # | ||
|
||
def initialize(opts) | ||
@host = opts[:host] | ||
end | ||
|
||
def params | ||
if cluster_mode_enabled? | ||
{ | ||
cluster: [uri], | ||
port: port, | ||
ssl: tls_mode? | ||
} | ||
else | ||
{ | ||
host: hostname, | ||
port: port, | ||
ssl: tls_mode? | ||
} | ||
end | ||
end | ||
|
||
def redis | ||
@redis ||= Redis.new(params) | ||
end | ||
|
||
def replication_group | ||
if tls_mode? | ||
matches[:second] | ||
else | ||
matches[:first] | ||
end | ||
end | ||
|
||
|
||
def slowlog(length = 128) | ||
resp = redis.slowlog('get', length) | ||
|
||
return resp if length > MAXLENGTH | ||
return resp if did_i_get_it_all?(resp) | ||
return slowlog(length * 2) | ||
end | ||
|
||
private | ||
|
||
def cluster_mode_enabled? | ||
if tls_mode? | ||
matches[:first] == 'clustercfg' | ||
else | ||
matches[:third] == '' | ||
end | ||
end | ||
|
||
def did_i_get_it_all?(slowlog) | ||
slowlog[-1][0] == 0 | ||
end | ||
|
||
def hostname | ||
URI.parse(@host).hostname or | ||
@host | ||
end | ||
|
||
def matches | ||
redis_uri_regex.match(@host) | ||
end | ||
|
||
def port | ||
regex_port = matches[:port].to_i | ||
if regex_port > 0 | ||
regex_port | ||
else | ||
6379 | ||
end | ||
end | ||
|
||
def uri | ||
'redis' + | ||
lambda { tls_mode? ? 's' : '' }.call + | ||
'://' + | ||
hostname + | ||
':' + | ||
port.to_s | ||
end | ||
|
||
def redis_uri_regex | ||
/((?<scheme>redi[s]+)\:\/\/){0,1}(?<first>[0-9A-Za-z_-]+)\.(?<second>[0-9A-Za-z_-]+)\.{0,1}(?<third>[0-9A-Za-z_]*)\.(?<region>[0-9A-Za-z_-]+)\.cache\.amazonaws\.com:{0,1}(?<port>[0-9]*)/ | ||
end | ||
|
||
def tls_mode? | ||
matches[:scheme] == 'rediss' or | ||
%w[master clustercfg].include?(matches[:first]) | ||
end | ||
|
||
end |
Oops, something went wrong.