2

I have a standard rails 4 app deployed on heroku.

I'm trying to add bugsnag to it.

First:

Gemfile

gem 'bugsnag'

config/initializers/bugsnag.rb

Bugsnag.configure do |config|
  config.api_key = '<API_KEY_FROM_BUGSNAG_UI>'
end

lib/tasks/test_exception.rake

namespace :myapp do

  desc 'Test task that raises an exception'
  task :test_exception do
    raise 'This is a sample exception'
  end

end
  • Then I commit, push and deploy with git push heroku master
  • make sure heroku is up-to-date heroku restart

Then heroku run rake myapp:test_exception

=>

Running rake myapp:test_exception on ⬢ myapp... up, run.2877 
W, [2016-06-27T12:23:56.017117 #3]  
WARN -- : ** [Bugsnag] No API key configured, couldn't notify rake aborted! 
This is a sample exception 
/app/lib/tasks/test_exception.rake:9:in `block (2 levels) in <top (required)>' 
/app/vendor/bundle/ruby/2.2.0/gems/bugsnag-4.2.1/lib/bugsnag/rake.rb:12:in `execute_with_bugsnag' 
Tasks: TOP => jd:sample_exception 
(See full trace by running task with --trace)

I want heroku to send exception to bugsnag. What did I miss?

Note: The bugsnag integration works fine (I see the exception in the bugsnag UI) if I run the command locally, eg: rake myapp:test_exception

Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236

1 Answers1

0

My task was missing :environment, causing heroku to not load the initializers before executing the task.

If I rewrite the task like this, bugsnag receives the exception:

namespace :myapp do

  desc 'Test task that raises an exception'
  task test_exception: :environment do # <- added :environment here
    raise 'This is a sample exception'
  end

end

Another reason your errors are not received by bugsnag is if you are running in another context or dyno.

For example, inside a sidekiq job, you need to use a specific gem (see Handle exception in Sidekiq 3 jobs).

In my case, I am running background jobs with clockwork. In order to instrument them, I had to do the following (this is specific to clockwork):

# File config/clock.rb

require 'clockwork'
require './config/boot'
require './config/environment'

module Clockwork

  error_handler do |error|
    Bugsnag.auto_notify(error)
  end

  # Check bugsnag is not dead
  every(1.day, 'ping_bugsnag', at: '09:00') do
    raise 'ping bugsnag'
  end

  # ... Other jobs here

end

For more custom bugsnag instrumentation, see https://github.com/bugsnag/bugsnag-ruby/blob/master/lib/bugsnag/sidekiq.rb

Community
  • 1
  • 1
Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236