Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a hash of hash response to Heartcheck #33

Merged
merged 10 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
= Heart Check - Changelog

== Version 2.1.0 :: 2021-04-09

* Add support for response formatters and a single hash formatter

== Version 2.0.0 :: 2020-03-10

* Add support for ruby 2.7
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ Returns general application info and a list of dependencies' URIs, but executes

/monitoring/inspect

## Response formatting

An optional formatter may be set for the monitoring response. We provide the default format
using a list of hashes and one with a single Hash. Please check `Heartcheck.use_hash_formatter!`

## Plugins

* [ActiveRecord](https://github.com/locaweb/heartcheck-activerecord)
Expand Down
19 changes: 19 additions & 0 deletions lib/heartcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Heartcheck
require 'heartcheck/errors'
require 'heartcheck/services'
require 'heartcheck/logger'
require 'heartcheck/formatters/base'
require 'heartcheck/formatters/hash_response'

@checks = []

Expand All @@ -16,6 +18,9 @@ class << self
# @attr [Heartcheck::Executors::Base] the checks executor backend
attr_accessor :executor

# @attr [Heartcheck::Formatters::Base] formats heart check result to something JSON encodedable
attr_accessor :formatter

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

Expand Down Expand Up @@ -95,6 +100,20 @@ def info_checks
checks.select { |ctx| ctx.respond_to?(:info) }
end

# transforms check results to a response
#
# @return [Heartcheck::Formatters::Base] or nil
def formatter
@formatter ||= Heartcheck::Formatters::Base.new
end

# change current formatter to a hash response
#
# @return [Heartcheck::Formatters::HashResponse]
def use_hash_formatter!
self.formatter = Heartcheck::Formatters::HashResponse.new
end

# an executor class that respond to dispatch(checkers)
#
# @return [Heartcheck::Executors::Base]
Expand Down
6 changes: 5 additions & 1 deletion lib/heartcheck/controllers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ def index
protected

def check(who)
MultiJson.dump(executor.dispatch(Heartcheck.send("#{who}_checks")))
checks = executor.dispatch(Heartcheck.send("#{who}_checks"))
MultiJson.dump(formatter.format(checks))
end

private
def formatter
Heartcheck.formatter
end

def executor
Heartcheck.executor
Expand Down
9 changes: 9 additions & 0 deletions lib/heartcheck/formatters/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Heartcheck
module Formatters
class Base
def format(checks)
checks
end
end
end
end
14 changes: 14 additions & 0 deletions lib/heartcheck/formatters/hash_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Heartcheck
module Formatters
class HashResponse
def format(checks)
checks.each_with_object({}) do |check, response|
name = check.keys.first
result = check.delete(name)

response[name] = result.merge(check)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/heartcheck/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Heartcheck
VERSION = '2.0.0'.freeze
VERSION = '2.1.0'.freeze
end
15 changes: 15 additions & 0 deletions spec/lib/heartcheck/formatters/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe Heartcheck::Formatters::Base do
describe "#format" do
let(:check_01) { {dummy1: {status: :ok}, time: 1100} }
let(:check_02) { {dummy2: {status: :ok}, time: 100} }

it "generates a list of with a hashes for every check" do
expect(subject.format([check_01, check_02])).to be_eql([
{dummy1: {status: :ok}, time: 1100},
{dummy2: {status: :ok}, time: 100}
])
end
end
end
15 changes: 15 additions & 0 deletions spec/lib/heartcheck/formatters/hash_response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe Heartcheck::Formatters::HashResponse do
describe "#format" do
let(:check_01) { {dummy1: {status: :ok}, time: 1100} }
let(:check_02) { {dummy2: {status: :ok}, time: 100} }

it "generates a single hash with every check as a key" do
expect(subject.format([check_01, check_02])).to be_eql({
dummy1: {status: :ok, time: 1100},
dummy2: {status: :ok, time: 100}
})
end
end
end
21 changes: 21 additions & 0 deletions spec/lib/heartcheck_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@
end
end

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

context 'with hash formatter' do
before(:each) { described_class.instance_variable_set :@formatter, nil }
it 'returns a Heartcheck::Formatters::Base' do
described_class.setup do |monitor|
described_class.use_hash_formatter!
expect(monitor.formatter).to be_a(Heartcheck::Formatters::HashResponse)
end
end
end
end

describe '#executor' do
context 'with default' do
it 'returns a Heartcheck::Executors::Base' do
Expand Down