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

4

1 回答 1

0

我的任务丢失了:environment,导致 heroku在执行任务之前没有加载初始化程序。

如果我这样重写任务,bugsnag 会收到异常:

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

bugsnag 未收到您的错误的另一个原因是您是否在另一个上下文或测功机中运行。

例如,在 sidekiq 作业中,您需要使用特定的 gem(请参阅Sidekiq 3 作业中的处理异常)。

就我而言,我正在使用发条运行后台作业。为了检测它们,我必须执行以下操作(这是特定于发条的):

# 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

有关更多自定义 bugsnag 检测,请参阅https://github.com/bugsnag/bugsnag-ruby/blob/master/lib/bugsnag/sidekiq.rb

于 2016-06-27T13:12:08.253 回答