Skip to content

awexome/has_heartbeat

Repository files navigation

HasHeartbeat

Controller with target for heartbeat monitoring with customizable status body text, HTTP status codes, and optional integration with Airbrake notifier.

Install

HasHeartbeat is intended for use in Rails applications. To add a heartbeat endpoint to your application, add HasHeartbeat to your Gemfile:

gem "has_heartbeat"

The gem has dependencies on versions of Rails from 3.1 and upward, as well as a dependency on the Airbrake notification gem, which will be added to your dependency set.

Usage

By adding the HasHeartbeat gem to your project, your project will have a “/heartbeat” endpoint added to your routes. You can add this URL to your heartbeat/monitoring service of choice or strike it directly to verify that your application is alive and well.

The heartbeat action responds with one of two possible responses. In the case of an app’s successfully beating heart:

HTTP Status:   200 OK
Response Body: Application Heart Beating OK
Logger Output: Application Heart Beating OK

And the case arising from internal issue or error:

HTTP Status:   500 Server Error
Response Body: 500 Internet Server Error: Application Heart Palpitations
Logger Output: 500 Internet Server Error: Application Heart Palpitations

Configure your monitoring service to check against status codes and/or response body text to properly triage responses.

Options

By default, HasHeartbeat simply responds with success or failure and can be used for up/down verification. You can customize its behavior as follows:

Check Database Up/Down

You can ask the heartbeat to also check that your application is connected to your database. To do so, configure like so:

# config/initializers/heartbeat.rb
HasHeartbeat.configure do |config|
  config.check_db!
end

HasHeartbeat will check against the database by default. You can turn this off, if you really don’t need it:

config.check_db!(false)

Custom Responses

By default, the heartbeat action will render text responses with the HTTP service codes as described above. You may wish to customize the success or failure messages or the failure status code. To do so, get back into your initializer:

# config/initializers/heartbeat.rb
HasHeartbeat.configure do |config|
  config.ok_text = "This is my OK output"
  config.fail_text = "This response text is output on DB connect issues"
  config.fail_status = 404      # Change from 500 to something more specific
end

External Notifications

Further, if you’d like HasHeartbeat to notify an external service when your application is experiencing a database connection error, you can ask it to piggy-back off an existing Airbrake configuration you are using. To do so, add the following to a config/initializer:

HasHeartbeat.configure do |config|
  config.use_airbrake!
end

By specifying use_airbrake!, HasHeartbeat will use any existing Airbrake configuration and send a notification to your Airbrake service in the following form:

{
  :error_class   => "Heartbeat Failure",                    # <= Generic title
  :error_message => "Heartbeat Failure: #{e.message}",      # <= Raised error message
  :parameters    => params                                  # <= Request parameters
}

If you don’t wish to use Airbrake, simply leave out the call to use_airbrake! and all is well.

Copyright 2014 Awexome Labs, LLC awexomelabs.com facebook.com/AwexomeLabs twitter.com/awexomelabs

About

Heartbeat monitoring target for Rails apps with light customization

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages