Skip to content

Commit

Permalink
Add a hash of hash response to Heartcheck
Browse files Browse the repository at this point in the history
Add a hash of hash response to Heartcheck
  • Loading branch information
Lucca Baratera committed May 18, 2020
1 parent 73e6045 commit 534f6b2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
15 changes: 11 additions & 4 deletions lib/heartcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ class << self
# @attr [Array<Checks>] the checks to use when checking
attr_accessor :checks

# @attr [Heartcheck::Executors::Base] the checks executor backend
# @attr [Heartcheck::Executors::Base] or [Heartcheck::Executors::HashResponse]
# the checks executor backend
attr_accessor :executor

# @attr [Boolean] the option for using a hash response or an array response
attr_reader :hash_response

# @attr_writer [Object] change the default logger
attr_writer :logger

Expand All @@ -38,7 +42,8 @@ def logger
# end
#
# @return [void]
def setup
def setup(options = {})
@hash_response = options.fetch(:hash_response, false)
yield(self)
end

Expand Down Expand Up @@ -97,9 +102,11 @@ def info_checks

# an executor class that respond to dispatch(checkers)
#
# @return [Heartcheck::Executors::Base]
# @return [Heartcheck::Executors::Base] or [Heartcheck::Executors::HashResponse]
def executor
@executor ||= Heartcheck::Executors::Base.new
@executor ||= hash_response ?
Heartcheck::Executors::HashResponse.new :
Heartcheck::Executors::Base.new
end

# change current executor to a threaded implementation
Expand Down
20 changes: 20 additions & 0 deletions lib/heartcheck/executors/hash_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Heartcheck
module Executors
class HashResponse
def dispatch(checkers)
checkers.map do |checker|
track_and_check(checker)
end.reduce({}, :merge)
end

def track_and_check(checker)
started = Time.now
checker.check.tap do |checked|
value = checked.values.first.merge(time:((Time.now - started) * 1_000.0))
checked.merge!(Hash[checked.keys.first => value])
Logger.info MultiJson.dump(checked)
end
end
end
end
end
39 changes: 39 additions & 0 deletions spec/lib/heartcheck/executors/hash_response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'heartcheck/executors/hash_response'

module Heartcheck
module Executors
describe HashResponse do
subject { described_class.new }

describe '#dispatch' do
let(:registered) { 3 }

let(:checkers) do
registered.times.collect do |current|
double.tap do |checker|
expect(checker).to receive(:check).and_return({ current: { status: 'ok' } })
end
end
end

it 'should register a log entry for each checker' do
expect(Logger).to receive(:info).exactly(registered).times
subject.dispatch(checkers)
end

it "should have a :time key in the checker response" do
subject.dispatch(checkers).each do |current|
expect(current.last).to include(:time)
end
end

it "should have a float value (time key)" do
subject.dispatch(checkers).each do |current|
expect(current.last[:time]).to be_a(Float)
end
end
end
end
end
end

15 changes: 13 additions & 2 deletions spec/lib/heartcheck_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'heartcheck/executors/hash_response'
describe Heartcheck do
let(:essential) { Heartcheck::Checks::Base.new }
let(:functional) { Heartcheck::Checks::Base.new.tap { |c| c.functional = true } }
Expand All @@ -22,21 +23,31 @@
end

describe '#executor' do
context 'with hash_response' do
before(:each) { described_class.instance_variable_set :@executor, nil }
it 'returns a Heartcheck::Executors::HashResponse' do
described_class.setup hash_response: true do |monitor|
expect(monitor.executor).to be_a(Heartcheck::Executors::HashResponse)
end
end
end

context 'with default' do
before(:each) { described_class.instance_variable_set :@executor, nil }
it 'returns a Heartcheck::Executors::Base' do
described_class.setup do |monitor|
expect(monitor.executor).to be_a(Heartcheck::Executors::Base)
end
end
end

context 'with custom' do
context 'with custom' do
it 'returns a custom custom executor' do
described_class.executor = 'lala'
expect(described_class.executor).to be_a(String)
end
end

context 'with threaded' do
it 'returns a threaded executor' do
described_class.use_threaded_executor!
Expand Down

0 comments on commit 534f6b2

Please sign in to comment.