Artur Trzop
Posted on March 8, 2021
After working for over 8 years with Heroku and Ruby on Rails projects I have my own favorite set of Heroku add-ons that work great with Rails apps. You are about to learn about the add-ons that come in handy in your daily Ruby developer life.
Heroku add-ons
Here it is, a list of my favorite Heroku add-ons from Heroku Marketplace and why I choose them for my Ruby on Rails projects hosted on Heroku.
Heroku Scheduler
Heroku Scheduler can run scheduled tasks every 10 minutes, every hour, or every day. I use it to run my scheduled rake tasks. For instance every day I run a rake task that will send a summary of users who signed up in the last 24 hours to my mailbox.
Heroku Scheduler add-on is free. The only limitation is that it has fewer options than the cron in the Unix system. If you need to run a rake task every Monday then you need to set up a rake task as a daily task in Heroku Scheduler and do a check of the day in the rake task itself to skip it when needed.
# lib/tasks/schedule/notify_users_about_past_due_subscription.rake
namespace :schedule do
desc 'Send notification about past due subscriptions to users'
task notify_users_about_past_due_subscription: :environment do
if Time.current.monday?
Billing::NotifyUsersAboutPastDueSubscriptionWorker.perform_async
else
Rails.logger.info("Skip schedule:notify_users_about_past_due_subscription task.")
end
end
end
New Relic APM
New Relic add-on does application performance monitoring. It's one of my favorite add-ons. It allows to track each process like puma/unicorn/sidekiq per dyno and its performance. You can see which Rails controller actions take the most time. You can see your API endpoints with the highest throughput and those which are time-consuming. New Relic helped me many times to debug bottlenecks in my app and thanks to that I was able to make Knapsack Pro API with an average 50ms response time. Who said the Rails app has to be slow? :)
Rollbar
Rollbar allows for exception tracking in your Ruby code and also in JS code on the front end side. It has a generous free plan with a 5000 exception limit per month.
You can easily ignore some common Rails exceptions to stay within the free plan limit.
# config/initializers/rollbar.rb
Rollbar.configure do |config|
config.access_token = ENV['ROLLBAR_ACCESS_TOKEN']
if Rails.env.test? || Rails.env.development?
config.enabled = false
end
# Add exception class names to the exception_level_filters hash to
# change the level that exception is reported at. Note that if an exception
# has already been reported and logged the level will need to be changed
# via the rollbar interface.
# Valid levels: 'critical', 'error', 'warning', 'info', 'debug', 'ignore'
# 'ignore' will cause the exception to not be reported at all.
config.exception_level_filters.merge!('ActionController::RoutingError' => 'ignore')
config.exception_level_filters.merge!('ActionController::InvalidAuthenticityToken' => 'ignore')
config.exception_level_filters.merge!('ActionController::BadRequest' => 'ignore')
config.exception_level_filters.merge!('ActiveRecord::RecordNotFound' => 'ignore')
config.exception_level_filters.merge!('Rack::Timeout::RequestTimeoutException' => 'ignore')
config.exception_level_filters.merge!('Rack::QueryParser::InvalidParameterError' => 'ignore')
config.exception_level_filters.merge!('ActionDispatch::Http::MimeNegotiation::InvalidType' => 'ignore')
config.environment = ENV['ROLLBAR_ENV'].presence || Rails.env
end
Logentries
Logentries - collects your logs from Heroku standard output so that you can browse them later on. If you need to find info about an issue that happened a few days ago in your logs then Logentries might be helpful.
Of course, you could use Heroku Command Line Interface and run heroku logs -n 10000 --app my-heroku-app
command in terminal to browse logs for the last 10,000 lines but this method has limitations. You can't go that much in past logs or easily filter logs as in Logentries.
Logentries has a 5 GB and 7 days retention period in a free plan. This is enough for small Rails apps.
A nice feature I like in Logentries is an option to save the query and later on quickly browse logs by it. You can also display charts based on logs. Maybe you want to see how often a particular worker in Sidekiq has been called? You could visualize it.
Redis Cloud
If you use Redis in your Ruby on Rails app then Redis Cloud is your add-on. It has a free plan and paid plans are more affordable than other add-ons have.
Redis Cloud add-on does automatic backups of your data and offers a nice web UI to preview the live Redis usage and historical usage of your database instance.
I like to use Redis Cloud + sidekiq gem in my Rails apps. Also, Redis is useful if you need to cache some data quickly in the memory and expire it after some time.
redis_connection = Redis.new(
# use REDISCLOUD_URL when app is running on Heroku,
# or fallback to local Redis (useful for development)
url: ENV.fetch('REDISCLOUD_URL', 'redis://localhost:6379/0'),
# tune network timeouts to be a little more lenient when you are seeing occasional timeout
# errors for Heroku Redis Cloud addon
# https://github.com/mperham/sidekiq/wiki/Using-Redis#life-in-the-cloud
timeout: 5
)
redis_connection.setex('my-key-name', 1.hour, 'this value will expire in 1 hour')
Twilio SendGrid
SendGrid is a free add-on that allows you to start sending emails from your Ruby on Rails. You can even connect your domain to it so your users get emails from your domain.
There are free 12,000 emails per month in the free plan.
Heroku Add-on to save you time & money
Here are a few of my favorite add-ons that will help you save money and time in your project.
AutoIdle
AutoIdle lets you save money by automatically putting your staging and review apps to sleep on Heroku. I use it to turn off my web and worker dyno for the staging app when there is no traffic to the app. No more paying for Heroku resources during the night and weekends. ;)
Rails Autoscale
Rails Autoscale is a powerful add-on that will help you save money on Heroku. It will measure requests queue time and based on that add or remove dynos for your web processes. If you have higher traffic during the day it will add more dynos. During the night when the traffic is low, it will remove dynos.
Rails Autoscale can also track your worker queue. For instance, if you have a lot of jobs scheduled in Sidekiq then Rails Autoscale will add more worker dynos to process your job queue faster. It can even shut down worker dyno when there are no jobs to be processed which can save you even more money.
Knapsack Pro
Knapsack Pro is a Heroku add-on and ruby gem that can run your Rails tests in RSpec, Cucumber, Minitest, etc, and automatically split the tests between parallel machines on any CI server. It works with Heroku CI, CircleCI, Buildkite, Travis CI, etc. It will help you save time by doing a dynamic split of tests with Queue Mode to ensure all parallel jobs finish work at a similar time. This way you optimize your CI build runs and save the most time.
Below you can see an example of the optimal distribution of tests, where each parallel CI machine performs tests for 10 minutes, thanks to which the entire CI build lasts only 10 minutes instead of 40 if you would run tests on a single CI server only.
I've been working on the Knapsack Pro add-on and I'd love to hear your feedback if you give it a try.
Posted on March 8, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.