health_monitor_lb 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +78 -0
- data/Rakefile +8 -0
- data/app/controllers/health_monitor/application_controller.rb +11 -0
- data/app/controllers/health_monitor/health_controller.rb +13 -0
- data/app/services/health_monitor/health_check_service.rb +40 -0
- data/bin/rails +14 -0
- data/config/routes.rb +3 -0
- data/lib/health_monitor/engine.rb +5 -0
- data/lib/health_monitor/version.rb +3 -0
- data/lib/health_monitor.rb +11 -0
- data/lib/tasks/health_monitor_tasks.rake +4 -0
- data/spec/controllers/health_monitor/health_controller_spec.rb +40 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +3 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +2 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/javascript/packs/application.js +15 -0
- data/spec/dummy/app/jobs/application_job.rb +7 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +15 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +33 -0
- data/spec/dummy/config/application.rb +22 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/database.yml +2 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +76 -0
- data/spec/dummy/config/environments/production.rb +120 -0
- data/spec/dummy/config/environments/test.rb +59 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/config/initializers/assets.rb +12 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +8 -0
- data/spec/dummy/config/initializers/content_security_policy.rb +28 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +6 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/permissions_policy.rb +11 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +33 -0
- data/spec/dummy/config/puma.rb +43 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/config.ru +6 -0
- data/spec/dummy/log/development.log +0 -0
- data/spec/dummy/log/test.log +184 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/tmp/development_secret.txt +1 -0
- data/spec/health_monitor_spec.rb +8 -0
- data/spec/rails_helper.rb +1 -0
- data/spec/spec_helper.rb +31 -0
- metadata +178 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f5c7c695382a747e409feb32c8140500f0481be76d2c6a3d734faa12e328682e
|
4
|
+
data.tar.gz: '086859234d7b215306081c53d48fdfb54cf940f5d75b05a18bbed8c5cde99186'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b8a0d67c9e4059adbb8805ec7a9b8f8bd0ee70c7436dfb55425d7bef55657c9c84733c94993143a7762a7e15cd4cc4c844c2c12235ffb0d0004f10c49b3656ae
|
7
|
+
data.tar.gz: 32a1358c803e76eb469eb8b001506613571d2073e648eedcff21bae1ddf280c8fd697a672caaf9e3ef906ebc14caf08554766e94673144eea53c450e97edb813
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# HealthMonitor
|
2
|
+
Add a health check endpoint for AWS Load Balancer monitoring
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'health_monitor'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
```bash
|
13
|
+
$ bundle
|
14
|
+
```
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
```bash
|
18
|
+
$ gem install health_monitor
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
Add the Rails engine included in the gem to your routes by adding
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
mount HealthMonitor::Engine, at: '/'
|
26
|
+
```
|
27
|
+
|
28
|
+
to config/routes.rb
|
29
|
+
|
30
|
+
Set the environment variable `HEALTH_MONITOR_API_TOKEN` to add a token which helps prevent denial of service attacks. Set the query parameter `API_TOKEN` with the token value when making an HTTP request. For example:
|
31
|
+
|
32
|
+
```http
|
33
|
+
https://myapp.com/health?API_TOKEN=12344321
|
34
|
+
```
|
35
|
+
|
36
|
+
### Custom Health Checks
|
37
|
+
The base checks test Postgres, Redis, and the Ruby interpreter as well as listing the Rails version. Additional health checks can be added. Each customer health check needs to be a class with a static method called `health_check`. For example:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class TestHealthCheck
|
41
|
+
def self.health_check
|
42
|
+
{ test_health_check: 'ok' }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
The class name needs to be added to the list of additional checks during setup (typically in an initializer file):
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
HealthMonitor.setup do |config|
|
51
|
+
config.additional_health_checks = ['TestHealthCheck']
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
## License
|
56
|
+
|
57
|
+
(The MIT License)
|
58
|
+
|
59
|
+
Copyright (c) 2009 Jonas Nicklas
|
60
|
+
|
61
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
62
|
+
a copy of this software and associated documentation files (the
|
63
|
+
'Software'), to deal in the Software without restriction, including
|
64
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
65
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
66
|
+
permit persons to whom the Software is furnished to do so, subject to
|
67
|
+
the following conditions:
|
68
|
+
|
69
|
+
The above copyright notice and this permission notice shall be
|
70
|
+
included in all copies or substantial portions of the Software.
|
71
|
+
|
72
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
73
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
74
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
75
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
76
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
77
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
78
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
module HealthMonitor
|
2
|
+
class ApplicationController < ActionController::API
|
3
|
+
before_action :check_api_token
|
4
|
+
|
5
|
+
def check_api_token
|
6
|
+
if ENV['HEALTH_MONITOR_API_TOKEN'].present? && (params['API_TOKEN'] != ENV['HEALTH_MONITOR_API_TOKEN'])
|
7
|
+
render json: 'unauthorized', status: :unauthorized
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module HealthMonitor
|
2
|
+
class HealthController < ApplicationController
|
3
|
+
def index
|
4
|
+
status = HealthCheckService.new.call
|
5
|
+
|
6
|
+
response.set_header('Cache-Control', 'no-cache, no-store, must-revalidate')
|
7
|
+
response.set_header('Pragma', 'no-cache')
|
8
|
+
response.set_header('Expires', '0')
|
9
|
+
|
10
|
+
render json: status, status: :ok
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module HealthMonitor
|
2
|
+
class HealthCheckService
|
3
|
+
def call
|
4
|
+
status = { rails_version: Rails::VERSION::STRING }
|
5
|
+
.merge(ruby_check)
|
6
|
+
.merge(postgres_check)
|
7
|
+
.merge(redis_check)
|
8
|
+
|
9
|
+
HealthMonitor.additional_health_checks.each do |health_check_klass|
|
10
|
+
status = status.merge(health_check_klass.safe_constantize.health_check)
|
11
|
+
end
|
12
|
+
|
13
|
+
status
|
14
|
+
end
|
15
|
+
|
16
|
+
def ruby_check
|
17
|
+
status = "2\n" == `ruby -e 'puts 1 + 1'` ? 'ok' : 'down'
|
18
|
+
|
19
|
+
{ ruby: status }
|
20
|
+
end
|
21
|
+
|
22
|
+
def postgres_check
|
23
|
+
return {} if ENV['DATABASE_URL'].blank?
|
24
|
+
|
25
|
+
pg_conn = PG::Connection.open(ENV['DATABASE_URL'])
|
26
|
+
status = pg_conn.connect_poll ? 'ok' : 'down'
|
27
|
+
|
28
|
+
{ db: status }
|
29
|
+
end
|
30
|
+
|
31
|
+
def redis_check
|
32
|
+
return {} if ENV['REDIS_URL'].blank?
|
33
|
+
|
34
|
+
redis = Redis.new(url: ENV['REDIS_URL'], ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE })
|
35
|
+
status = redis.ping == 'PONG' ? 'ok' : 'down'
|
36
|
+
|
37
|
+
{ redis: status }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/bin/rails
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This command will automatically be run when you run "rails" with Rails gems
|
3
|
+
# installed from the root of your application.
|
4
|
+
|
5
|
+
ENGINE_ROOT = File.expand_path('..', __dir__)
|
6
|
+
ENGINE_PATH = File.expand_path('../lib/health_monitor/engine', __dir__)
|
7
|
+
APP_PATH = File.expand_path('../spec/dummy/config/application', __dir__)
|
8
|
+
|
9
|
+
# Set up gems listed in the Gemfile.
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
|
11
|
+
require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
|
12
|
+
|
13
|
+
require "rails/all"
|
14
|
+
require "rails/engine/commands"
|
data/config/routes.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
module HealthControllerSpec
|
4
|
+
class TestHealthCheck
|
5
|
+
def self.health_check
|
6
|
+
{ 'test_health_check' => 'ok' }
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe HealthMonitor::HealthController, type: :controller do
|
12
|
+
routes { HealthMonitor::Engine.routes }
|
13
|
+
|
14
|
+
context 'not authorized' do
|
15
|
+
it 'returns unauthorized without a token' do
|
16
|
+
get :index
|
17
|
+
expect(response.status).to eq(401)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'authorized' do
|
22
|
+
it 'returns ok with a token' do
|
23
|
+
get :index, params: { API_TOKEN: ENV['HEALTH_MONITOR_API_TOKEN'] }
|
24
|
+
expect(response.status).to eq(200)
|
25
|
+
|
26
|
+
expect(response.parsed_body).to include({ 'rails_version' => Rails::VERSION::STRING, 'ruby' => 'ok' })
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'handles custom checks' do
|
30
|
+
HealthMonitor.additional_health_checks = ['HealthControllerSpec::TestHealthCheck']
|
31
|
+
|
32
|
+
get :index, params: { API_TOKEN: ENV['HEALTH_MONITOR_API_TOKEN'] }
|
33
|
+
expect(response.status).to eq(200)
|
34
|
+
|
35
|
+
expect(response.parsed_body).to include({ 'rails_version' => Rails::VERSION::STRING, 'ruby' => 'ok', 'test_health_check' => 'ok' })
|
36
|
+
|
37
|
+
HealthMonitor.additional_health_checks = []
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,15 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require rails-ujs
|
14
|
+
//= require activestorage
|
15
|
+
//= require_tree .
|
@@ -0,0 +1,7 @@
|
|
1
|
+
class ApplicationJob < ActiveJob::Base
|
2
|
+
# Automatically retry jobs that encountered a deadlock
|
3
|
+
# retry_on ActiveRecord::Deadlocked
|
4
|
+
|
5
|
+
# Most jobs are safe to ignore if the underlying records are no longer available
|
6
|
+
# discard_on ActiveJob::DeserializationError
|
7
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
6
|
+
<%= csrf_meta_tags %>
|
7
|
+
<%= csp_meta_tag %>
|
8
|
+
|
9
|
+
<%= stylesheet_link_tag 'application', media: 'all' %>
|
10
|
+
</head>
|
11
|
+
|
12
|
+
<body>
|
13
|
+
<%= yield %>
|
14
|
+
</body>
|
15
|
+
</html>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= yield %>
|
data/spec/dummy/bin/rake
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = File.expand_path('..', __dir__)
|
6
|
+
|
7
|
+
def system!(*args)
|
8
|
+
system(*args) || abort("\n== Command #{args} failed ==")
|
9
|
+
end
|
10
|
+
|
11
|
+
FileUtils.chdir APP_ROOT do
|
12
|
+
# This script is a way to set up or update your development environment automatically.
|
13
|
+
# This script is idempotent, so that you can run it at any time and get an expectable outcome.
|
14
|
+
# Add necessary setup steps to this file.
|
15
|
+
|
16
|
+
puts '== Installing dependencies =='
|
17
|
+
system! 'gem install bundler --conservative'
|
18
|
+
system('bundle check') || system!('bundle install')
|
19
|
+
|
20
|
+
# puts "\n== Copying sample files =="
|
21
|
+
# unless File.exist?('config/database.yml')
|
22
|
+
# FileUtils.cp 'config/database.yml.sample', 'config/database.yml'
|
23
|
+
# end
|
24
|
+
|
25
|
+
puts "\n== Preparing database =="
|
26
|
+
system! 'bin/rails db:prepare'
|
27
|
+
|
28
|
+
puts "\n== Removing old logs and tempfiles =="
|
29
|
+
system! 'bin/rails log:clear tmp:clear'
|
30
|
+
|
31
|
+
puts "\n== Restarting application server =="
|
32
|
+
system! 'bin/rails restart'
|
33
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative "boot"
|
2
|
+
|
3
|
+
require "rails/all"
|
4
|
+
|
5
|
+
# Require the gems listed in Gemfile, including any gems
|
6
|
+
# you've limited to :test, :development, or :production.
|
7
|
+
Bundler.require(*Rails.groups)
|
8
|
+
require "health_monitor"
|
9
|
+
|
10
|
+
module Dummy
|
11
|
+
class Application < Rails::Application
|
12
|
+
config.load_defaults Rails::VERSION::STRING.to_f
|
13
|
+
|
14
|
+
# Configuration for the application, engines, and railties goes here.
|
15
|
+
#
|
16
|
+
# These settings can be overridden in specific environments using the files
|
17
|
+
# in config/environments, which are processed later.
|
18
|
+
#
|
19
|
+
# config.time_zone = "Central Time (US & Canada)"
|
20
|
+
# config.eager_load_paths << Rails.root.join("extras")
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "active_support/core_ext/integer/time"
|
2
|
+
|
3
|
+
Rails.application.configure do
|
4
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
5
|
+
|
6
|
+
# In the development environment your application's code is reloaded any time
|
7
|
+
# it changes. This slows down response time but is perfect for development
|
8
|
+
# since you don't have to restart the web server when you make code changes.
|
9
|
+
config.cache_classes = false
|
10
|
+
|
11
|
+
# Do not eager load code on boot.
|
12
|
+
config.eager_load = false
|
13
|
+
|
14
|
+
# Show full error reports.
|
15
|
+
config.consider_all_requests_local = true
|
16
|
+
|
17
|
+
# Enable/disable caching. By default caching is disabled.
|
18
|
+
# Run rails dev:cache to toggle caching.
|
19
|
+
if Rails.root.join('tmp', 'caching-dev.txt').exist?
|
20
|
+
config.action_controller.perform_caching = true
|
21
|
+
config.action_controller.enable_fragment_cache_logging = true
|
22
|
+
|
23
|
+
config.cache_store = :memory_store
|
24
|
+
config.public_file_server.headers = {
|
25
|
+
'Cache-Control' => "public, max-age=#{2.days.to_i}"
|
26
|
+
}
|
27
|
+
else
|
28
|
+
config.action_controller.perform_caching = false
|
29
|
+
|
30
|
+
config.cache_store = :null_store
|
31
|
+
end
|
32
|
+
|
33
|
+
# Store uploaded files on the local file system (see config/storage.yml for options).
|
34
|
+
config.active_storage.service = :local
|
35
|
+
|
36
|
+
# Don't care if the mailer can't send.
|
37
|
+
config.action_mailer.raise_delivery_errors = false
|
38
|
+
|
39
|
+
config.action_mailer.perform_caching = false
|
40
|
+
|
41
|
+
# Print deprecation notices to the Rails logger.
|
42
|
+
config.active_support.deprecation = :log
|
43
|
+
|
44
|
+
# Raise exceptions for disallowed deprecations.
|
45
|
+
config.active_support.disallowed_deprecation = :raise
|
46
|
+
|
47
|
+
# Tell Active Support which deprecation messages to disallow.
|
48
|
+
config.active_support.disallowed_deprecation_warnings = []
|
49
|
+
|
50
|
+
# Raise an error on page load if there are pending migrations.
|
51
|
+
config.active_record.migration_error = :page_load
|
52
|
+
|
53
|
+
# Highlight code that triggered database queries in logs.
|
54
|
+
config.active_record.verbose_query_logs = true
|
55
|
+
|
56
|
+
# Debug mode disables concatenation and preprocessing of assets.
|
57
|
+
# This option may cause significant delays in view rendering with a large
|
58
|
+
# number of complex assets.
|
59
|
+
config.assets.debug = true
|
60
|
+
|
61
|
+
# Suppress logger output for asset requests.
|
62
|
+
config.assets.quiet = true
|
63
|
+
|
64
|
+
# Raises error for missing translations.
|
65
|
+
# config.i18n.raise_on_missing_translations = true
|
66
|
+
|
67
|
+
# Annotate rendered view with file names.
|
68
|
+
# config.action_view.annotate_rendered_view_with_filenames = true
|
69
|
+
|
70
|
+
# Use an evented file watcher to asynchronously detect changes in source code,
|
71
|
+
# routes, locales, etc. This feature depends on the listen gem.
|
72
|
+
# config.file_watcher = ActiveSupport::EventedFileUpdateChecker
|
73
|
+
|
74
|
+
# Uncomment if you wish to allow Action Cable access from any origin.
|
75
|
+
# config.action_cable.disable_request_forgery_protection = true
|
76
|
+
end
|