From fb80f56a7ddf6052c1fd2de43795e03ede34f177 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Wed, 13 Apr 2016 17:20:01 +0800 Subject: [PATCH] slack exception notifications --- .example-env | 7 +++- Gemfile | 2 + Gemfile.lock | 6 +++ config/initializers/exception_notification.rb | 37 +++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 config/initializers/exception_notification.rb diff --git a/.example-env b/.example-env index 54039a93..f3bace35 100644 --- a/.example-env +++ b/.example-env @@ -16,14 +16,17 @@ export DEVISE_SECRET_KEY='f71c467e526f23d614b3b08866cad4788c502bed869c282f06e73e # export AWS_ACCESS_KEY_ID # export AWS_SECRET_ACCESS_KEY # export SSO_KEY -# +# # export SMTP_DOMAIN # export SMTP_PASSWORD # export SMTP_PORT # export SMTP_SERVER # export SMTP_USERNAME + +# # send exception notifications to a slack incoming webhook +# export SLACK_EN_WEBHOOK_URL -#ruby garbage collection stuff +# ruby garbage collection stuff export RUBY_GC_TUNE=0 #set to 1 to enable GC test export RUBY_GC_TOKEN=4f4380fc9a2857d1f008005a3eb86928 diff --git a/Gemfile b/Gemfile index 90d55f04..815c5eee 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,7 @@ gem 'delayed_job_active_record', '~> 4.0.1' gem 'devise' gem 'doorkeeper' gem 'dotenv' +gem 'exception_notification' gem 'formtastic' gem 'formula' gem 'httparty' @@ -23,6 +24,7 @@ gem 'pundit_extra' gem 'rack-cors' gem 'rails3-jquery-autocomplete' gem 'redis' +gem 'slack-notifier' gem 'snorlax' gem 'uservoice-ruby' diff --git a/Gemfile.lock b/Gemfile.lock index 44873eda..59a2e295 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -89,6 +89,9 @@ GEM railties (>= 3.2) dotenv (2.1.0) erubis (2.7.0) + exception_notification (4.1.4) + actionmailer (~> 4.0) + activesupport (~> 4.0) execjs (2.6.0) ezcrypto (0.7.2) factory_girl (4.5.0) @@ -229,6 +232,7 @@ GEM json (~> 1.8) simplecov-html (~> 0.10.0) simplecov-html (0.10.0) + slack-notifier (1.5.1) slop (3.6.0) snorlax (0.1.5) rails (> 4.1) @@ -270,6 +274,7 @@ DEPENDENCIES devise doorkeeper dotenv + exception_notification factory_girl_rails formtastic formula @@ -296,6 +301,7 @@ DEPENDENCIES sass-rails shoulda-matchers simplecov + slack-notifier snorlax tunemygc uglifier diff --git a/config/initializers/exception_notification.rb b/config/initializers/exception_notification.rb new file mode 100644 index 00000000..5423334e --- /dev/null +++ b/config/initializers/exception_notification.rb @@ -0,0 +1,37 @@ +require 'exception_notification/rails' + +ExceptionNotification.configure do |config| + # Ignore additional exception types. + # ActiveRecord::RecordNotFound, AbstractController::ActionNotFound and + # ActionController::RoutingError are already added. + config.ignored_exceptions += %w( + ActionView::TemplateError CustomError UnauthorizedException + InvalidArgumentException InvalidEntityException InvalidRequestException + NotFoundException ValidationException + ) + + # Adds a condition to decide when an exception must be ignored or not. + # The ignore_if method can be invoked multiple times to add extra conditions. + config.ignore_if do |_exception, _options| + !Rails.env.production? + end + + # Notifiers ###### + + if ENV['SLACK_EN_WEBHOOK_URL'] + config.add_notifier :slack, webhook_url: ENV['SLACK_EN_WEBHOOK_URL'] + end + + # Email notifier sends notifications by email. + # config.add_notifier :email, { + # :email_prefix => "[ERROR] ", + # :sender_address => %{"Notifier" }, + # :exception_recipients => %w{exceptions@example.com} + # } + + # Webhook notifier sends notifications over HTTP protocol. Requires 'httparty' gem. + # config.add_notifier :webhook, { + # :url => 'http://example.com:5555/hubot/path', + # :http_method => :post + # } +end