All Projects → shlima → Health_bit

shlima / Health_bit

Licence: mit
Tiny health check of Rack apps like Rails, Sinatra for use with uptime checking systems like Kubernetes, Docker or Uptimerobot

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Health bit

node-healthchecks-api
The Node.js implementation of the Health Checks API by Hootsuite
Stars: ✭ 25 (-58.33%)
Mutual labels:  health-check, health
Health Checks Api
Standardize the way services and applications expose their status in a distributed application
Stars: ✭ 78 (+30%)
Mutual labels:  health, health-check
serverless-rack
Serverless plugin to deploy Ruby Rack applications (Sinatra/Rails/Padrino/Cuba etc.) and bundle gems
Stars: ✭ 58 (-3.33%)
Mutual labels:  rack, ruby-on-rails
covidliste
Speed-up Covid-19 vaccination campaigns 💉
Stars: ✭ 264 (+340%)
Mutual labels:  health, ruby-on-rails
health
A simple and flexible health check library for Go.
Stars: ✭ 494 (+723.33%)
Mutual labels:  health-check, health
aarogya seva
A beautiful 😍 covid-19 app with self - assessment and more.
Stars: ✭ 118 (+96.67%)
Mutual labels:  health-check, health
Lamby
Simple Rails & AWS Lambda Integration 🐑🛤
Stars: ✭ 336 (+460%)
Mutual labels:  ruby-on-rails, rack
Real World Rails
Real World Rails applications and their open source codebases for developers to learn from
Stars: ✭ 982 (+1536.67%)
Mutual labels:  ruby-on-rails
Browncoat
Container for testing app failures in orchestrators. It aims to misbehave.
Stars: ✭ 49 (-18.33%)
Mutual labels:  health-check
Health Blockchain
A blockchain for fitness data demo
Stars: ✭ 31 (-48.33%)
Mutual labels:  health
Open api parser
A parser for Open API specifications
Stars: ✭ 30 (-50%)
Mutual labels:  rack
Cuttlefish
Transactional email server with a lovely web interface
Stars: ✭ 985 (+1541.67%)
Mutual labels:  ruby-on-rails
Rackula
Generate a static site from any rack middleware.
Stars: ✭ 49 (-18.33%)
Mutual labels:  rack
Openmrs Core
OpenMRS API and web application code
Stars: ✭ 979 (+1531.67%)
Mutual labels:  health
Mri Analysis Pytorch
MRI analysis using PyTorch and MedicalTorch
Stars: ✭ 55 (-8.33%)
Mutual labels:  health
Dashvis
An open-source Dashboard built for users, to organize their resources via Tables and Folders.
Stars: ✭ 31 (-48.33%)
Mutual labels:  ruby-on-rails
Model probe
Schema introspection for ActiveModel
Stars: ✭ 58 (-3.33%)
Mutual labels:  ruby-on-rails
Goldiloader
Just the right amount of Rails eager loading
Stars: ✭ 1,074 (+1690%)
Mutual labels:  ruby-on-rails
Graphql Rails Generators
Graphql Rails Scaffold™. Automatically generate GraphQL types from your rails models.
Stars: ✭ 47 (-21.67%)
Mutual labels:  ruby-on-rails
Redis Rack
Redis session store for Rack
Stars: ✭ 46 (-23.33%)
Mutual labels:  rack

CI 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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].