Ready to know about downtime before your customers?
Status List delivers uptime monitoring and professional hosted status pages for sites of all shapes and sizes.
Trusted by 1000+ companies
Rails recently added the Rails::HealthController, but it’s only available in version >= 7.1. If you don’t want to upgrade, you can follow the linqueta/rails-healthcheck setup guide.
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
# gem "rails", "~> 7.0.4", ">= 7.0.4.3" -- original
gem "rails", :git => 'https://github.com/rails/rails', :ref => '539144d' # rails with HealthController
Your application may fail during startup because of invalid syntax or missing dependencies. By default Rails::HealthController will check your application for these startup exceptions. You don’t need to do anything to set this up.
You can test your health check by writing some invalid code in one of your .rb files and loading up 127.0.0.1:3000/up. You should see a an error screen like this:
Your app may also fail during the normal course of operation. You may run out of memory, have a dependency fail etc. If a dependency fails, we’d like an alert, but we don’t need to restart our app. On the other hand, if we run out memory, we should redirect traffic to another server and let our orchestration system restart the app. To make this work, let’s create two health checks.
Our first health check will check for application issues (like running out of memory). We can let the Rails::HealthController deal with this. Since it catches exceptions like this by default, there’s nothing more to do here. The endpoint for this is /up.
Second, we need to create a custom route to check our upstream services. Add create a new controller called CustomizedHealthController. You can use the rails generate controller CustomizedHealthController
command to generate it for you.
Trusted by 1000+ companies
upstream
that will do our upstream checks. In here, we’ll call things like check_active_record
and check_mail_service
.
class CustomizedHealthController < Rails::HealthController
def upstream
# todo
end
def check_mail_service
# todo
end
def check_active_record
# todo
end
end
class UpstreamStatus
attr_accessor :active_record, :mail_service
def is_ok
active_record == STATUS_OK and mail_service == STATUS_OK
end
end
CustomizedHealthController
, we can fill out the methods. Let’s fill out the upstream method. Upstream, needs to call our checks and fill out our UpstreamStatus object.
def upstream
status = UpstreamStatus.new
status.active_record = check_active_record
status.mail_service = check_mail_service
# ...
render_upstream(status: status, ok: status.is_ok)
end
def check_mail_service
# todo
return STATUS_OK
end
def check_active_record
begin
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection
if ActiveRecord::Base.connected?
return STATUS_OK
else
return "Failed to connect"
end
rescue => e
return e.full_message
end
end
private
def render_upstream(status:, ok:)
http_status = 200
unless ok
http_status = 503
end
render json: status, status: http_status
end
Trusted by 1000+ companies
Once we have our startup and runtime checks configured, we need to set routes for them. By default Rails::HealthController will use the /up route, but we can customize that.
Open your route.rb file add the following routes:
upstream
method.Here’s an example of how this might look.
get "/health/app" => "customized_health#show", as: :rails_health_check
get "/health/upstream" => "customized_health#upstream"
Once you have your checks setup, it’s time to setup your uptime monitor. Log into your uptime service and add monitors for both of your services. You’ll want to check both /health/app and /health/upstream separately. You may also want to configure a minimum response time for /health/app to ensure your app is responsive.
You may also want to show your app’s status on your status page. Ensure you update your status page configuration to pull your uptime stats in to your status page.
You now have uptime checks and alerts for your Rails application. If your app runs into issues you’ll be the first to know.
You may also be interested in our Heroku Uptime Service.
© Status List 2024