Skip to content

Commit 4e764e7

Browse files
authored
Merge pull request #9 from scribd/jimp/rubocop
apply rubocop recommendations
2 parents 9045901 + 9606f8e commit 4e764e7

File tree

10 files changed

+475
-469
lines changed

10 files changed

+475
-469
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.rspec
2+
.idea

Gemfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24
ruby '2.5.8'
35

4-
gem 'redis'
56
gem 'dogapi'
7+
gem 'redis'
68

79
group :development, :test do
10+
gem 'guard-rspec'
811
gem 'pry'
912
gem 'rspec'
10-
gem 'guard-rspec'
1113
gem 'timecop'
1214
end

Guardfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# A sample Guardfile
24
# More info at https://github.com/guard/guard#readme
35

@@ -24,8 +26,8 @@
2426
# * zeus: 'zeus rspec' (requires the server to be started separately)
2527
# * 'just' rspec: 'rspec'
2628

27-
guard :rspec, cmd: "bundle exec rspec" do
28-
require "guard/rspec/dsl"
29+
guard :rspec, cmd: 'bundle exec rspec' do
30+
require 'guard/rspec/dsl'
2931
dsl = Guard::RSpec::Dsl.new(self)
3032

3133
# Feel free to open issues for suggestions and improvements
@@ -39,5 +41,4 @@ guard :rspec, cmd: "bundle exec rspec" do
3941
# Ruby files
4042
ruby = dsl.ruby
4143
dsl.watch_spec_files_for(ruby.lib_files)
42-
4344
end

inject_slow_query.rb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
24
# WARNING: This will lock up CPU on the target Redis instance. Never run against production.
35
#
46
# Copyright 2020 Scribd, Inc.
@@ -20,20 +22,20 @@
2022
milliseconds = ARGV[0].to_i
2123
end
2224

23-
SCRIPT =<<HEREDOC
24-
-- From https://medium.com/@stockholmux/simulating-a-slow-command-with-node-redis-and-lua-efadbf913cd9
25-
local aTempKey = "a-temp-key"
26-
local cycles
27-
redis.call("SET",aTempKey,"1")
28-
redis.call("PEXPIRE",aTempKey, #{milliseconds})
29-
for i = 0, #{15000 * milliseconds}, 1 do
30-
local apttl = redis.call("PTTL",aTempKey)
31-
cycles = i;
32-
if apttl == 0 then
33-
break;
34-
end
35-
end
36-
return cycles
25+
SCRIPT = <<~HEREDOC
26+
-- From https://medium.com/@stockholmux/simulating-a-slow-command-with-node-redis-and-lua-efadbf913cd9
27+
local aTempKey = "a-temp-key"
28+
local cycles
29+
redis.call("SET",aTempKey,"1")
30+
redis.call("PEXPIRE",aTempKey, #{milliseconds})
31+
for i = 0, #{15_000 * milliseconds}, 1 do
32+
local apttl = redis.call("PTTL",aTempKey)
33+
cycles = i;
34+
if apttl == 0 then
35+
break;
36+
end
37+
end
38+
return cycles
3739
HEREDOC
3840

3941
LOGGER.info REDIS.eval(SCRIPT)

lambda_function.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
24
# Copyright 2020 Scribd, Inc.
35

46
require 'logger'
@@ -11,7 +13,7 @@
1113

1214
def event_time
1315
# DateTime because Time does not natively parse AWS CloudWatch Event time
14-
DateTime.rfc3339(@event.fetch("time", DateTime.now.rfc3339))
16+
DateTime.rfc3339(@event.fetch('time', DateTime.now.rfc3339))
1517
end
1618

1719
def log_context
@@ -22,15 +24,14 @@ def log_context
2224
LOGGER.info "Event time: #{event_time}."
2325
end
2426

25-
2627
def lambda_handler(event: {}, context: {})
2728
@event = event
2829
log_context
2930

3031
ssm_path = ENV.fetch('SSM_PATH', false)
3132
if ssm_path
3233
require 'aws-sdk-ssm'
33-
resp = Aws::SSM::Client.new().get_parameters_by_path(
34+
resp = Aws::SSM::Client.new.get_parameters_by_path(
3435
path: "/#{ssm_path}/",
3536
recursive: true,
3637
with_decryption: true
@@ -64,6 +65,4 @@ def lambda_handler(event: {}, context: {})
6465
nil
6566
end
6667

67-
if __FILE__ == $0
68-
lambda_handler
69-
end
68+
lambda_handler if __FILE__ == $PROGRAM_NAME

lib/slowlog_check.rb

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# Copyright 2020 Scribd, Inc.
24
require 'logger'
35

@@ -8,16 +10,17 @@ class SlowlogCheck
810

911
def initialize(params = {})
1012
@ddog = params.fetch(:ddog)
11-
@redis= SlowlogCheck::Redis.new(params.fetch(:redis))
13+
@redis = SlowlogCheck::Redis.new(params.fetch(:redis))
1214
@metricname = params.fetch(:metricname)
1315
@namespace = params.fetch(:namespace)
1416
@env = params.fetch(:env)
1517
end
1618

1719
def status_or_error(resp)
18-
return resp[1].fetch("status") if resp[1].key?("status")
19-
return resp[1].fetch("errors") if resp[1].key?("errors")
20-
return resp
20+
return resp[1].fetch('status') if resp[1].key?('status')
21+
return resp[1].fetch('errors') if resp[1].key?('errors')
22+
23+
resp
2124
end
2225

2326
def last_datadog_metrics_submitted_by_me_in_the_last_2_hours
@@ -27,7 +30,8 @@ def last_datadog_metrics_submitted_by_me_in_the_last_2_hours
2730
Time.now
2831
)
2932

30-
raise "Error getting last datadog metric submitted by me" unless status_or_error(resp) == "ok"
33+
raise 'Error getting last datadog metric submitted by me' unless status_or_error(resp) == 'ok'
34+
3135
resp
3236
end
3337

@@ -38,15 +42,15 @@ def minute_precision(time)
3842
end
3943

4044
def last_datadog_metric
41-
series = last_datadog_metrics_submitted_by_me_in_the_last_2_hours[1].fetch("series")
45+
series = last_datadog_metrics_submitted_by_me_in_the_last_2_hours[1].fetch('series')
4246
if series == [] # First invocation
43-
return minute_precision(Time.now - 3600)
47+
minute_precision(Time.now - 3600)
4448
else
4549
minute_precision(
4650
Time.at(
4751
series
4852
.first
49-
.fetch("pointlist")
53+
.fetch('pointlist')
5054
.map { |x| x[0] }
5155
.max
5256
.to_i / 1000
@@ -57,6 +61,7 @@ def last_datadog_metric
5761

5862
def last_time_submitted
5963
return @last_time_submitted if defined? @last_time_submitted
64+
6065
@last_time_submitted = last_datadog_metric
6166
end
6267

@@ -68,7 +73,6 @@ def slowlog_microseconds(slowlog)
6873
slowlog[2]
6974
end
7075

71-
7276
def reporting_interval
7377
now_i = minute_precision(Time.now).to_i - 60
7478
start_time_i = last_time_submitted.to_i + 60
@@ -105,7 +109,6 @@ def add_metric_to_bucket(prior, new)
105109
}
106110
end
107111

108-
109112
def empty_values
110113
{
111114
values: [],
@@ -137,7 +140,7 @@ def pad_results_with_zero(report)
137140
commands_to_zero = @seen_commands.keys - new_commands.keys
138141
commands_to_zero.each do |command|
139142
# inject_zeroing_commands
140-
report[timebucket].merge!({command => empty_values})
143+
report[timebucket].merge!({ command => empty_values })
141144
# And remove it from the seen_commands queue
142145
@seen_commands.delete(command)
143146
end
@@ -218,7 +221,7 @@ def emit_point(params)
218221
tags: tags
219222
}
220223
)
221-
raise "Error submitting metric for #{@redis.replication_group}" unless status_or_error(resp) == "ok"
224+
raise "Error submitting metric for #{@redis.replication_group}" unless status_or_error(resp) == 'ok'
222225

223226
# Sigh. After doing all the work to pass around Time objects, dogapi-rb changes this to an integer.
224227
@last_time_submitted = Time.at(points.first[0])
@@ -236,7 +239,7 @@ def ship_slowlogs
236239
all_metrics = timebucket.fetch(command)
237240

238241
# Emit most metrics
239-
[:avg, :count, :median, :min, :max].each do |metric|
242+
%i[avg count median min max].each do |metric|
240243
emit_point(
241244
metric: metric.to_s,
242245
type: metric == :count ? 'rate' : 'gauge',
@@ -251,76 +254,72 @@ def ship_slowlogs
251254
points: [[timestamp, all_metrics.fetch(:_95percentile)]],
252255
tags: default_tags.merge(command: command)
253256
)
254-
255257
end
256258
end
257259
end
258260

259-
260261
##
261262
# Metadata
262263

263264
def metric_metadatas
264-
[
265-
'avg',
266-
'median',
267-
'min',
268-
'max',
269-
'95percentile'
270-
].map { |metric|
265+
%w[
266+
avg
267+
median
268+
min
269+
max
270+
95percentile
271+
].map do |metric|
271272
{
272-
"name" => @metricname + '.' + metric,
273-
"description" => "slowlog duration #{metric} (µs)",
274-
"short_name" => "#{metric} (µs)",
275-
"integration" => nil,
276-
"statsd_interval" => 60,
277-
"per_unit" => nil,
278-
"type" => "gauge",
279-
"unit" => "microsecond"
273+
'name' => @metricname + '.' + metric,
274+
'description' => "slowlog duration #{metric} (µs)",
275+
'short_name' => "#{metric} (µs)",
276+
'integration' => nil,
277+
'statsd_interval' => 60,
278+
'per_unit' => nil,
279+
'type' => 'gauge',
280+
'unit' => 'microsecond'
280281
}
281-
}.push(
282+
end.push(
282283
{
283-
"name" => @metricname + '.count',
284-
"type" => 'rate',
285-
"description" => 'slowlog entries per minute',
286-
"short_name" => 'per minute',
287-
"per_unit" => 'minute',
288-
"integration" => nil,
289-
"unit" => 'entry',
290-
"statsd_interval" => 60
284+
'name' => @metricname + '.count',
285+
'type' => 'rate',
286+
'description' => 'slowlog entries per minute',
287+
'short_name' => 'per minute',
288+
'per_unit' => 'minute',
289+
'integration' => nil,
290+
'unit' => 'entry',
291+
'statsd_interval' => 60
291292
}
292293
)
293294
end
294295

295296
def get_metadatas
296-
[
297-
'avg',
298-
'median',
299-
'min',
300-
'max',
301-
'95percentile',
302-
'count'
297+
%w[
298+
avg
299+
median
300+
min
301+
max
302+
95percentile
303+
count
303304
].map do |metric|
304305
name = @metricname + '.' + metric
305306
@ddog.get_metadata(name)[1]
306-
.merge("name" => name)
307+
.merge('name' => name)
307308
end
308309
end
309310

310311
def diff_metadatas
311312
metric_metadatas - get_metadatas
312313
end
313314

314-
315315
def update_metadatas
316316
diff_metadatas.each do |metadata|
317-
name = metadata.delete("name")
317+
name = metadata.delete('name')
318318
resp = @ddog.update_metadata(
319319
name,
320-
metadata.transform_keys { |key| key.to_sym }
320+
metadata.transform_keys(&:to_sym)
321321
)
322322
LOGGER.info "Updating metadata for #{name} #{status_or_error(resp)}"
323323
end
324324
end
325-
326325
end

0 commit comments

Comments
 (0)