Awesome Open Source
Awesome Open Source
Sponsorship

pipeline status gem version

HealthBit

This gem was inspired by the health_check, but is simpler and more extensible and contains up to 95% less code.

Key differences:

  • is a rack application (just a lambda function)
  • can be used with rails, sinatra or any other rack application
  • can add custom checks
  • can add multiple endpoints with independent checks
  • can use any rack middleware (such as http basic auth, IP whitelist)

Toc

Check Examples

Installation

Add this line to your application's Gemfile:

gem 'health_bit'

Configuration

# config/initializers/health_bit.rb

HealthBit.configure do |c|
  # DEFAULT SETTINGS ARE SHOWN BELOW
  c.success_text = '%<count>d checks passed 🎉'
  c.headers = {
    'Content-Type' => 'text/plain;charset=utf-8',
    'Cache-Control' => 'private,max-age=0,must-revalidate,no-store'
  }
  c.success_code = 200
  c.fail_code = 500
  c.show_backtrace = false

  c.add('Check name') do
    # Body check, should returns `true`
    true
  end
end

Add Checks

By default, the gem does not contain any checks, you should add the necessary checks by yourself. The check should return false or nil to be considered unsuccessful or throw an exception, any other values are considered satisfactory.

Example checks:

## Successful checks
HealthBit.add('PostgreSQL') do
  ApplicationRecord.connection.select_value('SELECT 1') == 1
end

HealthBit.add('Custom') do
  true
end

## Failed checks
HealthBit.add('Database') do
  false
end

HealthBit.add('Docker service') do
  raise 'not responding'
end

# The Check can be added as an object responding to a call
# (to be able to test your check)
class Covid19Check
  def self.call
    false
  end
end

HealthBit.add('COVID-19 Checker', Covid19Check)

Add a Route

Since the gem is a rack application, you must mount it to app's routes. Below is an example for the Rails.

# config/routes.rb

Rails.application.routes.draw do
  mount HealthBit.rack => '/health'
end
curl --verbose http://localhost:3000/health

< HTTP/1.1 200 OK
< Content-Type: text/plain;charset=utf-8
< Cache-Control: private,max-age=0,must-revalidate,no-store
< X-Request-Id: 59a796b9-29f7-4302-b1ff-5d0b06dd6637
< X-Runtime: 0.006007
< Vary: Origin
< Transfer-Encoding: chunked

4 checks passed 🎉

Password Protection

Since the gem is a common rack application, you can add any rack middleware to it. Below is an example with HTTP-auth for the Rails.

# config/routes.rb

Rails.application.routes.draw do
  HealthBit.rack.use Rack::Auth::Basic do |username, password|
    ActiveSupport::SecurityUtils.secure_compare(Digest::SHA256.hexdigest(username), Digest::SHA256.hexdigest('user')) & ActiveSupport::SecurityUtils.secure_compare(Digest::SHA256.hexdigest(password), Digest::SHA256.hexdigest('password'))
  end

  mount HealthBit.rack => '/health'
end

Database check

HealthBit.add('Database') do
  ApplicationRecord.connection.select_value('SELECT 1') == 1
end

Redis check

HealthBit.add('Redis') do
  Redis.current.ping == 'PONG'
end

Sidekiq check

HealthBit.add('Sidekiq') do
  Sidekiq.redis(&:ping) == 'PONG'
end

Rails cache check

HealthBit.add('Rails cache') do
  Rails.cache.write('__health_bit__', '1', expires_in: 1.second)
end

Elasticsearch check

HealthBit.add('Elasticsearch') do
  Elasticsearch::Client.new.ping
end

RabbitMQ check

HealthBit.add('RabbitMQ') do
  Bunny::Connection.connect(&:connection)
end

HTTP check

HealthBit.add('HTTP check') do
  Net::HTTP.new('www.example.com', 80).request_get('/').kind_of?(Net::HTTPSuccess)
end

ClickHouse check

HealthBit.add('ClickHouse') do
  ClickHouse.connection.ping
end

Multiple endpoints

Sometimes you have to add several health check endpoints. Let's say you have to check the docker container health and the health of your application as a whole. Below is an example for the Rails.

# config/initializers/health_bit.rb

DockerCheck = HealthBit.clone
AppCheck = HealthBit.clone

DockerCheck.add('Docker Health') do
  true
end

AppCheck.add('App Health') do
  ApplicationRecord.connection.select_value("SELECT 't'::boolean")
end
# config/routes.rb

Rails.application.routes.draw do
  mount DockerCheck.rack => '/docker'
  mount AppCheck.rack => '/app'
end

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
ruby (12,200
ruby-on-rails (122
health (55
rack (40
health-check (21

Find Open Source By Browsing 7,000 Topics Across 59 Categories