From 4285d16a90192338ca6b90e00468e019ce5b4570 Mon Sep 17 00:00:00 2001 From: Karol Galanciak Date: Tue, 6 Apr 2021 08:30:48 +0200 Subject: [PATCH] add 'literal' Sentry error handler and make it separate from legacy Raven --- Gemfile | 1 + lib/hutch/cli.rb | 3 ++ lib/hutch/error_handlers.rb | 1 + lib/hutch/error_handlers/sentry.rb | 17 +++------ lib/hutch/error_handlers/sentry_raven.rb | 31 ++++++++++++++++ .../hutch/error_handlers/sentry_raven_spec.rb | 37 +++++++++++++++++++ spec/hutch/error_handlers/sentry_spec.rb | 6 ++- 7 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 lib/hutch/error_handlers/sentry_raven.rb create mode 100644 spec/hutch/error_handlers/sentry_raven_spec.rb diff --git a/Gemfile b/Gemfile index a4a584e1..240a229a 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/lib/hutch/cli.rb b/lib/hutch/cli.rb index aad65f43..22f0c270 100644 --- a/lib/hutch/cli.rb +++ b/lib/hutch/cli.rb @@ -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 diff --git a/lib/hutch/error_handlers.rb b/lib/hutch/error_handlers.rb index 83746c85..a645aa6c 100644 --- a/lib/hutch/error_handlers.rb +++ b/lib/hutch/error_handlers.rb @@ -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' diff --git a/lib/hutch/error_handlers/sentry.rb b/lib/hutch/error_handlers/sentry.rb index d5e25be4..21c6c2bc 100644 --- a/lib/hutch/error_handlers/sentry.rb +++ b/lib/hutch/error_handlers/sentry.rb @@ -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 diff --git a/lib/hutch/error_handlers/sentry_raven.rb b/lib/hutch/error_handlers/sentry_raven.rb new file mode 100644 index 00000000..953d89cc --- /dev/null +++ b/lib/hutch/error_handlers/sentry_raven.rb @@ -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 diff --git a/spec/hutch/error_handlers/sentry_raven_spec.rb b/spec/hutch/error_handlers/sentry_raven_spec.rb new file mode 100644 index 00000000..272417a2 --- /dev/null +++ b/spec/hutch/error_handlers/sentry_raven_spec.rb @@ -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 diff --git a/spec/hutch/error_handlers/sentry_spec.rb b/spec/hutch/error_handlers/sentry_spec.rb index acd772b1..e47cfd29 100644 --- a/spec/hutch/error_handlers/sentry_spec.rb +++ b/spec/hutch/error_handlers/sentry_spec.rb @@ -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 @@ -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