Exception Notification with Ruby on Rails and Slack Notifier

In Rails, exception_notification is popular gem used for Exception Notification to email. Slack is a new tool which is very popular among teams for collaboration. This article will help us understand the way to configure exception_notifier gem to send notifications to your Slack channel from Ruby on Rails application.

Rails Slack Integration

Introduction

Slack is a very popular tool used for the collaboration among teams. We have discussed here - the integration of Slack with Ruby on rails. Whenever you build a production-ready Ruby on Rails application, you need a notifier that notifies exceptions occurring to your Production application.

If you search for a good exception notification gem for Ruby on Rails, you will come across -

Out of these, exception_notifier has easiest configuration steps. It allows you to send exceptions to your Emails.

The latest version of execption_notifier allows us to send exceptions to Slack channel as well.

Integration Steps:

1. Create a Slack channel

Create a Slack channel to have exceptions sent to them. It would be better if ”#exceptions” is the name given to the channel. The intent becomes very clear. Once you are good with a slack channel, configure incoming webhook for that particular channel.

[Refer to Rails Slack Integration with Slack Notifier gem]

2. Integrate exception_notification gem

Integrate exception_notifiaction gem with your Rails application. You can follow steps given below.

1) Add exception_notification to Gemfile.

gem 'exception_notification'

2) Bundle Install

bundle install

Perform bundle install from your Rails project directory. It configures exception_notification gem to be used with you Ruby on Rails application.

Use exception_notifiaction middleware

This will configure exception_notification to send emails to given recipients with prefix and sender address options as configured above.

3. Add slack notifier gem

1) Add Gem slack-notifier to Gemfile

gem 'slack-notifier'

2) Bundle install

gem 'bundle install'

Perform bundle install from your Rails project directory. It configures slack-notifier gem to be used with you Ruby on Rails application.

4. Modify exception_notification middleware options

Modify exception_notification middleware options to send exception notification to slack as given below.

This goes in config/environment/production.rb (based on your environment)

Listing down the options and their implication.

  • webhook_url: This is URL that is generated specifically for your Slack account when you follow Step 1.
  • channel: This is the channel name in your Slack where notifications will be sent.
  • additional_parameters: This supports additional parameters, here we have enabled mrkdwn (markdown) in notifications being sent.

Summary

Now, whenever any exception is thrown from your Rails application, it will be sent to Slack channel along with the email options specified to be used with Exception Notification middleware.