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
Then mount the middleware in your app, like:
require 'rollbar/middleware/sinatra' class MyApp < Sinatra::Base use Rollbar::Middleware::Sinatra # other middleware/etc # ... end
There is a known conflict between Sinatra and other gems where Sinatra's top-level-binded methods get overriden by other gems (more details here: https://github.com/sinatra/sinatra-contrib/issues/111 and https://github.com/rollbar/rollbar-gem/issues/663). This came to attention when using
Rollbar.configure. If your top-level-binded methods get overshadowed by other gems, like
Rake::DSL.namespace, you will need to redefine the method manually as intended by Sinatra.
Here is an example if you want to use
configure do Rollbar.configure do |config| ... end # Redefine :namespace to use Sinatra's :namespace method self.instance_eval do undef :namespace define_singleton_method(:namespace) do |*args, &block| Sinatra::Delegator.target.send(:namespace, *args, &block) end end end
If you're not using Rails, you may first need to add the following to your Rakefile:
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
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.
When using a
stream, make sure to configure it's
errback as follows:
get '/test_stream_error' do stream do |out| out.errback do Rollbar.error($!) end raise 'OMG!' end end
For more information on rollbar-gem, please see the docs here.
Updated about 3 years ago