From b10e23dd16e0da5cd12d85422b618fdcbddabe94 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Thu, 4 Mar 2021 14:02:16 +0100 Subject: [PATCH] Upgrade to newest Sentry --- Gemfile | 3 ++- Gemfile.lock | 15 ++++++++++--- app/controllers/application_controller.rb | 12 +++++------ app/controllers/concerns/lti.rb | 2 +- .../execution_environments_controller.rb | 2 +- app/controllers/exercises_controller.rb | 4 ++-- app/controllers/submissions_controller.rb | 6 +++--- .../user_exercise_feedbacks_controller.rb | 2 +- app/models/linter_check_run.rb | 2 +- app/views/admin/dashboard/show.html.slim | 2 +- app/views/layouts/application.html.slim | 6 +++--- app/views/request_for_comments/show.html.slim | 2 +- config/initializers/raven.rb | 21 ------------------- config/initializers/sentry.rb | 16 ++++++++++++++ lib/docker_client.rb | 4 ++-- lib/docker_container_pool.rb | 18 ++++++++-------- lib/py_lint_adapter.rb | 10 ++++----- lib/py_unit_adapter.rb | 2 +- .../internal_users_controller_spec.rb | 4 ++-- spec/controllers/sessions_controller_spec.rb | 6 +++--- 20 files changed, 72 insertions(+), 67 deletions(-) delete mode 100644 config/initializers/raven.rb create mode 100644 config/initializers/sentry.rb diff --git a/Gemfile b/Gemfile index 421c4302..dc0d8c51 100644 --- a/Gemfile +++ b/Gemfile @@ -45,7 +45,8 @@ gem 'whenever', require: false gem 'concurrent-ruby' gem 'mnemosyne-ruby' gem 'newrelic_rpm' -gem 'sentry-raven' +gem 'sentry-rails' +gem 'sentry-ruby' group :development, :staging do gem 'better_errors' diff --git a/Gemfile.lock b/Gemfile.lock index fb40c0a6..c57b8e76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -228,7 +228,7 @@ GEM nested_form (0.3.2) netrc (0.11.0) newrelic_rpm (6.15.0) - nio4r (2.5.5) + nio4r (2.5.7) nokogiri (1.10.10) mini_portile2 (~> 2.4.0) nyan-cat-formatter (0.12.0) @@ -398,8 +398,16 @@ GEM childprocess (>= 0.5, < 4.0) rubyzip (>= 1.2.2) semantic_range (2.3.1) - sentry-raven (3.1.1) + sentry-rails (4.2.2) + rails (>= 5.0) + sentry-ruby-core (~> 4.2.0) + sentry-ruby (4.2.2) + concurrent-ruby (~> 1.0, >= 1.0.2) faraday (>= 1.0) + sentry-ruby-core (= 4.2.2) + sentry-ruby-core (4.2.2) + concurrent-ruby + faraday shoulda-matchers (4.5.1) activesupport (>= 4.2.0) simplecov (0.21.2) @@ -527,7 +535,8 @@ DEPENDENCIES rubyzip sass-rails selenium-webdriver - sentry-raven + sentry-rails + sentry-ruby shoulda-matchers simplecov slim-rails diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a158fc15..0492d7c7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,7 +7,7 @@ class ApplicationController < ActionController::Base MEMBER_ACTIONS = %i[destroy edit show update].freeze after_action :verify_authorized, except: %i[help welcome] - before_action :set_raven_context, :set_locale, :allow_iframe_requests, :load_embed_options + before_action :set_sentry_context, :set_locale, :allow_iframe_requests, :load_embed_options protect_from_forgery(with: :exception, prepend: true) rescue_from Pundit::NotAuthorizedError, with: :render_not_authorized @@ -20,17 +20,17 @@ class ApplicationController < ActionController::Base raise Pundit::NotAuthorizedError unless current_user end - def set_raven_context + def set_sentry_context return if current_user.blank? - Raven.user_context( + Sentry.set_user( id: current_user.id, type: current_user.class.name, username: current_user.displayname, consumer: current_user.consumer.name ) end - private :set_raven_context + private :set_sentry_context def render_not_authorized respond_to do |format| @@ -50,7 +50,7 @@ class ApplicationController < ActionController::Base def set_locale session[:locale] = params[:custom_locale] || params[:locale] || session[:locale] I18n.locale = session[:locale] || I18n.default_locale - Raven.extra_context(locale: I18n.locale) + Sentry.set_extras(locale: I18n.locale) end private :set_locale @@ -68,7 +68,7 @@ class ApplicationController < ActionController::Base else {} end - Raven.extra_context(@embed_options) + Sentry.set_extras(@embed_options) @embed_options end private :load_embed_options diff --git a/app/controllers/concerns/lti.rb b/app/controllers/concerns/lti.rb index c63d4390..6290d5a1 100644 --- a/app/controllers/concerns/lti.rb +++ b/app/controllers/concerns/lti.rb @@ -154,7 +154,7 @@ module Lti if provider.nil? {status: 'error'} elsif provider.outcome_service? - Raven.extra_context({ + Sentry.set_extras({ provider: provider.inspect, score: submission.normalized_score, lti_parameter: lti_parameter.inspect, diff --git a/app/controllers/execution_environments_controller.rb b/app/controllers/execution_environments_controller.rb index 66a212a8..b373926a 100644 --- a/app/controllers/execution_environments_controller.rb +++ b/app/controllers/execution_environments_controller.rb @@ -121,7 +121,7 @@ class ExecutionEnvironmentsController < ApplicationController rescue DockerClient::Error => error @docker_images = [] flash[:warning] = error.message - Raven.capture_exception(error) + Sentry.capture_exception(error) end private :set_docker_images diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index b4aa8073..44db28df 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -188,7 +188,7 @@ class ExercisesController < ApplicationController rescue Proforma::ProformaError render json: t('exercises.import_codeharbor.import_errors.invalid'), status: 400 rescue StandardError => e - Raven.capture_exception(e) + Sentry.capture_exception(e) render json: t('exercises.import_codeharbor.import_errors.internal_error'), status: 500 end @@ -392,7 +392,7 @@ class ExercisesController < ApplicationController end def redirect_to_lti_return_path - Raven.extra_context( + Sentry.set_extras( consumers_id: @submission.user&.consumer, external_users_id: @submission.user_id, exercises_id: @submission.exercise_id, diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index fd8b90bf..4b888fe3 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -196,7 +196,7 @@ class SubmissionsController < ApplicationController rescue JSON::ParserError => error socket.send data Rails.logger.debug('Rescued parsing error, sent the received client data to docker:' + data) - Raven.extra_context(data: data) + Sentry.set_extras(data: data) end end @@ -417,7 +417,7 @@ class SubmissionsController < ApplicationController container = Docker::Container.get(params[:container_id]) DockerClient.destroy_container(container) rescue Docker::Error::NotFoundError => error - Raven.capture_exception(error) + Sentry.capture_exception(error) ensure head :ok end @@ -447,7 +447,7 @@ class SubmissionsController < ApplicationController yield(server_sent_event) if block_given? server_sent_event.write({code: 200}, event: 'close') rescue => exception - Raven.capture_exception(exception) + Sentry.capture_exception(exception) logger.error(exception.message) logger.error(exception.backtrace.join("\n")) server_sent_event.write({code: 500}, event: 'close') diff --git a/app/controllers/user_exercise_feedbacks_controller.rb b/app/controllers/user_exercise_feedbacks_controller.rb index 19a5c75d..96560296 100644 --- a/app/controllers/user_exercise_feedbacks_controller.rb +++ b/app/controllers/user_exercise_feedbacks_controller.rb @@ -23,7 +23,7 @@ class UserExerciseFeedbacksController < ApplicationController end def create - Raven.extra_context(params: uef_params) + Sentry.set_extras(params: uef_params) @exercise = Exercise.find(uef_params[:exercise_id]) rfc = RequestForComment.unsolved.where(exercise_id: @exercise.id, user_id: current_user.id).first diff --git a/app/models/linter_check_run.rb b/app/models/linter_check_run.rb index 3c84881f..00f96a73 100644 --- a/app/models/linter_check_run.rb +++ b/app/models/linter_check_run.rb @@ -24,7 +24,7 @@ class LinterCheckRun < ApplicationRecord ) rescue ActiveRecord::RecordInvalid # Something bad happened. Probably, the RegEx in lib/py_lint_adapter.rb didn't work. - Raven.extra_context(testrun: testrun, linter_result: linter_result) + Sentry.set_extras(testrun: testrun, linter_result: linter_result) end end end diff --git a/app/views/admin/dashboard/show.html.slim b/app/views/admin/dashboard/show.html.slim index ae497ad5..b4d836f5 100644 --- a/app/views/admin/dashboard/show.html.slim +++ b/app/views/admin/dashboard/show.html.slim @@ -11,7 +11,7 @@ h2 Version div.mb-4 = "Release:" - pre = Raven.configuration.release + pre = Sentry.configuration.release h2 Docker diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 3b2ebc60..6afbb8a7 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -20,12 +20,12 @@ html lang="#{I18n.locale || I18n.default_locale}" script type="text/javascript" | I18n.defaultLocale = "#{I18n.default_locale}"; | I18n.locale = "#{I18n.locale}"; - - unless Raven.configuration.current_environment == "development" + - unless Sentry.configuration.environment == "development" | Sentry.init({ | dsn: 'https://637ca99538224b4bb28cd9e670e1b372@sentry.xikolo.de/33', | attachStacktrace: true, - | release: "#{Raven.configuration.release}", - | environment: "#{Raven.configuration.current_environment}", + | release: "#{Sentry.configuration.release}", + | environment: "#{Sentry.configuration.environment}", | }); - if @current_user | Sentry.configureScope(function(scope) { diff --git a/app/views/request_for_comments/show.html.slim b/app/views/request_for_comments/show.html.slim index 64fecaf4..183ddeda 100644 --- a/app/views/request_for_comments/show.html.slim +++ b/app/views/request_for_comments/show.html.slim @@ -40,7 +40,7 @@ - output_runs.each do |testrun| - output = testrun.try(:output) - if output - - Raven.extra_context(output: output) + - Sentry.set_extras(output: output) - begin - Timeout::timeout(2) do // (?:\\"|.) is required to correctly identify " within the output. diff --git a/config/initializers/raven.rb b/config/initializers/raven.rb deleted file mode 100644 index 6fe956d2..00000000 --- a/config/initializers/raven.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -require 'concurrent' - -Rails.application.tap do |app| - pool = ::Concurrent::ThreadPoolExecutor.new(max_queue: 10) - - Raven.configure do |config| - config.sanitize_fields = app.config.filter_parameters.map(&:to_s) - - config.processors -= [Raven::Processor::PostData] # Do this to send POST data - # config.excluded_exceptions -= ['ActionController::InvalidAuthenticityToken'] - - config.async = lambda do |event| - pool.post { ::Raven.send_event(event) } - end - - # Do not sent full list of gems with each event - config.send_modules = false - end -end diff --git a/config/initializers/sentry.rb b/config/initializers/sentry.rb new file mode 100644 index 00000000..b5209d26 --- /dev/null +++ b/config/initializers/sentry.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +Sentry.init do |config| + # Do not send full list of gems with each event + config.send_modules = false + + # Send some more data, such as request bodies + config.send_default_pii = true + + # Strip sensitive user data such as passwords from event annotations + filter = ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters) + config.before_send = lambda do |event, _hint| + event.request.data = filter.filter(event.request.data) + event + end +end diff --git a/lib/docker_client.rb b/lib/docker_client.rb index 675a13a1..c2a0df32 100644 --- a/lib/docker_client.rb +++ b/lib/docker_client.rb @@ -38,7 +38,7 @@ class DockerClient Pathname.new(local_workspace_path).children.each do |p| p.rmtree rescue Errno::ENOENT, Errno::EACCES => error - Raven.capture_exception(error) + Sentry.capture_exception(error) Rails.logger.error("clean_container_workspace: Got #{error.class.to_s}: #{error.to_s}") end # FileUtils.rmdir(Pathname.new(local_workspace_path)) @@ -237,7 +237,7 @@ class DockerClient clean_container_workspace(container) FileUtils.rmtree(local_workspace_path(container)) rescue Errno::ENOENT, Errno::EACCES => error - Raven.capture_exception(error) + Sentry.capture_exception(error) Rails.logger.error("clean_container_workspace: Got #{error.class.to_s}: #{error.to_s}") end diff --git a/lib/docker_container_pool.rb b/lib/docker_container_pool.rb index b2cfba85..abddb49d 100644 --- a/lib/docker_container_pool.rb +++ b/lib/docker_container_pool.rb @@ -16,16 +16,16 @@ class DockerContainerPool #Rails.logger.debug('created container ' + container.to_s + ' for execution environment ' + execution_environment.to_s) container rescue StandardError => e - Raven.extra_context({container: container.inspect, execution_environment: execution_environment.inspect, config: config.inspect}) - Raven.capture_exception(e) + Sentry.set_extras({container: container.inspect, execution_environment: execution_environment.inspect, config: config.inspect}) + Sentry.capture_exception(e) nil end def self.return_container(container, execution_environment) Faraday.get(config[:location] + "/docker_container_pool/return_container/" + container.id) rescue StandardError => e - Raven.extra_context({container: container.inspect, execution_environment: execution_environment.inspect, config: config.inspect}) - Raven.capture_exception(e) + Sentry.set_extras({container: container.inspect, execution_environment: execution_environment.inspect, config: config.inspect}) + Sentry.capture_exception(e) nil end @@ -36,8 +36,8 @@ class DockerContainerPool container_id = JSON.parse(Faraday.get(config[:location] + "/docker_container_pool/get_container/" + execution_environment.id.to_s).body)['id'] Docker::Container.get(container_id) unless container_id.blank? rescue StandardError => e - Raven.extra_context({container_id: container_id.inspect, execution_environment: execution_environment.inspect, config: config.inspect}) - Raven.capture_exception(e) + Sentry.set_extras({container_id: container_id.inspect, execution_environment: execution_environment.inspect, config: config.inspect}) + Sentry.capture_exception(e) nil end else @@ -53,15 +53,15 @@ class DockerContainerPool response = JSON.parse(Faraday.get(config[:location] + "/docker_container_pool/quantities").body) response.transform_keys(&:to_i) rescue StandardError => e - Raven.extra_context({response: response.inspect}) - Raven.capture_exception(e) + Sentry.set_extras({response: response.inspect}) + Sentry.capture_exception(e) [] end def self.dump_info JSON.parse(Faraday.get(config[:location] + "/docker_container_pool/dump_info").body) rescue StandardError => e - Raven.capture_exception(e) + Sentry.capture_exception(e) nil end end diff --git a/lib/py_lint_adapter.rb b/lib/py_lint_adapter.rb index 4e5ea235..d7d8c133 100644 --- a/lib/py_lint_adapter.rb +++ b/lib/py_lint_adapter.rb @@ -34,7 +34,7 @@ class PyLintAdapter < TestingFrameworkAdapter end || [] end rescue Timeout::Error - Raven.capture_message({stdout: output[:stdout], regex: ASSERTION_ERROR_REGEXP}.to_json) + Sentry.capture_message({stdout: output[:stdout], regex: ASSERTION_ERROR_REGEXP}.to_json) assertion_error_matches = [] end concatenated_errors = assertion_error_matches.map { |result| "#{result[:name]}: #{result[:result]}" }.flatten @@ -62,7 +62,7 @@ class PyLintAdapter < TestingFrameworkAdapter captures = message[:result].match(Regexp.new(regex))&.named_captures&.symbolize_keys if captures.nil? - Raven.capture_message({regex: regex, message: message[:result]}.to_json) + Sentry.capture_message({regex: regex, message: message[:result]}.to_json) replacement = {} else replacement = captures.each do |key, value| @@ -85,8 +85,8 @@ class PyLintAdapter < TestingFrameworkAdapter assessment rescue StandardError => e # A key was not defined or something really bad happened - Raven.extra_context(assessment) - Raven.capture_exception(e) + Sentry.set_extras(assessment) + Sentry.capture_exception(e) assessment end @@ -104,7 +104,7 @@ class PyLintAdapter < TestingFrameworkAdapter # Read config key I18n.t(keys.append('log_missing').join('.'), default: false) end - Raven.capture_message({key: key, default: default}.to_json) if translation == default && log_missing + Sentry.capture_message({key: key, default: default}.to_json) if translation == default && log_missing translation end end diff --git a/lib/py_unit_adapter.rb b/lib/py_unit_adapter.rb index a19839d4..30528c32 100644 --- a/lib/py_unit_adapter.rb +++ b/lib/py_unit_adapter.rb @@ -28,7 +28,7 @@ class PyUnitAdapter < TestingFrameworkAdapter }.flatten || [] end rescue Timeout::Error - Raven.capture_message({stderr: output[:stderr], regex: ASSERTION_ERROR_REGEXP}.to_json) + Sentry.capture_message({stderr: output[:stderr], regex: ASSERTION_ERROR_REGEXP}.to_json) assertion_error_matches = [] end {count: count, failed: failed + errors, error_messages: assertion_error_matches} diff --git a/spec/controllers/internal_users_controller_spec.rb b/spec/controllers/internal_users_controller_spec.rb index d5fce456..d354edde 100644 --- a/spec/controllers/internal_users_controller_spec.rb +++ b/spec/controllers/internal_users_controller_spec.rb @@ -166,7 +166,7 @@ describe InternalUsersController do describe 'GET #forgot_password' do context 'when no user is logged in' do before(:each) do - allow(controller).to receive(:set_raven_context).and_return(nil) + allow(controller).to receive(:set_sentry_context).and_return(nil) expect(controller).to receive(:current_user).and_return(nil) get :forgot_password @@ -178,7 +178,7 @@ describe InternalUsersController do context 'when a user is already logged in' do before(:each) do - allow(controller).to receive(:set_raven_context).and_return(nil) + allow(controller).to receive(:set_sentry_context).and_return(nil) expect(controller).to receive(:current_user).and_return(user) get :forgot_password diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 88833c0f..b7216abf 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -155,7 +155,7 @@ describe SessionsController do describe 'DELETE #destroy' do let(:user) { double } before(:each) { - allow(controller).to receive(:set_raven_context).and_return(nil) + allow(controller).to receive(:set_sentry_context).and_return(nil) expect(controller).to receive(:current_user).at_least(:once).and_return(user) } @@ -219,7 +219,7 @@ describe SessionsController do describe 'GET #new' do context 'when no user is logged in' do before(:each) do - allow(controller).to receive(:set_raven_context).and_return(nil) + allow(controller).to receive(:set_sentry_context).and_return(nil) expect(controller).to receive(:current_user).and_return(nil) get :new @@ -231,7 +231,7 @@ describe SessionsController do context 'when a user is already logged in' do before(:each) do - allow(controller).to receive(:set_raven_context).and_return(nil) + allow(controller).to receive(:set_sentry_context).and_return(nil) expect(controller).to receive(:current_user).and_return(FactoryBot.build(:teacher)) get :new