DEV Community

Cover image for How to send Rails deprecation warnings to Airbrake
Leonid Svyatov
Leonid Svyatov

Posted on

How to send Rails deprecation warnings to Airbrake

We’re upgrading our Ruby on Rails version from 4.2 to 5.2 this week. A long-awaited change for us. Of course, there are a lot of deprecation warnings that was thrown at us during this process. Rubocop and tests caught the vast majority of those, but we don’t have 100% code coverage, so some of these warnings got into production.

In development Rails send these warnings to the log file:

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
Enter fullscreen mode Exit fullscreen mode

A reasonable default, but I changed it to :raise. It's way faster to find and deal with the warnings this way in development.

For production environment Rails uses ActiveSupport::Notifications:

# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
Enter fullscreen mode Exit fullscreen mode

The problem is that nobody listens for it in production. Fortunately, that was easy to fix with Airbrake. I tried to “google” the solution at first but didn’t find anything useful, so I’m sharing my solution for other seeking souls like me :)

# config/initializers/deprecations_notifier.rb

ActiveSupport::Notifications.subscribe('deprecation.rails') do |_name, _start, _finish, _id, payload|
  error = ActiveSupport::DeprecationException.new(payload[:message])
  error.set_backtrace(payload[:callstack].map(&:to_s))

  params = {
    gem_name: payload[:gem_name],
    deprecation_horizon: payload[:deprecation_horizon]
  }

  Airbrake.notify(error, params) do |notice|
    notice[:context][:severity] = 'warning'
  end
end
Enter fullscreen mode Exit fullscreen mode

If you’re using CurrentAttributes and storing request.uuid there it would be a good idea to add current request UUID to the params as well.

Top comments (0)