From 70103e83142184cf49cb3978705648b016820169 Mon Sep 17 00:00:00 2001 From: Luiz Carlos Cavalcanti Date: Fri, 23 Nov 2018 11:18:56 -0200 Subject: [PATCH 1/5] Add inspect endpoint An inspection endpoint is being implemented in a non-intrusive way to support future functionality planned by IaaS team, such as dependencies graph building and advanced visual monitoring. All check plug-ins must implement their own version of #uri_info to work be shown in the dependencies collection. Such implementation will be provided soon in future PRs. Signed-off-by: Luiz Carlos Cavalcanti --- lib/heartcheck/app.rb | 1 + lib/heartcheck/checks/base.rb | 7 +++++++ lib/heartcheck/controllers/inspect.rb | 20 ++++++++++++++++++++ spec/lib/heartcheck/checks/base_spec.rb | 11 +++++++++++ 4 files changed, 39 insertions(+) create mode 100644 lib/heartcheck/controllers/inspect.rb diff --git a/lib/heartcheck/app.rb b/lib/heartcheck/app.rb index da434f9..7e75bda 100644 --- a/lib/heartcheck/app.rb +++ b/lib/heartcheck/app.rb @@ -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 } diff --git a/lib/heartcheck/checks/base.rb b/lib/heartcheck/checks/base.rb index 917216d..b68a324 100644 --- a/lib/heartcheck/checks/base.rb +++ b/lib/heartcheck/checks/base.rb @@ -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 diff --git a/lib/heartcheck/controllers/inspect.rb b/lib/heartcheck/controllers/inspect.rb new file mode 100644 index 0000000..7fc4c39 --- /dev/null +++ b/lib/heartcheck/controllers/inspect.rb @@ -0,0 +1,20 @@ +module Heartcheck + module Controllers + class Inspect < Base + def index + results = { + application_name: Rails.application.class.name, + environment: ENV['RAILS_ENV'], + dependencies: [] + } + + checks = Heartcheck.checks + results[:dependencies] += checks.reduce([]) do |acc, elem| + acc << elem.uri_info + end.flatten + + MultiJson.dump(results) + end + end + end +end diff --git a/spec/lib/heartcheck/checks/base_spec.rb b/spec/lib/heartcheck/checks/base_spec.rb index eedda93..97d654e 100644 --- a/spec/lib/heartcheck/checks/base_spec.rb +++ b/spec/lib/heartcheck/checks/base_spec.rb @@ -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 From 9fa5fc374f65a0e4d6e4be15176d8e78aa9ec910 Mon Sep 17 00:00:00 2001 From: Luiz Cavalcanti Date: Mon, 26 Nov 2018 15:35:40 -0200 Subject: [PATCH 2/5] Add environment variable for application name Signed-off-by: Luiz Cavalcanti --- README.md | 12 ++++++++++++ lib/heartcheck/controllers/inspect.rb | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a647e74..2967257 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/lib/heartcheck/controllers/inspect.rb b/lib/heartcheck/controllers/inspect.rb index 7fc4c39..43516fb 100644 --- a/lib/heartcheck/controllers/inspect.rb +++ b/lib/heartcheck/controllers/inspect.rb @@ -3,8 +3,8 @@ module Controllers class Inspect < Base def index results = { - application_name: Rails.application.class.name, - environment: ENV['RAILS_ENV'], + application_name: application_name, + environment: environment, dependencies: [] } @@ -15,6 +15,16 @@ def index MultiJson.dump(results) end + + private + + def application_name + ENV.fetch('HEARTCHECK_APP_NAME') + end + + def environment + ENV.fetch('RAILS_ENV') + end end end end From f065dc91c721feed2c151fcfb097a8dab69e7483 Mon Sep 17 00:00:00 2001 From: Luiz Cavalcanti Date: Mon, 26 Nov 2018 16:41:57 -0200 Subject: [PATCH 3/5] Implementation of #uri_info for Firewall check Signed-off-by: Luiz Cavalcanti --- lib/heartcheck/checks/firewall.rb | 10 ++++++++++ spec/lib/heartcheck/checks/firewall_spec.rb | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/heartcheck/checks/firewall.rb b/lib/heartcheck/checks/firewall.rb index 882ea21..00a4071 100644 --- a/lib/heartcheck/checks/firewall.rb +++ b/lib/heartcheck/checks/firewall.rb @@ -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) diff --git a/spec/lib/heartcheck/checks/firewall_spec.rb b/spec/lib/heartcheck/checks/firewall_spec.rb index c309266..e979ff1 100644 --- a/spec/lib/heartcheck/checks/firewall_spec.rb +++ b/spec/lib/heartcheck/checks/firewall_spec.rb @@ -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 } From 44f74c937c58ce885a4ca718f93b92c0751e5c27 Mon Sep 17 00:00:00 2001 From: Luiz Cavalcanti Date: Mon, 26 Nov 2018 17:27:04 -0200 Subject: [PATCH 4/5] Remove environment info from inspect response Signed-off-by: Luiz Cavalcanti --- lib/heartcheck/controllers/inspect.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/heartcheck/controllers/inspect.rb b/lib/heartcheck/controllers/inspect.rb index 43516fb..5211c97 100644 --- a/lib/heartcheck/controllers/inspect.rb +++ b/lib/heartcheck/controllers/inspect.rb @@ -4,7 +4,6 @@ class Inspect < Base def index results = { application_name: application_name, - environment: environment, dependencies: [] } @@ -21,10 +20,6 @@ def index def application_name ENV.fetch('HEARTCHECK_APP_NAME') end - - def environment - ENV.fetch('RAILS_ENV') - end end end end From 110e8cec10315c690c7366c89115e37261fdb17a Mon Sep 17 00:00:00 2001 From: Luiz Cavalcanti Date: Mon, 26 Nov 2018 18:37:13 -0200 Subject: [PATCH 5/5] Remove duplicated dependencies on inspect response Signed-off-by: Luiz Cavalcanti --- lib/heartcheck/controllers/inspect.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/heartcheck/controllers/inspect.rb b/lib/heartcheck/controllers/inspect.rb index 5211c97..1e130b1 100644 --- a/lib/heartcheck/controllers/inspect.rb +++ b/lib/heartcheck/controllers/inspect.rb @@ -10,7 +10,7 @@ def index checks = Heartcheck.checks results[:dependencies] += checks.reduce([]) do |acc, elem| acc << elem.uri_info - end.flatten + end.flatten.uniq MultiJson.dump(results) end