Skip to content

Commit

Permalink
add 'literal' Sentry error handler and make it separate from legacy R…
Browse files Browse the repository at this point in the history
…aven
  • Loading branch information
Azdaroth committed Apr 6, 2021
1 parent 0cffda5 commit 4285d16
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 13 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ group :development, :test do
gem "simplecov", "~> 0.12"

gem "sentry-raven"
gem "sentry-ruby"
gem "honeybadger"
gem "coveralls", "~> 0.8.15", require: false
gem "newrelic_rpm"
Expand Down
3 changes: 3 additions & 0 deletions lib/hutch/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def load_app
# handlers are set up. Due to this, we never got any Sentry notifications
# when an error occurred in any of the consumers.
if defined?(Raven)
Hutch::Config[:error_handlers] << Hutch::ErrorHandlers::SentryRaven.new
end
if defined?(Sentry)
Hutch::Config[:error_handlers] << Hutch::ErrorHandlers::Sentry.new
end

Expand Down
1 change: 1 addition & 0 deletions lib/hutch/error_handlers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Hutch
module ErrorHandlers
autoload :Logger, 'hutch/error_handlers/logger'
autoload :Sentry, 'hutch/error_handlers/sentry'
autoload :SentryRaven, 'hutch/error_handlers/sentry_raven'
autoload :Honeybadger, 'hutch/error_handlers/honeybadger'
autoload :Airbrake, 'hutch/error_handlers/airbrake'
autoload :Rollbar, 'hutch/error_handlers/rollbar'
Expand Down
17 changes: 6 additions & 11 deletions lib/hutch/error_handlers/sentry.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
require 'hutch/logging'
require 'raven'
require 'sentry-ruby'
require 'hutch/error_handlers/base'

module Hutch
module ErrorHandlers
class Sentry < Base

def initialize
unless Raven.respond_to?(:capture_exception)
raise "The Hutch Sentry error handler requires Raven >= 0.4.0"
end
end

def handle(properties, payload, consumer, ex)
message_id = properties.message_id
prefix = "message(#{message_id || '-'}):"
logger.error "#{prefix} Logging event to Sentry"
logger.error "#{prefix} #{ex.class} - #{ex.message}"
Raven.capture_exception(ex, extra: { payload: payload })
::Sentry.configure_scope do |scope|
scope.set_context("payload", payload)
end
::Sentry.capture_exception(ex)
end

def handle_setup_exception(ex)
logger.error "Logging setup exception to Sentry"
logger.error "#{ex.class} - #{ex.message}"
Raven.capture_exception(ex)
::Sentry.capture_exception(ex)
end

end
end
end
31 changes: 31 additions & 0 deletions lib/hutch/error_handlers/sentry_raven.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'hutch/logging'
require 'raven'
require 'hutch/error_handlers/base'

module Hutch
module ErrorHandlers
class SentryRaven < Base

def initialize
unless Raven.respond_to?(:capture_exception)
raise "The Hutch Sentry error handler requires Raven >= 0.4.0"
end
end

def handle(properties, payload, consumer, ex)
message_id = properties.message_id
prefix = "message(#{message_id || '-'}):"
logger.error "#{prefix} Logging event to Sentry"
logger.error "#{prefix} #{ex.class} - #{ex.message}"
Raven.capture_exception(ex, extra: { payload: payload })
end

def handle_setup_exception(ex)
logger.error "Logging setup exception to Sentry"
logger.error "#{ex.class} - #{ex.message}"
Raven.capture_exception(ex)
end

end
end
end
37 changes: 37 additions & 0 deletions spec/hutch/error_handlers/sentry_raven_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'spec_helper'

describe Hutch::ErrorHandlers::SentryRaven do
let(:error_handler) { Hutch::ErrorHandlers::SentryRaven.new }

describe '#handle' do
let(:properties) { OpenStruct.new(message_id: "1") }
let(:payload) { "{}" }
let(:error) do
begin
raise "Stuff went wrong"
rescue RuntimeError => err
err
end
end

it "logs the error to Sentry" do
expect(Raven).to receive(:capture_exception).with(error, extra: { payload: payload })
error_handler.handle(properties, payload, double, error)
end
end

describe '#handle_setup_exception' do
let(:error) do
begin
raise "Stuff went wrong during setup"
rescue RuntimeError => err
err
end
end

it "logs the error to Sentry" do
expect(Raven).to receive(:capture_exception).with(error)
error_handler.handle_setup_exception(error)
end
end
end
6 changes: 4 additions & 2 deletions spec/hutch/error_handlers/sentry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
end

it "logs the error to Sentry" do
expect(Raven).to receive(:capture_exception).with(error, extra: { payload: payload })
expect(::Sentry).to receive(:capture_exception).with(error).and_call_original

error_handler.handle(properties, payload, double, error)
end
end
Expand All @@ -30,7 +31,8 @@
end

it "logs the error to Sentry" do
expect(Raven).to receive(:capture_exception).with(error)
expect(::Sentry).to receive(:capture_exception).with(error).and_call_original

error_handler.handle_setup_exception(error)
end
end
Expand Down

1 comment on commit 4285d16

@TomA-R
Copy link

@TomA-R TomA-R commented on 4285d16 Jul 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @Azdaroth . Any chance we can get a new release cut with this? Thanks!

Please sign in to comment.