bugsnag / bugsnag-ruby

BugSnag error monitoring & reporting software for rails, sinatra, rack and ruby
https://docs.bugsnag.com/platforms/ruby
MIT License
248 stars 174 forks source link

Callbacks not working as expected #590

Closed djlebersilvestre closed 4 years ago

djlebersilvestre commented 4 years ago

Description

I would like to report to Datadog any Bugsnag notification (either the automatic reporter from Rails gem [global rescue], or manual from handled errors implemented by devs).

Issue

I tried using the before_notify_callbacks and a custom middleware, and it partially works because I can see/debug the code hitting the Datadog call I wrote.

However, I want to use data from the report object to derive some custom tags for Datadog.

Here's an example:

  def report_to_bugsnag(severity: 'error')
    Bugsnag.notify(error) do |report|
      report.severity = severity
    end
  end

On the callback/middleware:

Bugsnag.before_notify_callbacks << lambda do |report|
  tags = { severity: report.severity }  # No matter what I do, this is always `warning`
  StatsD.increment('Bugsnag', tags: tags)
end

Adding some binding.pry we quickly notice that the Bugsnag.notify(error) block is executed only after the callback/middleware, making it impossible to properly evaluate/leverage report.severity.

Any ideas? Thanks in advance

Environment

Library versions:

$ bundle list | grep -E "(bugsnag|rails|sidekiq|que|sinatra|resque|shoryuken|mailman|delayed_job)"
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
  * autoprefixer-rails (9.7.4)
  * bugsnag (6.13.0)
  * ci-queue (0.17.2)
  * datetime_picker_rails (0.0.7)
  * dotenv-rails (2.7.5)
  * factory_bot_rails (5.1.1)
  * graphiql-rails (1.7.0)
  * jquery-rails (4.3.5)
  * momentjs-rails (2.20.1)
  * pry-rails (0.3.9)
  * quilt_rails (1.11.1)
  * rails (6.0.2.2)
  * rails-dom-testing (2.0.3)
  * rails-html-sanitizer (1.3.0)
  * rails-reverse-proxy (0.9.1)
  * rubocop-rails (2.5.1)
  * sassc-rails (2.1.2)
  * selectize-rails (0.12.6)
  * sidekiq (6.0.6)
  * sidekiq-scheduler (3.0.1)
djlebersilvestre commented 4 years ago

Just a quick note. The code above works as expected when I pass true here: Bugsnag.notify(error, true) to auto_notify argument.

snmaynard commented 4 years ago

Probably the best bet would be to write your own custom Bugsnag middleware and then send things to datadog after calling the call function of the middleware, so that the report has already been sent to Bugsnag by that point and it's complete.

Similar to https://github.com/bugsnag/bugsnag-ruby/blob/master/lib/bugsnag/middleware/callbacks.rb#L14, but shift the callback execution until after the highlighted call call.

djlebersilvestre commented 4 years ago

It worked. Thank you 😄