Skip to content

Commit eaa4d29

Browse files
committed
feat: test the program with rspec
1 parent 92431b4 commit eaa4d29

File tree

8 files changed

+722
-263
lines changed

8 files changed

+722
-263
lines changed

Gemfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ source 'https://rubygems.org'
33
gem 'redis'
44
gem 'dogapi'
55

6-
group :development do
6+
group :development, :test do
77
gem 'pry'
8+
gem 'rspec'
9+
gem 'guard-rspec'
10+
gem 'timecop'
811
end

Gemfile.lock

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,69 @@ GEM
22
remote: https://rubygems.org/
33
specs:
44
coderay (1.1.2)
5+
diff-lcs (1.3)
56
dogapi (1.40.0)
67
multi_json
8+
ffi (1.12.2)
9+
formatador (0.2.5)
10+
guard (2.16.2)
11+
formatador (>= 0.2.4)
12+
listen (>= 2.7, < 4.0)
13+
lumberjack (>= 1.0.12, < 2.0)
14+
nenv (~> 0.1)
15+
notiffany (~> 0.0)
16+
pry (>= 0.9.12)
17+
shellany (~> 0.0)
18+
thor (>= 0.18.1)
19+
guard-compat (1.2.1)
20+
guard-rspec (4.7.3)
21+
guard (~> 2.1)
22+
guard-compat (~> 1.1)
23+
rspec (>= 2.99.0, < 4.0)
24+
listen (3.2.1)
25+
rb-fsevent (~> 0.10, >= 0.10.3)
26+
rb-inotify (~> 0.9, >= 0.9.10)
27+
lumberjack (1.2.4)
728
method_source (1.0.0)
829
multi_json (1.14.1)
30+
nenv (0.3.0)
31+
notiffany (0.1.3)
32+
nenv (~> 0.1)
33+
shellany (~> 0.0)
934
pry (0.13.1)
1035
coderay (~> 1.1)
1136
method_source (~> 1.0)
37+
rb-fsevent (0.10.3)
38+
rb-inotify (0.10.1)
39+
ffi (~> 1.0)
1240
redis (4.1.3)
41+
rspec (3.9.0)
42+
rspec-core (~> 3.9.0)
43+
rspec-expectations (~> 3.9.0)
44+
rspec-mocks (~> 3.9.0)
45+
rspec-core (3.9.1)
46+
rspec-support (~> 3.9.1)
47+
rspec-expectations (3.9.1)
48+
diff-lcs (>= 1.2.0, < 2.0)
49+
rspec-support (~> 3.9.0)
50+
rspec-mocks (3.9.1)
51+
diff-lcs (>= 1.2.0, < 2.0)
52+
rspec-support (~> 3.9.0)
53+
rspec-support (3.9.2)
54+
shellany (0.0.1)
55+
thor (1.0.1)
56+
timecop (0.9.1)
1357

1458
PLATFORMS
1559
ruby
1660

1761
DEPENDENCIES
1862
dogapi
63+
guard-rspec
1964
pry
2065
redis
66+
rspec
67+
timecop
2168

2269
BUNDLED WITH
2370
1.17.3

Guardfile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# A sample Guardfile
2+
# More info at https://github.com/guard/guard#readme
3+
4+
## Uncomment and set this to only include directories you want to watch
5+
# directories %w(app lib config test spec features) \
6+
# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}
7+
8+
## Note: if you are using the `directories` clause above and you are not
9+
## watching the project directory ('.'), then you will want to move
10+
## the Guardfile to a watched dir and symlink it back, e.g.
11+
#
12+
# $ mkdir config
13+
# $ mv Guardfile config/
14+
# $ ln -s config/Guardfile .
15+
#
16+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17+
18+
# Note: The cmd option is now required due to the increasing number of ways
19+
# rspec may be run, below are examples of the most common uses.
20+
# * bundler: 'bundle exec rspec'
21+
# * bundler binstubs: 'bin/rspec'
22+
# * spring: 'bin/rspec' (This will use spring if running and you have
23+
# installed the spring binstubs per the docs)
24+
# * zeus: 'zeus rspec' (requires the server to be started separately)
25+
# * 'just' rspec: 'rspec'
26+
27+
guard :rspec, cmd: "bundle exec rspec" do
28+
require "guard/rspec/dsl"
29+
dsl = Guard::RSpec::Dsl.new(self)
30+
31+
# Feel free to open issues for suggestions and improvements
32+
33+
# RSpec files
34+
rspec = dsl.rspec
35+
watch(rspec.spec_helper) { rspec.spec_dir }
36+
watch(rspec.spec_support) { rspec.spec_dir }
37+
watch(rspec.spec_files)
38+
39+
# Ruby files
40+
ruby = dsl.ruby
41+
dsl.watch_spec_files_for(ruby.lib_files)
42+
43+
end

lambda_function.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env ruby
2+
# Copyright 2020 Scribd, Inc.
3+
4+
require 'logger'
5+
require 'date'
6+
require 'redis'
7+
require 'dogapi'
8+
require_relative 'lib/slowlog_check'
9+
10+
LOGGER = Logger.new($stdout)
11+
LOGGER.level = Logger::DEBUG
12+
LOGGER.freeze
13+
14+
def event_time
15+
# DateTime because Time does not natively parse AWS CloudWatch Event time
16+
DateTime.rfc3339(@event.fetch("time", DateTime.now.rfc3339))
17+
end
18+
19+
def log_context
20+
LOGGER.debug('## ENVIRONMENT VARIABLES')
21+
LOGGER.debug(ENV.to_a)
22+
LOGGER.debug('## EVENT')
23+
LOGGER.debug(@event)
24+
LOGGER.info "Event time: #{event_time}."
25+
end
26+
27+
def lambda_handler(event: {}, context: {})
28+
@event = event
29+
log_context
30+
31+
@slowlog_check ||= SlowlogCheck.new(
32+
ddog: Dogapi::Client.new(
33+
ENV.fetch('DATADOG_API_KEY'),
34+
ENV.fetch('DATADOG_APP_KEY')
35+
),
36+
redis: Redis.new(
37+
host: ENV.fetch('REDIS_HOST'),
38+
ssl: :true
39+
),
40+
namespace: ENV.fetch('NAMESPACE'),
41+
env: ENV.fetch('ENV'),
42+
metricname: 'scribddev.redis.slowlog.micros'
43+
)
44+
45+
@slowlog_check.ship_slowlogs
46+
47+
nil
48+
end
49+
50+
if __FILE__ == $0
51+
lambda_handler
52+
53+
require 'pry'
54+
binding.pry
55+
end

0 commit comments

Comments
 (0)