22require 'logger'
33
44class SlowlogCheck
5+ require_relative 'slowlog_check/redis'
6+
57 ::LOGGER ||= ::Logger . new ( $stdout)
6- MAXLENGTH = 1048576 #255 levels of recursion for #
78
89 def initialize ( params = { } )
910 @ddog = params . fetch ( :ddog )
10- @redis = params . fetch ( :redis )
11+ @redis = SlowlogCheck :: Redis . new ( params . fetch ( :redis ) )
1112 @metricname = params . fetch ( :metricname )
1213 @namespace = params . fetch ( :namespace )
1314 @env = params . fetch ( :env )
1415 end
1516
16- def replication_group
17- host = @redis . connection . fetch ( :host )
18- matches = /\w \. (?<replication_group>[\w -]+)\. \w +\. \w +\. cache\. amazonaws\. com/ . match ( host )
19- if matches
20- matches [ :replication_group ]
21- else
22- raise "Unable to parse REDIS_HOST. Is #{ host } a valid elasticache endpoint?"
23- end
24- end
25-
2617 def status_or_error ( resp )
2718 return resp [ 1 ] . fetch ( "status" ) if resp [ 1 ] . key? ( "status" )
2819 return resp [ 1 ] . fetch ( "errors" ) if resp [ 1 ] . key? ( "errors" )
@@ -31,7 +22,7 @@ def status_or_error(resp)
3122
3223 def last_datadog_metrics_submitted_by_me_in_the_last_2_hours
3324 resp = @ddog . get_points (
34- "#{ @metricname } .95percentile{replication_group:#{ replication_group } }" ,
25+ "#{ @metricname } .95percentile{replication_group:#{ @redis . replication_group } }" ,
3526 Time . now - 7200 ,
3627 Time . now
3728 )
@@ -114,17 +105,6 @@ def add_metric_to_bucket(prior, new)
114105 }
115106 end
116107
117- def did_i_get_it_all? ( slowlog )
118- slowlog [ -1 ] [ 0 ] == 0
119- end
120-
121- def redis_slowlog ( length = 128 )
122- resp = @redis . slowlog ( 'get' , length )
123-
124- return resp if length > MAXLENGTH
125- return resp if did_i_get_it_all? ( resp )
126- return redis_slowlog ( length * 2 )
127- end
128108
129109 def empty_values
130110 {
@@ -169,7 +149,7 @@ def pad_results_with_zero(report)
169149
170150 def slowlogs_by_flush_interval
171151 result = reporting_interval
172- redis_slowlog . each do |slowlog |
152+ @redis . slowlog . each do |slowlog |
173153 time = slowlog_time ( slowlog )
174154 break if minute_precision ( time ) <= minute_precision ( last_time_submitted )
175155
@@ -211,8 +191,8 @@ def slowlogs_by_flush_interval
211191
212192 def default_tags
213193 {
214- replication_group : replication_group ,
215- service : replication_group ,
194+ replication_group : @redis . replication_group ,
195+ service : @redis . replication_group ,
216196 namespace : @namespace ,
217197 aws : 'true' ,
218198 env : @env
@@ -224,7 +204,7 @@ def emit_point(params)
224204 type = params . fetch ( :type , 'gauge' )
225205 interval = params . fetch ( :interval , 60 )
226206 points = params . fetch ( :points )
227- host = params . fetch ( :host , replication_group )
207+ host = params . fetch ( :host , @redis . replication_group )
228208 tags = params . fetch ( :tags , default_tags )
229209
230210 LOGGER . info "Sending slowlog entry: #{ metric } : #{ points . first [ 1 ] } µs executing #{ tags [ :command ] } at #{ points . first [ 0 ] } ."
@@ -238,7 +218,7 @@ def emit_point(params)
238218 tags : tags
239219 }
240220 )
241- raise "Error submitting metric for #{ replication_group } " unless status_or_error ( resp ) == "ok"
221+ raise "Error submitting metric for #{ @redis . replication_group } " unless status_or_error ( resp ) == "ok"
242222
243223 # Sigh. After doing all the work to pass around Time objects, dogapi-rb changes this to an integer.
244224 @last_time_submitted = Time . at ( points . first [ 0 ] )
0 commit comments