Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 'literal' Sentry error handler and make it separate from legacy Raven #363

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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