Rollbar - Automated Error Logging for Ruby Apps

Posted Oct 21, 2016
by Rivkah Standig, Support Engineer/Developer Advocate at Rollbar

When you're in the midst of a hackathon, the last thing you want to do is spend time trying to track down errors and debugging your app. You're trying to make the coolest app and attain glory, after all. That's where Rollbar comes in. Don't stress about unknown errors in your Rails app; use the Rollbar gem and rest easy knowing your errors are automatically logged and reported.

Getting Started

To get started with Rollbar, you'll need to sign up for an account. Ruby Rampage participants get 100,000 error events tracked for free, so you can see if Rollbar is right for you. And there's an awesome bonus for Ruby Rampage winners in addition to the 100,000 free events: you'll get 1 year of Rollbar on the Bootstrap plan for free ($588 value, 100,000 events per month) per team member! Winners will also receive a Star Wars BB-8 Droid from Rollbar. :)

Once you've signed up for an account, you'll create a project.

Since we're using Ruby, we'll select that for our language. On the next page, you'll receive a client-side access token which you'll need to save for later.

Installing the Gem

Installing the Gem is easy! First, add this line to your application's Gemfile:

gem 'rollbar'

And then execute:

$ bundle install
# Or if you don't use bundler:
$ gem install rollbar

Unless you are using JRuby, we suggest also installing Oj for JSON serialization. Add this line to your Gemfile:

gem 'oj', '~> 2.12.14'

and then run bundle install again.

Basic Ruby Integration

Rollbar isn't dependent on Rack or Rails for most of its functionality. In a regular script, assuming you've installed the rollbar gem:

  1. Require rollbar
  2. Configure rollbar
  3. Send Rollbar data
require 'rollbar'

Rollbar.configure do |config|
  config.access_token = "POST_SERVER_ITEM_ACCESS_TOKEN"
  # Other Configuration Settings
end

Rollbar.debug("Running Script")

begin
  run_script ARGV
rescue Exception => e # Never rescue Exception *unless* you re-raise in rescue body
  Rollbar.error(e)
  raise e
end

Rollbar.info("Script ran successfully")

Test Your Installation

To confirm that it worked, run:

$ rake rollbar:test

This will raise an exception within a test request; if it works, you'll see a stacktrace in the console, and the exception will appear in the Rollbar dashboard.

If you're not using Rails, you may first need to add the following to your Rakefile:

require 'rollbar/rake_tasks'

You may also need to add an :environment task to your Rakefile if you haven't already defined one. At a bare minimum, this task should call Rollbar.configure() and set your access token.

task :environment do
  Rollbar.configure do |config |
    config.access_token = '...'
  end
end

Using Rails

Run the following command from your Rails root:

$ rails generate rollbar POST_SERVER_ITEM_ACCESS_TOKEN

Be sure to replace POST_SERVER_ITEM_ACCESS_TOKEN with your project's post_server_item access token, which you can find in the Rollbar.com interface.

That will create the file config/initializers/rollbar.rb, which initializes Rollbar and holds your access token and other configuration values.

If you want to store your access token outside of your repo, run the same command without arguments and create an environment variable ROLLBAR_ACCESS_TOKEN that holds your server-side access token:

$ rails generate rollbar
$ export ROLLBAR_ACCESS_TOKEN=POST_SERVER_ITEM_ACCESS_TOKEN

For Heroku users:

If you're on Heroku, you can store the access token in your Heroku config:

$ heroku config:add ROLLBAR_ACCESS_TOKEN=POST_SERVER_ITEM_ACCESS_TOKEN

Using Rack

Initialize Rollbar with your access token somewhere during startup:

Rollbar.configure do |config|
  config.access_token = 'POST_SERVER_ITEM_ACCESS_TOKEN'
  # other configuration settings
  # ...
end

Be sure to replace POST_SERVER_ITEM_ACCESS_TOKEN with your project's post_server_item access token, which you can find in the Rollbar.com interface.

This monkey patches Rack::Builder to work with Rollbar automatically.

For more control, disable the monkey patch:

Rollbar.configure do |config|
  config.disable_monkey_patch = true
  # other configuration settings
  # ...
end

Then mount the middleware in your app, like:

class MyApp < Sinatra::Base
  use Rollbar::Middleware::Sinatra
  # other middleware/etc
  # ...
end

For further information, you'll want to check out Rollbar's docs for information on how to configure the gem to enable Rollbar.js on your site, and other advanced ways to use the gem.

Working with Rollbar

Now that you have Rollbar integrated into your Ruby app, any errors that occur will be automatically captured and viewable from your Rollbar Dashboard and Error Items page. You'll be able to easily see what errors are occurring, how often they occur and the full context and analytics into your exceptions.

Rollbar provides a full stack trace of every exception allowing you to see exactly what's happening in your application when an error occurs. You can also view the user's browser and OS where the error occurred.

Errors are grouped by frequency so that you can prioritize which ones to work on first. You'll be able to view trends over time. If a certain error spikes in frequency, you'll know about it right away.

Rollbar also allows you to manually handle errors in your application and prioritize which errors are the most critical to your application's health.

Moving Forward

Now that you have Rollbar integrated into your Ruby app, you can rest assured that you'll know about any bugs that might make their into your Ruby Rampage project. We're excited to sponsor Ruby Rampage and can't wait to see all of the amazing projects that are going to be built! For more information about the Ruby gem, view the docs. Happy hacking!


Rails Rumble is now Ruby Rampage!

Posted Sep 9, 2016

Throughout the years, teams have always asked if they had to use Rails, or could they use another Ruby framework instead. And we've always said you could use any Ruby framework you wanted. Well, now it's time to make it official. Rails Rumble is now Ruby Rampage! The rules are still the same, though. Teams of up to 4 people compete over a 48 hour period to build the best app they can, using Ruby.

Oh yeah, and we're also keeping the BELT. You didn't think we were getting rid of the belt, did you? Come on, we're not animals.

Looking for the original RailsRumble site? Sure thing. You can find last year's archive site at r15.railsrumble.com or visit the alumni archive project for stuff from the long long ago.