55
66class SlowlogCheck
77 class Redis
8- MAXLENGTH = 1_048_576
9- CONNECT_TIMEOUT = ( ENV [ 'REDIS_CONNECT_TIMEOUT' ] || 5 ) . to_i
10- RW_TIMEOUT = ( ENV [ 'REDIS_RW_TIMEOUT' ] || 5 ) . to_i
11- RECONNECT_TRIES = ( ENV [ 'REDIS_RECONNECT_ATTEMPTS' ] || 2 ) . to_i
8+ MAXLENGTH = 1_048_576 # 255 levels of recursion for #
129
1310 def initialize ( opts )
1411 @host = opts [ :host ]
1512 end
1613
1714 def params
18- base =
19- if cluster_mode_enabled?
20- { cluster : [ uri ] , port : port , ssl : tls_mode? }
21- else
22- { host : hostname , port : port , ssl : tls_mode? }
23- end
24-
25- password = ENV [ 'REDIS_PASSWORD' ]
26- base [ :password ] = password unless password . nil? || password . empty?
27-
28- base [ :connect_timeout ] = CONNECT_TIMEOUT
29- base [ :read_timeout ] = RW_TIMEOUT
30- base [ :write_timeout ] = RW_TIMEOUT
31- base [ :reconnect_attempts ] = RECONNECT_TRIES
32- base
15+ if cluster_mode_enabled?
16+ {
17+ cluster : [ uri ] ,
18+ port : port ,
19+ ssl : tls_mode?
20+ }
21+ else
22+ {
23+ host : hostname ,
24+ port : port ,
25+ ssl : tls_mode?
26+ }
27+ end
3328 end
3429
3530 def redis_rb
@@ -44,19 +39,10 @@ def replication_group
4439 end
4540 end
4641
47- # Always returns an Array (possibly empty). Never raises to the caller.
4842 def slowlog_get ( length = 128 )
49- resp = begin
50- redis_rb . slowlog ( 'get' , length )
51- rescue ::Redis ::BaseError , StandardError => e
52- LOGGER &.warn ( "SLOWLOG GET failed (Redis): #{ e . class } : #{ e . message } " ) rescue nil
53- [ ]
54- end
55-
56- resp = [ ] unless resp . is_a? ( Array )
57- resp = resp . select { |e | e . is_a? ( Array ) }
43+ resp = redis_rb . slowlog ( 'get' , length )
5844
59- return resp if length >= MAXLENGTH
45+ return resp if length > MAXLENGTH
6046 return resp if did_i_get_it_all? ( resp )
6147
6248 slowlog_get ( length * 2 )
@@ -65,63 +51,51 @@ def slowlog_get(length = 128)
6551 private
6652
6753 def cluster_mode_enabled?
68- return false if matches . nil?
6954 if tls_mode?
7055 matches [ :first ] == 'clustercfg'
7156 else
72- matches [ :third ] . to_s == ''
57+ matches [ :third ] == ''
7358 end
7459 end
7560
76- # Nil/shape safe
7761 def did_i_get_it_all? ( slowlog )
78- entries = Array ( slowlog ) . select { |e | e . is_a? ( Array ) }
79- return true if entries . empty?
80- id = entries [ -1 ] [ 0 ] rescue nil
81- return true if id . nil?
82- id . to_i . zero?
62+ slowlog [ -1 ] [ 0 ] . zero?
8363 end
8464
8565 def hostname
86- URI . parse ( @host ) . hostname || @host
87- rescue URI ::InvalidURIError
88- @host
66+ URI . parse ( @host ) . hostname or
67+ @host
8968 end
9069
9170 def matches
92- @matches ||= redis_uri_regex . match ( @host )
71+ redis_uri_regex . match ( @host )
9372 end
9473
9574 def port
96- p = matches && matches [ :port ] . to_i
97- p . positive? ? p : 6379
75+ regex_port = matches [ :port ] . to_i
76+ if regex_port . positive?
77+ regex_port
78+ else
79+ 6379
80+ end
9881 end
9982
10083 def uri
101- scheme = tls_mode? ? 'rediss' : 'redis'
102- "#{ scheme } ://#{ hostname } :#{ port } "
84+ 'redis' +
85+ -> { tls_mode? ? 's' : '' } . call +
86+ '://' +
87+ hostname +
88+ ':' +
89+ port . to_s
10390 end
10491
10592 def redis_uri_regex
106- %r{
107- ((?<scheme>redi[s]+)\: //){0,1}
108- (?<first>[0-9A-Za-z_-]+)\.
109- (?<second>[0-9A-Za-z_-]+)\. {0,1}
110- (?<third>[0-9A-Za-z_]*)\.
111- (?<region>[0-9A-Za-z_-]+)\. cache\. amazonaws\. com
112- :{0,1}(?<port>[0-9]*)
113- }x
93+ %r{((?<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]*)}
11494 end
11595
116- # TLS required when:
117- # - env REDIS_TLS=true, OR
118- # - scheme is rediss://, OR
119- # - endpoint starts with master./clustercfg. (ElastiCache with in-transit encryption required)
12096 def tls_mode?
121- return true if ENV [ 'REDIS_TLS' ] . to_s . downcase == 'true'
122- m = matches
123- return false if m . nil?
124- m [ :scheme ] == 'rediss' || %w[ master clustercfg ] . include? ( m [ :first ] )
97+ matches [ :scheme ] == 'rediss' or
98+ %w[ master clustercfg ] . include? ( matches [ :first ] )
12599 end
126100 end
127101end
0 commit comments