Skip to content

Using Airbrake asynchronously

dougc84 edited this page Dec 1, 2012 · 5 revisions

When your user experiences an error using your application, it gets sent to the Airbrake server. This introduces a considerable latency in the response.

Asynchronous notification sending deals with this problem. Airbrake uses girl_friday to achieve this. (kudos to @mperham)

Make sure you have girl_friday installed if you want to use the default async behavior.

It's disabled by default and you can enable it in your Airbrake configuration.

Airbrake.configure do |config|
  # ...
  config.async = true
end

Note that this feature is enabled with JRuby 1.6+, Rubinius 2.0+ and Ruby 1.9+. It does not support Ruby 1.8 because of its poor threading support.

To implement custom asynchronous notice delivery, send a block to config.async. It receives a notice parameter. Pass it to the Airbrake.sender.send_to_airbrake method to do the actual delivery. This way it's possible to move Airbrake notification to a background worker (e.g. Resque or Sidekiq).

# Use thread

Airbrake.configure do |config|
  # ...
  config.async do |notice|
    Thread.new { Airbrake.sender.send_to_airbrake(notice) }
  end
end

# Use Resque

Airbrake.configure do |config|
  # ...
  config.async do |notice|
    Resque.enqueue(AirbrakeDeliveryWorker, notice)
  end
end

class AirbrakeDeliveryWorker
  def self.perform(notice)
    Airbrake.sender.send_to_airbrake notice
  end
end

# Use Sidekiq

Airbrake.configure do |config|
  # ...
  config.async do |notice|
    AirbrakeDeliveryWorker.perform_async(notice)
  end
end

class AirbrakeDeliveryWorker
  include Sidekiq::Worker

  def perform(notice)
    Airbrake.sender.send_to_airbrake notice
  end
end

# Use Delayed::Job

Airbrake.configure do |config|
  # ...
  config.async do |notice|
    Airbrake.sender.send_to_airbrake(notice).delay
  end
end