Giter Club home page Giter Club logo

rack-healthcheck's Introduction

Gem Version Build Status Test Coverage Maintainability

Rack::Healthcheck

Is a middleware that verifies if your app and all resources that it needs are available.

Installation

Add this line to your application's Gemfile:

gem 'rack-healthcheck'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-healthcheck

Usage

Rails
# config/application.rb
config.middleware.use Rack::Healthcheck::Middleware

This may, depending on setup need a position specified. If you see Rack errors in your tests, or elsewhere, try this.

# config/application.rb
config.middleware.insert_before 0, Rack::Healthcheck::Middleware

If it still doesn't work, some users are positionally specifying that the middleware come before another by class name.

# config/application.rb
config.middleware.insert_before ActiveRecord::Migration::CheckPending, Rack::Healthcheck::Middleware

This name may change depending on the framework & version you use this with, and may not be available which is why absolute position is provided above.

Sinatra
# config.ru
use Rack::Healthcheck::Middleware

Setting up

Create an initializer file, where you'll setup all services that you use.

Rack::Healthcheck.configure do |config|
  config.app_name = "Your Application"
  config.app_version = 1.0
  config.checks = [
    Rack::Healthcheck::Checks::ActiveRecord.new("Database"),
    Rack::Healthcheck::Checks::RabbitMQ.new(name, config = {
      hosts: [localhost],
      port: 5672,
      user: guest,
      pass: guest
    }),
    Rack::Healthcheck::Checks::Redis.new(name, config = {
      url: "redis://localhost:6379",
      password: "pass"
    })
    Rack::Healthcheck::Checks::HTTPRequest.new(name, config = {
      url: localhost,
      headers: {"Host" => "something"},
      service_type: "INTERNAL_SERVICE",
      expected_result: "LIVE"
    })
  ]
end
Available checks
  • Rack::Healthcheck::Checks::ActiveRecord.new(name, config = {})
  • Rack::Healthcheck::Checks::MongoDB.new(name, config = {})
  • Rack::Healthcheck::Checks::RabbitMQ.new(name, config = {})
  • Rack::Healthcheck::Checks::Redis.new(name, config = {})
  • Rack::Healthcheck::Checks::HTTPRequest.new(name, config = {})

You can inform if one of your checks is optional, so this check will be disregarded in final result. To do that you just need to pass a hash with :optional key.

Rack::Healthcheck::Checks::ActiveRecord.new("Test", {optional: true})

If you want to display the service url in final result, you need to pass it in hash config too.

Rack::Healthcheck::Checks::ActiveRecord.new("Test", {optional: true, url: "http://myservice.com/healthcheck"})

Routes

By default this gem creates two routes /healthcheck and /healthcheck/complete.

The /healthcheck route doesn't verify the services used by application. This is the fastest way to tell to load balancer if it should or not keep the machine in the pool. These are the available HTTP methods for this route:

  • GET -> Returns the current status (LIVE|DEAD)
  • POST -> Changes the current status to LIVE
  • DELETE -> Changes the current status to DEAD

The /healthcheck/complete performs all configured checks and returns a JSON with a lot of information (elapsed time, status for each service, global status considering only required services). Example:

{
  "name": "My application",
  "status": true,
  "version": 1.0,
  "checks": [
    {
      "name": "Oracle database",
      "type": "DATABASE",
      "status": true,
      "optional": false,
      "time": 0.036443
    }
  ]
}
Changing default routes

If you want to change the default route, you just need to pass the new route to the middleware

# For rails
# config/application.rb
config.middleware.use Rack::Healthcheck::Middleware, "/myroute"
# or
# For sinatra
# config.ru
use Rack::Healthcheck::Middleware, "/myroute"

Contributing

  1. Fork it ( https://github.com/downgba/rack-healthcheck/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

The gem is available as open source under the terms of the MIT License.

rack-healthcheck's People

Contributors

leandro-maduro avatar

Stargazers

Lukáš Voda avatar Jonian Guveli avatar Michael Telford avatar Nicolas Brousse avatar Arturs Mekss avatar Keiji Matsuzaki avatar Bram Swenson avatar Cadu Ribeiro avatar Felipe Gentil avatar

Watchers

 avatar

rack-healthcheck's Issues

README suggestion: Refer to config.middleware.insert_before

Hi!

As a new user of the gem, following instructions, it can be confusing if other middlewares take action before rack-healthcheck can show status.

If, in Rails, placed before ActiveRecord::Migration::CheckPending it works fine.

     # Healthcheck
     config.middleware.insert_before ActiveRecord::Migration::CheckPending, Rack::Healthcheck::Middleware

Would you like a PR with this change?

404 when not localhost:3000

So I've encountered something (likely user setup related)

Locally and in-browser I can reach the healthcheck, but using curl I cannot.

Do you have any ideas why that might be?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.