Skip to content

Commit

Permalink
Merge pull request #26 from locaweb/inspect
Browse files Browse the repository at this point in the history
Add inspect endpoint
  • Loading branch information
rhruiz authored Nov 27, 2018
2 parents f73d8af + 110e8ce commit 261f9c6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ Then edit the generated file by adding your checks on it and restart your
server. Now you should be able to make a HTTP request for `/monitoring` and
get a JSON response that contains the status for each monitored service.

The following environment variables are needed by heartcheck:

HEARTCHECK_APP_NAME=MyApplicationName


### Using built-in checks

#### Firewall check
Expand Down Expand Up @@ -89,6 +94,13 @@ checks:

/monitoring/health_check


#### General info and dependencies

Returns general application info and a list of dependencies' URIs, but executes no checking:

/monitoring/inspect

## Plugins

* [ActiveRecord](https://github.com/locaweb/heartcheck-activerecord)
Expand Down
1 change: 1 addition & 0 deletions lib/heartcheck/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class App
'/functional' => Controllers::Functional,
'/dev' => Controllers::Dev,
'/info' => Controllers::Info,
'/inspect' => Controllers::Inspect,
'/health_check' => Controllers::HealthCheck,
'/environment' => Controllers::Environment
}
Expand Down
7 changes: 7 additions & 0 deletions lib/heartcheck/checks/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ def inspect
"#<#{self.class.name} name: #{name}, functional: #{functional?}, dev: #{dev?}>"
end

# Returns a structure comprised of host, port and
# schema (protocol) for the check
#
# @return [Hash]
def uri_info
{ error: "#{self.class.name} #url_info not implemented." }
end

def informations
info
Expand Down
10 changes: 10 additions & 0 deletions lib/heartcheck/checks/firewall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ def validate
end
end

def uri_info
services.map do |s|
{
host: s.uri.host,
port: s.uri.port,
scheme: s.uri.scheme
}
end
end

private

def custom_error(service)
Expand Down
25 changes: 25 additions & 0 deletions lib/heartcheck/controllers/inspect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Heartcheck
module Controllers
class Inspect < Base
def index
results = {
application_name: application_name,
dependencies: []
}

checks = Heartcheck.checks
results[:dependencies] += checks.reduce([]) do |acc, elem|
acc << elem.uri_info
end.flatten.uniq

MultiJson.dump(results)
end

private

def application_name
ENV.fetch('HEARTCHECK_APP_NAME')
end
end
end
end
11 changes: 11 additions & 0 deletions spec/lib/heartcheck/checks/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,15 @@
end
end
end

describe '#uri_info' do
context 'for the base class' do
it 'returns a hash with an error message' do
expect(subject.uri_info).to include(:error)
expect(subject.uri_info).not_to include(:host)
expect(subject.uri_info).not_to include(:port)
expect(subject.uri_info).not_to include(:schema)
end
end
end
end
18 changes: 18 additions & 0 deletions spec/lib/heartcheck/checks/firewall_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
end
end

describe '#uri_info' do
context 'when multiple services are being checked' do
before do
subject.add_service(url: 'http://url1.com')
subject.add_service(url: 'https://url2.com')
subject.add_service(url: 'http://url3.com')
end

it 'returs a list os URI hashes' do
result = subject.uri_info

expect(result).to eq([{host: 'url1.com', port: 80, scheme: 'http'},
{host: 'url2.com', port: 443, scheme: 'https'},
{host: 'url3.com', port: 80, scheme: 'http'}])
end
end
end

describe '#validate' do
subject { instance_default }

Expand Down

0 comments on commit 261f9c6

Please sign in to comment.