Skip to content

Commit

Permalink
Merge pull request #3 from customink/drinks/callbacks
Browse files Browse the repository at this point in the history
Implement Reporters
  • Loading branch information
drinks authored Feb 4, 2020
2 parents f97437c + 82d4b56 commit f52cdc5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.3.0] - 2020-02-04

### Added

- Handler now exposes a `reporter` method to log or track time series statistics of check performance

## [1.2.1] - 2020-02-01

### Fixed

- There is no `hostname` on Lambda.


## [1.2.0] - 2019-11-15

### Added
Expand Down
1 change: 1 addition & 0 deletions lib/is_it_working.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module IsItWorking
autoload :Check, File.expand_path("../is_it_working/check.rb", __FILE__)
autoload :Filter, File.expand_path("../is_it_working/filter.rb", __FILE__)
autoload :Handler, File.expand_path("../is_it_working/handler.rb", __FILE__)
autoload :Reporter, File.expand_path("../is_it_working/reporter.rb", __FILE__)
autoload :Status, File.expand_path("../is_it_working/status.rb", __FILE__)
autoload :Timer, File.expand_path("../is_it_working/timer.rb", __FILE__)

Expand Down
6 changes: 6 additions & 0 deletions lib/is_it_working/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def initialize(app=nil, route_path="/is_it_working", &block)
@hostname = `hostname`.to_s.chomp
@timers = []
@filters = []
@reporters = []
@mutex = Mutex.new
yield self if block_given?
end
Expand All @@ -45,6 +46,7 @@ def call(env)
t = Time.now
statuses = Filter.run_filters(@filters)
Timer.run_timers(@timers)
Reporter.run_reporters(@reporters, @filters)
render(statuses, Time.now - t)
else
@app.call(env)
Expand Down Expand Up @@ -115,6 +117,10 @@ def timer(*args, **kwargs)
@timers << Timer.new(*args, **kwargs) { yield }
end

def reporter(filters, &block)
@reporters << Reporter.new(filters, block)
end

protected

# Lookup a status check filter from the name and arguments
Expand Down
29 changes: 29 additions & 0 deletions lib/is_it_working/reporter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module IsItWorking
class Reporter
attr_reader :name, :filters, :func

def initialize(filters, func)
@filters = filters
@func = func
end

def report_on?(filter)
filters.include? filter.name
end

def report(filter)
func.call(filter.name, filter.status&.time.to_f)
end

class << self
# Run reporters for each named filter in their respective lists
def run_reporters(reporters, filters)
reporters.each do |reporter|
filters.each do |filter|
reporter.report(filter) if reporter.report_on? filter
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/is_it_working/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module IsItWorking
VERSION = '1.2.1'.freeze
VERSION = '1.3.0'.freeze
end
19 changes: 18 additions & 1 deletion spec/handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
response.last.flatten.join.should include("WARN: block - runtime exceeded warning threshold")
end

it "should return a failure status if a check exceeds a warning timeout and a timeout" do
it "should return a failure status if a check exceeds a warning timeout and a timeout" do
handler = IsItWorking::Handler.new do |h|
h.timer(warn_after: 5, fail_after: 9) do
h.check :block do |status|
Expand All @@ -134,6 +134,23 @@
response.last.flatten.join.should include("FAIL: block - runtime exceeded critical threshold")
end

it "should invoke a reporter with the specified filters" do
handler = IsItWorking::Handler.new do |h|
h.check :block do |status|
sleep 0.1
status.ok('That took a while! 😅')
end

h.reporter [:block] do |name, time|
@name = name
@time = time
end
end
handler.call({})
@name.should == :block
@time.should > 0.1
end

it "should be able to be used in a middleware stack with the route /is_it_working" do
app_response = [200, {"Content-Type" => "text/plain"}, ["OK"]]
app = lambda{|env| app_response}
Expand Down

0 comments on commit f52cdc5

Please sign in to comment.